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

Source Code for Module fife.extensions.serializers.xml_loader_tools

  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  """ utilities for xml maploading process """ 
 24  from __future__ import print_function 
 25  from __future__ import division 
 26   
 27  from builtins import range 
 28  from past.utils import old_div 
 29  import os 
 30  import math 
 31   
32 -def loadImportFile(loader, path, engine, debug=False):
33 """ uses XMLObjectLoader to load import files from path 34 35 @type path: string 36 @param path: path to import file 37 @type debug: bool 38 @param debug: flag to activate / deactivate print statements 39 """ 40 loader.loadResource(path) 41 if debug: print('imported object file ' + path)
42
43 -def loadImportDir(loader, path, engine, debug=False):
44 """ helper function to call loadImportFile on a directory 45 46 @type path: string 47 @param path: path to import directory 48 @type debug: bool 49 @param debug: flag to activate / deactivate print statements 50 """ 51 for _file in [f for f in engine.getVFS().listFiles(path) if f.split('.')[-1] == 'xml']: 52 loadImportFile(loader, '/'.join([path, _file]), engine, debug)
53
54 -def loadImportDirRec(loader, path, engine, debug=False):
55 """ helper function to call loadImportFile recursive on a directory 56 57 @type path: string 58 @param path: path to import directory 59 @type debug: bool 60 @param debug: flag to activate / deactivate print statements 61 """ 62 loadImportDir(loader, path, engine, debug) 63 64 for _dir in [d for d in engine.getVFS().listDirectories(path) if not d.startswith('.')]: 65 loadImportDirRec(loader, '/'.join([path, _dir]), engine, debug)
66
67 -def root_subfile(masterfile, subfile):
68 """ 69 Returns new path for given subfile (path), which is rooted against masterfile 70 E.g. if masterfile is ./../foo/bar.xml and subfile is ./../foo2/subfoo.xml, 71 returned path is ../foo2/subfoo.xml 72 NOTE: masterfile is expected to be *file*, not directory. subfile can be either 73 """ 74 s = '/' 75 76 masterfile = norm_path(os.path.abspath(masterfile)) 77 subfile = norm_path(os.path.abspath(subfile)) 78 79 master_fragments = masterfile.split(s) 80 sub_fragments = subfile.split(s) 81 82 master_leftovers = [] 83 sub_leftovers = [] 84 85 for i in range(len(master_fragments)): 86 try: 87 if master_fragments[i] == sub_fragments[i]: 88 master_leftovers = master_fragments[i+1:] 89 sub_leftovers = sub_fragments[i+1:] 90 except IndexError: 91 break 92 93 pathstr = '' 94 for f in master_leftovers[:-1]: 95 pathstr += '..' + s 96 pathstr += s.join(sub_leftovers) 97 return pathstr
98
99 -def reverse_root_subfile(masterfile, subfile):
100 """ 101 does inverse operation to root_subfile. E.g. 102 E.g. if masterfile is ./../foo/bar.xml and subfile is ../foo2/subfoo.xml, 103 returned path ./../foo2/subfoo.xml 104 Usually this function is used to convert saved paths into engine relative paths 105 NOTE: masterfile is expected to be *file*, not directory. subfile can be either 106 """ 107 s = '/' 108 109 masterfile = norm_path(os.path.abspath(masterfile)).split(s)[:-1] 110 subfile = norm_path(os.path.abspath( s.join(masterfile) + s + subfile )) 111 masterfile = norm_path(os.getcwd()) + s + 'foo.bar' # cheat a little to satisfy root_subfile 112 return root_subfile(masterfile, subfile)
113
114 -def norm_path(path):
115 """ 116 Makes the path use '/' delimited separators. FIFE always uses these delimiters, but some os-related 117 routines will default to os.path.sep. 118 """ 119 if os.path.sep == '/': 120 return path 121 122 return '/'.join(path.split(os.path.sep))
123 124
125 -def frange(limit1, limit2 = None, increment = 1.):
126 """Range function that accepts floats (and integers). 127 If only one limit is specified, assumes 0 as lower limit. 128 129 Usage: 130 frange(-2, 2, 0.1) 131 frange(10) 132 frange(10, increment = 0.5) 133 134 The returned value is an iterator. Use list(frange) for a list. 135 136 source: U{http://code.activestate.com/recipes/ 137 66472-frange-a-range-function-with-float-increments/} 138 139 @type limit1: float 140 @param limit1: lower range limit 141 @type limit2: float 142 @param limit2: upper range limit 143 @type increment: float 144 @param increment: length of each step 145 @rtype generator 146 @return iterable over (limit2 - limit1) / increment steps 147 """ 148 149 if limit2 is None: 150 limit2, limit1 = float(limit1), 0. 151 else: 152 limit1 = float(limit1) 153 154 count = int(math.ceil(old_div((limit2 - limit1),increment))) 155 return (limit1 + n*increment for n in range(count))
156