B Y @sddlZddlZddlmZddlmZmZddlm Z ddl m Z ddl m Z mZddlmZdd lmZdd lmZmZmZmZmZdd lmZdd lmZmZdd lmZdddddddddg Zee efZ!ee ej"fZ#edddZ$e dddZ%eeej&dddZ'dd Z(e!e#ed!d"dZ)d1e!e#e e ed%d&dZ*e!e#e+d!d'dZ,d2e!e#e e e d%d(dZ-ee!e#eed!d)dZ.e!e e/d*d+dZ0e!ee dd,dZ1Gd-d.d.ej&Z2d/d0Z3dS)3N)abc)contextmanagersuppress) import_module)ResourceLoader)BytesIO TextIOWrapper)Path) ModuleType)IterableIteratorOptionalSetUnion)cast)BinaryIOTextIO)ZipImportErrorPackageResourcecontents is_resource open_binary open_textpath read_binary read_text)returncCs\t|dr0|jjdkr*td|jjqX|Sn(t|}|jjdkrTtd|n|SdS)N__spec__z{!r} is not a package)hasattrrsubmodule_search_locations TypeErrorformatnamer)packagemoduler'6/opt/alt/python37/lib/python3.7/importlib/resources.py _get_package"s   r)cCs,tj|\}}|r$td|n|SdS)Nz{!r} must be only a file name)osrsplit ValueErrorr#)rparent file_namer'r'r(_normalize_path6sr/)r%rcCs,|j}t|jdr(ttj|j|jSdS)Nget_resource_reader)rr loaderr resources_abcResourceReaderr0r$)r%specr'r'r(_get_resource_readerBs  r5cCs&|jjdks|jjs"td|dS)NzPackage has no location )rorigin has_locationFileNotFoundError)r%r'r'r(_check_locationPsr9)r%resourcerc Cst|}t|}t|}|dk r*||St|tj|jj }tj |}tj ||}y t |ddSt k rtt|jj}d}t|jjdrtt ||}WdQRX|dkr|jj}d||} t| nt|SYnXdS)Nrb)modeget_dataz{!r} resource not found in {!r})r/r)r5 open_resourcer9r*rabspathrr6dirnamejoinopenOSErrorrrr1r rr=r$r#r8r) r%r:readerabsolute_package_path package_path full_pathr1data package_namemessager'r'r(rUs.     utf-8strict)r%r:encodingerrorsrc Cst|}t|}t|}|dk r2t||||St|tj|j j }tj |}tj ||}yt |d||dStk rtt|j j}d} t|j jdrtt||} WdQRX| dkr|j j} d|| } t| ntt| ||SYnXdS)Nr)r<rMrNr=z{!r} resource not found in {!r})r/r)r5r r>r9r*rr?rr6r@rArBrCrrr1r rr=r$r#r8r) r%r:rMrNrDrErFrGr1rHrIrJr'r'r(rts.   c Cs.t|}t|}t|| }|SQRXdS)N)r/r)rread)r%r:fpr'r'r(rs c Cs2t|}t|}t|||| }|SQRXdS)N)r/r)rrP)r%r:rMrNrQr'r'r(rs c cst|}t|}t|}|dk rLyt||VdStk rHYqTXnt|t|jjj }||}| rz|Vnvt ||}| }WdQRXt \}}z$t||t|t|VWdyt|Wntk rYnXXdS)N)r/r)r5r resource_pathr8r9rr6r-existsrrPtempfileZmkstempr*writecloseremove) r%r:rDpackage_directoryZ file_pathrQrHfdZraw_pathr'r'r(rs2     )r%r$rc Cszt|}t|t|}|dk r*||Sytt|}Wnttfk rRdSX||kr`dSt|j j j |}| S)NF) r)r/r5rsetrNotADirectoryErrorr8r rr6r-is_file)r%r$rDZpackage_contentsrr'r'r(rs cCsTt|}t|}|dk r |S|jjdks4|jjs8dSt|jjj}t |SdS)Nr') r)r5rrr6r7r r-r*listdir)r%rDrXr'r'r(rsc@s4eZdZddZddZddZddZd d Zd S) _ZipImportResourceReadercCs||_||_dS)N) zipimporterfullname)selfr_r`r'r'r(__init__sz!_ZipImportResourceReader.__init__cCsN|jdd}|d|}yt|j|Stk rHt|YnXdS)N./)r`replacerr_r=rCr8)rar:fullname_as_pathrr'r'r(r>s z&_ZipImportResourceReader.open_resourcecCstdS)N)r8)rar:r'r'r(rRsz&_ZipImportResourceReader.resource_pathcCsF|jdd}|d|}y|j|Wntk r@dSXdS)NrcrdFT)r`rer_r=rC)rar$rfrr'r'r(r%sz$_ZipImportResourceReader.is_resourcec cst|j|j}||jj}|j}t}xp|jjD]d}yt||}Wnt k rbw6YnX|jj }t |dkr|j Vq6||kr6| ||Vq6WdS)Nr) r r_ get_filenamer`Z relative_toarchiver-rZ_filesr,r$lenadd)raZ fullname_pathZ relative_pathrFZ subdirs_seenfilenameZrelative parent_namer'r'r(r0s   z!_ZipImportResourceReader.contentsN)__name__ __module__ __qualname__rbr>rRrrr'r'r'r(r^ s  r^cCs2y||sdSWntk r&dSXt||S)N) is_packagerr^)r_r`r'r'r(_zipimport_get_resource_readerQs  rr)rKrL)rKrL)4r*rTrr2 contextlibrr importlibrZ importlib.abcriorr Zpathlibr typesr typingr r rrrrZ typing.iorrZ zipimportr__all__strrPathLikerr)r/r3r5r9rrbytesrrrboolrrr^rrr'r'r'r(sN          !  ,E