Mercurial > fife-parpg
comparison engine/extensions/serializers/__init__.py @ 0:4a0efb7baf70
* Datasets becomes the new trunk and retires after that :-)
author | mvbarracuda@33b003aa-7bff-0310-803a-e67f0ece8222 |
---|---|
date | Sun, 29 Jun 2008 18:44:17 +0000 |
parents | |
children | 9a1529f9625e |
comparison
equal
deleted
inserted
replaced
-1:000000000000 | 0:4a0efb7baf70 |
---|---|
1 import fife, sys, os | |
2 from traceback import print_exc | |
3 | |
4 __all__ = ('ET', 'SerializerError', 'InvalidFormat', 'WrongFileType', 'NameClash', 'NotFound', 'warn', 'root_subfile', 'reverse_root_subfile') | |
5 | |
6 try: | |
7 import xml.etree.cElementTree as ET | |
8 except: | |
9 import xml.etree.ElementTree as ET | |
10 | |
11 class SerializerError(Exception): | |
12 pass | |
13 | |
14 class InvalidFormat(SerializerError): | |
15 pass | |
16 | |
17 class WrongFileType(SerializerError): | |
18 pass | |
19 | |
20 class NameClash(SerializerError): | |
21 pass | |
22 | |
23 class NotFound(SerializerError): | |
24 pass | |
25 | |
26 def warn(self, msg): | |
27 print 'Warning (%s): %s' % (self.filename, msg) | |
28 | |
29 def root_subfile(masterfile, subfile): | |
30 ''' | |
31 Returns new path for given subfile (path), which is rooted against masterfile | |
32 E.g. if masterfile is ./../foo/bar.xml and subfile is ./../foo2/subfoo.xml, | |
33 returned path is ../foo2/subfoo.xml | |
34 NOTE: masterfile is expected to be *file*, not directory. subfile can be either | |
35 ''' | |
36 s = '/' | |
37 | |
38 masterfile = norm_path(os.path.abspath(masterfile)) | |
39 subfile = norm_path(os.path.abspath(subfile)) | |
40 | |
41 master_fragments = masterfile.split(s) | |
42 sub_fragments = subfile.split(s) | |
43 | |
44 master_leftovers = [] | |
45 sub_leftovers = [] | |
46 | |
47 for i in xrange(len(master_fragments)): | |
48 try: | |
49 if master_fragments[i] == sub_fragments[i]: | |
50 master_leftovers = master_fragments[i+1:] | |
51 sub_leftovers = sub_fragments[i+1:] | |
52 except IndexError: | |
53 break | |
54 | |
55 pathstr = '' | |
56 for f in master_leftovers[:-1]: | |
57 pathstr += '..' + s | |
58 pathstr += s.join(sub_leftovers) | |
59 return pathstr | |
60 | |
61 def reverse_root_subfile(masterfile, subfile): | |
62 ''' | |
63 does inverse operation to root_subfile. E.g. | |
64 E.g. if masterfile is ./../foo/bar.xml and subfile is ../foo2/subfoo.xml, | |
65 returned path ./../foo2/subfoo.xml | |
66 Usually this function is used to convert saved paths into engine relative paths | |
67 NOTE: masterfile is expected to be *file*, not directory. subfile can be either | |
68 ''' | |
69 s = '/' | |
70 | |
71 masterfile = norm_path(os.path.abspath(masterfile)).split(s)[:-1] | |
72 subfile = norm_path(os.path.abspath( s.join(masterfile) + s + subfile )) | |
73 masterfile = norm_path(os.getcwd()) + s + 'foo.bar' # cheat a little to satisfy root_subfile | |
74 return root_subfile(masterfile, subfile) | |
75 | |
76 def norm_path(path): | |
77 ''' | |
78 Makes the path use '/' delimited separators. FIFE always uses these delimiters, but some os-related | |
79 routines will default to os.path.sep. | |
80 ''' | |
81 if os.path.sep == '/': | |
82 return path | |
83 | |
84 return '/'.join(path.split(os.path.sep)) |