<s> """<STR_LIT>""" <EOL> from warnings import warnpy3k , warn <EOL> warnpy3k ( "<STR_LIT>" , stacklevel = <NUM_LIT:2> ) <EOL> del warnpy3k <EOL> import __builtin__ <EOL> import imp <EOL> import os <EOL> import sys <EOL> __all__ = [ "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT>" , <EOL> "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT>" ] <EOL> VERBOSE = <NUM_LIT:0> <EOL> from imp import C_EXTENSION , PY_SOURCE , PY_COMPILED <EOL> from imp import C_BUILTIN , PY_FROZEN , PKG_DIRECTORY <EOL> BUILTIN_MODULE = C_BUILTIN <EOL> FROZEN_MODULE = PY_FROZEN <EOL> class _Verbose : <EOL> def __init__ ( self , verbose = VERBOSE ) : <EOL> self . verbose = verbose <EOL> def get_verbose ( self ) : <EOL> return self . verbose <EOL> def set_verbose ( self , verbose ) : <EOL> self . verbose = verbose <EOL> def note ( self , * args ) : <EOL> if self . verbose : <EOL> self . message ( * args ) <EOL> def message ( self , format , * args ) : <EOL> if args : <EOL> print format % args <EOL> else : <EOL> print format <EOL> class BasicModuleLoader ( _Verbose ) : <EOL> """<STR_LIT>""" <EOL> def find_module ( self , name , path = None ) : <EOL> if path is None : <EOL> path = [ None ] + self . default_path ( ) <EOL> for dir in path : <EOL> stuff = self . find_module_in_dir ( name , dir ) <EOL> if stuff : return stuff <EOL> return None <EOL> def default_path ( self ) : <EOL> return sys . path <EOL> def find_module_in_dir ( self , name , dir ) : <EOL> if dir is None : <EOL> return self . find_builtin_module ( name ) <EOL> else : <EOL> try : <EOL> return imp . find_module ( name , [ dir ] ) <EOL> except ImportError : <EOL> return None <EOL> def find_builtin_module ( self , name ) : <EOL> if imp . is_builtin ( name ) : <EOL> return None , '<STR_LIT>' , ( '<STR_LIT>' , '<STR_LIT>' , BUILTIN_MODULE ) <EOL> if imp . is_frozen ( name ) : <EOL> return None , '<STR_LIT>' , ( '<STR_LIT>' , '<STR_LIT>' , FROZEN_MODULE ) <EOL> return None <EOL> def load_module ( self , name , stuff ) : <EOL> file , filename , info = stuff <EOL> try : <EOL> return imp . load_module ( name , file , filename , info ) <EOL> finally : <EOL> if file : file . close ( ) <EOL> class Hooks ( _Verbose ) : <EOL> """<STR_LIT>""" <EOL> def get_suffixes ( self ) : return imp . get_suffixes ( ) <EOL> def new_module ( self , name ) : return imp . new_module ( name ) <EOL> def is_builtin ( self , name ) : return imp . is_builtin ( name ) <EOL> def init_builtin ( self , name ) : return imp . init_builtin ( name ) <EOL> def is_frozen ( self , name ) : return imp . is_frozen ( name ) <EOL> def init_frozen ( self , name ) : return imp . init_frozen ( name ) <EOL> def get_frozen_object ( self , name ) : return imp . get_frozen_object ( name ) <EOL> def load_source ( self , name , filename , file = None ) : <EOL> return imp . load_source ( name , filename , file ) <EOL> def load_compiled ( self , name , filename , file = None ) : <EOL> return imp . load_compiled ( name , filename , file ) <EOL> def load_dynamic ( self , name , filename , file = None ) : <EOL> return imp . load_dynamic ( name , filename , file ) <EOL> def load_package ( self , name , filename , file = None ) : <EOL> return imp . load_module ( name , file , filename , ( "<STR_LIT>" , "<STR_LIT>" , PKG_DIRECTORY ) ) <EOL> def add_module ( self , name ) : <EOL> d = self . modules_dict ( ) <EOL> if name in d : return d [ name ] <EOL> d [ name ] = m = self . new_module ( name ) <EOL> return m <EOL> def modules_dict ( self ) : return sys . modules <EOL> def default_path ( self ) : return sys . path <EOL> def path_split ( self , x ) : return os . path . split ( x ) <EOL> def path_join ( self , x , y ) : return os . path . join ( x , y ) <EOL> def path_isabs ( self , x ) : return os . path . isabs ( x ) <EOL> def path_exists ( self , x ) : return os . path . exists ( x ) <EOL> def path_isdir ( self , x ) : return os . path . isdir ( x ) <EOL> def path_isfile ( self , x ) : return os . path . isfile ( x ) <EOL> def path_islink ( self , x ) : return os . path . islink ( x ) <EOL> def openfile ( self , * x ) : return open ( * x ) <EOL> openfile_error = IOError <EOL> def listdir ( self , x ) : return os . listdir ( x ) <EOL> listdir_error = os . error <EOL> class ModuleLoader ( BasicModuleLoader ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , hooks = None , verbose = VERBOSE ) : <EOL> BasicModuleLoader . __init__ ( self , verbose ) <EOL> self . hooks = hooks or Hooks ( verbose ) <EOL> def default_path ( self ) : <EOL> return self . hooks . default_path ( ) <EOL> def modules_dict ( self ) : <EOL> return self . hooks . modules_dict ( ) <EOL> def get_hooks ( self ) : <EOL> return self . hooks <EOL> def set_hooks ( self , hooks ) : <EOL> self . hooks = hooks <EOL> def find_builtin_module ( self , name ) : <EOL> if self . hooks . is_builtin ( name ) : <EOL> return None , '<STR_LIT>' , ( '<STR_LIT>' , '<STR_LIT>' , BUILTIN_MODULE ) <EOL> if self . hooks . is_frozen ( name ) : <EOL> return None , '<STR_LIT>' , ( '<STR_LIT>' , '<STR_LIT>' , FROZEN_MODULE ) <EOL> return None <EOL> def find_module_in_dir ( self , name , dir , allow_packages = <NUM_LIT:1> ) : <EOL> if dir is None : <EOL> return self . find_builtin_module ( name ) <EOL> if allow_packages : <EOL> fullname = self . hooks . path_join ( dir , name ) <EOL> if self . hooks . path_isdir ( fullname ) : <EOL> stuff = self . find_module_in_dir ( "<STR_LIT>" , fullname , <NUM_LIT:0> ) <EOL> if stuff : <EOL> file = stuff [ <NUM_LIT:0> ] <EOL> if file : file . close ( ) <EOL> return None , fullname , ( '<STR_LIT>' , '<STR_LIT>' , PKG_DIRECTORY ) <EOL> for info in self . hooks . get_suffixes ( ) : <EOL> suff , mode , type = info <EOL> fullname = self . hooks . path_join ( dir , name + suff ) <EOL> try : <EOL> fp = self . hooks . openfile ( fullname , mode ) <EOL> return fp , fullname , info <EOL> except self . hooks . openfile_error : <EOL> pass <EOL> return None <EOL> def load_module ( self , name , stuff ) : <EOL> file , filename , info = stuff <EOL> ( suff , mode , type ) = info <EOL> try : <EOL> if type == BUILTIN_MODULE : <EOL> return self . hooks . init_builtin ( name ) <EOL> if type == FROZEN_MODULE : <EOL> return self . hooks . init_frozen ( name ) <EOL> if type == C_EXTENSION : <EOL> m = self . hooks . load_dynamic ( name , filename , file ) <EOL> elif type == PY_SOURCE : <EOL> m = self . hooks . load_source ( name , filename , file ) <EOL> elif type == PY_COMPILED : <EOL> m = self . hooks . load_compiled ( name , filename , file ) <EOL> elif type == PKG_DIRECTORY : <EOL> m = self . hooks . load_package ( name , filename , file ) <EOL> else : <EOL> raise ImportError , "<STR_LIT>" % ( type , name ) <EOL> finally : <EOL> if file : file . close ( ) <EOL> m . __file__ = filename <EOL> return m <EOL> class FancyModuleLoader ( ModuleLoader ) : <EOL> """<STR_LIT>""" <EOL> def load_module ( self , name , stuff ) : <EOL> file , filename , ( suff , mode , type ) = stuff <EOL> realfilename = filename <EOL> path = None <EOL> if type == PKG_DIRECTORY : <EOL> initstuff = self . find_module_in_dir ( "<STR_LIT>" , filename , <NUM_LIT:0> ) <EOL> if not initstuff : <EOL> raise ImportError , "<STR_LIT>" % name <EOL> initfile , initfilename , initinfo = initstuff <EOL> initsuff , initmode , inittype = initinfo <EOL> if inittype not in ( PY_COMPILED , PY_SOURCE ) : <EOL> if initfile : initfile . close ( ) <EOL> raise ImportError , "<STR_LIT>" % ( <EOL> inittype , name ) <EOL> path = [ filename ] <EOL> file = initfile <EOL> realfilename = initfilename <EOL> type = inittype <EOL> if type == FROZEN_MODULE : <EOL> code = self . hooks . get_frozen_object ( name ) <EOL> elif type == PY_COMPILED : <EOL> import marshal <EOL> file . seek ( <NUM_LIT:8> ) <EOL> code = marshal . load ( file ) <EOL> elif type == PY_SOURCE : <EOL> data = file . read ( ) <EOL> code = compile ( data , realfilename , '<STR_LIT>' ) <EOL> else : <EOL> return ModuleLoader . load_module ( self , name , stuff ) <EOL> m = self . hooks . add_module ( name ) <EOL> if path : <EOL> m . __path__ = path <EOL> m . __file__ = filename <EOL> try : <EOL> exec code in m . __dict__ <EOL> except : <EOL> d = self . hooks . modules_dict ( ) <EOL> if name in d : <EOL> del d [ name ] <EOL> raise <EOL> return m <EOL> class BasicModuleImporter ( _Verbose ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , loader = None , verbose = VERBOSE ) : <EOL> _Verbose . __init__ ( self , verbose ) <EOL> self . loader = loader or ModuleLoader ( None , verbose ) <EOL> self . modules = self . loader . modules_dict ( ) <EOL> def get_loader ( self ) : <EOL> return self . loader <EOL> def set_loader ( self , loader ) : <EOL> self . loader = loader <EOL> def get_hooks ( self ) : <EOL> return self . loader . get_hooks ( ) <EOL> def set_hooks ( self , hooks ) : <EOL> return self . loader . set_hooks ( hooks ) <EOL> def import_module ( self , name , globals = { } , locals = { } , fromlist = [ ] ) : <EOL> name = str ( name ) <EOL> if name in self . modules : <EOL> return self . modules [ name ] <EOL> stuff = self . loader . find_module ( name ) <EOL> if not stuff : <EOL> raise ImportError , "<STR_LIT>" % name <EOL> return self . loader . load_module ( name , stuff ) <EOL> def reload ( self , module , path = None ) : <EOL> name = str ( module . __name__ ) <EOL> stuff = self . loader . find_module ( name , path ) <EOL> if not stuff : <EOL> raise ImportError , "<STR_LIT>" % name <EOL> return self . loader . load_module ( name , stuff ) <EOL> def unload ( self , module ) : <EOL> del self . modules [ str ( module . __name__ ) ] <EOL> def install ( self ) : <EOL> self . save_import_module = __builtin__ . __import__ <EOL> self . save_reload = __builtin__ . reload <EOL> if not hasattr ( __builtin__ , '<STR_LIT>' ) : <EOL> __builtin__ . unload = None <EOL> self . save_unload = __builtin__ . unload <EOL> __builtin__ . __import__ = self . import_module <EOL> __builtin__ . reload = self . reload <EOL> __builtin__ . unload = self . unload <EOL> def uninstall ( self ) : <EOL> __builtin__ . __import__ = self . save_import_module <EOL> __builtin__ . reload = self . save_reload <EOL> __builtin__ . unload = self . save_unload <EOL> if not __builtin__ . unload : <EOL> del __builtin__ . unload <EOL> class ModuleImporter ( BasicModuleImporter ) : <EOL> """<STR_LIT>""" <EOL> def import_module ( self , name , globals = None , locals = None , fromlist = None , <EOL> level = - <NUM_LIT:1> ) : <EOL> parent = self . determine_parent ( globals , level ) <EOL> q , tail = self . find_head_package ( parent , str ( name ) ) <EOL> m = self . load_tail ( q , tail ) <EOL> if not fromlist : <EOL> return q <EOL> if hasattr ( m , "<STR_LIT>" ) : <EOL> self . ensure_fromlist ( m , fromlist ) <EOL> return m <EOL> def determine_parent ( self , globals , level = - <NUM_LIT:1> ) : <EOL> if not globals or not level : <EOL> return None <EOL> pkgname = globals . get ( '<STR_LIT>' ) <EOL> if pkgname is not None : <EOL> if not pkgname and level > <NUM_LIT:0> : <EOL> raise ValueError , '<STR_LIT>' <EOL> else : <EOL> modname = globals . get ( '<STR_LIT>' ) <EOL> if modname is None : <EOL> return None <EOL> if "<STR_LIT>" in globals : <EOL> pkgname = modname <EOL> else : <EOL> if '<STR_LIT:.>' not in modname : <EOL> if level > <NUM_LIT:0> : <EOL> raise ValueError , ( '<STR_LIT>' <EOL> '<STR_LIT>' ) <EOL> globals [ '<STR_LIT>' ] = None <EOL> return None <EOL> pkgname = modname . rpartition ( '<STR_LIT:.>' ) [ <NUM_LIT:0> ] <EOL> globals [ '<STR_LIT>' ] = pkgname <EOL> if level > <NUM_LIT:0> : <EOL> dot = len ( pkgname ) <EOL> for x in range ( level , <NUM_LIT:1> , - <NUM_LIT:1> ) : <EOL> try : <EOL> dot = pkgname . rindex ( '<STR_LIT:.>' , <NUM_LIT:0> , dot ) <EOL> except ValueError : <EOL> raise ValueError ( '<STR_LIT>' <EOL> '<STR_LIT>' ) <EOL> pkgname = pkgname [ : dot ] <EOL> try : <EOL> return sys . modules [ pkgname ] <EOL> except KeyError : <EOL> if level < <NUM_LIT:1> : <EOL> warn ( "<STR_LIT>" <EOL> "<STR_LIT>" % pkgname , RuntimeWarning , <NUM_LIT:1> ) <EOL> return None <EOL> else : <EOL> raise SystemError , ( "<STR_LIT>" <EOL> "<STR_LIT>" % pkgname ) <EOL> def find_head_package ( self , parent , name ) : <EOL> if '<STR_LIT:.>' in name : <EOL> i = name . find ( '<STR_LIT:.>' ) <EOL> head = name [ : i ] <EOL> tail = name [ i + <NUM_LIT:1> : ] <EOL> else : <EOL> head = name <EOL> tail = "<STR_LIT>" <EOL> if parent : <EOL> qname = "<STR_LIT>" % ( parent . __name__ , head ) <EOL> else : <EOL> qname = head <EOL> q = self . import_it ( head , qname , parent ) <EOL> if q : return q , tail <EOL> if parent : <EOL> qname = head <EOL> parent = None <EOL> q = self . import_it ( head , qname , parent ) <EOL> if q : return q , tail <EOL> raise ImportError , "<STR_LIT>" % qname <EOL> def load_tail ( self , q , tail ) : <EOL> m = q <EOL> while tail : <EOL> i = tail . find ( '<STR_LIT:.>' ) <EOL> if i < <NUM_LIT:0> : i = len ( tail ) <EOL> head , tail = tail [ : i ] , tail [ i + <NUM_LIT:1> : ] <EOL> mname = "<STR_LIT>" % ( m . __name__ , head ) <EOL> m = self . import_it ( head , mname , m ) <EOL> if not m : <EOL> raise ImportError , "<STR_LIT>" % mname <EOL> return m <EOL> def ensure_fromlist ( self , m , fromlist , recursive = <NUM_LIT:0> ) : <EOL> for sub in fromlist : <EOL> if sub == "<STR_LIT:*>" : <EOL> if not recursive : <EOL> try : <EOL> all = m . __all__ <EOL> except AttributeError : <EOL> pass <EOL> else : <EOL> self . ensure_fromlist ( m , all , <NUM_LIT:1> ) <EOL> continue <EOL> if sub != "<STR_LIT:*>" and not hasattr ( m , sub ) : <EOL> subname = "<STR_LIT>" % ( m . __name__ , sub ) <EOL> submod = self . import_it ( sub , subname , m ) <EOL> if not submod : <EOL> raise ImportError , "<STR_LIT>" % subname <EOL> def import_it ( self , partname , fqname , parent , force_load = <NUM_LIT:0> ) : <EOL> if not partname : <EOL> return parent <EOL> if not force_load : <EOL> try : <EOL> return self . modules [ fqname ] <EOL> except KeyError : <EOL> pass <EOL> try : <EOL> path = parent and parent . __path__ <EOL> except AttributeError : <EOL> return None <EOL> partname = str ( partname ) <EOL> stuff = self . loader . find_module ( partname , path ) <EOL> if not stuff : <EOL> return None <EOL> fqname = str ( fqname ) <EOL> m = self . loader . load_module ( fqname , stuff ) <EOL> if parent : <EOL> setattr ( parent , partname , m ) <EOL> return m <EOL> def reload ( self , module ) : <EOL> name = str ( module . __name__ ) <EOL> if '<STR_LIT:.>' not in name : <EOL> return self . import_it ( name , name , None , force_load = <NUM_LIT:1> ) <EOL> i = name . rfind ( '<STR_LIT:.>' ) <EOL> pname = name [ : i ] <EOL> parent = self . modules [ pname ] <EOL> return self . import_it ( name [ i + <NUM_LIT:1> : ] , name , parent , force_load = <NUM_LIT:1> ) <EOL> default_importer = None <EOL> current_importer = None <EOL> def install ( importer = None ) : <EOL> global current_importer <EOL> current_importer = importer or default_importer or ModuleImporter ( ) <EOL> current_importer . install ( ) <EOL> def uninstall ( ) : <EOL> global current_importer <EOL> current_importer . uninstall ( ) </s>
