<s> """<STR_LIT>""" <EOL> from warnings import warnpy3k <EOL> warnpy3k ( "<STR_LIT>" , stacklevel = <NUM_LIT:2> ) <EOL> del warnpy3k <EOL> import imp <EOL> import sys <EOL> import __builtin__ <EOL> import struct <EOL> import marshal <EOL> __all__ = [ "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT>" ] <EOL> _StringType = type ( '<STR_LIT>' ) <EOL> _ModuleType = type ( sys ) <EOL> class ImportManager : <EOL> "<STR_LIT>" <EOL> def install ( self , namespace = vars ( __builtin__ ) ) : <EOL> "<STR_LIT>" <EOL> if isinstance ( namespace , _ModuleType ) : <EOL> namespace = vars ( namespace ) <EOL> self . previous_importer = namespace [ '<STR_LIT>' ] <EOL> self . namespace = namespace <EOL> namespace [ '<STR_LIT>' ] = self . _import_hook <EOL> def uninstall ( self ) : <EOL> "<STR_LIT>" <EOL> self . namespace [ '<STR_LIT>' ] = self . previous_importer <EOL> def add_suffix ( self , suffix , importFunc ) : <EOL> assert hasattr ( importFunc , '<STR_LIT>' ) <EOL> self . fs_imp . add_suffix ( suffix , importFunc ) <EOL> clsFilesystemImporter = None <EOL> def __init__ ( self , fs_imp = None ) : <EOL> if not _os_stat : <EOL> _os_bootstrap ( ) <EOL> if fs_imp is None : <EOL> cls = self . clsFilesystemImporter or _FilesystemImporter <EOL> fs_imp = cls ( ) <EOL> self . fs_imp = fs_imp <EOL> for desc in imp . get_suffixes ( ) : <EOL> if desc [ <NUM_LIT:2> ] == imp . C_EXTENSION : <EOL> self . add_suffix ( desc [ <NUM_LIT:0> ] , <EOL> DynLoadSuffixImporter ( desc ) . import_file ) <EOL> self . add_suffix ( '<STR_LIT>' , py_suffix_importer ) <EOL> def _import_hook ( self , fqname , globals = None , locals = None , fromlist = None ) : <EOL> """<STR_LIT>""" <EOL> parts = fqname . split ( '<STR_LIT:.>' ) <EOL> parent = self . _determine_import_context ( globals ) <EOL> if parent : <EOL> module = parent . __importer__ . _do_import ( parent , parts , fromlist ) <EOL> if module : <EOL> return module <EOL> try : <EOL> top_module = sys . modules [ parts [ <NUM_LIT:0> ] ] <EOL> except KeyError : <EOL> top_module = self . _import_top_module ( parts [ <NUM_LIT:0> ] ) <EOL> if not top_module : <EOL> raise ImportError , '<STR_LIT>' + fqname <EOL> if len ( parts ) == <NUM_LIT:1> : <EOL> if not fromlist : <EOL> return top_module <EOL> if not top_module . __dict__ . get ( '<STR_LIT>' ) : <EOL> return top_module <EOL> importer = top_module . __dict__ . get ( '<STR_LIT>' ) <EOL> if importer : <EOL> return importer . _finish_import ( top_module , parts [ <NUM_LIT:1> : ] , fromlist ) <EOL> if len ( parts ) == <NUM_LIT:2> and hasattr ( top_module , parts [ <NUM_LIT:1> ] ) : <EOL> if fromlist : <EOL> return getattr ( top_module , parts [ <NUM_LIT:1> ] ) <EOL> else : <EOL> return top_module <EOL> raise ImportError , '<STR_LIT>' + fqname <EOL> def _determine_import_context ( self , globals ) : <EOL> """<STR_LIT>""" <EOL> if not globals or not globals . get ( '<STR_LIT>' ) : <EOL> return None <EOL> parent_fqname = globals [ '<STR_LIT>' ] <EOL> if globals [ '<STR_LIT>' ] : <EOL> parent = sys . modules [ parent_fqname ] <EOL> assert globals is parent . __dict__ <EOL> return parent <EOL> i = parent_fqname . rfind ( '<STR_LIT:.>' ) <EOL> if i == - <NUM_LIT:1> : <EOL> return None <EOL> parent_fqname = parent_fqname [ : i ] <EOL> parent = sys . modules [ parent_fqname ] <EOL> assert parent . __name__ == parent_fqname <EOL> return parent <EOL> def _import_top_module ( self , name ) : <EOL> for item in sys . path : <EOL> if isinstance ( item , _StringType ) : <EOL> module = self . fs_imp . import_from_dir ( item , name ) <EOL> else : <EOL> module = item . import_top ( name ) <EOL> if module : <EOL> return module <EOL> return None <EOL> def _reload_hook ( self , module ) : <EOL> "<STR_LIT>" <EOL> importer = module . __dict__ . get ( '<STR_LIT>' ) <EOL> if not importer : <EOL> pass <EOL> raise SystemError , "<STR_LIT>" <EOL> class Importer : <EOL> "<STR_LIT>" <EOL> def import_top ( self , name ) : <EOL> "<STR_LIT>" <EOL> return self . _import_one ( None , name , name ) <EOL> def _finish_import ( self , top , parts , fromlist ) : <EOL> bottom = self . _load_tail ( top , parts ) <EOL> if not fromlist : <EOL> return top <EOL> if bottom . __ispkg__ : <EOL> self . _import_fromlist ( bottom , fromlist ) <EOL> return bottom <EOL> def _import_one ( self , parent , modname , fqname ) : <EOL> "<STR_LIT>" <EOL> try : <EOL> return sys . modules [ fqname ] <EOL> except KeyError : <EOL> pass <EOL> result = self . get_code ( parent , modname , fqname ) <EOL> if result is None : <EOL> return None <EOL> module = self . _process_result ( result , fqname ) <EOL> if parent : <EOL> setattr ( parent , modname , module ) <EOL> return module <EOL> def _process_result ( self , result , fqname ) : <EOL> ispkg , code , values = result <EOL> is_module = isinstance ( code , _ModuleType ) <EOL> if is_module : <EOL> module = code <EOL> else : <EOL> module = imp . new_module ( fqname ) <EOL> module . __importer__ = self <EOL> module . __ispkg__ = ispkg <EOL> module . __dict__ . update ( values ) <EOL> sys . modules [ fqname ] = module <EOL> if not is_module : <EOL> try : <EOL> exec code in module . __dict__ <EOL> except : <EOL> if fqname in sys . modules : <EOL> del sys . modules [ fqname ] <EOL> raise <EOL> module = sys . modules [ fqname ] <EOL> module . __name__ = fqname <EOL> return module <EOL> def _load_tail ( self , m , parts ) : <EOL> """<STR_LIT>""" <EOL> for part in parts : <EOL> fqname = "<STR_LIT>" % ( m . __name__ , part ) <EOL> m = self . _import_one ( m , part , fqname ) <EOL> if not m : <EOL> raise ImportError , "<STR_LIT>" + fqname <EOL> return m <EOL> def _import_fromlist ( self , package , fromlist ) : <EOL> '<STR_LIT>' <EOL> if '<STR_LIT:*>' in fromlist : <EOL> fromlist = list ( fromlist ) + list ( package . __dict__ . get ( '<STR_LIT>' , [ ] ) ) <EOL> for sub in fromlist : <EOL> if sub != '<STR_LIT:*>' and not hasattr ( package , sub ) : <EOL> subname = "<STR_LIT>" % ( package . __name__ , sub ) <EOL> submod = self . _import_one ( package , sub , subname ) <EOL> if not submod : <EOL> raise ImportError , "<STR_LIT>" + subname <EOL> def _do_import ( self , parent , parts , fromlist ) : <EOL> """<STR_LIT>""" <EOL> top_name = parts [ <NUM_LIT:0> ] <EOL> top_fqname = parent . __name__ + '<STR_LIT:.>' + top_name <EOL> top_module = self . _import_one ( parent , top_name , top_fqname ) <EOL> if not top_module : <EOL> return None <EOL> return self . _finish_import ( top_module , parts [ <NUM_LIT:1> : ] , fromlist ) <EOL> def get_code ( self , parent , modname , fqname ) : <EOL> """<STR_LIT>""" <EOL> raise RuntimeError , "<STR_LIT>" <EOL> _suffix_char = __debug__ and '<STR_LIT:c>' or '<STR_LIT:o>' <EOL> _suffix = '<STR_LIT>' + _suffix_char <EOL> def _compile ( pathname , timestamp ) : <EOL> """<STR_LIT>""" <EOL> codestring = open ( pathname , '<STR_LIT>' ) . read ( ) <EOL> if codestring and codestring [ - <NUM_LIT:1> ] != '<STR_LIT:\n>' : <EOL> codestring = codestring + '<STR_LIT:\n>' <EOL> code = __builtin__ . compile ( codestring , pathname , '<STR_LIT>' ) <EOL> try : <EOL> f = open ( pathname + _suffix_char , '<STR_LIT:wb>' ) <EOL> except IOError : <EOL> pass <EOL> else : <EOL> f . write ( '<STR_LIT>' ) <EOL> f . write ( struct . pack ( '<STR_LIT>' , timestamp ) ) <EOL> marshal . dump ( code , f ) <EOL> f . flush ( ) <EOL> f . seek ( <NUM_LIT:0> , <NUM_LIT:0> ) <EOL> f . write ( imp . get_magic ( ) ) <EOL> f . close ( ) <EOL> return code <EOL> _os_stat = _os_path_join = None <EOL> def _os_bootstrap ( ) : <EOL> "<STR_LIT>" <EOL> names = sys . builtin_module_names <EOL> join = None <EOL> if '<STR_LIT>' in names : <EOL> sep = '<STR_LIT:/>' <EOL> from posix import stat <EOL> elif '<STR_LIT>' in names : <EOL> sep = '<STR_LIT:\\>' <EOL> from nt import stat <EOL> elif '<STR_LIT>' in names : <EOL> sep = '<STR_LIT:\\>' <EOL> from dos import stat <EOL> elif '<STR_LIT>' in names : <EOL> sep = '<STR_LIT:\\>' <EOL> from os2 import stat <EOL> else : <EOL> raise ImportError , '<STR_LIT>' <EOL> if join is None : <EOL> def join ( a , b , sep = sep ) : <EOL> if a == '<STR_LIT>' : <EOL> return b <EOL> lastchar = a [ - <NUM_LIT:1> : ] <EOL> if lastchar == '<STR_LIT:/>' or lastchar == sep : <EOL> return a + b <EOL> return a + sep + b <EOL> global _os_stat <EOL> _os_stat = stat <EOL> global _os_path_join <EOL> _os_path_join = join <EOL> def _os_path_isdir ( pathname ) : <EOL> "<STR_LIT>" <EOL> try : <EOL> s = _os_stat ( pathname ) <EOL> except OSError : <EOL> return None <EOL> return ( s . st_mode & <NUM_LIT:0> <NUM_LIT> ) == <NUM_LIT> <NUM_LIT> <EOL> def _timestamp ( pathname ) : <EOL> "<STR_LIT>" <EOL> try : <EOL> s = _os_stat ( pathname ) <EOL> except OSError : <EOL> return None <EOL> return long ( s . st_mtime ) <EOL> class BuiltinImporter ( Importer ) : <EOL> def get_code ( self , parent , modname , fqname ) : <EOL> if parent : <EOL> return None <EOL> if imp . is_builtin ( modname ) : <EOL> type = imp . C_BUILTIN <EOL> elif imp . is_frozen ( modname ) : <EOL> type = imp . PY_FROZEN <EOL> else : <EOL> return None <EOL> module = imp . load_module ( modname , None , modname , ( '<STR_LIT>' , '<STR_LIT>' , type ) ) <EOL> return <NUM_LIT:0> , module , { } <EOL> class _FilesystemImporter ( Importer ) : <EOL> def __init__ ( self ) : <EOL> self . suffixes = [ ] <EOL> def add_suffix ( self , suffix , importFunc ) : <EOL> assert hasattr ( importFunc , '<STR_LIT>' ) <EOL> self . suffixes . append ( ( suffix , importFunc ) ) <EOL> def import_from_dir ( self , dir , fqname ) : <EOL> result = self . _import_pathname ( _os_path_join ( dir , fqname ) , fqname ) <EOL> if result : <EOL> return self . _process_result ( result , fqname ) <EOL> return None <EOL> def get_code ( self , parent , modname , fqname ) : <EOL> assert parent <EOL> for submodule_path in parent . __path__ : <EOL> code = self . _import_pathname ( _os_path_join ( submodule_path , modname ) , fqname ) <EOL> if code is not None : <EOL> return code <EOL> return self . _import_pathname ( _os_path_join ( parent . __pkgdir__ , modname ) , <EOL> fqname ) <EOL> def _import_pathname ( self , pathname , fqname ) : <EOL> if _os_path_isdir ( pathname ) : <EOL> result = self . _import_pathname ( _os_path_join ( pathname , '<STR_LIT>' ) , <EOL> fqname ) <EOL> if result : <EOL> values = result [ <NUM_LIT:2> ] <EOL> values [ '<STR_LIT>' ] = pathname <EOL> values [ '<STR_LIT>' ] = [ pathname ] <EOL> return <NUM_LIT:1> , result [ <NUM_LIT:1> ] , values <EOL> return None <EOL> for suffix , importFunc in self . suffixes : <EOL> filename = pathname + suffix <EOL> try : <EOL> finfo = _os_stat ( filename ) <EOL> except OSError : <EOL> pass <EOL> else : <EOL> return importFunc ( filename , finfo , fqname ) <EOL> return None <EOL> def py_suffix_importer ( filename , finfo , fqname ) : <EOL> file = filename [ : - <NUM_LIT:3> ] + _suffix <EOL> t_py = long ( finfo [ <NUM_LIT:8> ] ) <EOL> t_pyc = _timestamp ( file ) <EOL> code = None <EOL> if t_pyc is not None and t_pyc >= t_py : <EOL> f = open ( file , '<STR_LIT:rb>' ) <EOL> if f . read ( <NUM_LIT:4> ) == imp . get_magic ( ) : <EOL> t = struct . unpack ( '<STR_LIT>' , f . read ( <NUM_LIT:4> ) ) [ <NUM_LIT:0> ] <EOL> if t == t_py : <EOL> code = marshal . load ( f ) <EOL> f . close ( ) <EOL> if code is None : <EOL> file = filename <EOL> code = _compile ( file , t_py ) <EOL> return <NUM_LIT:0> , code , { '<STR_LIT>' : file } <EOL> class DynLoadSuffixImporter : <EOL> def __init__ ( self , desc ) : <EOL> self . desc = desc <EOL> def import_file ( self , filename , finfo , fqname ) : <EOL> fp = open ( filename , self . desc [ <NUM_LIT:1> ] ) <EOL> module = imp . load_module ( fqname , fp , filename , self . desc ) <EOL> module . __file__ = filename <EOL> return <NUM_LIT:0> , module , { } <EOL> def _print_importers ( ) : <EOL> items = sys . modules . items ( ) <EOL> items . sort ( ) <EOL> for name , module in items : <EOL> if module : <EOL> print name , module . __dict__ . get ( '<STR_LIT>' , '<STR_LIT>' ) <EOL> else : <EOL> print name , '<STR_LIT>' <EOL> def _test_revamp ( ) : <EOL> ImportManager ( ) . install ( ) <EOL> sys . path . insert ( <NUM_LIT:0> , BuiltinImporter ( ) ) </s>
