comparison engine/python/fife/extensions/serializers/__init__.py @ 378:64738befdf3b

bringing in the changes from the build_system_rework branch in preparation for the 0.3.0 release. This commit will require the Jan2010 devkit. Clients will also need to be modified to the new way to import fife.
author vtchill@33b003aa-7bff-0310-803a-e67f0ece8222
date Mon, 11 Jan 2010 23:34:52 +0000
parents
children 466d76db9701
comparison
equal deleted inserted replaced
377:fe6fb0e0ed23 378:64738befdf3b
1 # -*- coding: utf-8 -*-
2
3 # ####################################################################
4 # Copyright (C) 2005-2009 by the FIFE team
5 # http://www.fifengine.de
6 # This file is part of FIFE.
7 #
8 # FIFE is free software; you can redistribute it and/or
9 # modify it under the terms of the GNU Lesser General Public
10 # License as published by the Free Software Foundation; either
11 # version 2.1 of the License, or (at your option) any later version.
12 #
13 # This library is distributed in the hope that it will be useful,
14 # but WITHOUT ANY WARRANTY; without even the implied warranty of
15 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 # Lesser General Public License for more details.
17 #
18 # You should have received a copy of the GNU Lesser General Public
19 # License along with this library; if not, write to the
20 # Free Software Foundation, Inc.,
21 # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
22 # ####################################################################
23
24 import fife, sys, os
25 from traceback import print_exc
26
27 __all__ = ('ET', 'SerializerError', 'InvalidFormat', 'WrongFileType', 'NameClash', 'NotFound', 'warn', 'root_subfile', 'reverse_root_subfile')
28
29 try:
30 import xml.etree.cElementTree as ET
31 except:
32 import xml.etree.ElementTree as ET
33
34 class SerializerError(Exception):
35 pass
36
37 class InvalidFormat(SerializerError):
38 pass
39
40 class WrongFileType(SerializerError):
41 pass
42
43 class NameClash(SerializerError):
44 pass
45
46 class NotFound(SerializerError):
47 pass
48
49 def warn(self, msg):
50 print 'Warning (%s): %s' % (self.filename, msg)
51
52 def root_subfile(masterfile, subfile):
53 '''
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,
56 returned path is ../foo2/subfoo.xml
57 NOTE: masterfile is expected to be *file*, not directory. subfile can be either
58 '''
59 s = '/'
60
61 masterfile = norm_path(os.path.abspath(masterfile))
62 subfile = norm_path(os.path.abspath(subfile))
63
64 master_fragments = masterfile.split(s)
65 sub_fragments = subfile.split(s)
66
67 master_leftovers = []
68 sub_leftovers = []
69
70 for i in xrange(len(master_fragments)):
71 try:
72 if master_fragments[i] == sub_fragments[i]:
73 master_leftovers = master_fragments[i+1:]
74 sub_leftovers = sub_fragments[i+1:]
75 except IndexError:
76 break
77
78 pathstr = ''
79 for f in master_leftovers[:-1]:
80 pathstr += '..' + s
81 pathstr += s.join(sub_leftovers)
82 return pathstr
83
84 def reverse_root_subfile(masterfile, subfile):
85 '''
86 does inverse operation to root_subfile. E.g.
87 E.g. if masterfile is ./../foo/bar.xml and subfile is ../foo2/subfoo.xml,
88 returned path ./../foo2/subfoo.xml
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
91 '''
92 s = '/'
93
94 masterfile = norm_path(os.path.abspath(masterfile)).split(s)[:-1]
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
97 return root_subfile(masterfile, subfile)
98
99 def norm_path(path):
100 '''
101 Makes the path use '/' delimited separators. FIFE always uses these delimiters, but some os-related
102 routines will default to os.path.sep.
103 '''
104 if os.path.sep == '/':
105 return path
106
107 return '/'.join(path.split(os.path.sep))