۶eTc@s?dZddlZddlZddlZeZdaaa a dZ e dZ dZ dZdZdZd dd YZd efd YZd efdYZdefdYZdefdYZdefdYZdefdYZdZdddYZeegZdS(s] Provides an emulator/replacement for Python's standard import system. @@TR: Be warned that Import Hooks are in the deepest, darkest corner of Python's jungle. If you need to start hacking with this, be prepared to get lost for a while. Also note, this module predates the newstyle import hooks in Python 2.3 http://www.python.org/peps/pep-0302.html. This is a hacked/documented version of Gordon McMillan's iu.py. I have: - made it a little less terse - added docstrings and explanatations - standardized the variable naming scheme - reorganized the code layout to enhance readability iNcCsKtj}d}}d|kr>d}ddlm}m}nd|krid}ddlm}m}nd|krd}ddlm}m}ned|krd}ddlm}m}n:d |krddl m}m}d }n t d |dkr|d }n|dkr/|d }n|a |a |a |adS(sISet up 'os' module replacement functions for use during import bootstrap.tposixt/i(tstattgetcwdtnts\tdostos2tmaccSsN|dkr|Sd|kr)d|}n|ddkrF|d}n||S(Ntt:i((tatb((sD/opt/freeware/lib64/python2.7/site-packages/Cheetah/ImportManager.pytjoin7s    sno os specific module foundcSsF|dkr|S|d}|dks2||kr:||S|||S(NRiR((R R tseptlastchar((sD/opt/freeware/lib64/python2.7/site-packages/Cheetah/ImportManager.pyR Cs   cSsUxNtt|dddD]0}||}|dksE||kr|| SqWdS(NiiRR(trangetlen(R R titc((sD/opt/freeware/lib64/python2.7/site-packages/Cheetah/ImportManager.pytdirnameLs #  N(tsystbuiltin_module_namestNoneRRRRRRRt ImportErrort_os_statt _os_path_joint_os_path_dirnamet _os_getcwd(tnamesR RR RR((sD/opt/freeware/lib64/python2.7/site-packages/Cheetah/ImportManager.pyt _os_bootstrap#s4          cCsGx<tt|dddD]}||dkrPqqWdS|| S(Niit.R(RR(tsR((sD/opt/freeware/lib64/python2.7/site-packages/Cheetah/ImportManager.pyt packageNameas #cCsg}d}}xKtt|D]7}||dkr#|j|||!|d}q#q#W|t|kr|j||n|S(NiRi(RRtappend(RtrsltRtj((sD/opt/freeware/lib64/python2.7/site-packages/Cheetah/ImportManager.pyt nameSplitis cCsCx<tt|dddD]}||dkr||SqWdS(NiiRR(RR(tfnmR((sD/opt/freeware/lib64/python2.7/site-packages/Cheetah/ImportManager.pyt getPathExtts# cCs7yt|}Wntk r$dSX|dd@dkS(s&Local replacement for os.path.isdir().iii@N(RtOSErrorR(tpathnameR((sD/opt/freeware/lib64/python2.7/site-packages/Cheetah/ImportManager.pyt pathIsDirzs  cCsIt|}x6tjD](\}}}||kr|||fSqWdS(N(R&timpt get_suffixes(R%texttsuffixtmodettyp((sD/opt/freeware/lib64/python2.7/site-packages/Cheetah/ImportManager.pytgetDescrs  tOwnercBs)eZdZdZdZdZRS(sAn Owner does imports from a particular piece of turf That is, there's an Owner for each thing on sys.path There are owners for directories and .pyz files. There could be owners for zip files, or even URLs. A shadowpath (a dictionary mapping the names in sys.path to their owners) is used so that sys.path (or a package's __path__) is still a bunch of strings, cCs ||_dS(N(tpath(tselfR2((sD/opt/freeware/lib64/python2.7/site-packages/Cheetah/ImportManager.pyt__init__scCs|jS(N(R2(R3((sD/opt/freeware/lib64/python2.7/site-packages/Cheetah/ImportManager.pyt__str__scCsdS(N(R(R3tnm((sD/opt/freeware/lib64/python2.7/site-packages/Cheetah/ImportManager.pytgetmods(t__name__t __module__t__doc__R4R5R7(((sD/opt/freeware/lib64/python2.7/site-packages/Cheetah/ImportManager.pyR1s  tDirOwnercBs,eZdZejejejdZRS(cCsK|dkrt}nt|s7td|ntj||dS(NRs%s is not a directory(RR)t ValueErrorR1R4(R3R2((sD/opt/freeware/lib64/python2.7/site-packages/Cheetah/ImportManager.pyR4s    c Cst|j|}|ddfg}t|rU|jdt|dd|fnd}}x|D]\}} } x|D]\} } } || }yt|}WnqX| tjkrt|d}tj |||| | | f}||_ |S| tj kr||f}q||f}qW|s8|rfPqfqfW|dkr\|dkr\dSxt rT|dks|r|dd|ddkry2t t|ddjd|dd}PWqQtk r}d |dGH|jGHqQXq_|rMt|ddj}y||d}PWqQttfk rId}qQXq_dSq_W||}|j|_ | r| g|_t|j}|j|_n||_|S( NiR4itrbitrs texecsInvalid syntax in %s(RR2RR)tinsertRR*t C_EXTENSIONtopent load_modulet__file__t PY_SOURCEtTruetcompiletreadt SyntaxErrortargsR<tEOFErrort co_filenamet__path__tPathImportDirectorR7t __importsub__t__co__(R3R6t getsuffixestloadcotnewmodtptht possiblestpytpyctispkgtpkgpthR,R.R/tattempttsttfptmodtcotetstufft subimporter((sD/opt/freeware/lib64/python2.7/site-packages/Cheetah/ImportManager.pyR7sb %  !   .-       ( R8R9R4R*R+tmarshaltloadst new_moduleR7(((sD/opt/freeware/lib64/python2.7/site-packages/Cheetah/ImportManager.pyR;s tImportDirectorcBseZdZRS(sImportDirectors live on the metapath There's one for builtins, one for frozen modules, and one for sys.path Windows gets one for modules gotten from the Registry Mac would have them for PY_RESOURCE modules etc. A generalization of Owner - their concept of 'turf' is broader(R8R9R:(((sD/opt/freeware/lib64/python2.7/site-packages/Cheetah/ImportManager.pyRestBuiltinImportDirectorcBs&eZdZdZejdZRS(s"Directs imports of builtin modulescCs d|_dS(NtBuiltins(R2(R3((sD/opt/freeware/lib64/python2.7/site-packages/Cheetah/ImportManager.pyR4scCs8||r4tj|d|ddtjf}|SdS(NR(R*RCRt C_BUILTIN(R3R6t isbuiltinR]((sD/opt/freeware/lib64/python2.7/site-packages/Cheetah/ImportManager.pyR7s $(R8R9R:R4R*t is_builtinR7(((sD/opt/freeware/lib64/python2.7/site-packages/Cheetah/ImportManager.pyRfs tFrozenImportDirectorcBs,eZdZdZejejdZRS(s!Directs imports of frozen modulescCs d|_dS(Nt FrozenModules(R2(R3((sD/opt/freeware/lib64/python2.7/site-packages/Cheetah/ImportManager.pyR4scCsY||rU||d|ddtjf}t|drQ||d|_n|SdS(NRRMcSs|j|d|S(NR(R7(tnametpnametowner((sD/opt/freeware/lib64/python2.7/site-packages/Cheetah/ImportManager.pyts(RR*t PY_FROZENthasattrRO(R3R6tisFrozentloadModR]((sD/opt/freeware/lib64/python2.7/site-packages/Cheetah/ImportManager.pyR7s  !(R8R9R:R4R*t is_frozenRCR7(((sD/opt/freeware/lib64/python2.7/site-packages/Cheetah/ImportManager.pyRks tRegistryImportDirectorcBs eZdZdZdZRS(s9Directs imports of modules stored in the Windows RegistrycCs=d|_i|_yddl}Wntk r5nXd}d}d}dtj}x||fD]}y|j||d|}WnqbX|j|\}} } xt|D]s} |j || } |j|| d|} |j | d}t |d}|d|f|j| <| j qW|j PqbWdS( NtWindowsRegistryiiii?s%Software\Python\PythonCore\%s\ModulesiR( R2tmaptwin32apiRRtwinvert RegOpenKeyExtRegQueryInfoKeyRt RegEnumKeytRegQueryValueExR0tClose(R3RytHKEY_CURRENT_USERtHKEY_LOCAL_MACHINEtKEY_ALL_ACCESStsubkeytrootthkeyt numsubkeyst numvaluest lastmodifiedRt subkeynamethskeytvaltdesc((sD/opt/freeware/lib64/python2.7/site-packages/Cheetah/ImportManager.pyR4s2     cCs\|jj|}|rX|\}}t|d}tj||||}||_|SdS(NR=(RxtgetRBR*RCRDR(R3R6R`R%RR\R]((sD/opt/freeware/lib64/python2.7/site-packages/Cheetah/ImportManager.pyR7$s  (R8R9R:R4R7(((sD/opt/freeware/lib64/python2.7/site-packages/Cheetah/ImportManager.pyRvs RNcBs2eZdZddddZdZdZRS(s4Directs imports of modules stored on the filesystem.cCsv|dkrtj|_n ||_|dkr<t|_n ||_|rW||_n i|_t|_i|_dS(N( RRR2t_globalOwnerTypest _ownertypest _shadowPathtFalset _inMakeOwnert _building(R3tpathlistt importerst ownertypes((sD/opt/freeware/lib64/python2.7/site-packages/Cheetah/ImportManager.pyR41s        cCsd}x|jD]}t|tr~|jj|d}|dkrc|j|}|j|s.    <    C +7