Dfc@sdZddlmZmZeddd[ddlZddlZddlZddlZddd d d d d dgZdZ ddlm Z m Z m Z ddlm Z mZmZe ZeZdddYZdefdYZdefdYZd efdYZd efdYZd efdYZd efdYZdZdaddZdZdS(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(). i(twarnpy3ktwarns0the ihooks module has been removed in Python 3.0t stackleveliNtBasicModuleLoadertHookst ModuleLoadertFancyModuleLoadertBasicModuleImportertModuleImportertinstallt uninstalli(t C_EXTENSIONt PY_SOURCEt PY_COMPILED(t C_BUILTINt PY_FROZENt PKG_DIRECTORYt_VerbosecBs8eZedZdZdZdZdZRS(cCs ||_dS(N(tverbose(tselfR((s/usr/lib64/python2.7/ihooks.pyt__init__KscCs|jS(N(R(R((s/usr/lib64/python2.7/ihooks.pyt get_verboseNscCs ||_dS(N(R(RR((s/usr/lib64/python2.7/ihooks.pyt set_verboseQscGs|jr|j|ndS(N(Rtmessage(Rtargs((s/usr/lib64/python2.7/ihooks.pytnoteVs cGs|r||GHn|GHdS(N((RtformatR((s/usr/lib64/python2.7/ihooks.pyRZs (t__name__t __module__tVERBOSERRRRR(((s/usr/lib64/python2.7/ihooks.pyRIs     cBs>eZdZddZdZdZdZdZRS(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. cCsS|dkr"dg|j}nx*|D]"}|j||}|r)|Sq)WdS(N(tNonet default_pathtfind_module_in_dir(Rtnametpathtdirtstuff((s/usr/lib64/python2.7/ihooks.pyt find_modulers  cCstjS(N(tsysR"(R((s/usr/lib64/python2.7/ihooks.pyRzscCsI|dkr|j|Sytj||gSWntk rDdSXdS(N(Rtfind_builtin_moduletimpR%t ImportError(RR!R#((s/usr/lib64/python2.7/ihooks.pyR }s    cCsNtj|r%ddddtffStj|rJddddtffSdS(Nt(R(t is_builtinRtBUILTIN_MODULEt is_frozent FROZEN_MODULE(RR!((s/usr/lib64/python2.7/ihooks.pyR's cCsD|\}}}ztj||||SWd|r?|jnXdS(N(R(t load_moduletclose(RR!R$tfiletfilenametinfo((s/usr/lib64/python2.7/ihooks.pyR/s N( RRt__doc__RR%RR R'R/(((s/usr/lib64/python2.7/ihooks.pyRas    cBseZdZdZdZdZdZdZdZdZ ddZ dd Z dd Z dd Zd Zd ZdZdZdZdZdZdZdZdZdZeZdZejZRS(sHooks 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 tjS(N(R(t get_suffixes(R((s/usr/lib64/python2.7/ihooks.pyR5scCs tj|S(N(R(t new_module(RR!((s/usr/lib64/python2.7/ihooks.pyR6scCs tj|S(N(R(R+(RR!((s/usr/lib64/python2.7/ihooks.pyR+scCs tj|S(N(R(t init_builtin(RR!((s/usr/lib64/python2.7/ihooks.pyR7scCs tj|S(N(R(R-(RR!((s/usr/lib64/python2.7/ihooks.pyR-scCs tj|S(N(R(t init_frozen(RR!((s/usr/lib64/python2.7/ihooks.pyR8scCs tj|S(N(R(tget_frozen_object(RR!((s/usr/lib64/python2.7/ihooks.pyR9scCstj|||S(N(R(t load_source(RR!R2R1((s/usr/lib64/python2.7/ihooks.pyR:scCstj|||S(N(R(t load_compiled(RR!R2R1((s/usr/lib64/python2.7/ihooks.pyR;scCstj|||S(N(R(t load_dynamic(RR!R2R1((s/usr/lib64/python2.7/ihooks.pyR<scCstj|||ddtfS(NR*(R(R/R(RR!R2R1((s/usr/lib64/python2.7/ihooks.pyt load_packagescCs;|j}||kr ||S|j|||<}|S(N(t modules_dictR6(RR!tdtm((s/usr/lib64/python2.7/ihooks.pyt add_modules   cCstjS(N(R&tmodules(R((s/usr/lib64/python2.7/ihooks.pyR>scCstjS(N(R&R"(R((s/usr/lib64/python2.7/ihooks.pyRscCstjj|S(N(tosR"tsplit(Rtx((s/usr/lib64/python2.7/ihooks.pyt path_splitscCstjj||S(N(RCR"tjoin(RREty((s/usr/lib64/python2.7/ihooks.pyt path_joinscCstjj|S(N(RCR"tisabs(RRE((s/usr/lib64/python2.7/ihooks.pyt path_isabsscCstjj|S(N(RCR"texists(RRE((s/usr/lib64/python2.7/ihooks.pyt path_existsscCstjj|S(N(RCR"tisdir(RRE((s/usr/lib64/python2.7/ihooks.pyt path_isdirscCstjj|S(N(RCR"tisfile(RRE((s/usr/lib64/python2.7/ihooks.pyt path_isfilescCstjj|S(N(RCR"tislink(RRE((s/usr/lib64/python2.7/ihooks.pyt path_islinkscGs t|S(N(topen(RRE((s/usr/lib64/python2.7/ihooks.pytopenfilescCs tj|S(N(RCtlistdir(RRE((s/usr/lib64/python2.7/ihooks.pyRVsN( RRR4R5R6R+R7R-R8R9RR:R;R<R=RAR>RRFRIRKRMRORQRSRUtIOErrortopenfile_errorRVRCterrort listdir_error(((s/usr/lib64/python2.7/ihooks.pyRs4                        cBs_eZdZd edZdZdZdZdZ dZ ddZ d Z RS( sDefault 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)tj|||pt||_dS(N(RRRthooks(RR[R((s/usr/lib64/python2.7/ihooks.pyRscCs |jjS(N(R[R(R((s/usr/lib64/python2.7/ihooks.pyRscCs |jjS(N(R[R>(R((s/usr/lib64/python2.7/ihooks.pyR>scCs|jS(N(R[(R((s/usr/lib64/python2.7/ihooks.pyt get_hooksscCs ||_dS(N(R[(RR[((s/usr/lib64/python2.7/ihooks.pyt set_hooksscCsT|jj|r(ddddtffS|jj|rPddddtffSdS(NR*(R[R+RR,R-R.(RR!((s/usr/lib64/python2.7/ihooks.pyR's ic Cs |dkr|j|S|r|jj||}|jj|r|jd|d}|r|d}|r~|jnd|ddtffSqnx|jjD]n}|\}} } |jj|||}y&|jj || } | ||fSWq|jj k rqXqWdS(NRiR*( RR'R[RIROR R0RR5RURX( RR!R#tallow_packagestfullnameR$R1R3tsufftmodettypetfp((s/usr/lib64/python2.7/ihooks.pyR s(    c Cs-|\}}}|\}}}z|tkr=|jj|S|tkrY|jj|S|tkr|jj|||} n|tkr|jj|||} na|t kr|jj |||} n:|t kr|jj |||} nt d||fWd|r|jnX|| _| S(Ns$Unrecognized module type (%r) for %s(R,R[R7R.R8R R<R R:R R;RR=R)R0t__file__( RR!R$R1R2R3R`RaRbR@((s/usr/lib64/python2.7/ihooks.pyR/s*       N( RRR4RRRRR>R\R]R'R R/(((s/usr/lib64/python2.7/ihooks.pyRs      cBseZdZdZRS(s8Fancy module loader -- parses and execs the code itself.cBs|\}}\}}}|}d} |ekr|jd|d} | s[ed|n| \} } } | \}}}|eefkr| r| jned||fn|g} | }| }|}n|ekr|jj |}nw|ekr*ddl }|j d|j |}n@|ekrW|j }e||d}nej|||S|jj|}| r| |_n||_y||jUWn/|jj}||kr||=nnX|S(NRis No __init__ module in package %ss/Bad type (%r) for __init__ module in package %siitexec(RRR R)R R R0R.R[R9tmarshaltseektloadtreadtcompileRR/RAt__path__Rdt__dict__R>(RR!R$R1R2R`RaRbt realfilenameR"t initstufftinitfilet initfilenametinitinfotinitsufftinitmodetinittypetcodeRftdataR@R?((s/usr/lib64/python2.7/ihooks.pyR/"sR              (RRR4R/(((s/usr/lib64/python2.7/ihooks.pyRscBszeZdZd edZdZdZdZdZ iigdZ d dZ dZ d Z d ZRS( ssBasic module importer; uses module loader. This provides basic import facilities but no package imports. cCs>tj|||p"td||_|jj|_dS(N(RRRRtloaderR>RB(RRwR((s/usr/lib64/python2.7/ihooks.pyRYscCs|jS(N(Rw(R((s/usr/lib64/python2.7/ihooks.pyt get_loader^scCs ||_dS(N(Rw(RRw((s/usr/lib64/python2.7/ihooks.pyt set_loaderascCs |jjS(N(RwR\(R((s/usr/lib64/python2.7/ihooks.pyR\dscCs|jj|S(N(RwR](RR[((s/usr/lib64/python2.7/ihooks.pyR]gscCsat|}||jkr&|j|S|jj|}|sNtd|n|jj||S(NsNo module named %s(tstrRBRwR%R)R/(RR!tglobalstlocalstfromlistR$((s/usr/lib64/python2.7/ihooks.pyt import_modulejs  cCsMt|j}|jj||}|s:td|n|jj||S(NsModule %s not found for reload(RzRRwR%R)R/(RtmoduleR"R!R$((s/usr/lib64/python2.7/ihooks.pytreloadss cCs|jt|j=dS(N(RBRzR(RR((s/usr/lib64/python2.7/ihooks.pytunloadzscCsgtj|_tj|_ttds3dt_ntj|_|j t_|jt_|jt_dS(NR( t __builtin__t __import__tsave_import_moduleRt save_reloadthasattrRRt save_unloadR~(R((s/usr/lib64/python2.7/ihooks.pyR ~s      cCs:|jt_|jt_|jt_tjs6t`ndS(N(RRRRRRR(R((s/usr/lib64/python2.7/ihooks.pyR s     N(RRR4RRRRxRyR\R]R~RRR R (((s/usr/lib64/python2.7/ihooks.pyRQs       cBsbeZdZd d d ddZddZdZdZddZddZ d Z RS( s)A module importer that supports packages.ic Csr|j||}|j|t|\}}|j||} |sL|St| drn|j| |n| S(NRk(tdetermine_parenttfind_head_packageRzt load_tailRtensure_fromlist( RR!R{R|R}tleveltparenttqttailR@((s/usr/lib64/python2.7/ihooks.pyR~scCs| s| rdS|jd}|dk rO| r|dkrtdqn|jd}|dkrndSd|kr|}nEd|kr|dkrtdnd|d3s2     58P3?