Mercurial > fife-parpg
comparison engine/python/fife/extensions/serializers/__init__.py @ 569:466d76db9701
Some small code cleanups in the extensions.
The SimpleXMLSerializer now makes use of exceptions.
author | prock@33b003aa-7bff-0310-803a-e67f0ece8222 |
---|---|
date | Mon, 28 Jun 2010 19:28:53 +0000 |
parents | 64738befdf3b |
children | e3140f01749d |
comparison
equal
deleted
inserted
replaced
568:bfbf329e1da8 | 569:466d76db9701 |
---|---|
48 | 48 |
49 def warn(self, msg): | 49 def warn(self, msg): |
50 print 'Warning (%s): %s' % (self.filename, msg) | 50 print 'Warning (%s): %s' % (self.filename, msg) |
51 | 51 |
52 def root_subfile(masterfile, subfile): | 52 def root_subfile(masterfile, subfile): |
53 ''' | 53 """ |
54 Returns new path for given subfile (path), which is rooted against masterfile | 54 Returns new path for given subfile (path), which is rooted against masterfile |
55 E.g. if masterfile is ./../foo/bar.xml and subfile is ./../foo2/subfoo.xml, | 55 E.g. if masterfile is ./../foo/bar.xml and subfile is ./../foo2/subfoo.xml, |
56 returned path is ../foo2/subfoo.xml | 56 returned path is ../foo2/subfoo.xml |
57 NOTE: masterfile is expected to be *file*, not directory. subfile can be either | 57 NOTE: masterfile is expected to be *file*, not directory. subfile can be either |
58 ''' | 58 """ |
59 s = '/' | 59 s = '/' |
60 | 60 |
61 masterfile = norm_path(os.path.abspath(masterfile)) | 61 masterfile = norm_path(os.path.abspath(masterfile)) |
62 subfile = norm_path(os.path.abspath(subfile)) | 62 subfile = norm_path(os.path.abspath(subfile)) |
63 | 63 |
80 pathstr += '..' + s | 80 pathstr += '..' + s |
81 pathstr += s.join(sub_leftovers) | 81 pathstr += s.join(sub_leftovers) |
82 return pathstr | 82 return pathstr |
83 | 83 |
84 def reverse_root_subfile(masterfile, subfile): | 84 def reverse_root_subfile(masterfile, subfile): |
85 ''' | 85 """ |
86 does inverse operation to root_subfile. E.g. | 86 does inverse operation to root_subfile. E.g. |
87 E.g. if masterfile is ./../foo/bar.xml and subfile is ../foo2/subfoo.xml, | 87 E.g. if masterfile is ./../foo/bar.xml and subfile is ../foo2/subfoo.xml, |
88 returned path ./../foo2/subfoo.xml | 88 returned path ./../foo2/subfoo.xml |
89 Usually this function is used to convert saved paths into engine relative paths | 89 Usually this function is used to convert saved paths into engine relative paths |
90 NOTE: masterfile is expected to be *file*, not directory. subfile can be either | 90 NOTE: masterfile is expected to be *file*, not directory. subfile can be either |
91 ''' | 91 """ |
92 s = '/' | 92 s = '/' |
93 | 93 |
94 masterfile = norm_path(os.path.abspath(masterfile)).split(s)[:-1] | 94 masterfile = norm_path(os.path.abspath(masterfile)).split(s)[:-1] |
95 subfile = norm_path(os.path.abspath( s.join(masterfile) + s + subfile )) | 95 subfile = norm_path(os.path.abspath( s.join(masterfile) + s + subfile )) |
96 masterfile = norm_path(os.getcwd()) + s + 'foo.bar' # cheat a little to satisfy root_subfile | 96 masterfile = norm_path(os.getcwd()) + s + 'foo.bar' # cheat a little to satisfy root_subfile |
97 return root_subfile(masterfile, subfile) | 97 return root_subfile(masterfile, subfile) |
98 | 98 |
99 def norm_path(path): | 99 def norm_path(path): |
100 ''' | 100 """ |
101 Makes the path use '/' delimited separators. FIFE always uses these delimiters, but some os-related | 101 Makes the path use '/' delimited separators. FIFE always uses these delimiters, but some os-related |
102 routines will default to os.path.sep. | 102 routines will default to os.path.sep. |
103 ''' | 103 """ |
104 if os.path.sep == '/': | 104 if os.path.sep == '/': |
105 return path | 105 return path |
106 | 106 |
107 return '/'.join(path.split(os.path.sep)) | 107 return '/'.join(path.split(os.path.sep)) |