<s> """<STR_LIT>""" <EOL> import sys <EOL> import types <EOL> def subclass ( class_ = None , module = None ) : <EOL> attrs = { <EOL> '<STR_LIT>' : module . __name__ , <EOL> } <EOL> return type ( class_ . __name__ , ( class_ , ) , attrs ) <EOL> class RegisterError ( Exception ) : <EOL> pass <EOL> class ModuleRegistry ( object ) : <EOL> def __init__ ( self , module ) : <EOL> self . elements = { } <EOL> self . module = module <EOL> def _subclass ( self , model_class ) : <EOL> return subclass ( model_class , self . module ) <EOL> def register ( self , thing , subclass = True ) : <EOL> name = thing . __name__ <EOL> if name in self . elements : <EOL> raise RegisterError ( '<STR_LIT>' % ( <EOL> name , self . module . __name__ <EOL> ) ) <EOL> new = thing if not subclass else self . _subclass ( thing ) <EOL> self . elements [ name ] = new <EOL> setattr ( self . module , name , new ) <EOL> def unregister ( self , name ) : <EOL> del self . elements [ name ] <EOL> delattr ( self . module , name ) <EOL> def unregister_all ( self ) : <EOL> for name in self . elements . keys ( ) : <EOL> self . unregister ( name ) <EOL> def __str__ ( self ) : <EOL> return "<STR_LIT>" % ( self . __class__ . __name__ , self . elements ) <EOL> def __unicode__ ( self ) : <EOL> return unicode ( str ( self ) ) <EOL> def __repr__ ( self ) : <EOL> return unicode ( self ) <EOL> def __contains__ ( self , model_class_name ) : <EOL> return model_class_name in self . elements <EOL> def __item__ ( self ) : <EOL> return self . elements . values ( ) <EOL> def __getitem__ ( self , name ) : <EOL> return self . elements [ name ] <EOL> def __call__ ( self , model_class ) : <EOL> return self . register ( model_class ) <EOL> def names ( self ) : <EOL> return self . elements . keys ( ) <EOL> def values ( self ) : <EOL> return self . elements . values ( ) <EOL> def new_registry ( module_or_name ) : <EOL> if issubclass ( module_or_name . __class__ , types . ModuleType ) : <EOL> module = module_or_name <EOL> else : <EOL> module = sys . modules [ module_or_name ] <EOL> return ModuleRegistry ( module ) </s>
