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-2019 by the FIFE team 
  4  #  http://www.fifengine.net 
  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  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   
35 -class SerializerError(Exception):
36 pass
37
38 -class InvalidFormat(SerializerError):
39 pass
40
41 -class WrongFileType(SerializerError):
42 pass
43
44 -class NameClash(SerializerError):
45 pass
46
47 -class NotFound(SerializerError):
48 pass
49
50 -def warn(self, msg):
51 print('Warning (%s): %s' % (self.filename, msg))
52
53 -def root_subfile(masterfile, subfile):
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
85 -def reverse_root_subfile(masterfile, subfile):
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' # cheat a little to satisfy root_subfile 98 return root_subfile(masterfile, subfile)
99
100 -def norm_path(path):
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
110 -def loadImportFile(loader, path, engine, debug=False):
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
121 -def loadImportDir(loader, path, engine, debug=False):
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
132 -def loadImportDirRec(loader, path, engine, debug=False):
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
145 -def root_subfile(masterfile, subfile):
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