3 Pf*@sddlmZddlZddlZddlZddlZddlZddlZddlZddl Z ddl Z ddl m Z ddl mZmZmZmZejeZdaGdddeZGdd d eZGd d d eZGd d d eZGdddeZGdddeZedee jeiZyFy ddl Z!Wne"k r$ddl#Z!YnXeee!j$<eee!j%<[!Wne"e&fk rXYnXddZ'iZ(ddZ)e j*e+dZ,ddZ-dS))unicode_literalsN)DistlibException)cached_propertyget_cache_basepath_to_cache_dirCachecs.eZdZdfdd ZddZddZZS) ResourceCacheNcs0|dkrtjjttd}tt|j|dS)Nzresource-cache)ospathjoinrstrsuperr __init__)selfbase) __class__/usr/lib/python3.6/resources.pyrszResourceCache.__init__cCsdS)z Is the cache stale for the given resource? :param resource: The :class:`Resource` being cached. :param path: The path of the resource in the cache. :return: True if the cache is stale. Tr)rresourcer rrris_stale#s zResourceCache.is_stalec Cs|jj|\}}|dkr|}n~tjj|j|j||}tjj|}tjj|sXtj |tjj |sjd}n |j ||}|rt |d}|j |jWdQRX|S)z Get a resource into the cache, :param resource: A :class:`Resource` instance. :return: The pathname of the resource in the cache. NTwb)finderget_cache_infor r r rZ prefix_to_dirdirnameisdirmakedirsexistsropenwritebytes)rrprefixr resultrZstalefrrrget.s      zResourceCache.get)N)__name__ __module__ __qualname__rrr$ __classcell__rr)rrr s r c@seZdZddZdS) ResourceBasecCs||_||_dS)N)rname)rrr*rrrrIszResourceBase.__init__N)r%r&r'rrrrrr)Hsr)c@s@eZdZdZdZddZeddZeddZed d Z d S) Resourcez A class representing an in-package resource, such as a data file. This is not normally instantiated by user code, but rather by a :class:`ResourceFinder` which manages the resource. FcCs |jj|S)z Get the resource as a stream. This is not a property to make it obvious that it returns a new stream each time. )r get_stream)rrrr as_streamVszResource.as_streamcCstdkrtatj|S)N)cacher r$)rrrr file_path_szResource.file_pathcCs |jj|S)N)r get_bytes)rrrrr fszResource.bytescCs |jj|S)N)rget_size)rrrrsizejsz Resource.sizeN) r%r&r'__doc__ is_containerr-rr/r r2rrrrr+Ns   r+c@seZdZdZeddZdS)ResourceContainerTcCs |jj|S)N)r get_resources)rrrr resourcesrszResourceContainer.resourcesN)r%r&r'r4rr7rrrrr5osr5c@seZdZdZejjdrdZnd ZddZdd Z d d Z d d Z ddZ ddZ ddZddZddZddZddZeejjZddZdS)!ResourceFinderz4 Resource finder for file system resources. java.pyc.pyo.classcCs.||_t|dd|_tjjt|dd|_dS)N __loader____file__)modulegetattrloaderr r rr)rr@rrrrszResourceFinder.__init__cCs tjj|S)N)r r realpath)rr rrr _adjust_pathszResourceFinder._adjust_pathcCsBt|trd}nd}|j|}|jd|jtjj|}|j|S)N//r) isinstancer splitinsertrr r r rD)r resource_nameseppartsr"rrr _make_paths   zResourceFinder._make_pathcCs tjj|S)N)r r r)rr rrr_findszResourceFinder._findcCs d|jfS)N)r )rrrrrrszResourceFinder.get_cache_infocCsD|j|}|j|sd}n&|j|r0t||}n t||}||_|S)N)rMrN _is_directoryr5r+r )rrJr r"rrrfinds     zResourceFinder.findcCs t|jdS)Nrb)rr )rrrrrr,szResourceFinder.get_streamc Cs t|jd }|jSQRXdS)NrQ)rr read)rrr#rrrr0szResourceFinder.get_bytescCstjj|jS)N)r r getsize)rrrrrr1szResourceFinder.get_sizecs*fddtfddtj|jDS)Ncs|dko|jj S)N __pycache__)endswithskipped_extensions)r#)rrrallowedsz-ResourceFinder.get_resources..allowedcsg|]}|r|qSrr).0r#)rWrr sz0ResourceFinder.get_resources..)setr listdirr )rrr)rWrrr6s zResourceFinder.get_resourcescCs |j|jS)N)rOr )rrrrrr4szResourceFinder.is_containerccs|j|}|dk r|g}xn|r|jd}|V|jr|j}xH|jD]>}|sP|}ndj||g}|j|}|jrz|j|qB|VqBWqWdS)NrrF)rPpopr4r*r7r append)rrJrZtodoZrnamer*new_nameZchildrrriterators      zResourceFinder.iteratorN)r:r;r<)r:r;)r%r&r'r3sysplatform startswithrVrrDrMrNrrPr,r0r1r6r4 staticmethodr r rrOr_rrrrr8ws"    r8cs`eZdZdZfddZddZddZdd Zd d Zd d Z ddZ ddZ ddZ Z S)ZipResourceFinderz6 Resource finder for resources in .zip files. csZtt|j||jj}dt||_t|jdr>|jj|_n t j ||_t |j|_ dS)Nr_files) rrdrrBarchivelen prefix_lenhasattrre zipimport_zip_directory_cachesortedindex)rr@rf)rrrrs   zZipResourceFinder.__init__cCs|S)Nr)rr rrrrDszZipResourceFinder._adjust_pathc Cs||jd}||jkrd}nX|r:|dtjkr:|tj}tj|j|}y|j|j|}Wntk rtd}YnX|stj d||j j ntj d||j j |S)NTrFz_find failed: %r %rz_find worked: %r %r) rhrer rKbisectrmrb IndexErrorloggerdebugrBr!)rr r"irrrrNs   zZipResourceFinder._findcCs&|jj}|jdt|d}||fS)Nr)rBrfr rg)rrr!r rrrrsz ZipResourceFinder.get_cache_infocCs|jj|jS)N)rBget_datar )rrrrrr0szZipResourceFinder.get_bytescCstj|j|S)N)ioBytesIOr0)rrrrrr,szZipResourceFinder.get_streamcCs|j|jd}|j|dS)N)r rhre)rrr rrrr1szZipResourceFinder.get_sizecCs|j|jd}|r,|dtjkr,|tj7}t|}t}tj|j|}xV|t|jkr|j|j|sjP|j||d}|j |j tjdd|d7}qJW|S)Nrrrn) r rhr rKrgrZrormrbaddrH)rrr Zplenr"rssrrrr6s  zZipResourceFinder.get_resourcesc Csj||jd}|r*|dtjkr*|tj7}tj|j|}y|j|j|}Wntk rdd}YnX|S)NrFrn)rhr rKrormrbrp)rr rsr"rrrrOs  zZipResourceFinder._is_directory)r%r&r'r3rrDrNrr0r,r1r6rOr(rr)rrrds rdcCs|tt|<dS)N)_finder_registrytype)rB finder_makerrrrregister_finder0sr}cCs|tkrt|}nv|tjkr$t|tj|}t|dd}|dkrJtdt|dd}tjt|}|dkrxtd|||}|t|<|S)z Return a resource finder for a package. :param package: The name of the package. :return: A :class:`ResourceFinder` instance for the package. __path__Nz8You cannot get a finder for a module, only for a packager=zUnable to locate finder for %r) _finder_cacher`modules __import__rArrzr$r{)packager"r@r rBr|rrrr6s      rZ __dummy__cCsRd}tj|tjj|}tjt|}|rNt}tj j |d|_ ||_ ||}|S)z Return a resource finder for a path, which should represent a container. :param path: The path. :return: A :class:`ResourceFinder` instance for the path. Nr?) pkgutilZ get_importerr`path_importer_cacher$rzr{ _dummy_moduler r r r>r=)r r"rBrr@rrrfinder_for_pathRs  r).Z __future__rroruZloggingr rZshutilr`typesrjr?rutilrrrrZ getLoggerr%rqr.r objectr)r+r5r8rdr{ zipimporterrz_frozen_importlib_externalZ_fi ImportError_frozen_importlibSourceFileLoader FileFinderAttributeErrorr}rr ModuleTyper rrrrrrsH   ,!ZN