<s> '''<STR_LIT>''' <EOL> __docformat__ = '<STR_LIT>' <EOL> __version__ = '<STR_LIT>' <EOL> import os <EOL> import xml . dom <EOL> import xml . dom . minidom <EOL> xml_to_python = { <EOL> '<STR_LIT>' : unicode , <EOL> '<STR_LIT:int>' : int , <EOL> '<STR_LIT:float>' : float , <EOL> '<STR_LIT:bool>' : bool , <EOL> } <EOL> class ResourceError ( Exception ) : <EOL> pass <EOL> class Resource ( dict ) : <EOL> cache = { } <EOL> def __init__ ( self , filename , paths = None ) : <EOL> self . filename = filename <EOL> if paths is None : <EOL> self . paths = [ ] <EOL> else : <EOL> self . paths = paths <EOL> self . namespaces = { } <EOL> dom = xml . dom . minidom . parse ( filename ) <EOL> tag = dom . documentElement <EOL> if tag . tagName != '<STR_LIT>' : <EOL> raise ResourceError ( '<STR_LIT>' % <EOL> tag . tagName ) <EOL> try : <EOL> self . handle ( dom . documentElement ) <EOL> finally : <EOL> dom . unlink ( ) <EOL> NOT_LOADED = '<STR_LIT>' <EOL> @ classmethod <EOL> def load ( cls , filename , paths = None ) : <EOL> '''<STR_LIT>''' <EOL> dirname = os . path . dirname ( filename ) <EOL> if dirname : <EOL> if paths : <EOL> paths = list ( paths ) <EOL> else : <EOL> paths = [ ] <EOL> paths . append ( dirname ) <EOL> if filename in cls . cache : <EOL> if cls . cache [ filename ] is cls . NOT_LOADED : <EOL> raise ResourceError ( '<STR_LIT>' % filename ) <EOL> return cls . cache [ filename ] <EOL> cls . cache [ filename ] = cls . NOT_LOADED <EOL> obj = cls ( filename , paths ) <EOL> cls . cache [ filename ] = obj <EOL> return obj <EOL> def find_file ( self , filename ) : <EOL> if os . path . isabs ( filename ) : <EOL> return filename <EOL> if os . path . exists ( filename ) : <EOL> return filename <EOL> for path in self . paths : <EOL> fn = os . path . join ( path , filename ) <EOL> if os . path . exists ( fn ) : <EOL> return fn <EOL> raise ResourceError ( '<STR_LIT>' % filename ) <EOL> def resource_factory ( self , tag ) : <EOL> for tag in tag . childNodes : <EOL> self . handle ( tag ) <EOL> def requires_factory ( self , tag ) : <EOL> filename = self . find_file ( tag . getAttribute ( '<STR_LIT:file>' ) ) <EOL> resource = Resource . load ( filename ) <EOL> ns = tag . getAttribute ( '<STR_LIT>' ) <EOL> if ns : <EOL> self . namespaces [ ns ] = resource . file <EOL> else : <EOL> self . update ( resource ) <EOL> factories = { <EOL> '<STR_LIT>' : resource_factory , <EOL> '<STR_LIT>' : requires_factory , <EOL> } <EOL> @ classmethod <EOL> def add_factory ( cls , name , factory ) : <EOL> cls . factories [ name ] = factory <EOL> def handle ( self , tag ) : <EOL> if not hasattr ( tag , '<STR_LIT>' ) : return <EOL> ref = tag . getAttribute ( '<STR_LIT>' ) <EOL> if not ref : <EOL> return self . factories [ tag . tagName ] ( self , tag ) <EOL> return self . get_resource ( ref ) <EOL> def add_resource ( self , id , resource ) : <EOL> self [ id ] = resource <EOL> def get_resource ( self , ref ) : <EOL> if '<STR_LIT::>' in ref : <EOL> ns , ref = ref . split ( '<STR_LIT::>' , <NUM_LIT:1> ) <EOL> resources = self . cache [ self . namespaces [ ns ] ] <EOL> return resources [ ref ] <EOL> return self [ ref ] <EOL> @ staticmethod <EOL> def handle_properties ( tag ) : <EOL> properties = { } <EOL> for tag in tag . getElementsByTagName ( '<STR_LIT>' ) : <EOL> name = tag . getAttribute ( '<STR_LIT:name>' ) <EOL> type = tag . getAttribute ( '<STR_LIT:type>' ) or '<STR_LIT>' <EOL> value = tag . getAttribute ( '<STR_LIT:value>' ) <EOL> properties [ name ] = xml_to_python [ type ] ( value ) <EOL> return properties <EOL> def register_factory ( name ) : <EOL> def decorate ( func ) : <EOL> Resource . add_factory ( name , func ) <EOL> return func <EOL> return decorate </s>
