3 \ƒ*ã@sdZddlmZddlmZddlmZddlmZddlmZddlm Z ddlm Z dd lm Z dd lm Z dd lm Z d d lmZd dlZd dlZd dlZd dlZdd„Zd!dd„Zd"dd„Zedd„ƒZdd„Zdd„Zdd„ZGdd„dejƒZGdd „d ejƒZdS)#z-Utility code for constructing importers, etc.é)Úabc)Úmodule_from_spec)Ú _resolve_name)Úspec_from_loader)Ú _find_spec)Ú MAGIC_NUMBER)Úcache_from_source)Ú decode_source)Úsource_from_cache)Úspec_from_file_locationé)ÚcontextmanagerNcCs^|jdƒs|S|s&tdt|ƒ›d�ƒ‚d}x|D]}|dkr>P|d7}q0Wt||d…||ƒS)z2Resolve a relative module name to an absolute one.Ú.zno package specified for z% (required for relative module names)r rN)Ú startswithÚ ValueErrorÚreprr)ÚnameÚpackageÚlevelÚ character©rú&/usr/lib64/python3.6/importlib/util.pyÚ resolve_names   rc Csx|tjkrt||ƒStj|}|dkr*dSy |j}Wn$tk rXtdj|ƒƒd‚YnX|dkrptdj|ƒƒ‚|SdS)a‡Return the spec for the specified module. First, sys.modules is checked to see if the module was already imported. If so, then sys.modules[name].__spec__ is returned. If that happens to be set to None, then ValueError is raised. If the module is not in sys.modules, then sys.meta_path is searched for a suitable spec with the value of 'path' given to the finders. None is returned if no spec could be found. Dotted names do not have their parent packages implicitly imported. You will most likely need to explicitly import all parent packages in the proper order for a submodule to get the correct spec. Nz{}.__spec__ is not setz{}.__spec__ is None)ÚsysÚmodulesrÚ__spec__ÚAttributeErrorrÚformat)rÚpathÚmoduleÚspecrrrÚ_find_spec_from_path#s    r!c Cs¾|jdƒrt||ƒn|}|tjkrZ|jdƒd}|rNt|dgd�}t||jƒSt|dƒSn`tj|}|dkrpdSy |j}Wn$t k ržt dj |ƒƒd‚YnX|dkr¶t dj |ƒƒ‚|SdS)a¯Return the spec for the specified module. First, sys.modules is checked to see if the module was already imported. If so, then sys.modules[name].__spec__ is returned. If that happens to be set to None, then ValueError is raised. If the module is not in sys.modules, then sys.meta_path is searched for a suitable spec with the value of 'path' given to the finders. None is returned if no spec could be found. If the name is for submodule (contains a dot), the parent module is automatically imported. The name and package arguments work the same as importlib.import_module(). In other words, relative module names (with leading dots) work. rr Ú__path__)ÚfromlistNz{}.__spec__ is not setz{}.__spec__ is None) rrrrÚ rpartitionÚ __import__rr"rrrr)rrÚfullnameÚ parent_nameÚparentrr rrrÚ find_specBs"     r)ccsŽ|tjk}tjj|ƒ}|s6ttƒ|ƒ}d|_|tj|<zJy |VWn:tk r||sxy tj|=Wntk rvYnXYnXWdd|_XdS)NTF)rrÚgetÚtypeÚ__initializing__Ú ExceptionÚKeyError)rÚ is_reloadrrrrÚ_module_to_loadjs      r0cstjˆƒ‡fdd„ƒ}|S)zOSet __package__ on the returned module. This function is deprecated. csRtjdtdd�ˆ||Ž}t|ddƒdkrN|j|_t|dƒsN|jjdƒd|_|S)Nz7The import system now takes care of this automatically.é)Ú stacklevelÚ __package__r"rr )ÚwarningsÚwarnÚDeprecationWarningÚgetattrÚ__name__r3Úhasattrr$)ÚargsÚkwargsr)ÚfxnrrÚset_package_wrapperŠs   z(set_package..set_package_wrapper)Ú functoolsÚwraps)r<r=r)r<rÚ set_package„s r@cstjˆƒ‡fdd„ƒ}|S)zNSet __loader__ on the returned module. This function is deprecated. cs:tjdtdd�ˆ|f|ž|Ž}t|ddƒdkr6||_|S)Nz7The import system now takes care of this automatically.r1)r2Ú __loader__)r4r5r6r7rA)Úselfr:r;r)r<rrÚset_loader_wrapper�s  z&set_loader..set_loader_wrapper)r>r?)r<rCr)r<rÚ set_loader—srDcs*tjdtdd�tjˆƒ‡fdd„ƒ}|S)a*Decorator to handle selecting the proper module for loaders. The decorated function is passed the module to use instead of the module name. The module passed in to the function is either from sys.modules if it already exists or is a new module. If the module is new, then __name__ is set the first argument to the method, __loader__ is set to self, and __package__ is set accordingly (if self.is_package() is defined) will be set before it is passed to the decorated function (if self.is_package() does not work for the module it will be set post-load). If an exception is raised and the decorator created the module it is subsequently removed from sys.modules. The decorator assumes that the decorated function takes the module name as the second argument. z7The import system now takes care of this automatically.r1)r2cspt|ƒ�^}||_y|j|ƒ}Wnttfk r6YnX|rD||_n|jdƒd|_ˆ||f|ž|ŽSQRXdS)Nrr )r0rAÚ is_packageÚ ImportErrorrr3r$)rBr&r:r;rrE)r<rrÚmodule_for_loader_wrapper¼s z4module_for_loader..module_for_loader_wrapper)r4r5r6r>r?)r<rGr)r<rÚmodule_for_loader¨s rHc@s eZdZdZdd„Zdd„ZdS)Ú _LazyModulezKA subclass of the module type which triggers loading upon attribute access.c CsÎtj|_|jj}|jjd}|jjd}|j}i}xF|jƒD]:\}}||krV|||<qConstruct a callable which returns the eager loader made lazy.csˆˆ||ŽƒS)Nr)r:r;)ÚclsrQrrÚ sz$LazyLoader.factory..)Ú_LazyLoader__check_eager_loader)rerQr)rerQrÚfactorys zLazyLoader.factorycCs|j|ƒ||_dS)N)rgrQ)rBrQrrrÚ__init__ s zLazyLoader.__init__cCs |jj|ƒS)N)rQÚ create_module)rBr rrrrjszLazyLoader.create_modulecCs@|j|j_|j|_i}|jjƒ|d<|j|d<||j_t|_dS)zMake the module load lazily.rJrKN)rQrrArJÚcopyrKrNrI)rBrrNrrrrRs  zLazyLoader.exec_moduleN) r8r_r`raÚ staticmethodrgÚ classmethodrhrirjrRrrrrrbþs   rb)N)N)raÚrÚ _bootstraprrrrÚ_bootstrap_externalrrr r r Ú contextlibr r>rrLr4rr!r)r0r@rDrHrMrIÚLoaderrbrrrrÚs0             ( '/