<s> import sys <EOL> import traceback <EOL> from types import ModuleType <EOL> from importlib import import_module <EOL> from django . apps import apps <EOL> from django . conf import settings <EOL> from django . utils import six <EOL> from django . utils . functional import cached_property <EOL> from . exceptions import RegistryError <EOL> allowed = None <EOL> def should_fix_type ( typ ) : <EOL> global allowed <EOL> if allowed is None : <EOL> allowed = [ '<STR_LIT>' ] <EOL> for app in apps . get_app_configs ( ) : <EOL> allowed . append ( app . name ) <EOL> return any ( typ . __module__ . startswith ( name ) for name in allowed ) <EOL> class ModuleAttribute ( object ) : <EOL> def __init__ ( self , module_name , name ) : <EOL> self . module_name = module_name <EOL> self . name = name <EOL> self . _accessed = False <EOL> self . _access_traceback = None <EOL> self . _injection_handlers = [ ] <EOL> def inject ( self , handler ) : <EOL> self . _injection_handlers . insert ( <NUM_LIT:0> , handler ) <EOL> @ property <EOL> def is_accesed ( self ) : <EOL> return self . _accessed <EOL> @ property <EOL> def is_assigned ( self ) : <EOL> return hasattr ( self , '<STR_LIT>' ) <EOL> def access ( self ) : <EOL> if not self . _accessed : <EOL> if getattr ( settings , '<STR_LIT>' , False ) : <EOL> self . _access_traceback = traceback . extract_stack ( ) <EOL> value = self . _value <EOL> for handler in self . _injection_handlers : <EOL> try : <EOL> value = handler ( value ) <EOL> except Exception as ex : <EOL> raise RegistryError , ex , sys . exc_info ( ) [ <NUM_LIT:2> ] <EOL> if isinstance ( value , type ) and should_fix_type ( value ) : <EOL> value . __module__ = self . module_name <EOL> self . _accessed = True <EOL> self . _value = value <EOL> return self . _value <EOL> def assign ( self , value ) : <EOL> if self . is_accesed : <EOL> filename , line , name , text = '<STR_LIT:?>' , '<STR_LIT:?>' , '<STR_LIT:?>' , '<STR_LIT:?>' <EOL> if self . _access_traceback is not None : <EOL> filename , line , name , text = self . _access_traceback [ - <NUM_LIT:3> ] <EOL> raise RegistryError ( <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" % ( <EOL> self . name , <EOL> filename , <EOL> line , <EOL> name , <EOL> text ) ) <EOL> self . _value = value <EOL> class BaseModule ( ModuleType ) : <EOL> _imports = None <EOL> _imported_attrs = None <EOL> _modules = { } <EOL> def __new__ ( cls , fullname ) : <EOL> if fullname in cls . _modules : <EOL> raise Exception ( ) <EOL> module = super ( BaseModule , cls ) . __new__ ( cls , fullname ) <EOL> cls . _modules [ fullname ] = module <EOL> return module <EOL> def __init__ ( self , fullname ) : <EOL> super ( BaseModule , self ) . __init__ ( fullname ) <EOL> self . _attrs = { } <EOL> def _import ( self ) : <EOL> if self . _imported_attrs is None : <EOL> self . _imported_attrs = { } <EOL> if self . _imports : <EOL> module = import_module ( self . _imports ) <EOL> self . _imported_attrs . update ( <EOL> ** { <EOL> name : value <EOL> for name , value in six . iteritems ( vars ( module ) ) <EOL> if not name . startswith ( '<STR_LIT:_>' ) <EOL> } <EOL> ) <EOL> def __getattribute__ ( self , name ) : <EOL> if name . startswith ( '<STR_LIT:_>' ) : <EOL> try : <EOL> return super ( BaseModule , self ) . __getattribute__ ( name ) <EOL> except AttributeError : <EOL> pass <EOL> elif not self [ name ] . is_assigned : <EOL> try : <EOL> value = super ( BaseModule , self ) . __getattribute__ ( name ) <EOL> except AttributeError : <EOL> self . _import ( ) <EOL> if name in self . _imported_attrs : <EOL> self [ name ] . assign ( self . _imported_attrs [ name ] ) <EOL> else : <EOL> self [ name ] . assign ( value ) <EOL> if self [ name ] . is_assigned : <EOL> return self [ name ] . access ( ) <EOL> raise AttributeError ( name ) <EOL> @ property <EOL> def __all__ ( self ) : <EOL> names = set ( ) <EOL> for name in six . iterkeys ( self . _attrs ) : <EOL> names . add ( name ) <EOL> for name in six . iterkeys ( self . _base_attrs ) : <EOL> names . add ( name ) <EOL> return list ( names ) <EOL> def __setattr__ ( self , name , value ) : <EOL> if name . startswith ( '<STR_LIT:_>' ) : <EOL> self . __dict__ [ name ] = value <EOL> else : <EOL> self [ name ] . assign ( value ) <EOL> def __getitem__ ( self , name ) : <EOL> if name not in self . _attrs : <EOL> self . _attrs [ name ] = ModuleAttribute ( self . __name__ , name ) <EOL> return self . _attrs [ name ] <EOL> @ classmethod <EOL> def imports ( cls , module ) : <EOL> return type ( '<STR_LIT>' , ( cls , ) , { '<STR_LIT>' : module } ) <EOL> @ classmethod <EOL> def find_module ( cls , fullname , path = None ) : <EOL> if fullname in cls . _modules : <EOL> return cls <EOL> @ classmethod <EOL> def load_module ( cls , fullname ) : <EOL> module = cls . _modules [ fullname ] <EOL> module . __loader__ = cls <EOL> module . __file__ = "<STR_LIT>" % cls . __name__ <EOL> module . __package__ = fullname . rpartition ( '<STR_LIT:.>' ) [ <NUM_LIT:0> ] <EOL> module . _import ( ) <EOL> sys . modules . setdefault ( fullname , module ) <EOL> return module </s>
