3 ^*c@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 ValueErrorZreprr)namepackageZlevelZ 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. N{}.__spec__ is not set{}.__spec__ is None)sysmodulesr__spec__AttributeErrorrformat)rZpathmodulespecrrr_find_spec_from_path#s    rc 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. r r __path__)ZfromlistNrr) rrrr rpartitionZ __import__rrrrrr)rrfullnameZ parent_nameZparentrrrrr find_specBs"     r"ccs|tjk}tjj|}|s6tt|}d|_|tj|<zJy |VWn:tk r||sxy tj|=Wntk rvYnXYnXWdd|_XdS)NTF)rrZgetZtypeZ__initializing__Z ExceptionZKeyError)rZ is_reloadrrrr_module_to_loadjs      r#ctjfdd}|S)zOSet __package__ on the returned module. This function is deprecated. csRtjdtdd||}t|dddkrN|j|_t|dsN|jjdd|_|S)N7The import system now takes care of this automatically.Z stacklevel __package__rr r )warningswarnDeprecationWarninggetattr__name__r(hasattrr )argskwargsrfxnrrset_package_wrappers   z(set_package..set_package_wrapper functoolswraps)r2r3rr1r set_packages r7cr$)zNSet __loader__ on the returned module. This function is deprecated. cs:tjdtdd|f||}t|dddkr6||_|S)Nr%r&r' __loader__)r)r*r+r,r8)selfr/r0rr1rrset_loader_wrappers  z&set_loader..set_loader_wrapperr4)r2r:rr1r set_loadersr;cs*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. r%r&r'cspt|^}||_y|j|}Wnttfk r6YnX|rD||_n|jdd|_||f||SQRXdS)Nr r )r#r8 is_packageZ ImportErrorrr(r )r9r!r/r0rr<r1rrmodule_for_loader_wrappers z4module_for_loader..module_for_loader_wrapper)r)r*r+r5r6)r2r=rr1rmodule_for_loaders r>c@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||SNr)r/r0ZclsrErrZ sz$LazyLoader.factory..)_LazyLoader__check_eager_loaderrOrrOrfactorys zLazyLoader.factorycCs|j|||_dSrN)rPrE)r9rErrr__init__ s zLazyLoader.__init__cCs |jj|SrN)rE create_module)r9rrrrrSszLazyLoader.create_modulecCs@|j|j_|j|_i}|jj|d<|j|d<||j_t|_dS)zMake the module load lazily.r@rAN)rErr8r@ZcopyrArDr?)r9rrDrrrrFs  zLazyLoader.exec_moduleN) r-rJrKrLZ staticmethodrPZ classmethodrQrRrSrFrrrrrMs   rMrNrN)rLZrZ _bootstraprrrrZ_bootstrap_externalrrrr r Z contextlibr r5rrBr)rrr"r#r7r;r>rCr?ZLoaderrMrrrrZs0             ( '/