<s> """<STR_LIT>""" <EOL> import os <EOL> import sys <EOL> import imp <EOL> import os . path <EOL> from types import ModuleType <EOL> __all__ = [ <EOL> '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , <EOL> '<STR_LIT>' , '<STR_LIT>' , <EOL> '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , <EOL> ] <EOL> def read_code ( stream ) : <EOL> import marshal <EOL> magic = stream . read ( <NUM_LIT:4> ) <EOL> if magic != imp . get_magic ( ) : <EOL> return None <EOL> stream . read ( <NUM_LIT:4> ) <EOL> return marshal . load ( stream ) <EOL> def simplegeneric ( func ) : <EOL> """<STR_LIT>""" <EOL> registry = { } <EOL> def wrapper ( * args , ** kw ) : <EOL> ob = args [ <NUM_LIT:0> ] <EOL> try : <EOL> cls = ob . __class__ <EOL> except AttributeError : <EOL> cls = type ( ob ) <EOL> try : <EOL> mro = cls . __mro__ <EOL> except AttributeError : <EOL> try : <EOL> class cls ( cls , object ) : <EOL> pass <EOL> mro = cls . __mro__ [ <NUM_LIT:1> : ] <EOL> except TypeError : <EOL> mro = object , <EOL> for t in mro : <EOL> if t in registry : <EOL> return registry [ t ] ( * args , ** kw ) <EOL> else : <EOL> return func ( * args , ** kw ) <EOL> try : <EOL> wrapper . __name__ = func . __name__ <EOL> except ( TypeError , AttributeError ) : <EOL> pass <EOL> def register ( typ , func = None ) : <EOL> if func is None : <EOL> return lambda f : register ( typ , f ) <EOL> registry [ typ ] = func <EOL> return func <EOL> wrapper . __dict__ = func . __dict__ <EOL> wrapper . __doc__ = func . __doc__ <EOL> wrapper . register = register <EOL> return wrapper <EOL> def walk_packages ( path = None , prefix = '<STR_LIT>' , onerror = None ) : <EOL> """<STR_LIT>""" <EOL> def seen ( p , m = { } ) : <EOL> if p in m : <EOL> return True <EOL> m [ p ] = True <EOL> for importer , name , ispkg in iter_modules ( path , prefix ) : <EOL> yield importer , name , ispkg <EOL> if ispkg : <EOL> try : <EOL> __import__ ( name ) <EOL> except ImportError : <EOL> if onerror is not None : <EOL> onerror ( name ) <EOL> except Exception : <EOL> if onerror is not None : <EOL> onerror ( name ) <EOL> else : <EOL> raise <EOL> else : <EOL> path = getattr ( sys . modules [ name ] , '<STR_LIT>' , None ) or [ ] <EOL> path = [ p for p in path if not seen ( p ) ] <EOL> for item in walk_packages ( path , name + '<STR_LIT:.>' , onerror ) : <EOL> yield item <EOL> def iter_modules ( path = None , prefix = '<STR_LIT>' ) : <EOL> """<STR_LIT>""" <EOL> if path is None : <EOL> importers = iter_importers ( ) <EOL> else : <EOL> importers = map ( get_importer , path ) <EOL> yielded = { } <EOL> for i in importers : <EOL> for name , ispkg in iter_importer_modules ( i , prefix ) : <EOL> if name not in yielded : <EOL> yielded [ name ] = <NUM_LIT:1> <EOL> yield i , name , ispkg <EOL> def iter_importer_modules ( importer , prefix = '<STR_LIT>' ) : <EOL> if not hasattr ( importer , '<STR_LIT>' ) : <EOL> return [ ] <EOL> return importer . iter_modules ( prefix ) <EOL> iter_importer_modules = simplegeneric ( iter_importer_modules ) <EOL> class ImpImporter : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , path = None ) : <EOL> self . path = path <EOL> def find_module ( self , fullname , path = None ) : <EOL> subname = fullname . split ( "<STR_LIT:.>" ) [ - <NUM_LIT:1> ] <EOL> if subname != fullname and self . path is None : <EOL> return None <EOL> if self . path is None : <EOL> path = None <EOL> else : <EOL> path = [ os . path . realpath ( self . path ) ] <EOL> try : <EOL> file , filename , etc = imp . find_module ( subname , path ) <EOL> except ImportError : <EOL> return None <EOL> return ImpLoader ( fullname , file , filename , etc ) <EOL> def iter_modules ( self , prefix = '<STR_LIT>' ) : <EOL> if self . path is None or not os . path . isdir ( self . path ) : <EOL> return <EOL> yielded = { } <EOL> import inspect <EOL> filenames = os . listdir ( self . path ) <EOL> filenames . sort ( ) <EOL> for fn in filenames : <EOL> modname = inspect . getmodulename ( fn ) <EOL> if modname == '<STR_LIT>' or modname in yielded : <EOL> continue <EOL> path = os . path . join ( self . path , fn ) <EOL> ispkg = False <EOL> if not modname and os . path . isdir ( path ) and '<STR_LIT:.>' not in fn : <EOL> modname = fn <EOL> for fn in os . listdir ( path ) : <EOL> subname = inspect . getmodulename ( fn ) <EOL> if subname == '<STR_LIT>' : <EOL> ispkg = True <EOL> break <EOL> else : <EOL> continue <EOL> if modname and '<STR_LIT:.>' not in modname : <EOL> yielded [ modname ] = <NUM_LIT:1> <EOL> yield prefix + modname , ispkg <EOL> class ImpLoader : <EOL> """<STR_LIT>""" <EOL> code = source = None <EOL> def __init__ ( self , fullname , file , filename , etc ) : <EOL> self . file = file <EOL> self . filename = filename <EOL> self . fullname = fullname <EOL> self . etc = etc <EOL> def load_module ( self , fullname ) : <EOL> self . _reopen ( ) <EOL> try : <EOL> mod = imp . load_module ( fullname , self . file , self . filename , self . etc ) <EOL> finally : <EOL> if self . file : <EOL> self . file . close ( ) <EOL> return mod <EOL> def get_data ( self , pathname ) : <EOL> return open ( pathname , "<STR_LIT:rb>" ) . read ( ) <EOL> def _reopen ( self ) : <EOL> if self . file and self . file . closed : <EOL> mod_type = self . etc [ <NUM_LIT:2> ] <EOL> if mod_type == imp . PY_SOURCE : <EOL> self . file = open ( self . filename , '<STR_LIT>' ) <EOL> elif mod_type in ( imp . PY_COMPILED , imp . C_EXTENSION ) : <EOL> self . file = open ( self . filename , '<STR_LIT:rb>' ) <EOL> def _fix_name ( self , fullname ) : <EOL> if fullname is None : <EOL> fullname = self . fullname <EOL> elif fullname != self . fullname : <EOL> raise ImportError ( "<STR_LIT>" <EOL> "<STR_LIT>" % ( self . fullname , fullname ) ) <EOL> return fullname <EOL> def is_package ( self , fullname ) : <EOL> fullname = self . _fix_name ( fullname ) <EOL> return self . etc [ <NUM_LIT:2> ] == imp . PKG_DIRECTORY <EOL> def get_code ( self , fullname = None ) : <EOL> fullname = self . _fix_name ( fullname ) <EOL> if self . code is None : <EOL> mod_type = self . etc [ <NUM_LIT:2> ] <EOL> if mod_type == imp . PY_SOURCE : <EOL> source = self . get_source ( fullname ) <EOL> self . code = compile ( source , self . filename , '<STR_LIT>' ) <EOL> elif mod_type == imp . PY_COMPILED : <EOL> self . _reopen ( ) <EOL> try : <EOL> self . code = read_code ( self . file ) <EOL> finally : <EOL> self . file . close ( ) <EOL> elif mod_type == imp . PKG_DIRECTORY : <EOL> self . code = self . _get_delegate ( ) . get_code ( ) <EOL> return self . code <EOL> def get_source ( self , fullname = None ) : <EOL> fullname = self . _fix_name ( fullname ) <EOL> if self . source is None : <EOL> mod_type = self . etc [ <NUM_LIT:2> ] <EOL> if mod_type == imp . PY_SOURCE : <EOL> self . _reopen ( ) <EOL> try : <EOL> self . source = self . file . read ( ) <EOL> finally : <EOL> self . file . close ( ) <EOL> elif mod_type == imp . PY_COMPILED : <EOL> if os . path . exists ( self . filename [ : - <NUM_LIT:1> ] ) : <EOL> f = open ( self . filename [ : - <NUM_LIT:1> ] , '<STR_LIT>' ) <EOL> self . source = f . read ( ) <EOL> f . close ( ) <EOL> elif mod_type == imp . PKG_DIRECTORY : <EOL> self . source = self . _get_delegate ( ) . get_source ( ) <EOL> return self . source <EOL> def _get_delegate ( self ) : <EOL> return ImpImporter ( self . filename ) . find_module ( '<STR_LIT>' ) <EOL> def get_filename ( self , fullname = None ) : <EOL> fullname = self . _fix_name ( fullname ) <EOL> mod_type = self . etc [ <NUM_LIT:2> ] <EOL> if self . etc [ <NUM_LIT:2> ] == imp . PKG_DIRECTORY : <EOL> return self . _get_delegate ( ) . get_filename ( ) <EOL> elif self . etc [ <NUM_LIT:2> ] in ( imp . PY_SOURCE , imp . PY_COMPILED , imp . C_EXTENSION ) : <EOL> return self . filename <EOL> return None <EOL> try : <EOL> import zipimport <EOL> from zipimport import zipimporter <EOL> def iter_zipimport_modules ( importer , prefix = '<STR_LIT>' ) : <EOL> dirlist = zipimport . _zip_directory_cache [ importer . archive ] . keys ( ) <EOL> dirlist . sort ( ) <EOL> _prefix = importer . prefix <EOL> plen = len ( _prefix ) <EOL> yielded = { } <EOL> import inspect <EOL> for fn in dirlist : <EOL> if not fn . startswith ( _prefix ) : <EOL> continue <EOL> fn = fn [ plen : ] . split ( os . sep ) <EOL> if len ( fn ) == <NUM_LIT:2> and fn [ <NUM_LIT:1> ] . startswith ( '<STR_LIT>' ) : <EOL> if fn [ <NUM_LIT:0> ] not in yielded : <EOL> yielded [ fn [ <NUM_LIT:0> ] ] = <NUM_LIT:1> <EOL> yield fn [ <NUM_LIT:0> ] , True <EOL> if len ( fn ) != <NUM_LIT:1> : <EOL> continue <EOL> modname = inspect . getmodulename ( fn [ <NUM_LIT:0> ] ) <EOL> if modname == '<STR_LIT>' : <EOL> continue <EOL> if modname and '<STR_LIT:.>' not in modname and modname not in yielded : <EOL> yielded [ modname ] = <NUM_LIT:1> <EOL> yield prefix + modname , False <EOL> iter_importer_modules . register ( zipimporter , iter_zipimport_modules ) <EOL> except ImportError : <EOL> pass <EOL> def get_importer ( path_item ) : <EOL> """<STR_LIT>""" <EOL> try : <EOL> importer = sys . path_importer_cache [ path_item ] <EOL> except KeyError : <EOL> for path_hook in sys . path_hooks : <EOL> try : <EOL> importer = path_hook ( path_item ) <EOL> break <EOL> except ImportError : <EOL> pass <EOL> else : <EOL> importer = None <EOL> sys . path_importer_cache . setdefault ( path_item , importer ) <EOL> if importer is None : <EOL> try : <EOL> importer = ImpImporter ( path_item ) <EOL> except ImportError : <EOL> importer = None <EOL> return importer <EOL> def iter_importers ( fullname = "<STR_LIT>" ) : <EOL> """<STR_LIT>""" <EOL> if fullname . startswith ( '<STR_LIT:.>' ) : <EOL> raise ImportError ( "<STR_LIT>" ) <EOL> if '<STR_LIT:.>' in fullname : <EOL> pkg = '<STR_LIT:.>' . join ( fullname . split ( '<STR_LIT:.>' ) [ : - <NUM_LIT:1> ] ) <EOL> if pkg not in sys . modules : <EOL> __import__ ( pkg ) <EOL> path = getattr ( sys . modules [ pkg ] , '<STR_LIT>' , None ) or [ ] <EOL> else : <EOL> for importer in sys . meta_path : <EOL> yield importer <EOL> path = sys . path <EOL> for item in path : <EOL> yield get_importer ( item ) <EOL> if '<STR_LIT:.>' not in fullname : <EOL> yield ImpImporter ( ) <EOL> def get_loader ( module_or_name ) : <EOL> """<STR_LIT>""" <EOL> if module_or_name in sys . modules : <EOL> module_or_name = sys . modules [ module_or_name ] <EOL> if isinstance ( module_or_name , ModuleType ) : <EOL> module = module_or_name <EOL> loader = getattr ( module , '<STR_LIT>' , None ) <EOL> if loader is not None : <EOL> return loader <EOL> fullname = module . __name__ <EOL> else : <EOL> fullname = module_or_name <EOL> return find_loader ( fullname ) <EOL> def find_loader ( fullname ) : <EOL> """<STR_LIT>""" <EOL> for importer in iter_importers ( fullname ) : <EOL> loader = importer . find_module ( fullname ) <EOL> if loader is not None : <EOL> return loader <EOL> return None <EOL> def extend_path ( path , name ) : <EOL> """<STR_LIT>""" <EOL> if not isinstance ( path , list ) : <EOL> return path <EOL> pname = os . path . join ( * name . split ( '<STR_LIT:.>' ) ) <EOL> sname = os . extsep . join ( name . split ( '<STR_LIT:.>' ) ) <EOL> sname_pkg = sname + os . extsep + "<STR_LIT>" <EOL> init_py = "<STR_LIT>" + os . extsep + "<STR_LIT>" <EOL> path = path [ : ] <EOL> for dir in sys . path : <EOL> if not isinstance ( dir , basestring ) or not os . path . isdir ( dir ) : <EOL> continue <EOL> subdir = os . path . join ( dir , pname ) <EOL> initfile = os . path . join ( subdir , init_py ) <EOL> if subdir not in path and os . path . isfile ( initfile ) : <EOL> path . append ( subdir ) <EOL> pkgfile = os . path . join ( dir , sname_pkg ) <EOL> if os . path . isfile ( pkgfile ) : <EOL> try : <EOL> f = open ( pkgfile ) <EOL> except IOError , msg : <EOL> sys . stderr . write ( "<STR_LIT>" % <EOL> ( pkgfile , msg ) ) <EOL> else : <EOL> for line in f : <EOL> line = line . rstrip ( '<STR_LIT:\n>' ) <EOL> if not line or line . startswith ( '<STR_LIT:#>' ) : <EOL> continue <EOL> path . append ( line ) <EOL> f . close ( ) <EOL> return path <EOL> def get_data ( package , resource ) : <EOL> """<STR_LIT>""" <EOL> loader = get_loader ( package ) <EOL> if loader is None or not hasattr ( loader , '<STR_LIT>' ) : <EOL> return None <EOL> mod = sys . modules . get ( package ) or loader . load_module ( package ) <EOL> if mod is None or not hasattr ( mod , '<STR_LIT>' ) : <EOL> return None <EOL> parts = resource . split ( '<STR_LIT:/>' ) <EOL> parts . insert ( <NUM_LIT:0> , os . path . dirname ( mod . __file__ ) ) <EOL> resource_name = os . path . join ( * parts ) <EOL> return loader . get_data ( resource_name ) </s>
