U e5d@% @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)d-e!e#e e ed%d&dZ*e!e#e+d!d'dZ,d.e!e#e e e d%d(dZ-ee!e#eed!d)dZ.e!e e/d*d+dZ0e!ee dd,dZ1dS)/N)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)zTake a package name or module object and return the module. If a name, the module is imported. If the passed or imported module object is not a package, raise an exception. __spec__Nz{!r} is not a package)hasattrrsubmodule_search_locations TypeErrorformatnamer)packagemoduler'+/usr/lib64/python3.8/importlib/resources.py _get_package"s   r)cCs,tj|\}}|r$td|n|SdS)zNormalize a path by ensuring it is a string. If the resulting string contains path separators, an exception is raised. z{!r} must be only a file nameN)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 ||}zt |ddWSt k rtt|jj}d}t|jjdrtt ||}W5QRX|dkr|jj}d||} t| n t|YSYnXdS)zDReturn a file-like object opened for binary reading of the resource.Nrb)modeget_data{!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(rUs2    utf-8strict)r%r:encodingerrorsrc Cs t|}t|}t|}|dk r2t||||St|tj|j j }tj |}tj ||}zt |d||dWStk rtt|j j}d} t|j jdrtt||} W5QRX| dkr|j j} d|| } t| ntt| ||YSYnXdS)zBReturn a file-like object opened for text reading of the resource.Nr)r<rNrOr=r>)r/r)r5r r?r9r*rr@rr6rArBrCrDrrr1r rr=r$r#r8r) r%r:rNrOrErFrGrHr1rIrJrKr'r'r(rts2   c Cs:t|}t|}t||}|W5QRSQRXdS)z+Return the binary contents of the resource.N)r/r)rread)r%r:fpr'r'r(rs c Cs>t|}t|}t||||}|W5QRSQRXdS)zReturn the decoded string of the resource. The decoding-related arguments have the same semantics as those of bytes.decode(). N)r/r)rrQ)r%r:rNrOrRr'r'r(rs c cst|}t|}t|}|dk rNzt||VWdStk rJYqVXnt|d}|jjdk r|t|jjj }||}|dk r| r|Vnxt ||}| }W5QRXt \}}z$t||t|t|VW5zt|Wntk rYnXXdS)akA context manager providing a file path object to the resource. If the resource does not already exist on its own on the file system, a temporary file will be created. If the file was created, the file will be deleted upon exiting the context manager (no exception is raised if the file was deleted prior to the context manager exiting). N)r/r)r5r resource_pathr8r9rr6r-existsrrQtempfileZmkstempr*removewriteclose) r%r:rEZ file_pathpackage_directoryrRrIfdZraw_pathr'r'r(rs6      )r%r$rc Cs|t|}t|t|}|dk r*||Sztt|}Wnttfk rTYdSX||krbdSt|j j j |}| S)zYTrue if 'name' is a resource inside 'package'. Directories are *not* resources. NF) r)r/r5rsetrNotADirectoryErrorr8r rr6r-is_file)r%r$rEZpackage_contentsrr'r'r(rs cCsTt|}t|}|dk r |S|jjdks4|jjs8dSt|jjj}t |SdS)zReturn an iterable of entries in 'package'. Note that not all entries are resources. Specifically, directories are not considered resources. Use `is_resource()` on each entry returned here to check if it is a resource or not. Nr') r)r5rrr6r7r r-r*listdir)r%rErYr'r'r(rs)rLrM)rLrM)2r*rUrr2 contextlibrr importlibr importlib.abcriorr pathlibr typesr typingr r rrrrZ typing.iorrZ zipimportr__all__strrPathLikerr)r/r3r5r9rrbytesrrrboolrrr'r'r'r(sh          ! "  .