1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23 import fife, sys, os
24 from traceback import print_exc
25
26 __all__ = ('ET', 'SerializerError', 'InvalidFormat', 'WrongFileType', 'NameClash', 'NotFound', 'warn', 'root_subfile', 'reverse_root_subfile')
27
28 try:
29 import xml.etree.cElementTree as ET
30 except ImportError:
31 import xml.etree.ElementTree as ET
32
35
38
41
44
47
49 print 'Warning (%s): %s' % (self.filename, msg)
50
52 """
53 Returns new path for given subfile (path), which is rooted against masterfile
54 E.g. if masterfile is ./../foo/bar.xml and subfile is ./../foo2/subfoo.xml,
55 returned path is ../foo2/subfoo.xml
56 NOTE: masterfile is expected to be *file*, not directory. subfile can be either
57 """
58 s = '/'
59
60 masterfile = norm_path(os.path.abspath(masterfile))
61 subfile = norm_path(os.path.abspath(subfile))
62
63 master_fragments = masterfile.split(s)
64 sub_fragments = subfile.split(s)
65
66 master_leftovers = []
67 sub_leftovers = []
68
69 for i in xrange(len(master_fragments)):
70 try:
71 if master_fragments[i] == sub_fragments[i]:
72 master_leftovers = master_fragments[i+1:]
73 sub_leftovers = sub_fragments[i+1:]
74 except IndexError:
75 break
76
77 pathstr = ''
78 for f in master_leftovers[:-1]:
79 pathstr += '..' + s
80 pathstr += s.join(sub_leftovers)
81 return pathstr
82
84 """
85 does inverse operation to root_subfile. E.g.
86 E.g. if masterfile is ./../foo/bar.xml and subfile is ../foo2/subfoo.xml,
87 returned path ./../foo2/subfoo.xml
88 Usually this function is used to convert saved paths into engine relative paths
89 NOTE: masterfile is expected to be *file*, not directory. subfile can be either
90 """
91 s = '/'
92
93 masterfile = norm_path(os.path.abspath(masterfile)).split(s)[:-1]
94 subfile = norm_path(os.path.abspath( s.join(masterfile) + s + subfile ))
95 masterfile = norm_path(os.getcwd()) + s + 'foo.bar'
96 return root_subfile(masterfile, subfile)
97
99 """
100 Makes the path use '/' delimited separators. FIFE always uses these delimiters, but some os-related
101 routines will default to os.path.sep.
102 """
103 if os.path.sep == '/':
104 return path
105
106 return '/'.join(path.split(os.path.sep))
107
109 """ uses XMLObjectLoader to load import files from path
110
111 @type path: string
112 @param path: path to import file
113 @type debug: bool
114 @param debug: flag to activate / deactivate print statements
115 """
116 loader.loadResource(fife.ResourceLocation(path))
117 if debug: print 'imported object file ' + path
118
120 """ helper function to call loadImportFile on a directory
121
122 @type path: string
123 @param path: path to import directory
124 @type debug: bool
125 @param debug: flag to activate / deactivate print statements
126 """
127 for _file in filter(lambda f: f.split('.')[-1] == 'xml', engine.getVFS().listFiles(path)):
128 loadImportFile(loader, '/'.join([path, _file]), engine, debug)
129
131 """ helper function to call loadImportFile recursive on a directory
132
133 @type path: string
134 @param path: path to import directory
135 @type debug: bool
136 @param debug: flag to activate / deactivate print statements
137 """
138 loadImportDir(loader, path, engine, debug)
139
140 for _dir in filter(lambda d: not d.startswith('.'), engine.getVFS().listDirectories(path)):
141 loadImportDirRec(loader, '/'.join([path, _dir]), engine, debug)
142
144 """
145 Returns new path for given subfile (path), which is rooted against masterfile
146 E.g. if masterfile is ./../foo/bar.xml and subfile is ./../foo2/subfoo.xml,
147 returned path is ../foo2/subfoo.xml
148 NOTE: masterfile is expected to be *file*, not directory. subfile can be either
149 """
150 s = '/'
151
152 masterfile = norm_path(os.path.abspath(masterfile))
153 subfile = norm_path(os.path.abspath(subfile))
154
155 master_fragments = masterfile.split(s)
156 sub_fragments = subfile.split(s)
157
158 master_leftovers = []
159 sub_leftovers = []
160
161 for i in xrange(len(master_fragments)):
162 try:
163 if master_fragments[i] == sub_fragments[i]:
164 master_leftovers = master_fragments[i+1:]
165 sub_leftovers = sub_fragments[i+1:]
166 except IndexError:
167 break
168
169 pathstr = ''
170 for f in master_leftovers[:-1]:
171 pathstr += '..' + s
172 pathstr += s.join(sub_leftovers)
173 return pathstr
174