<s> """<STR_LIT>""" <EOL> __metaclass__ = type <EOL> from os . path import dirname , split as splitpath <EOL> import sys <EOL> import zipimport <EOL> import inspect <EOL> import warnings <EOL> from zope . interface import Interface , implements <EOL> from twisted . python . components import registerAdapter <EOL> from twisted . python . filepath import FilePath , UnlistableError <EOL> from twisted . python . zippath import ZipArchive <EOL> from twisted . python . reflect import namedAny <EOL> _nothing = object ( ) <EOL> PYTHON_EXTENSIONS = [ '<STR_LIT>' ] <EOL> OPTIMIZED_MODE = __doc__ is None <EOL> if OPTIMIZED_MODE : <EOL> PYTHON_EXTENSIONS . append ( '<STR_LIT>' ) <EOL> else : <EOL> PYTHON_EXTENSIONS . append ( '<STR_LIT>' ) <EOL> def _isPythonIdentifier ( string ) : <EOL> """<STR_LIT>""" <EOL> return ( '<STR_LIT:U+0020>' not in string and <EOL> '<STR_LIT:.>' not in string and <EOL> '<STR_LIT:->' not in string ) <EOL> def _isPackagePath ( fpath ) : <EOL> extless = fpath . splitext ( ) [ <NUM_LIT:0> ] <EOL> basend = splitpath ( extless ) [ <NUM_LIT:1> ] <EOL> return basend == "<STR_LIT>" <EOL> class _ModuleIteratorHelper : <EOL> """<STR_LIT>""" <EOL> def iterModules ( self ) : <EOL> """<STR_LIT>""" <EOL> yielded = { } <EOL> if not self . filePath . exists ( ) : <EOL> return <EOL> for placeToLook in self . _packagePaths ( ) : <EOL> try : <EOL> children = placeToLook . children ( ) <EOL> except UnlistableError : <EOL> continue <EOL> children . sort ( ) <EOL> for potentialTopLevel in children : <EOL> ext = potentialTopLevel . splitext ( ) [ <NUM_LIT:1> ] <EOL> potentialBasename = potentialTopLevel . basename ( ) [ : - len ( ext ) ] <EOL> if ext in PYTHON_EXTENSIONS : <EOL> if not _isPythonIdentifier ( potentialBasename ) : <EOL> continue <EOL> modname = self . _subModuleName ( potentialBasename ) <EOL> if modname . split ( "<STR_LIT:.>" ) [ - <NUM_LIT:1> ] == '<STR_LIT>' : <EOL> continue <EOL> if modname not in yielded : <EOL> yielded [ modname ] = True <EOL> pm = PythonModule ( modname , potentialTopLevel , self . _getEntry ( ) ) <EOL> assert pm != self <EOL> yield pm <EOL> else : <EOL> if ( ext or not _isPythonIdentifier ( potentialBasename ) <EOL> or not potentialTopLevel . isdir ( ) ) : <EOL> continue <EOL> modname = self . _subModuleName ( potentialTopLevel . basename ( ) ) <EOL> for ext in PYTHON_EXTENSIONS : <EOL> initpy = potentialTopLevel . child ( "<STR_LIT>" + ext ) <EOL> if initpy . exists ( ) : <EOL> yielded [ modname ] = True <EOL> pm = PythonModule ( modname , initpy , self . _getEntry ( ) ) <EOL> assert pm != self <EOL> yield pm <EOL> break <EOL> def walkModules ( self , importPackages = False ) : <EOL> """<STR_LIT>""" <EOL> yield self <EOL> for package in self . iterModules ( ) : <EOL> for module in package . walkModules ( importPackages = importPackages ) : <EOL> yield module <EOL> def _subModuleName ( self , mn ) : <EOL> """<STR_LIT>""" <EOL> return mn <EOL> def _packagePaths ( self ) : <EOL> """<STR_LIT>""" <EOL> raise NotImplementedError ( ) <EOL> def _getEntry ( self ) : <EOL> """<STR_LIT>""" <EOL> raise NotImplementedError ( ) <EOL> def __getitem__ ( self , modname ) : <EOL> """<STR_LIT>""" <EOL> for module in self . iterModules ( ) : <EOL> if module . name == self . _subModuleName ( modname ) : <EOL> return module <EOL> raise KeyError ( modname ) <EOL> def __iter__ ( self ) : <EOL> """<STR_LIT>""" <EOL> raise NotImplementedError ( ) <EOL> class PythonAttribute : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , name , onObject , loaded , pythonValue ) : <EOL> """<STR_LIT>""" <EOL> self . name = name <EOL> self . onObject = onObject <EOL> self . _loaded = loaded <EOL> self . pythonValue = pythonValue <EOL> def __repr__ ( self ) : <EOL> return '<STR_LIT>' % ( self . name , ) <EOL> def isLoaded ( self ) : <EOL> """<STR_LIT>""" <EOL> return self . _loaded <EOL> def load ( self , default = _nothing ) : <EOL> """<STR_LIT>""" <EOL> return self . pythonValue <EOL> def iterAttributes ( self ) : <EOL> for name , val in inspect . getmembers ( self . load ( ) ) : <EOL> yield PythonAttribute ( self . name + '<STR_LIT:.>' + name , self , True , val ) <EOL> class PythonModule ( _ModuleIteratorHelper ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , name , filePath , pathEntry ) : <EOL> """<STR_LIT>""" <EOL> assert not name . endswith ( "<STR_LIT>" ) <EOL> self . name = name <EOL> self . filePath = filePath <EOL> self . parentPath = filePath . parent ( ) <EOL> self . pathEntry = pathEntry <EOL> def _getEntry ( self ) : <EOL> return self . pathEntry <EOL> def __repr__ ( self ) : <EOL> """<STR_LIT>""" <EOL> return '<STR_LIT>' % ( self . name , ) <EOL> def isLoaded ( self ) : <EOL> """<STR_LIT>""" <EOL> return self . pathEntry . pythonPath . moduleDict . get ( self . name ) is not None <EOL> def iterAttributes ( self ) : <EOL> """<STR_LIT>""" <EOL> if not self . isLoaded ( ) : <EOL> raise NotImplementedError ( <EOL> "<STR_LIT>" ) <EOL> for name , val in inspect . getmembers ( self . load ( ) ) : <EOL> yield PythonAttribute ( self . name + '<STR_LIT:.>' + name , self , True , val ) <EOL> def isPackage ( self ) : <EOL> """<STR_LIT>""" <EOL> return _isPackagePath ( self . filePath ) <EOL> def load ( self , default = _nothing ) : <EOL> """<STR_LIT>""" <EOL> try : <EOL> return self . pathEntry . pythonPath . moduleLoader ( self . name ) <EOL> except : <EOL> if default is not _nothing : <EOL> return default <EOL> raise <EOL> def __eq__ ( self , other ) : <EOL> """<STR_LIT>""" <EOL> if not isinstance ( other , PythonModule ) : <EOL> return False <EOL> return other . name == self . name <EOL> def __ne__ ( self , other ) : <EOL> """<STR_LIT>""" <EOL> if not isinstance ( other , PythonModule ) : <EOL> return True <EOL> return other . name != self . name <EOL> def walkModules ( self , importPackages = False ) : <EOL> if importPackages and self . isPackage ( ) : <EOL> self . load ( ) <EOL> return super ( PythonModule , self ) . walkModules ( importPackages = importPackages ) <EOL> def _subModuleName ( self , mn ) : <EOL> """<STR_LIT>""" <EOL> return self . name + '<STR_LIT:.>' + mn <EOL> def _packagePaths ( self ) : <EOL> """<STR_LIT>""" <EOL> if not self . isPackage ( ) : <EOL> return <EOL> if self . isLoaded ( ) : <EOL> load = self . load ( ) <EOL> if hasattr ( load , '<STR_LIT>' ) : <EOL> for fn in load . __path__ : <EOL> if fn == self . parentPath . path : <EOL> assert self . parentPath . exists ( ) <EOL> yield self . parentPath <EOL> else : <EOL> smp = self . pathEntry . pythonPath . _smartPath ( fn ) <EOL> if smp . exists ( ) : <EOL> yield smp <EOL> else : <EOL> yield self . parentPath <EOL> class PathEntry ( _ModuleIteratorHelper ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , filePath , pythonPath ) : <EOL> """<STR_LIT>""" <EOL> self . filePath = filePath <EOL> self . pythonPath = pythonPath <EOL> def _getEntry ( self ) : <EOL> return self <EOL> def __repr__ ( self ) : <EOL> return '<STR_LIT>' % ( self . filePath , ) <EOL> def _packagePaths ( self ) : <EOL> yield self . filePath <EOL> class IPathImportMapper ( Interface ) : <EOL> """<STR_LIT>""" <EOL> def mapPath ( self , pathLikeString ) : <EOL> """<STR_LIT>""" <EOL> class _DefaultMapImpl : <EOL> """<STR_LIT>""" <EOL> implements ( IPathImportMapper ) <EOL> def mapPath ( self , fsPathString ) : <EOL> return FilePath ( fsPathString ) <EOL> _theDefaultMapper = _DefaultMapImpl ( ) <EOL> class _ZipMapImpl : <EOL> """<STR_LIT>""" <EOL> implements ( IPathImportMapper ) <EOL> def __init__ ( self , importer ) : <EOL> self . importer = importer <EOL> def mapPath ( self , fsPathString ) : <EOL> """<STR_LIT>""" <EOL> za = ZipArchive ( self . importer . archive ) <EOL> myPath = FilePath ( self . importer . archive ) <EOL> itsPath = FilePath ( fsPathString ) <EOL> if myPath == itsPath : <EOL> return za <EOL> segs = itsPath . segmentsFrom ( myPath ) <EOL> zp = za <EOL> for seg in segs : <EOL> zp = zp . child ( seg ) <EOL> return zp <EOL> registerAdapter ( _ZipMapImpl , zipimport . zipimporter , IPathImportMapper ) <EOL> def _defaultSysPathFactory ( ) : <EOL> """<STR_LIT>""" <EOL> return sys . path <EOL> class PythonPath : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , <EOL> sysPath = None , <EOL> moduleDict = sys . modules , <EOL> sysPathHooks = sys . path_hooks , <EOL> importerCache = sys . path_importer_cache , <EOL> moduleLoader = namedAny , <EOL> sysPathFactory = None ) : <EOL> """<STR_LIT>""" <EOL> if sysPath is not None : <EOL> sysPathFactory = lambda : sysPath <EOL> elif sysPathFactory is None : <EOL> sysPathFactory = _defaultSysPathFactory <EOL> self . _sysPathFactory = sysPathFactory <EOL> self . _sysPath = sysPath <EOL> self . moduleDict = moduleDict <EOL> self . sysPathHooks = sysPathHooks <EOL> self . importerCache = importerCache <EOL> self . moduleLoader = moduleLoader <EOL> def _getSysPath ( self ) : <EOL> """<STR_LIT>""" <EOL> return self . _sysPathFactory ( ) <EOL> sysPath = property ( _getSysPath ) <EOL> def _findEntryPathString ( self , modobj ) : <EOL> """<STR_LIT>""" <EOL> topPackageObj = modobj <EOL> while '<STR_LIT:.>' in topPackageObj . __name__ : <EOL> topPackageObj = self . moduleDict [ '<STR_LIT:.>' . join ( <EOL> topPackageObj . __name__ . split ( '<STR_LIT:.>' ) [ : - <NUM_LIT:1> ] ) ] <EOL> if _isPackagePath ( FilePath ( topPackageObj . __file__ ) ) : <EOL> rval = dirname ( dirname ( topPackageObj . __file__ ) ) <EOL> else : <EOL> rval = dirname ( topPackageObj . __file__ ) <EOL> if rval not in self . importerCache : <EOL> warnings . warn ( <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" % ( <EOL> rval , modobj . __name__ ) , <EOL> stacklevel = <NUM_LIT:3> ) <EOL> return rval <EOL> def _smartPath ( self , pathName ) : <EOL> """<STR_LIT>""" <EOL> importr = self . importerCache . get ( pathName , _nothing ) <EOL> if importr is _nothing : <EOL> for hook in self . sysPathHooks : <EOL> try : <EOL> importr = hook ( pathName ) <EOL> except ImportError : <EOL> pass <EOL> if importr is _nothing : <EOL> importr = None <EOL> return IPathImportMapper ( importr , _theDefaultMapper ) . mapPath ( pathName ) <EOL> def iterEntries ( self ) : <EOL> """<STR_LIT>""" <EOL> for pathName in self . sysPath : <EOL> fp = self . _smartPath ( pathName ) <EOL> yield PathEntry ( fp , self ) <EOL> def __getitem__ ( self , modname ) : <EOL> """<STR_LIT>""" <EOL> moduleObject = self . moduleDict . get ( modname ) <EOL> if moduleObject is not None : <EOL> pe = PathEntry ( <EOL> self . _smartPath ( <EOL> self . _findEntryPathString ( moduleObject ) ) , <EOL> self ) <EOL> mp = self . _smartPath ( moduleObject . __file__ ) <EOL> return PythonModule ( modname , mp , pe ) <EOL> if '<STR_LIT:.>' in modname : <EOL> pkg = self <EOL> for name in modname . split ( '<STR_LIT:.>' ) : <EOL> pkg = pkg [ name ] <EOL> return pkg <EOL> for module in self . iterModules ( ) : <EOL> if module . name == modname : <EOL> return module <EOL> raise KeyError ( modname ) <EOL> def __repr__ ( self ) : <EOL> """<STR_LIT>""" <EOL> return "<STR_LIT>" % ( self . sysPath , self . moduleDict ) <EOL> def iterModules ( self ) : <EOL> """<STR_LIT>""" <EOL> for entry in self . iterEntries ( ) : <EOL> for module in entry . iterModules ( ) : <EOL> yield module <EOL> def walkModules ( self , importPackages = False ) : <EOL> """<STR_LIT>""" <EOL> for package in self . iterModules ( ) : <EOL> for module in package . walkModules ( importPackages = False ) : <EOL> yield module <EOL> theSystemPath = PythonPath ( ) <EOL> def walkModules ( importPackages = False ) : <EOL> """<STR_LIT>""" <EOL> return theSystemPath . walkModules ( importPackages = importPackages ) <EOL> def iterModules ( ) : <EOL> """<STR_LIT>""" <EOL> return theSystemPath . iterModules ( ) <EOL> def getModule ( moduleName ) : <EOL> """<STR_LIT>""" <EOL> return theSystemPath [ moduleName ] </s>
