<s> import inspect <EOL> import pyfora . Exceptions as Exceptions <EOL> class PureImplementationMapping ( object ) : <EOL> """<STR_LIT>""" <EOL> def getMappablePythonTypes ( self ) : <EOL> """<STR_LIT>""" <EOL> raise Exceptions . PyforaNotImplementedError ( <EOL> "<STR_LIT>" <EOL> % ( self . getMappablePythonTypes . __name__ , <EOL> type ( self ) . __name__ ) <EOL> ) <EOL> def getMappableInstances ( self ) : <EOL> """<STR_LIT>""" <EOL> raise Exceptions . PyforaNotImplementedError ( <EOL> "<STR_LIT>" <EOL> % ( self . getMappableInstances . __name__ , <EOL> type ( self ) . __name__ ) <EOL> ) <EOL> def getPurePythonTypes ( self ) : <EOL> """<STR_LIT>""" <EOL> raise Exceptions . PyforaNotImplementedError ( <EOL> "<STR_LIT>" <EOL> % ( self . getPurePythonTypes . __name__ , <EOL> type ( self ) . __name__ ) <EOL> ) <EOL> def mapPythonInstanceToPyforaInstance ( self , instance ) : <EOL> """<STR_LIT>""" <EOL> raise Exceptions . PyforaNotImplementedError ( <EOL> "<STR_LIT>" <EOL> % ( self . mapPythonInstanceToPyforaInstance . __name__ , <EOL> type ( self ) . __name__ ) <EOL> ) <EOL> def mapPyforaInstanceToPythonInstance ( self , instance ) : <EOL> """<STR_LIT>""" <EOL> raise Exceptions . PyforaNotImplementedError ( <EOL> "<STR_LIT>" <EOL> % ( self . mapPyforaInstanceToPythonInstance . __name__ , <EOL> type ( self ) . __name__ ) <EOL> ) <EOL> class InstanceMapping ( PureImplementationMapping ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , instance , pureClass ) : <EOL> self . instance = instance <EOL> self . pureClass = pureClass <EOL> def getMappablePythonTypes ( self ) : <EOL> return [ ] <EOL> def getMappableInstances ( self ) : <EOL> return [ self . instance ] <EOL> def getPurePythonTypes ( self ) : <EOL> return [ self . pureClass ] <EOL> def mapPythonInstanceToPyforaInstance ( self , instance ) : <EOL> assert instance is self . instance <EOL> if self . pureClass is None : <EOL> raise Exceptions . PyforaNotImplementedError ( <EOL> "<STR_LIT>" % <EOL> ( instance . __name__ if hasattr ( instance , "<STR_LIT>" ) else instance ) ) <EOL> return self . pureClass ( ) <EOL> def mapPyforaInstanceToPythonInstance ( self , instance ) : <EOL> assert instance . __class__ is self . pureClass <EOL> return self . instance <EOL> class PureMappingRegistry ( object ) : <EOL> mappings = { } <EOL> @ classmethod <EOL> def mappingsForRootModule ( cls , root ) : <EOL> return cls . mappings . get ( root , [ ] ) <EOL> def pureMapping ( instance_or_mapping ) : <EOL> def registerMapping ( root_module , mapping ) : <EOL> mappings = PureMappingRegistry . mappings . get ( root_module ) <EOL> if mappings is None : <EOL> mappings = [ ] <EOL> PureMappingRegistry . mappings [ root_module ] = mappings <EOL> mappings . append ( mapping ) <EOL> if inspect . isclass ( instance_or_mapping ) and issubclass ( instance_or_mapping , <EOL> PureImplementationMapping ) : <EOL> root_module = instance_or_mapping . __module__ . split ( '<STR_LIT:.>' ) [ - <NUM_LIT:1> ] <EOL> if root_module . startswith ( '<STR_LIT>' ) : <EOL> root_module = root_module [ len ( '<STR_LIT>' ) : ] <EOL> registerMapping ( root_module , instance_or_mapping ( ) ) <EOL> return instance_or_mapping <EOL> def wrap ( cls ) : <EOL> module = None <EOL> if hasattr ( instance_or_mapping , '<STR_LIT>' ) : <EOL> module = instance_or_mapping . __module__ <EOL> elif hasattr ( instance_or_mapping , '<STR_LIT>' ) : <EOL> module = instance_or_mapping . __class__ . __module__ <EOL> assert module is not None , "<STR_LIT>" % ( <EOL> instance_or_mapping , ) <EOL> root_module = module . split ( '<STR_LIT:.>' ) [ <NUM_LIT:0> ] <EOL> registerMapping ( root_module , InstanceMapping ( instance_or_mapping , cls ) ) <EOL> return cls <EOL> return wrap </s>
