Package fife :: Package extensions :: Package serializers
[hide private]
[frames] | no frames]

Source Code for Package fife.extensions.serializers

  1  # -*- coding: utf-8 -*- 
  2  # #################################################################### 
  3  #  Copyright (C) 2005-2010 by the FIFE team 
  4  #  http://www.fifengine.de 
  5  #  This file is part of FIFE. 
  6  # 
  7  #  FIFE is free software; you can redistribute it and/or 
  8  #  modify it under the terms of the GNU Lesser General Public 
  9  #  License as published by the Free Software Foundation; either 
 10  #  version 2.1 of the License, or (at your option) any later version. 
 11  # 
 12  #  This library is distributed in the hope that it will be useful, 
 13  #  but WITHOUT ANY WARRANTY; without even the implied warranty of 
 14  #  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU 
 15  #  Lesser General Public License for more details. 
 16  # 
 17  #  You should have received a copy of the GNU Lesser General Public 
 18  #  License along with this library; if not, write to the 
 19  #  Free Software Foundation, Inc., 
 20  #  51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA 
 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   
33 -class SerializerError(Exception):
34 pass
35
36 -class InvalidFormat(SerializerError):
37 pass
38
39 -class WrongFileType(SerializerError):
40 pass
41
42 -class NameClash(SerializerError):
43 pass
44
45 -class NotFound(SerializerError):
46 pass
47
48 -def warn(self, msg):
49 print 'Warning (%s): %s' % (self.filename, msg)
50
51 -def root_subfile(masterfile, subfile):
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
83 -def reverse_root_subfile(masterfile, subfile):
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' # cheat a little to satisfy root_subfile 96 return root_subfile(masterfile, subfile)
97
98 -def norm_path(path):
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
108 -def loadImportFile(loader, path, engine, debug=False):
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
119 -def loadImportDir(loader, path, engine, debug=False):
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
130 -def loadImportDirRec(loader, path, engine, debug=False):
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
143 -def root_subfile(masterfile, subfile):
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