<s> from types import ModuleType <EOL> import os . path , glob , imp , sys <EOL> NonExistent = object ( ) <EOL> def importModule ( moduleName ) : <EOL> """<STR_LIT>""" <EOL> mod = __import__ ( moduleName ) <EOL> return sys . modules [ moduleName ] <EOL> def importParent ( module ) : <EOL> dot = module . __name__ . rfind ( "<STR_LIT:.>" ) <EOL> parentName = module . __name__ [ <NUM_LIT:0> : dot ] if dot != - <NUM_LIT:1> else "<STR_LIT>" <EOL> return importObject ( parentName ) if parentName else None <EOL> def importObject ( objectName , base = None , finder = imp . find_module , loader = imp . load_module ) : <EOL> """<STR_LIT>""" <EOL> for name in objectName . split ( "<STR_LIT:.>" ) : <EOL> if base : <EOL> child = getattr ( base , name , NonExistent ) <EOL> if child is NonExistent : <EOL> if isinstance ( base , ModuleType ) : <EOL> base = _importFromModule ( name , base , finder , loader ) <EOL> else : <EOL> raise ImportError ( "<STR_LIT>" % objectName ) <EOL> else : <EOL> base = child <EOL> else : <EOL> base = _importFromModule ( name ) <EOL> return base <EOL> def importObjectModule ( objectName , base = None , finder = imp . find_module , loader = imp . load_module , <EOL> stopAtFailure = False ) : <EOL> """<STR_LIT>""" <EOL> module = base <EOL> objectNames = objectName . split ( "<STR_LIT:.>" ) <EOL> for i in xrange ( <NUM_LIT:0> , len ( objectNames ) ) : <EOL> name = objectNames [ i ] <EOL> if module : <EOL> child = getattr ( module , name , NonExistent ) <EOL> if child is NonExistent : <EOL> if isinstance ( module , ModuleType ) : <EOL> if stopAtFailure : <EOL> try : <EOL> module = _importFromModule ( name , module , finder , loader ) <EOL> except ImportError , exc : <EOL> if module is base : <EOL> try : <EOL> module = __import__ ( name ) <EOL> except ImportError , exc : <EOL> return module , objectNames [ i : ] <EOL> else : <EOL> return module , objectNames [ i : ] <EOL> else : <EOL> module = _importFromModule ( name , module , finder , loader ) <EOL> else : <EOL> raise ImportError ( "<STR_LIT>" % objectName ) <EOL> else : <EOL> if not isinstance ( child , ModuleType ) : <EOL> return module , objectNames [ i : ] <EOL> else : <EOL> module = child <EOL> else : <EOL> module = _importFromModule ( name ) <EOL> return module , [ ] <EOL> def findModule ( moduleName , finder = imp . find_module ) : <EOL> """<STR_LIT>""" <EOL> path = None <EOL> for name in moduleName . split ( "<STR_LIT:.>" ) : <EOL> y , path , z = finder ( name , [ path ] if path else None ) <EOL> return path <EOL> def absModuleName ( name , base , finder = imp . find_module ) : <EOL> """<STR_LIT>""" <EOL> if "<STR_LIT:.>" in name : <EOL> name , rest = name . split ( "<STR_LIT:.>" , <NUM_LIT:1> ) <EOL> else : <EOL> rest = None <EOL> absName , f , path , desc = _findModule ( name , base , finder ) <EOL> if rest : <EOL> return absName + "<STR_LIT:.>" + rest <EOL> else : <EOL> return absName <EOL> def _importFromModule ( name , base = None , finder = imp . find_module , loader = imp . load_module ) : <EOL> if base : <EOL> if not hasattr ( base , "<STR_LIT>" ) : <EOL> baseName = base . __name__ <EOL> if "<STR_LIT:.>" in baseName : <EOL> baseName , tail = base . __name__ . rsplit ( "<STR_LIT:.>" , <NUM_LIT:1> ) <EOL> package = importObject ( baseName , None , finder , loader ) <EOL> child = getattr ( package , name , NonExistent ) <EOL> if child is not NonExistent : <EOL> return child <EOL> absName , f , path , desc = _findModule ( name , base , finder ) <EOL> module = loader ( absName , f , path , desc ) <EOL> setattr ( base , name , module ) <EOL> else : <EOL> module = __import__ ( name ) <EOL> return module <EOL> def _findModule ( name , base , finder = imp . find_module ) : <EOL> packagePath = getPackagePath ( base ) <EOL> paths = ( [ packagePath ] + sys . path ) if os . path . isdir ( packagePath ) else None <EOL> found = finder ( name , paths ) <EOL> if not found : <EOL> raise ImportError ( "<STR_LIT>" % name ) <EOL> f , path , desc = found <EOL> if os . path . normcase ( os . path . dirname ( path ) ) == os . path . normcase ( packagePath ) : <EOL> absName = getPackageChildName ( base , name ) <EOL> else : <EOL> absName = name <EOL> return absName , f , path , desc <EOL> def getRootPackagePath ( module , finder = imp . find_module , loader = imp . load_module ) : <EOL> """<STR_LIT>""" <EOL> packageName = module . __name__ <EOL> if '<STR_LIT:.>' in packageName : <EOL> packageName , tail = module . __name__ . split ( '<STR_LIT:.>' , <NUM_LIT:1> ) <EOL> package = importObject ( packageName , finder = finder , loader = loader ) <EOL> packagePath , = package . __path__ <EOL> return os . path . dirname ( packagePath ) <EOL> def getPackagePath ( module ) : <EOL> if hasattr ( module , "<STR_LIT>" ) : <EOL> return module . __path__ [ <NUM_LIT:0> ] <EOL> else : <EOL> return os . path . dirname ( module . __file__ ) <EOL> def getPackageChildName ( module , childName ) : <EOL> baseName = module . __name__ <EOL> if childName == "<STR_LIT>" : <EOL> return baseName <EOL> if not hasattr ( module , "<STR_LIT>" ) and "<STR_LIT:.>" in baseName : <EOL> baseName , x = baseName . rsplit ( "<STR_LIT:.>" , <NUM_LIT:1> ) <EOL> if baseName : <EOL> return baseName + "<STR_LIT:.>" + childName <EOL> else : <EOL> return childName <EOL> def walkModules ( moduleName ) : <EOL> """<STR_LIT>""" <EOL> for subModuleName in walkModuleNames ( moduleName ) : <EOL> try : <EOL> yield importModule ( subModuleName ) <EOL> except GeneratorExit : <EOL> raise <EOL> except : <EOL> print "<STR_LIT>" % subModuleName <EOL> raise <EOL> def walkModuleNames ( moduleName ) : <EOL> """<STR_LIT>""" <EOL> ignoreNames = [ "<STR_LIT>" ] <EOL> def iter ( moduleName , modulePath , isDir ) : <EOL> if isDir : <EOL> names = os . listdir ( modulePath ) <EOL> if not "<STR_LIT>" in names : <EOL> return <EOL> else : <EOL> moduleName , baseName = moduleName . rsplit ( "<STR_LIT:.>" , <NUM_LIT:1> ) <EOL> names = [ baseName + "<STR_LIT>" ] <EOL> for name in names : <EOL> filePath = os . path . join ( modulePath , name ) <EOL> if os . path . isdir ( filePath ) : <EOL> dirModuleName = "<STR_LIT:.>" . join ( ( moduleName , name ) ) <EOL> dirPath = os . path . join ( modulePath , name ) <EOL> for name in iter ( dirModuleName , dirPath , True ) : <EOL> yield name <EOL> else : <EOL> baseName , ext = os . path . splitext ( name ) <EOL> if ext == "<STR_LIT>" : <EOL> if baseName == "<STR_LIT>" : <EOL> yield moduleName <EOL> elif baseName not in ignoreNames : <EOL> yield "<STR_LIT:.>" . join ( ( moduleName , baseName ) ) <EOL> modPath = findModule ( moduleName ) <EOL> if modPath : <EOL> isDir = os . path . isdir ( modPath ) <EOL> if isDir : <EOL> dirPath = modPath <EOL> modPath = os . path . join ( modPath , "<STR_LIT>" ) <EOL> else : <EOL> dirPath = os . path . dirname ( modPath ) <EOL> for name in iter ( moduleName , dirPath , isDir ) : <EOL> yield name <EOL> def getCallerGlobals ( ) : <EOL> """<STR_LIT>""" <EOL> return getFrameGlobals ( <NUM_LIT:2> ) <EOL> def getFrameGlobals ( n ) : <EOL> """<STR_LIT>""" <EOL> return sys . _getframe ( n + <NUM_LIT:1> ) . f_globals </s>
