3 \*@sdZddlmZddlmZddlmZddlmZddlmZddlm Z ddlm Z dd lm Z dd lm Z dd lm Z d d lmZd dlZd dlZd dlZd dlZddZd!ddZd"ddZeddZddZddZddZGdddejZGdd d ejZdS)#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^|jds|S|s&tdt|dd}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 ValueErrorreprr)namepackagelevel characterr&/usr/lib64/python3.6/importlib/util.py resolve_names   rc Csx|tjkrt||Stj|}|dkr*dSy |j}Wn$tk rXtdj|dYnX|dkrptdj||SdS)aReturn 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)sysmodulesr__spec__AttributeErrorrformat)rpathmodulespecrrr_find_spec_from_path#s    r!c Cs|jdrt||n|}|tjkrZ|jdd}|rNt|dgd}t||jSt|dSn`tj|}|dkrpdSy |j}Wn$t k rt dj |dYnX|dkrt dj ||SdS)aReturn 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)rrfullname parent_nameparentrr rrr find_specBs"     r)ccs|tjk}tjj|}|s6tt|}d|_|tj|<zJy |VWn:tk r||sxy tj|=Wntk rvYnXYnXWdd|_XdS)NTF)rrgettype__initializing__ ExceptionKeyError)r is_reloadrrrr_module_to_loadjs      r0cstjfdd}|S)zOSet __package__ on the returned module. This function is deprecated. csRtjdtdd||}t|dddkrN|j|_t|dsN|jjdd|_|S)Nz7The import system now takes care of this automatically.) stacklevel __package__r"rr )warningswarnDeprecationWarninggetattr__name__r3hasattrr$)argskwargsr)fxnrrset_package_wrappers   z(set_package..set_package_wrapper) functoolswraps)r<r=r)r<r set_packages r@cstjfdd}|S)zNSet __loader__ on the returned module. This function is deprecated. cs:tjdtdd|f||}t|dddkr6||_|S)Nz7The import system now takes care of this automatically.r1)r2 __loader__)r4r5r6r7rA)selfr:r;r)r<rrset_loader_wrappers  z&set_loader..set_loader_wrapper)r>r?)r<rCr)r<r set_loadersrDcs*tjdtddtjfdd}|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|jdd|_||f||SQRXdS)Nrr )r0rA is_package ImportErrorrr3r$)rBr&r:r;rrE)r<rrmodule_for_loader_wrappers z4module_for_loader..module_for_loader_wrapper)r4r5r6r>r?)r<rGr)r<rmodule_for_loaders rHc@s eZdZdZddZddZdS) _LazyModulezKA subclass of the module type which triggers loading upon attribute access.c Cstj|_|jj}|jjd}|jjd}|j}i}xF|jD]:\}}||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)rerQrfactorys 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)rQrrArJcopyrKrNrI)rBrrNrrrrRs  zLazyLoader.exec_moduleN) r8r_r`ra staticmethodrg classmethodrhrirjrRrrrrrbs   rb)N)N)rar _bootstraprrrr_bootstrap_externalrrr r r contextlibr r>rrLr4rr!r)r0r@rDrHrMrILoaderrbrrrrs0             ( '/