B °-_ÕSã @sxdZddlmZddlmZddlZddlZddlZddl Z ddl Z ddl Z ddl m Z ddlZdddd d d d d ddddg ZeddƒZde_dd„Zdd„Zd+dd „Zd,dd „Zed-dd„ƒZd.dd„Ze ejje¡dd„ZGd d „d ƒZGd!d„dƒZy.ddlZdd"lmZd/d#d$„Ze ee¡Wne k r@YnXd%d„Z!d0d&d„Z"d'd„Z#d(d „Z$d)d„Z%d*d „Z&dS)1zUtilities to support packages.é)Ú namedtuple)ÚsingledispatchN)Ú ModuleTypeÚ get_importerÚiter_importersÚ get_loaderÚ find_loaderÚ walk_packagesÚ iter_modulesÚget_dataÚ ImpImporterÚ ImpLoaderÚ read_codeÚ extend_pathÚ ModuleInfozmodule_finder name ispkgz.A namedtuple with minimal info about a module.cCsLy |j}Wn4tk r>| |¡}|dkr0dStj ||¡SX||ƒSdS)z'Return the finder-specific module spec.N)Ú find_specÚAttributeErrorÚ find_moduleÚ importlibÚutilÚspec_from_loader)ÚfinderÚnamerÚloader©rú/usr/lib/python3.7/pkgutil.pyÚ _get_specs  rcCs6ddl}| d¡}|tjjkr"dS| d¡| |¡S)Nréé )ÚmarshalÚreadrrÚ MAGIC_NUMBERÚload)ÚstreamrÚmagicrrrr(s    Úc #sÐifdd„‰x¾t||ƒD]°}|V|jryt|jƒWnNtk r\|dk rX||jƒYqtk r„|dk r~||jƒn‚YqXttj|jddƒpœg}‡fdd„|Dƒ}t ||jd|ƒEdHqWdS)a«Yields ModuleInfo for all modules recursively on path, or, if path is None, all accessible modules. 'path' should be either None or a list of paths to look for modules in. 'prefix' is a string to output on the front of every module name on output. Note that this function must import all *packages* (NOT all modules!) on the given path, in order to access the __path__ attribute to find submodules. 'onerror' is a function which gets called with one argument (the name of the package which was being imported) if any exception occurs while trying to import a package. If no onerror function is supplied, ImportErrors are caught and ignored, while all other exceptions are propagated, terminating the search. Examples: # list all modules python can access walk_packages() # list all submodules of ctypes walk_packages(ctypes.__path__, ctypes.__name__+'.') cSs||kr dSd||<dS)NTr)ÚpÚmrrrÚseenRszwalk_packages..seenNÚ__path__csg|]}ˆ|ƒs|‘qSrr)Ú.0r&)r(rrú isz!walk_packages..Ú.) r ÚispkgÚ __import__rÚ ImportErrorÚ ExceptionÚgetattrÚsysÚmodulesr )ÚpathÚprefixÚonerrorÚinfor)r(rr 5s   ccsz|dkrtƒ}nt|tƒr$tdƒ‚n tt|ƒ}i}xB|D]:}x4t||ƒD]&\}}||krHd||<t|||ƒVqHWq8WdS)aYields ModuleInfo for all submodules on path, or, if path is None, all top-level modules on sys.path. 'path' should be either None or a list of paths to look for modules in. 'prefix' is a string to output on the front of every module name on output. Nz9path must be None or list of paths to look for modules iné)rÚ isinstanceÚstrÚ ValueErrorÚmaprÚiter_importer_modulesr)r4r5Z importersÚyieldedÚirr-rrrr ns     cCst|dƒsgS| |¡S)Nr )Úhasattrr )Úimporterr5rrrr=ˆs r=c cs0|jdkstj |j¡sdSi}ddl}yt |j¡}Wntk rPg}YnX| ¡xÐ|D]È}| |¡}|dks`||kr€q`tj |j|¡}d}|stj |¡rd|kr|}yt |¡} Wntk rÜg} YnXx&| D]}| |¡} | dkräd}PqäWq`|r`d|kr`d||<|||fVq`WdS)NrÚ__init__Fr,Tr8) r4ÚosÚisdirÚinspectÚlistdirÚOSErrorÚsortÚ getmodulenameÚjoin) rAr5r>rEÚ filenamesÚfnÚmodnamer4r-Ú dircontentsÚsubnamerrrÚ_iter_file_finder_moduless<       rPc Cs.t ¡t dt¡t d¡aWdQRXdS)NÚignoreÚimp)ÚwarningsÚcatch_warningsÚ simplefilterÚDeprecationWarningrÚ import_modulerRrrrrÚ _import_imp¼s  rXc@s.eZdZdZd dd„Zd dd„Zd dd „ZdS) r aPEP 302 Finder that wraps Python's "classic" import algorithm ImpImporter(dirname) produces a PEP 302 finder that searches that directory. ImpImporter(None) produces a PEP 302 finder that searches the current sys.path, plus any modules that are frozen or built-in. Note that ImpImporter does not currently support being used by placement on sys.meta_path. NcCst dt¡tƒ||_dS)Nz5This emulation is deprecated, use 'importlib' instead)rSÚwarnrVrXr4)Úselfr4rrrrBÍszImpImporter.__init__cCs~| d¡d}||kr$|jdkr$dS|jdkr4d}ntj |j¡g}yt ||¡\}}}Wntk rndSXt||||ƒS)Nr,éÿÿÿÿ)Úsplitr4rCÚrealpathrRrr/r )rZÚfullnamer4rOÚfileÚfilenameÚetcrrrrÔs zImpImporter.find_moduler%c cs0|jdkstj |j¡sdSi}ddl}yt |j¡}Wntk rPg}YnX| ¡xÐ|D]È}| |¡}|dks`||kr€q`tj |j|¡}d}|stj |¡rd|kr|}yt |¡} Wntk rÜg} YnXx&| D]}| |¡} | dkräd}PqäWq`|r`d|kr`d||<|||fVq`WdS)NrrBFr,Tr8) r4rCrDrErFrGrHrIrJ) rZr5r>rErKrLrMr4r-rNrOrrrr ãs<       zImpImporter.iter_modules)N)N)r%)Ú__name__Ú __module__Ú __qualname__Ú__doc__rBrr rrrrr Âs   c@sneZdZdZdZZdd„Zdd„Zdd„Zd d „Z d d „Z d d„Z ddd„Z ddd„Z dd„Zddd„ZdS)r zBPEP 302 Loader that wraps Python's "classic" import algorithm NcCs.t dt¡tƒ||_||_||_||_dS)Nz5This emulation is deprecated, use 'importlib' instead)rSrYrVrXr_r`r^ra)rZr^r_r`rarrrrBszImpLoader.__init__cCs:| ¡zt ||j|j|j¡}Wd|jr4|j ¡X|S)N)Ú_reopenrRÚ load_moduler_r`raÚclose)rZr^Úmodrrrrgs  zImpLoader.load_modulec Cst|dƒ }| ¡SQRXdS)NÚrb)Úopenr )rZÚpathnamer_rrrr %s zImpLoader.get_datacCsT|jrP|jjrP|jd}|tjkr2t|jdƒ|_n|tjtjfkrPt|jdƒ|_dS)NéÚrrj) r_ÚclosedrarRÚ PY_SOURCErkr`Ú PY_COMPILEDÚ C_EXTENSION)rZÚmod_typerrrrf)s   zImpLoader._reopencCs0|dkr|j}n||jkr,td|j|fƒ‚|S)Nz,Loader for module %s cannot handle module %s)r^r/)rZr^rrrÚ _fix_name1s  zImpLoader._fix_namecCs| |¡}|jdtjkS)Nrm)rtrarRÚ PKG_DIRECTORY)rZr^rrrÚ is_package9s zImpLoader.is_packagecCs”| |¡}|jdkrŽ|jd}|tjkrD| |¡}t||jdƒ|_nJ|tjkrv|  ¡zt |j ƒ|_Wd|j   ¡Xn|tj krŽ| ¡ ¡|_|jS)NrmÚexec)rtÚcoderarRrpÚ get_sourceÚcompiler`rqrfrr_rhruÚ _get_delegateÚget_code)rZr^rsÚsourcerrrr|=s       zImpLoader.get_codec Csº| |¡}|jdkr´|jd}|tjkrP| ¡z|j ¡|_Wd|j ¡Xnd|tj krœt j   |j dd…¡r´t|j dd…dƒ}| ¡|_WdQRXn|tjkr´| ¡ ¡|_|jS)Nrmr[rn)rtr}rarRrprfr_r rhrqrCr4Úexistsr`rkrur{ry)rZr^rsÚfrrrryNs      zImpLoader.get_sourcecCst|jƒ}t|dƒ}|jS)NrB)r r`rr)rZrÚspecrrrr{`s  zImpLoader._get_delegatecCsH| |¡}|jd}|tjkr*| ¡ ¡S|tjtjtjfkrD|j SdS)Nrm) rtrarRrur{Ú get_filenamerprqrrr`)rZr^rsrrrres    zImpLoader.get_filename)N)N)N)rbrcrdrerxr}rBrgr rfrtrvr|ryr{rrrrrr s    )Ú zipimporterc csðttj|jƒ}|j}t|ƒ}i}ddl}xÀ|D]¸}| |¡s@q0||d… t j ¡}t|ƒdkr˜|d d¡r˜|d|kr˜d||d<||ddfVt|ƒdkr¦q0|  |d¡}|dkr¾q0|r0d|kr0||kr0d||<||dfVq0WdS) Nrrmr8z __init__.pyTrBr,F) ÚsortedÚ zipimportÚ_zip_directory_cacheÚarchiver5ÚlenrEÚ startswithr\rCÚseprI) rAr5ZdirlistÚ_prefixZplenr>rErLrMrrrÚiter_zipimport_modulesss*     r‹c Csnytj|}WnZtk rhxDtjD]6}y||ƒ}tj ||¡PWq&tk rZYq&Xq&Wd}YnX|S)zÿRetrieve a finder for the given path item The returned finder is cached in sys.path_importer_cache if it was newly created by a path hook. The cache (or part of it) can be cleared manually if a rescan of sys.path_hooks is necessary. N)r2Úpath_importer_cacheÚKeyErrorÚ path_hooksÚ setdefaultr/)Z path_itemrAÚ path_hookrrrr•s    ccs„| d¡rd |¡}t|ƒ‚d|krV| d¡d}t |¡}t|ddƒ}|dkrhdSntjEdHtj }x|D]}t |ƒVqnWdS)a¹Yield finders for the given module name If fullname contains a '.', the finders will be for the package containing fullname, otherwise they will be all registered top level finders (i.e. those on both sys.meta_path and sys.path_hooks). If the named module is in a package, that package is imported as a side effect of invoking this function. If no module name is specified, all top level finders are produced. r,z'Relative module name {!r} not supportedrr)N) rˆÚformatr/Ú rpartitionrrWr1r2Ú meta_pathr4r)r^ÚmsgZpkg_nameZpkgr4Úitemrrrr­s      cCsn|tjkr tj|}|dkr dSt|tƒrb|}t|ddƒ}|dk rF|St|ddƒdkrZdS|j}n|}t|ƒS)zøGet a "loader" object for module_or_name Returns None if the module cannot be found or imported. If the named module is not already imported, its containing package (if any) is imported, in order to establish the package __path__. NÚ __loader__Ú__spec__)r2r3r9rr1rbr)Zmodule_or_nameÚmodulerr^rrrrÊs    c Cs†| d¡rd |¡}t|ƒ‚ytj |¡}WnFttttfk rr}z d}t| |t |ƒ|¡ƒ|‚Wdd}~XYnX|dk r‚|j SdS)zàFind a "loader" object for fullname This is a backwards compatibility wrapper around importlib.util.find_spec that converts most failures to ImportError and only returns the loader rather than the full spec r,z'Relative module name {!r} not supportedz,Error while finding loader for {!r} ({}: {})N) rˆr‘r/rrrrÚ TypeErrorr;Útyper)r^r”r€Zexrrrrâs  *c Cs¨t|tƒs|S|d}|dd…}| d¡\}}}|rbytj|j}Wqhttfk r^|SXntj}x8|D].}t|t ƒs‚qpt |ƒ}|dk rög} t |dƒr¾|  |¡} | dk rÖ| j pºg} nt |dƒrÖ| |¡\}} x| D]} | |krÜ| | ¡qÜWtj ||¡} tj | ¡rpy t| ƒ} Wn8tk rT}ztj d| |f¡Wdd}~XYqpX| @x8| D]0}| d¡}|rb| d¡r†qb| |¡qbWWdQRXqpW|S) a‹Extend a package's path. Intended use is to place the following code in a package's __init__.py: from pkgutil import extend_path __path__ = extend_path(__path__, __name__) This will add to the package's __path__ all subdirectories of directories on sys.path named after the package. This is useful if one wants to distribute different parts of a single logical package as multiple directories. It also looks for *.pkg files beginning where * matches the name argument. This feature is similar to *.pth files (see site.py), except that it doesn't special-case lines starting with 'import'. A *.pkg file is trusted at face value: apart from checking for duplicates, all entries found in a *.pkg file are added to the path, regardless of whether they are exist the filesystem. (This is a feature.) If the input path is not a list (as is the case for frozen packages) it is returned unchanged. The input path is not modified; an extended copy is returned. Items are only appended to the copy at the end. It is assumed that sys.path is a sequence. Items of sys.path that are not (unicode or 8-bit) strings referring to existing directories are ignored. Unicode items of sys.path that cause errors when used as filenames may cause this function to raise an exception (in line with os.path.isdir() behavior). z.pkgNr,rrzCan't open %s: %s Ú ú#)r9Úlistr’r2r3r)rrr4r:rr@rÚsubmodule_search_locationsrÚappendrCrJÚisfilerkrGÚstderrÚwriteÚrstriprˆ)r4rZ sname_pkgZparent_packageÚ_Z final_nameZ search_pathÚdirrÚportionsr€ZportionZpkgfilerr”Úlinerrrr÷sP!            cCs˜tj |¡}|dkrdS|j}|dks0t|dƒs4dStj |¡pJtj  |¡}|dks^t|dƒsbdS|  d¡}|  dt j  |j¡¡t j j|Ž}| |¡S)afGet a resource from a package. This is a wrapper round the PEP 302 loader get_data API. The package argument should be the name of a package, in standard module format (foo.bar). The resource argument should be in the form of a relative filename, using '/' as the path separator. The parent directory name '..' is not allowed, and nor is a rooted name (starting with a '/'). The function returns a binary string, which is the contents of the specified resource. For packages located in the filesystem, which have already been imported, this is the rough equivalent of d = os.path.dirname(sys.modules[package].__file__) data = open(os.path.join(d, resource), 'rb').read() If the package cannot be located or loaded, or it uses a PEP 302 loader which does not support get_data(), then None is returned. Nr Ú__file__ú/r)rrrrr@r2r3ÚgetÚ _bootstrapÚ_loadr\ÚinsertrCr4Údirnamer¨rJr )ÚpackageÚresourcer€rriÚpartsZ resource_namerrrr Us     )Nr%N)Nr%)r%)r%)r%)r%)'reÚ collectionsrÚ functoolsrZ simplegenericrÚimportlib.utilÚimportlib.machineryrCÚos.pathr2ÚtypesrrSÚ__all__rrrr r r=rPÚregisterÚ machineryÚ FileFinderrXr r r„r‚r‹r/rrrrrr rrrrÚsR     9   ( Jc   ^