Changeset 1921 for TI02-CSML


Ignore:
Timestamp:
22/12/06 14:03:18 (13 years ago)
Author:
domlowe
Message:

overriddent setattr and getattribute special methods in csElement class - allows convenient setting of attributes without having to use fully qualified names

File:
1 edited

Legend:

Unmodified
Added
Removed
  • TI02-CSML/trunk/csml/parser.py

    r1912 r1921  
    5757#Some variable definitions: these things are often repeated so store in variables. 
    5858FILEFORMATS=[CSML('NetCDFExtract'),CSML('NASAAmesExtract'), CSML('GRIBExtract'),CSML('CDMLExtract'), CSML('RawFileExtract')] 
    59   
     59 
    6060 
    6161class csElement(object): 
     
    6565    def __init__(self, **kwargs): 
    6666        if not hasattr(self, 'ATTRIBUTES'): 
    67             self.ATTRIBUTES=[] 
    68              
     67            self.__dict__['ATTRIBUTES']=[] 
     68     
     69    #The __setattr__ and __getattribute__ special methods have been overridden. 
     70    #This is so that attributes can be called by their fully qualified name internally 
     71    #while being referenced by their short name externally. 
     72    # so 'id' and {'http://www.opengis.net/gml'}id always have the same value 
     73    #you can set either of them at any time and the other will be kept in sync. 
     74         
     75    def __setattr__(self, name, value): 
     76        if hasattr(self, CSML(name)): 
     77            object.__setattr__(self, CSML(name), value) 
     78            object.__setattr__(self, name, value) 
     79        elif hasattr(self,GML(name)): 
     80            object.__setattr__(self, GML(name), value) 
     81            object.__setattr__(self, name, value) 
     82        else: 
     83            if hasattr(self, 'ATTRIBUTES'): 
     84                if CSML(name) in self.__dict__['ATTRIBUTES']: 
     85                    object.__setattr__(self, CSML(name), value) 
     86                    object.__setattr__(self, name, value) 
     87                elif GML(name) in self.__dict__['ATTRIBUTES']: 
     88                    object.__setattr__(self, GML(name), value) 
     89                    object.__setattr__(self, name, value) 
     90                elif name in self.__dict__['ATTRIBUTES']: 
     91                    object.__setattr__(self, name, value) 
     92                    try: 
     93                        name=name.split('}')[1] 
     94                    except IndexError: 
     95                        pass 
     96                    object.__setattr__(self, name, value) 
     97                else: 
     98                    object.__setattr__(self, name, value) 
     99            else: 
     100                    object.__setattr__(self, name, value) 
     101     
     102    def __getattribute__(self, name): 
     103        if CSML(name) in object.__dict__: 
     104            return object.__getattribute__(self,CSML(name)) 
     105        elif GML(name) in object.__dict__: 
     106            return object.__getattribute__(self,GML(name)) 
     107        else: 
     108            return object.__getattribute__(self,name)  
    69109         
    70110    def __removeURI(self, qname): 
     
    76116         
    77117    def _getSubstitutionType(self,tag): 
    78         print 'splitting tag' 
    79118        return tag.split('}')[1] 
    80119         
     
    154193                self.CONTENT = csmlfrag.text 
    155194        for item in csmlfrag.items(): 
    156             print item 
    157195            if item[0] in self.ATTRIBUTES: 
    158196                setattr(self, item[0], item[1]) 
Note: See TracChangeset for help on using the changeset viewer.