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