<s> """<STR_LIT>""" <EOL> __author__ = "<STR_LIT>" <EOL> __revision__ = "<STR_LIT>" [ <NUM_LIT:11> : - <NUM_LIT:2> ] <EOL> import sys <EOL> import imp <EOL> import marshal <EOL> try : <EOL> True , False <EOL> except NameError : <EOL> True , False = ( <NUM_LIT:1> == <NUM_LIT:1> ) , ( <NUM_LIT:1> == <NUM_LIT:0> ) <EOL> _installed = False <EOL> STRINGTYPE = type ( '<STR_LIT>' ) <EOL> _os_stat = _os_path_join = _os_getcwd = _os_path_dirname = None <EOL> def _os_bootstrap ( ) : <EOL> """<STR_LIT>""" <EOL> names = sys . builtin_module_names <EOL> join = dirname = None <EOL> if '<STR_LIT>' in names : <EOL> sep = '<STR_LIT:/>' <EOL> from posix import stat , getcwd <EOL> elif '<STR_LIT>' in names : <EOL> sep = '<STR_LIT:\\>' <EOL> from nt import stat , getcwd <EOL> elif '<STR_LIT>' in names : <EOL> sep = '<STR_LIT:\\>' <EOL> from dos import stat , getcwd <EOL> elif '<STR_LIT>' in names : <EOL> sep = '<STR_LIT:\\>' <EOL> from os2 import stat , getcwd <EOL> elif '<STR_LIT>' in names : <EOL> from mac import stat , getcwd <EOL> def join ( a , b ) : <EOL> if a == '<STR_LIT>' : <EOL> return b <EOL> path = s <EOL> if '<STR_LIT::>' not in a : <EOL> a = '<STR_LIT::>' + a <EOL> if a [ - <NUM_LIT:1> : ] != '<STR_LIT::>' : <EOL> a = a + '<STR_LIT::>' <EOL> return a + b <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> if dirname is None : <EOL> def dirname ( a , sep = sep ) : <EOL> for i in range ( len ( a ) - <NUM_LIT:1> , - <NUM_LIT:1> , - <NUM_LIT:1> ) : <EOL> c = a [ i ] <EOL> if c == '<STR_LIT:/>' or c == sep : <EOL> return a [ : i ] <EOL> return '<STR_LIT>' <EOL> global _os_stat <EOL> _os_stat = stat <EOL> global _os_path_join <EOL> _os_path_join = join <EOL> global _os_path_dirname <EOL> _os_path_dirname = dirname <EOL> global _os_getcwd <EOL> _os_getcwd = getcwd <EOL> _os_bootstrap ( ) <EOL> def packageName ( s ) : <EOL> for i in range ( len ( s ) - <NUM_LIT:1> , - <NUM_LIT:1> , - <NUM_LIT:1> ) : <EOL> if s [ i ] == '<STR_LIT:.>' : <EOL> break <EOL> else : <EOL> return '<STR_LIT>' <EOL> return s [ : i ] <EOL> def nameSplit ( s ) : <EOL> rslt = [ ] <EOL> i = j = <NUM_LIT:0> <EOL> for j in range ( len ( s ) ) : <EOL> if s [ j ] == '<STR_LIT:.>' : <EOL> rslt . append ( s [ i : j ] ) <EOL> i = j + <NUM_LIT:1> <EOL> if i < len ( s ) : <EOL> rslt . append ( s [ i : ] ) <EOL> return rslt <EOL> def getPathExt ( fnm ) : <EOL> for i in range ( len ( fnm ) - <NUM_LIT:1> , - <NUM_LIT:1> , - <NUM_LIT:1> ) : <EOL> if fnm [ i ] == '<STR_LIT:.>' : <EOL> return fnm [ i : ] <EOL> return '<STR_LIT>' <EOL> def pathIsDir ( pathname ) : <EOL> "<STR_LIT>" <EOL> try : <EOL> s = _os_stat ( pathname ) <EOL> except OSError : <EOL> return None <EOL> return ( s [ <NUM_LIT:0> ] & <NUM_LIT:0> <NUM_LIT> ) == <NUM_LIT> <NUM_LIT> <EOL> def getDescr ( fnm ) : <EOL> ext = getPathExt ( fnm ) <EOL> for ( suffix , mode , typ ) in imp . get_suffixes ( ) : <EOL> if suffix == ext : <EOL> return ( suffix , mode , typ ) <EOL> class Owner : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , path ) : <EOL> self . path = path <EOL> def __str__ ( self ) : <EOL> return self . path <EOL> def getmod ( self , nm ) : <EOL> return None <EOL> class DirOwner ( Owner ) : <EOL> def __init__ ( self , path ) : <EOL> if path == '<STR_LIT>' : <EOL> path = _os_getcwd ( ) <EOL> if not pathIsDir ( path ) : <EOL> raise ValueError , "<STR_LIT>" % path <EOL> Owner . __init__ ( self , path ) <EOL> def getmod ( self , nm , <EOL> getsuffixes = imp . get_suffixes , loadco = marshal . loads , newmod = imp . new_module ) : <EOL> pth = _os_path_join ( self . path , nm ) <EOL> possibles = [ ( pth , <NUM_LIT:0> , None ) ] <EOL> if pathIsDir ( pth ) : <EOL> possibles . insert ( <NUM_LIT:0> , ( _os_path_join ( pth , '<STR_LIT>' ) , <NUM_LIT:1> , pth ) ) <EOL> py = pyc = None <EOL> for pth , ispkg , pkgpth in possibles : <EOL> for ext , mode , typ in getsuffixes ( ) : <EOL> attempt = pth + ext <EOL> try : <EOL> st = _os_stat ( attempt ) <EOL> except : <EOL> pass <EOL> else : <EOL> if typ == imp . C_EXTENSION : <EOL> fp = open ( attempt , '<STR_LIT:rb>' ) <EOL> mod = imp . load_module ( nm , fp , attempt , ( ext , mode , typ ) ) <EOL> mod . __file__ = attempt <EOL> return mod <EOL> elif typ == imp . PY_SOURCE : <EOL> py = ( attempt , st ) <EOL> else : <EOL> pyc = ( attempt , st ) <EOL> if py or pyc : <EOL> break <EOL> if py is None and pyc is None : <EOL> return None <EOL> while <NUM_LIT:1> : <EOL> if pyc is None or py and pyc [ <NUM_LIT:1> ] [ <NUM_LIT:8> ] < py [ <NUM_LIT:1> ] [ <NUM_LIT:8> ] : <EOL> try : <EOL> co = compile ( open ( py [ <NUM_LIT:0> ] , '<STR_LIT:r>' ) . read ( ) + '<STR_LIT:\n>' , py [ <NUM_LIT:0> ] , '<STR_LIT>' ) <EOL> break <EOL> except SyntaxError , e : <EOL> print "<STR_LIT>" % py [ <NUM_LIT:0> ] <EOL> print e . args <EOL> raise <EOL> elif pyc : <EOL> stuff = open ( pyc [ <NUM_LIT:0> ] , '<STR_LIT:rb>' ) . read ( ) <EOL> try : <EOL> co = loadco ( stuff [ <NUM_LIT:8> : ] ) <EOL> break <EOL> except ( ValueError , EOFError ) : <EOL> pyc = None <EOL> else : <EOL> return None <EOL> mod = newmod ( nm ) <EOL> mod . __file__ = co . co_filename <EOL> if ispkg : <EOL> mod . __path__ = [ pkgpth ] <EOL> subimporter = PathImportDirector ( mod . __path__ ) <EOL> mod . __importsub__ = subimporter . getmod <EOL> mod . __co__ = co <EOL> return mod <EOL> class ImportDirector ( Owner ) : <EOL> """<STR_LIT>""" <EOL> pass <EOL> class BuiltinImportDirector ( ImportDirector ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self ) : <EOL> self . path = '<STR_LIT>' <EOL> def getmod ( self , nm , isbuiltin = imp . is_builtin ) : <EOL> if isbuiltin ( nm ) : <EOL> mod = imp . load_module ( nm , None , nm , ( '<STR_LIT>' , '<STR_LIT>' , imp . C_BUILTIN ) ) <EOL> return mod <EOL> return None <EOL> class FrozenImportDirector ( ImportDirector ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self ) : <EOL> self . path = '<STR_LIT>' <EOL> def getmod ( self , nm , <EOL> isFrozen = imp . is_frozen , loadMod = imp . load_module ) : <EOL> if isFrozen ( nm ) : <EOL> mod = loadMod ( nm , None , nm , ( '<STR_LIT>' , '<STR_LIT>' , imp . PY_FROZEN ) ) <EOL> if hasattr ( mod , '<STR_LIT>' ) : <EOL> mod . __importsub__ = lambda name , pname = nm , owner = self : owner . getmod ( pname + '<STR_LIT:.>' + name ) <EOL> return mod <EOL> return None <EOL> class RegistryImportDirector ( ImportDirector ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self ) : <EOL> self . path = "<STR_LIT>" <EOL> self . map = { } <EOL> try : <EOL> import win32api <EOL> except ImportError : <EOL> pass <EOL> else : <EOL> HKEY_CURRENT_USER = - <NUM_LIT> <EOL> HKEY_LOCAL_MACHINE = - <NUM_LIT> <EOL> KEY_ALL_ACCESS = <NUM_LIT> <EOL> subkey = r"<STR_LIT>" % sys . winver <EOL> for root in ( HKEY_CURRENT_USER , HKEY_LOCAL_MACHINE ) : <EOL> try : <EOL> hkey = win32api . RegOpenKeyEx ( root , subkey , <NUM_LIT:0> , KEY_ALL_ACCESS ) <EOL> except : <EOL> pass <EOL> else : <EOL> numsubkeys , numvalues , lastmodified = win32api . RegQueryInfoKey ( hkey ) <EOL> for i in range ( numsubkeys ) : <EOL> subkeyname = win32api . RegEnumKey ( hkey , i ) <EOL> hskey = win32api . RegOpenKeyEx ( hkey , subkeyname , <NUM_LIT:0> , KEY_ALL_ACCESS ) <EOL> val = win32api . RegQueryValueEx ( hskey , '<STR_LIT>' ) <EOL> desc = getDescr ( val [ <NUM_LIT:0> ] ) <EOL> self . map [ subkeyname ] = ( val [ <NUM_LIT:0> ] , desc ) <EOL> hskey . Close ( ) <EOL> hkey . Close ( ) <EOL> break <EOL> def getmod ( self , nm ) : <EOL> stuff = self . map . get ( nm ) <EOL> if stuff : <EOL> fnm , desc = stuff <EOL> fp = open ( fnm , '<STR_LIT:rb>' ) <EOL> mod = imp . load_module ( nm , fp , fnm , desc ) <EOL> mod . __file__ = fnm <EOL> return mod <EOL> return None <EOL> class PathImportDirector ( ImportDirector ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , pathlist = None , importers = None , ownertypes = None ) : <EOL> if pathlist is None : <EOL> self . path = sys . path <EOL> else : <EOL> self . path = pathlist <EOL> if ownertypes == None : <EOL> self . _ownertypes = _globalOwnerTypes <EOL> else : <EOL> self . _ownertypes = ownertypes <EOL> if importers : <EOL> self . _shadowPath = importers <EOL> else : <EOL> self . _shadowPath = { } <EOL> self . _inMakeOwner = False <EOL> self . _building = { } <EOL> def getmod ( self , nm ) : <EOL> mod = None <EOL> for thing in self . path : <EOL> if type ( thing ) is STRINGTYPE : <EOL> owner = self . _shadowPath . get ( thing , - <NUM_LIT:1> ) <EOL> if owner == - <NUM_LIT:1> : <EOL> owner = self . _shadowPath [ thing ] = self . _makeOwner ( thing ) <EOL> if owner : <EOL> mod = owner . getmod ( nm ) <EOL> else : <EOL> mod = thing . getmod ( nm ) <EOL> if mod : <EOL> break <EOL> return mod <EOL> def _makeOwner ( self , path ) : <EOL> if self . _building . get ( path ) : <EOL> return None <EOL> self . _building [ path ] = <NUM_LIT:1> <EOL> owner = None <EOL> for klass in self . _ownertypes : <EOL> try : <EOL> owner = klass ( path ) <EOL> except : <EOL> pass <EOL> else : <EOL> break <EOL> del self . _building [ path ] <EOL> return owner <EOL> UNTRIED = - <NUM_LIT:1> <EOL> class ImportManager : <EOL> def __init__ ( self ) : <EOL> self . metapath = [ <EOL> BuiltinImportDirector ( ) , <EOL> FrozenImportDirector ( ) , <EOL> RegistryImportDirector ( ) , <EOL> PathImportDirector ( ) <EOL> ] <EOL> self . threaded = <NUM_LIT:0> <EOL> self . rlock = None <EOL> self . locker = None <EOL> self . setThreaded ( ) <EOL> def setThreaded ( self ) : <EOL> thread = sys . modules . get ( '<STR_LIT>' , None ) <EOL> if thread and not self . threaded : <EOL> self . threaded = <NUM_LIT:1> <EOL> self . rlock = thread . allocate_lock ( ) <EOL> self . _get_ident = thread . get_ident <EOL> def install ( self ) : <EOL> import __builtin__ <EOL> __builtin__ . __import__ = self . importHook <EOL> __builtin__ . reload = self . reloadHook <EOL> def importHook ( self , name , globals = None , locals = None , fromlist = None ) : <EOL> _sys_modules_get = sys . modules . get <EOL> contexts = [ None ] <EOL> if globals : <EOL> importernm = globals . get ( '<STR_LIT>' , '<STR_LIT>' ) <EOL> if importernm : <EOL> if hasattr ( _sys_modules_get ( importernm ) , '<STR_LIT>' ) : <EOL> contexts . insert ( <NUM_LIT:0> , importernm ) <EOL> else : <EOL> pkgnm = packageName ( importernm ) <EOL> if pkgnm : <EOL> contexts . insert ( <NUM_LIT:0> , pkgnm ) <EOL> nmparts = nameSplit ( name ) <EOL> _self_doimport = self . doimport <EOL> threaded = self . threaded <EOL> for context in contexts : <EOL> ctx = context <EOL> for i in range ( len ( nmparts ) ) : <EOL> nm = nmparts [ i ] <EOL> if ctx : <EOL> fqname = ctx + '<STR_LIT:.>' + nm <EOL> else : <EOL> fqname = nm <EOL> if threaded : <EOL> self . _acquire ( ) <EOL> mod = _sys_modules_get ( fqname , UNTRIED ) <EOL> if mod is UNTRIED : <EOL> mod = _self_doimport ( nm , ctx , fqname ) <EOL> if threaded : <EOL> self . _release ( ) <EOL> if mod : <EOL> ctx = fqname <EOL> else : <EOL> break <EOL> else : <EOL> i = i + <NUM_LIT:1> <EOL> if i : <EOL> break <EOL> if i < len ( nmparts ) : <EOL> if ctx and hasattr ( sys . modules [ ctx ] , nmparts [ i ] ) : <EOL> return sys . modules [ nmparts [ <NUM_LIT:0> ] ] <EOL> del sys . modules [ fqname ] <EOL> raise ImportError , "<STR_LIT>" % fqname <EOL> if fromlist is None : <EOL> if context : <EOL> return sys . modules [ context + '<STR_LIT:.>' + nmparts [ <NUM_LIT:0> ] ] <EOL> return sys . modules [ nmparts [ <NUM_LIT:0> ] ] <EOL> bottommod = sys . modules [ ctx ] <EOL> if hasattr ( bottommod , '<STR_LIT>' ) : <EOL> fromlist = list ( fromlist ) <EOL> i = <NUM_LIT:0> <EOL> while i < len ( fromlist ) : <EOL> nm = fromlist [ i ] <EOL> if nm == '<STR_LIT:*>' : <EOL> fromlist [ i : i + <NUM_LIT:1> ] = list ( getattr ( bottommod , '<STR_LIT>' , [ ] ) ) <EOL> if i >= len ( fromlist ) : <EOL> break <EOL> nm = fromlist [ i ] <EOL> i = i + <NUM_LIT:1> <EOL> if not hasattr ( bottommod , nm ) : <EOL> if self . threaded : <EOL> self . _acquire ( ) <EOL> mod = self . doimport ( nm , ctx , ctx + '<STR_LIT:.>' + nm ) <EOL> if self . threaded : <EOL> self . _release ( ) <EOL> if not mod : <EOL> raise ImportError , "<STR_LIT>" % ( nm , ctx ) <EOL> return bottommod <EOL> def doimport ( self , nm , parentnm , fqname ) : <EOL> if parentnm : <EOL> parent = sys . modules [ parentnm ] <EOL> if hasattr ( parent , '<STR_LIT>' ) : <EOL> importfunc = getattr ( parent , '<STR_LIT>' , None ) <EOL> if not importfunc : <EOL> subimporter = PathImportDirector ( parent . __path__ ) <EOL> importfunc = parent . __importsub__ = subimporter . getmod <EOL> mod = importfunc ( nm ) <EOL> if mod : <EOL> setattr ( parent , nm , mod ) <EOL> else : <EOL> return None <EOL> else : <EOL> for director in self . metapath : <EOL> mod = director . getmod ( nm ) <EOL> if mod : <EOL> break <EOL> if mod : <EOL> mod . __name__ = fqname <EOL> sys . modules [ fqname ] = mod <EOL> if hasattr ( mod , '<STR_LIT>' ) : <EOL> co = mod . __co__ <EOL> del mod . __co__ <EOL> exec co in mod . __dict__ <EOL> if fqname == '<STR_LIT>' and not self . threaded : <EOL> self . setThreaded ( ) <EOL> else : <EOL> sys . modules [ fqname ] = None <EOL> return mod <EOL> def reloadHook ( self , mod ) : <EOL> fqnm = mod . __name__ <EOL> nm = nameSplit ( fqnm ) [ - <NUM_LIT:1> ] <EOL> parentnm = packageName ( fqnm ) <EOL> newmod = self . doimport ( nm , parentnm , fqnm ) <EOL> mod . __dict__ . update ( newmod . __dict__ ) <EOL> def _acquire ( self ) : <EOL> if self . rlock . locked ( ) : <EOL> if self . locker == self . _get_ident ( ) : <EOL> self . lockcount = self . lockcount + <NUM_LIT:1> <EOL> return <EOL> self . rlock . acquire ( ) <EOL> self . locker = self . _get_ident ( ) <EOL> self . lockcount = <NUM_LIT:0> <EOL> def _release ( self ) : <EOL> if self . lockcount : <EOL> self . lockcount = self . lockcount - <NUM_LIT:1> <EOL> else : <EOL> self . rlock . release ( ) <EOL> _globalOwnerTypes = [ <EOL> DirOwner , <EOL> Owner , <EOL> ] </s>
