Changeset 474


Ignore:
Timestamp:
02/02/10 13:27:25 (10 years ago)
Author:
maximinus_parpg
Message:

Cleaned up FIFE 0.3.0 transition code.
Is now compliant with our standards.

Location:
trunk/game/local_loaders
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • trunk/game/local_loaders/__init__.py

    r473 r474  
    2626from traceback import print_exc 
    2727 
    28 __all__ = ('ET', 'SerializerError', 'InvalidFormat', 'WrongFileType', 'NameClash', 'NotFound', 'warn', 'root_subfile', 'reverse_root_subfile') 
     28__all__ = ('ET', 'SerializerError', 'InvalidFormat', 'WrongFileType', 
     29           'NameClash', 'NotFound', 'displayWarning', 'getRootSubfile', 
     30           'reverseRootSubfile') 
    2931 
    3032try: 
    31         import xml.etree.cElementTree as ET 
     33    import xml.etree.cElementTree as ET 
    3234except: 
    33         import xml.etree.ElementTree as ET 
     35    import xml.etree.ElementTree as ET 
    3436 
    3537class SerializerError(Exception): 
    36         pass 
     38    pass 
    3739 
    3840class InvalidFormat(SerializerError): 
    39         pass 
     41    pass 
    4042 
    4143class WrongFileType(SerializerError): 
    42         pass 
     44    pass 
    4345 
    4446class NameClash(SerializerError): 
    45         pass 
     47    pass 
    4648 
    4749class NotFound(SerializerError): 
    48         pass 
     50    pass 
    4951 
    50 def warn(self, msg): 
    51         print 'Warning (%s): %s' % (self.filename, msg) 
     52def displayWarning(self, msg): 
     53    print 'Warning (%s): %s' % (self.filename, msg) 
    5254 
    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 = '/' 
     55def getRootSubfile(masterfile, subfile): 
     56    """ 
     57    Returns path for given subfile (path), which is rooted against masterfile 
     58    E.g. if masterfile is ./../foo/bar.xml and subfile is ./../foo2/subfoo.xml, 
     59    returned path is ../foo2/subfoo.xml 
     60    NOTE: masterfile is expected to be a file, not directory. 
     61    subfile can be either 
     62    """ 
     63    s = '/' 
    6164 
    62         masterfile = norm_path(os.path.abspath(masterfile)) 
    63         subfile = norm_path(os.path.abspath(subfile)) 
     65    masterfile = normalizePath(os.path.abspath(masterfile)) 
     66    subfile = normalizePath(os.path.abspath(subfile)) 
    6467 
    65         master_fragments = masterfile.split(s) 
    66         sub_fragments = subfile.split(s) 
     68    master_fragments = masterfile.split(s) 
     69    sub_fragments = subfile.split(s) 
    6770 
    68         master_leftovers = [] 
    69         sub_leftovers = [] 
     71    master_leftovers = [] 
     72    sub_leftovers = [] 
    7073 
    71         for i in xrange(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 
     74    for i in xrange(len(master_fragments)): 
     75        try: 
     76            if master_fragments[i] == sub_fragments[i]: 
     77                master_leftovers = master_fragments[i+1:] 
     78                sub_leftovers = sub_fragments[i+1:] 
     79        except IndexError: 
     80            break 
    7881 
    79         pathstr = '' 
    80         for f in master_leftovers[:-1]: 
    81                 pathstr += '..' + s 
    82         pathstr += s.join(sub_leftovers) 
    83         return pathstr 
     82    pathstr = '' 
     83    for f in master_leftovers[:-1]: 
     84        pathstr += '..' + s 
     85    pathstr += s.join(sub_leftovers) 
     86    return pathstr 
    8487 
    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 = '/' 
     88def reverseRootSubfile(masterfile, subfile): 
     89    """ 
     90    does inverse operation to getRootSubfile. E.g.  
     91    E.g. if masterfile is ./../foo/bar.xml and subfile is ../foo2/subfoo.xml, 
     92    returned path ./../foo2/subfoo.xml 
     93    This function is used to convert saved paths into engine relative paths 
     94    NOTE: masterfile is expected to be a file, not directory. 
     95    subfile can be either 
     96    """ 
     97    s = '/' 
     98    masterfile = normalizePath(os.path.abspath(masterfile)).split(s)[:-1] 
     99    subfile = normalizePath(os.path.abspath( s.join(masterfile) + s + subfile )) 
     100    masterfile = normalizePath(os.getcwd()) + s + 'foo.bar' 
     101    # cheat a little to satisfy getRootSubfile 
     102    return getRootSubfile(masterfile, subfile) 
    94103 
    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, but some os-related 
    103   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))         
     104def normalizePath(path): 
     105    ''' 
     106    Makes the path use '/' delimited separators. FIFE always uses these 
     107    delimiters, but some os-related routines will default to os.path.sep. 
     108    ''' 
     109    if os.path.sep == '/': 
     110        return path 
     111    return '/'.join(path.split(os.path.sep)) 
  • trunk/game/local_loaders/xmlmap.py

    r473 r474  
    3232FORMAT = '1.0' 
    3333 
    34 def root_subfile(masterfile, subfile): 
    35         ''' 
    36         Returns new path for given subfile (path), which is rooted against masterfile 
    37         E.g. if masterfile is ./../foo/bar.xml and subfile is ./../foo2/subfoo.xml, 
    38         returned path is ../foo2/subfoo.xml 
    39         NOTE: masterfile is expected to be *file*, not directory. subfile can be either 
    40         ''' 
    41         s = '/' 
    42  
    43         masterfile = norm_path(os.path.abspath(masterfile)) 
    44         subfile = norm_path(os.path.abspath(subfile)) 
    45  
    46         master_fragments = masterfile.split(s) 
    47         sub_fragments = subfile.split(s) 
    48  
    49         master_leftovers = [] 
    50         sub_leftovers = [] 
    51  
    52         for i in xrange(len(master_fragments)): 
    53                 try: 
    54                         if master_fragments[i] == sub_fragments[i]: 
    55                                 master_leftovers = master_fragments[i+1:] 
    56                                 sub_leftovers = sub_fragments[i+1:] 
    57                 except IndexError: 
    58                         break 
    59  
    60         pathstr = '' 
    61         for f in master_leftovers[:-1]: 
    62                 pathstr += '..' + s 
    63         pathstr += s.join(sub_leftovers) 
    64         return pathstr 
    65  
    66 def reverse_root_subfile(masterfile, subfile): 
    67         ''' 
    68         does inverse operation to root_subfile. E.g.  
    69         E.g. if masterfile is ./../foo/bar.xml and subfile is ../foo2/subfoo.xml, 
    70         returned path ./../foo2/subfoo.xml 
    71         Usually this function is used to convert saved paths into engine relative paths 
    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)).split(s)[:-1] 
    77         subfile = norm_path(os.path.abspath( s.join(masterfile) + s + subfile )) 
    78         masterfile = norm_path(os.getcwd()) + s + 'foo.bar' # cheat a little to satisfy root_subfile 
    79         return root_subfile(masterfile, subfile) 
     34def getRootSubfile(masterfile, subfile): 
     35    """ 
     36    Returns new path for given subfile (path), which is rooted against masterfile 
     37    E.g. if masterfile is ./../foo/bar.xml and subfile is ./../foo2/subfoo.xml, 
     38    returned path is ../foo2/subfoo.xml 
     39    NOTE: masterfile is expected to be *file*, not directory. subfile can be either 
     40    """ 
     41    s = '/' 
     42 
     43    masterfile = norm_path(os.path.abspath(masterfile)) 
     44    subfile = norm_path(os.path.abspath(subfile)) 
     45 
     46    master_fragments = masterfile.split(s) 
     47    sub_fragments = subfile.split(s) 
     48 
     49    master_leftovers = [] 
     50    sub_leftovers = [] 
     51 
     52    for i in xrange(len(master_fragments)): 
     53        try: 
     54            if master_fragments[i] == sub_fragments[i]: 
     55                master_leftovers = master_fragments[i+1:] 
     56                sub_leftovers = sub_fragments[i+1:] 
     57        except IndexError: 
     58            break 
     59 
     60    pathstr = '' 
     61    for f in master_leftovers[:-1]: 
     62        pathstr += '..' + s 
     63    pathstr += s.join(sub_leftovers) 
     64    return pathstr 
     65 
     66def reverseRootSubfile(masterfile, subfile): 
     67    """ 
     68    does inverse operation to getRootSubfile. E.g.  
     69    E.g. if masterfile is ./../foo/bar.xml and subfile is ../foo2/subfoo.xml, 
     70    returned path ./../foo2/subfoo.xml 
     71    Usually this function is used to convert saved paths into engine relative paths 
     72    NOTE: masterfile is expected to be *file*, not directory. subfile can be either 
     73    """ 
     74    s = '/' 
     75    masterfile = norm_path(os.path.abspath(masterfile)).split(s)[:-1] 
     76    subfile = norm_path(os.path.abspath( s.join(masterfile) + s + subfile )) 
     77    masterfile = norm_path(os.getcwd()) + s + 'foo.bar'  
     78    # cheat a little to satisfy getRootSubfile 
     79    return getRootSubfile(masterfile, subfile) 
    8080 
    8181def norm_path(path): 
    82         ''' 
    83         Makes the path use '/' delimited separators. FIFE always uses these delimiters, but some os-related 
    84   routines will default to os.path.sep. 
    85         ''' 
    86         if os.path.sep == '/': 
    87                 return path 
    88  
    89         return '/'.join(path.split(os.path.sep))         
     82    """ 
     83    Makes the path use '/' delimited separators. FIFE always uses these delimiters, but some os-related 
     84    routines will default to os.path.sep. 
     85    """ 
     86    if os.path.sep == '/': 
     87        return path 
     88    return '/'.join(path.split(os.path.sep))     
    9089 
    9190class XMLMapLoader(fife.ResourceLoader): 
     
    141140        id,format = map_elt.get('id'),map_elt.get('format') 
    142141 
    143         if not format == FORMAT: self._err(''.join(['This file has format ', \ 
    144                                             format, \ 
    145                                             ' but this loader has format ', \ 
    146                                             FORMAT])) 
     142        if not format == FORMAT: 
     143            self._err(''.join(['This file has format ', format, 
     144                               ' but this loader has format ', FORMAT])) 
    147145        if not id: self._err('Map declared without an identifier.') 
    148146 
     
    182180            file = item.get('file') 
    183181            if file: 
    184                 file = reverse_root_subfile(self.source, file) 
     182                file = reverseRootSubfile(self.source, file) 
    185183            dir = item.get('dir') 
    186184            if dir: 
    187                 dir = reverse_root_subfile(self.source, dir) 
     185                dir = reverseRootSubfile(self.source, dir) 
    188186 
    189187            # Don't parse duplicate imports 
Note: See TracChangeset for help on using the changeset viewer.