mς %U²Ic@sHdZdkZdkZdkZdkZdddddddd gZd Zd klZlZl Z d kl Z l Z l Z e Z e Zd fd„ƒYZdefd„ƒYZdefd„ƒYZdefd„ƒYZdefd„ƒYZdefd„ƒYZdefd„ƒYZdZdadd„Zd„ZdS(s, Import hook support. Consistent use of this module will make it possible to change the different mechanisms involved in loading modules independently. While the built-in module imp exports interfaces to the built-in module searching and loading algorithm, and it is possible to replace the built-in function __import__ in order to change the semantics of the import statement, until now it has been difficult to combine the effect of different __import__ hacks, like loading modules from URLs by rimport.py, or restricted execution by rexec.py. This module defines three new concepts: 1) A "file system hooks" class provides an interface to a filesystem. One hooks class is defined (Hooks), which uses the interface provided by standard modules os and os.path. It should be used as the base class for other hooks classes. 2) A "module loader" class provides an interface to search for a module in a search path and to load it. It defines a method which searches for a module in a single directory; by overriding this method one can redefine the details of the search. If the directory is None, built-in and frozen modules are searched instead. Two module loader class are defined, both implementing the search strategy used by the built-in __import__ function: ModuleLoader uses the imp module's find_module interface, while HookableModuleLoader uses a file system hooks class to interact with the file system. Both use the imp module's load_* interfaces to actually load the module. 3) A "module importer" class provides an interface to import a module, as well as interfaces to reload and unload a module. It also provides interfaces to install and uninstall itself instead of the default __import__ and reload (and unload) functions. One module importer class is defined (ModuleImporter), which uses a module loader instance passed in (by default HookableModuleLoader is instantiated). The classes defined here should be used as base classes for extended functionality along those lines. If a module importer class supports dotted names, its import_module() must return a different value depending on whether it is called on behalf of a "from ... import ..." statement or not. (This is caused by the way the __import__ hook is used by the Python interpreter.) It would also do wise to install a different version of reload(). NtBasicModuleLoadertHookst ModuleLoadertFancyModuleLoadertBasicModuleImportertModuleImportertinstallt uninstalli(s C_EXTENSIONs PY_SOURCEs PY_COMPILED(s C_BUILTINs PY_FROZENs PKG_DIRECTORYt_VerbosecBs8tZed„Zd„Zd„Zd„Zd„ZRS(NcCs ||_dS(N(tverbosetself(R R ((t#/data/zmath/lib/python2.4/ihooks.pyt__init__IscCs|iS(N(R R (R ((R t get_verboseLscCs ||_dS(N(R R (R R ((R t set_verboseOscGs|io|i|ŒndS(N(R R tmessagetargs(R R((R tnoteTs cGs|o ||GHn|GHdS(N(Rtformat(R RR((R RXs (t__name__t __module__tVERBOSER R RRR(((R RGs     cBs>tZdZed„Zd„Zd„Zd„Zd„ZRS(s;Basic module loader. This provides the same functionality as built-in import. It doesn't deal with checking sys.modules -- all it provides is find_module() and a load_module(), as well as find_module_in_dir() which searches just one directory, and can be overridden by a derived class to change the module search algorithm when the basic dependency on sys.path is unchanged. The interface is a little more convenient than imp's: find_module(name, [path]) returns None or 'stuff', and load_module(name, stuff) loads the module. cCsZ|djodg|iƒ}nx/|D]'}|i||ƒ}|o|Sq+q+WdS(N(tpathtNoneR t default_pathtdirtfind_module_in_dirtnametstuff(R RRRR((R t find_moduleps  cCstiS(N(tsysR(R ((R RxscCsS|djo|i|ƒSn2yti||gƒSWntj o dSnXdS(N(RRR tfind_builtin_moduleRtimpRt ImportError(R RR((R R{s  cCsXti|ƒoddddtffSnti|ƒoddddtffSndS(Nt(R t is_builtinRRtBUILTIN_MODULEt is_frozent FROZEN_MODULE(R R((R R„s cCsF|\}}}zti||||ƒSWd|o|iƒnXdS(N(RtfiletfilenametinfoR t load_moduleRtclose(R RRR)R(R'((R R*Œs ( RRt__doc__RRRRRR*(((R R_s    cBsψtZdZd„Zd„Zd„Zd„Zd„Zd„Zd„Z e d„Z e d „Z e d „Z e d „Zd „Zd „Zd„Zd„Zd„Zd„Zd„Zd„Zd„Zd„Zd„ZeZd„ZeiZRS(sίHooks into the filesystem and interpreter. By deriving a subclass you can redefine your filesystem interface, e.g. to merge it with the URL space. This base class behaves just like the native filesystem. cCs tiƒS(N(R t get_suffixes(R ((R R- scCs ti|ƒS(N(R t new_moduleR(R R((R R.‘scCs ti|ƒS(N(R R#R(R R((R R#’scCs ti|ƒS(N(R t init_builtinR(R R((R R/£scCs ti|ƒS(N(R R%R(R R((R R%€scCs ti|ƒS(N(R t init_frozenR(R R((R R0₯scCs ti|ƒS(N(R tget_frozen_objectR(R R((R R1¦scCsti|||ƒS(N(R t load_sourceRR(R'(R RR(R'((R R2§scCsti|||ƒS(N(R t load_compiledRR(R'(R RR(R'((R R3©scCsti|||ƒS(N(R t load_dynamicRR(R'(R RR(R'((R R4«scCsti|||ddtfƒS(NR"(R R*RR'R(t PKG_DIRECTORY(R RR(R'((R t load_package­scCs@|iƒ}||jo ||Sn|i|ƒ||<}|S(N(R t modules_dicttdRR.tm(R RR9R8((R t add_module°s   cCstiS(N(Rtmodules(R ((R R7·scCstiS(N(RR(R ((R RΈscCstii|ƒS(N(tosRtsplittx(R R>((R t path_splitΊscCstii||ƒS(N(R<RtjoinR>ty(R R>RA((R t path_join»scCstii|ƒS(N(R<RtisabsR>(R R>((R t path_isabsΌscCstii|ƒS(N(R<RtexistsR>(R R>((R t path_existsΏscCstii|ƒS(N(R<RtisdirR>(R R>((R t path_isdirΐscCstii|ƒS(N(R<RtisfileR>(R R>((R t path_isfileΑscCstii|ƒS(N(R<RtislinkR>(R R>((R t path_islinkΒscGs t|ŒS(N(topenR>(R R>((R topenfileΕscCs ti|ƒS(N(R<tlistdirR>(R R>((R ROΗs( RRR,R-R.R#R/R%R0R1RR2R3R4R6R:R7RR?RBRDRFRHRJRLRNtIOErrortopenfile_errorROR<terrort listdir_error(((R R”s4                        cBs_tZdZeed„Zd„Zd„Zd„Zd„Z d„Z dd„Z d „Z RS( sχDefault module loader; uses file system hooks. By defining suitable hooks, you might be able to load modules from other sources than the file system, e.g. from compressed or encrypted files, tar files or (if you're brave!) URLs. cCs*ti||ƒ|p t|ƒ|_dS(N(RR R R thooksR(R RTR ((R R ΦscCs |iiƒS(N(R RTR(R ((R RΪscCs |iiƒS(N(R RTR7(R ((R R7έscCs|iS(N(R RT(R ((R t get_hooksΰscCs ||_dS(N(RTR (R RT((R t set_hooksγscCs^|ii|ƒoddddtffSn|ii|ƒoddddtffSndS(NR"(R RTR#RRR$R%R&(R R((R Rζs ic Cs2|djo|i|ƒSn|o…|ii||ƒ} |ii | ƒoY|i d| dƒ}|o9|d} | o| i ƒnd| ddtffSq¦qͺnx|iiƒD]p}|\}}} |ii|||ƒ} y&|ii| |ƒ}|| |fSWqΊ|iij oqΊXqΊWdS(NR iR"(RRR RRtallow_packagesRTRBtfullnameRHRRR'R+R5R-R)tsufftmodettypeRNtfpRQ( R RRRWR)R\RYRRZR'RXR[((R Rξs*  " c CsA|\}}}|\}}} zύ| tjo|i i |ƒSn| t jo|i i |ƒSn| tjo|i i|||ƒ}n| tjo|i i|||ƒ}nf| tjo|i i|||ƒ}n=| tjo|i i|||ƒ}ntd| |f‚Wd|o|iƒnX||_|S(Ns$Unrecognized module type (%r) for %s(RR'R(R)RYRZR[R$R RTR/RR&R0t C_EXTENSIONR4R9t PY_SOURCER2t PY_COMPILEDR3R5R6R!R+t__file__( R RRR)RYR9R(RZR'R[((R R*s*       ( RRR,RRR RR7RURVRRR*(((R RΜs       cBstZdZd„ZRS(s8Fancy module loader -- parses and execs the code itself.cBsλ|\}}\}}} |}d} | e jo©|i d|dƒ} | pe d|‚n| \}}} | \}}}|eefjo,|o|iƒne d||f‚n|g} |}|}|} n| ejo|ii|ƒ}ny| ejo)dk}|idƒ|i|ƒ}nC| ejo"|iƒ} e!| |dƒ}ne"i#|||ƒS|ii$|ƒ}| o | |_&n||_'y||i(UWn1|ii)ƒ}||jo ||=n‚nX|S(NR is No __init__ module in package %ss/Bad type (%r) for __init__ module in package %sitexec(+RR'R(RYRZR[t realfilenameRRR5R Rt initstuffR!Rtinitfilet initfilenametinitinfotinitsufftinitmodetinittypeR_R^R+R&RTR1tcodetmarshaltseektloadtreadtdatatcompileRR*R:R9t__path__R`t__dict__R7R8(R RRRjRYRhR'R(ReR[RcRfRRoR8RdRbR9RiRgRZRk((R R* sN             (RRR,R*(((R Rs cBsztZdZeed„Zd„Zd„Zd„Zd„Z hhgd„Z ed„Z d„Z d „Z d „ZRS( ssBasic module importer; uses module loader. This provides basic import facilities but no package imports. cCs?ti||ƒ|p td|ƒ|_|iiƒ|_dS(N( RR R R tloaderRRR7R;(R RsR ((R R WscCs|iS(N(R Rs(R ((R t get_loader\scCs ||_dS(N(RsR (R Rs((R t set_loader_scCs |iiƒS(N(R RsRU(R ((R RUbscCs|ii|ƒS(N(R RsRVRT(R RT((R RVescCsht|ƒ}||ijo|i|Sn|ii|ƒ}|ptd|‚n|ii||ƒS(NsNo module named %s( tstrRR R;RsRRR!R*(R RtglobalstlocalstfromlistR((R t import_modulehs cCsOt|iƒ}|ii||ƒ}|pt d|‚n|ii ||ƒS(NsModule %s not found for reload( RvtmoduleRRR RsRRRR!R*(R R{RRR((R treloadqs cCs|it|iƒ=dS(N(R R;RvR{R(R R{((R tunloadxscCsiti|_ti|_ttdƒp dt_nti|_ |i t_|it_|it_dS(NR}( t __builtin__t __import__R tsave_import_moduleR|t save_reloadthasattrRR}t save_unloadRz(R ((R R|s      cCs<|it_|it_|it_tip t`ndS(N(R R€R~RRR|RƒR}(R ((R R†s     (RRR,RRR RtRuRURVRzR|R}RR(((R ROs        cBs\tZdZeeed„Zd„Zd„Zd„Zdd„Zdd„Z d„Z RS( s)A module importer that supports packages.c Csv|i|ƒ}|i|t|ƒƒ\}}|i ||ƒ}|p|Snt |dƒo|i ||ƒn|S(NRq(R tdetermine_parentRwtparenttfind_head_packageRvRtqttailt load_tailR9RyR‚tensure_fromlist( R RRwRxRyR…R9R‡Rˆ((R Rz’scCsˆ| p d|jodSn|d}d|jo|i|}|Snd|jo.|idƒ}|| }|i|}|SndS(NRRqt.(RwRtpnameR R;R…trfindti(R RwR…RŽRŒ((R R„œs      cCsδd|jo+|idƒ}|| }||d}n |}d}|od|i|f}n|}|i |||ƒ}|o||fSn|o:|}d}|i |||ƒ}|o||fSqΣnt d|‚dS(NR‹iR"s%s.%ssNo module named ( RtfindRŽtheadRˆR…RtqnameR t import_itR‡RR!(R R…RRRŽR‘R‡Rˆ((R R†¬s&  cCsž|}x‘|o‰|idƒ}|djot|ƒ}n|| ||d}}d|i|f}|i |||ƒ}|pt d|‚q q W|S(NR‹iis%s.%ssNo module named ( R‡R9RˆRRŽtlenRRtmnameR R’R!(R R‡RˆRRŽR”R9((R R‰Αs icCsΜxΕ|D]½}|djoH|p:y |i}Wntj oq[X|i||dƒqqn|djoUt ||ƒ oDd|i |f}|i |||ƒ}|ptd|‚qΔqqWdS(Nt*is%s.%ssNo module named (Rytsubt recursiveR9t__all__talltAttributeErrorR RŠR‚RtsubnameR’tsubmodR!(R R9RyR—R™R›RœR–((R RŠΝs  cCsβ|p td‚n|p)y|i|SWqDtj oqDXny|o|i}Wnt j o dSnXt |ƒ}|i i||ƒ}|pdSnt |ƒ}|i i||ƒ}|ot|||ƒn|S(NsEmpty module name(tpartnamet ValueErrort force_loadR R;tfqnametKeyErrorR…RqRRšRRvRsRRR*R9tsetattr(R RR R…RŸR9RR((R R’ήs(     cCs€t|iƒ}d|jo|i||dddƒSn|idƒ}|| }|i |}|i||d||ddƒS(NR‹RŸi( RvR{RRR R’RRRŽRŒR;R…(R R{RR…RŽRŒ((R R|τs   ( RRR,RRzR„R†R‰RŠR’R|(((R RŽs     cCs%|ptptƒatiƒdS(N(timportertdefault_importerRtcurrent_importerR(R£((R RscCstiƒdS(N(R₯R(((R Rs(R,R~R R<RR˜RR]R^R_t C_BUILTINt PY_FROZENR5R$R&RRRRRRRRR€R₯RR(R~RR˜R R€R]RR¦RR_RR5R§RR^R&RR$RRRR<RR((R t?3s*    58P3?p