<s> '''<STR_LIT>''' <EOL> import sys <EOL> import re <EOL> class BaseCollection ( list ) : <EOL> def __init__ ( self , entities = None ) : <EOL> list . __init__ ( self ) <EOL> if entities is not None : <EOL> self . extend ( entities ) <EOL> def extend ( self , entities ) : <EOL> for e in entities : <EOL> self . append ( e ) <EOL> def clear ( self ) : <EOL> del self [ : ] <EOL> def resolve_absolute ( self , key , full_path , entity = None , root = None ) : <EOL> if root is None : <EOL> root = entity . _descriptor . resolve_root <EOL> if root : <EOL> full_path = '<STR_LIT>' % ( root , full_path ) <EOL> module_path , classname = full_path . rsplit ( '<STR_LIT:.>' , <NUM_LIT:1> ) <EOL> module = sys . modules [ module_path ] <EOL> res = getattr ( module , classname , None ) <EOL> if res is None : <EOL> if entity is not None : <EOL> raise Exception ( "<STR_LIT>" <EOL> % ( key , full_path , entity . __name__ ) ) <EOL> else : <EOL> raise Exception ( "<STR_LIT>" <EOL> % ( key , full_path ) ) <EOL> return res <EOL> def __getattr__ ( self , key ) : <EOL> return self . resolve ( key ) <EOL> class GlobalEntityCollection ( BaseCollection ) : <EOL> def __init__ ( self , entities = None ) : <EOL> self . _entities = { } <EOL> super ( GlobalEntityCollection , self ) . __init__ ( entities ) <EOL> def append ( self , entity ) : <EOL> '''<STR_LIT>''' <EOL> super ( EntityCollection , self ) . append ( entity ) <EOL> existing_entities = self . _entities . setdefault ( entity . __name__ , [ ] ) <EOL> existing_entities . append ( entity ) <EOL> def resolve ( self , key , entity = None ) : <EOL> '''<STR_LIT>''' <EOL> if '<STR_LIT:.>' in key : <EOL> return self . resolve_absolute ( key , key , entity ) <EOL> else : <EOL> res = self . _entities . get ( key , None ) <EOL> if res is None : <EOL> if entity : <EOL> raise Exception ( "<STR_LIT>" <EOL> % ( key , entity . __name__ ) ) <EOL> else : <EOL> raise Exception ( "<STR_LIT>" <EOL> "<STR_LIT>" <EOL> % key ) <EOL> elif len ( res ) > <NUM_LIT:1> : <EOL> raise Exception ( "<STR_LIT>" <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" % key ) <EOL> else : <EOL> return res [ <NUM_LIT:0> ] <EOL> def clear ( self ) : <EOL> self . _entities = { } <EOL> super ( GlobalEntityCollection , self ) . clear ( ) <EOL> EntityCollection = GlobalEntityCollection <EOL> _leading_dots = re . compile ( '<STR_LIT>' ) <EOL> class RelativeEntityCollection ( BaseCollection ) : <EOL> def resolve ( self , key , entity ) : <EOL> '''<STR_LIT>''' <EOL> full_path = key <EOL> if '<STR_LIT:.>' not in key or key . startswith ( '<STR_LIT:.>' ) : <EOL> num_dots = _leading_dots . match ( full_path ) . end ( <NUM_LIT:1> ) <EOL> full_path = full_path [ num_dots : ] <EOL> chunks = entity . __module__ . split ( '<STR_LIT:.>' ) <EOL> chunkstokeep = len ( chunks ) - num_dots <EOL> if chunkstokeep < <NUM_LIT:0> : <EOL> raise Exception ( "<STR_LIT>" <EOL> "<STR_LIT>" % ( key , entity . __module__ ) ) <EOL> entity_module = '<STR_LIT:.>' . join ( chunks [ : chunkstokeep ] ) <EOL> if entity_module and entity_module is not '<STR_LIT:__main__>' : <EOL> full_path = '<STR_LIT>' % ( entity_module , full_path ) <EOL> root = '<STR_LIT>' <EOL> else : <EOL> root = None <EOL> return self . resolve_absolute ( key , full_path , entity , root = root ) <EOL> def __getattr__ ( self , key ) : <EOL> raise NotImplementedError </s>
