a DOg)@sddlZddlmZddlmZddlmZddlmZmZddl m Z ddl m Z dd l mZmZdd lmZdd lmZdd lmZmZmZmZdd lmZddlmZmZgdZeeefZeeej fZ!edddZ"edddZ#edddZ$eeej%dddZ&ddZ'ee!edddZ(d4ee!eeed!d"d#Z)ee!e*dd$d%Z+d5ee!eeed!d&d'Z,eej-dd(d)Z.ee!d*dd+d,Z/ed-d.Z0eee1d/d0d1Z2eeedd2d3Z3dS)6N)abc)_common)as_file)contextmanagersuppress) import_module)ResourceLoader)BytesIO TextIOWrapper)Path) ModuleType)ContextManagerIterableOptionalUnion)cast)BinaryIOTextIO) PackageResourcercontentsfiles is_resource open_binary open_textpath read_binary read_text)returncCst|dr|St|S)z)If name is a string, resolve to a module.__spec__)hasattrrnamer$8/opt/alt/python39/lib64/python3.9/importlib/resources.py_resolve$s r&cCs&t|}|jjdur"td||S)zTake a package name or module object and return the module. If a name, the module is imported. If the resolved module object is not a package, raise an exception. Nz{!r} is not a package)r&r submodule_search_locations TypeErrorformat)packagemoduler$r$r% _get_package+s r,cCs&tj|\}}|r"td||S)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 name)osrsplit ValueErrorr))rparent file_namer$r$r%_normalize_path7sr2)r*rcCs,|j}t|jdr(ttj|j|jSdS)Nget_resource_reader)r r!loaderr resources_abcResourceReaderr3r#)r*specr$r$r%_get_resource_readerBs   r8cCs&|jjdus|jjs"td|dS)NzPackage has no location )r origin has_locationFileNotFoundErrorr*r$r$r%_check_locationPsr=)r*resourcerc Cst|}t|}t|}|dur*||Stj|jjp:d}tj |}tj ||}zt |ddWSt yt t|jj}d}t|jjdrtt ||}Wdn1s0Y|dur|jj}d||} t| t|YS0dS)zDReturn a file-like object opened for binary reading of the resource.Nznon-existent filerb)modeget_dataz{!r} resource not found in {!r})r2r,r8 open_resourcer-rabspathr r9dirnamejoinopenOSErrorrr r4r!rrAr#r)r;r ) r*r>readerZabsolute_package_path package_path full_pathr4dataZ package_namemessager$r$r%rUs2     (rutf-8strict)r*r>encodingerrorsrcCstt||||dS)zBReturn a file-like object opened for text reading of the resource.)rOrP)r r)r*r>rOrPr$r$r%rss rcCs6t||}|WdS1s(0YdS)z+Return the binary contents of the resource.N)rread)r*r>fpr$r$r%r|s rcCs:t||||}|WdS1s,0YdS)zReturn the decoded string of the resource. The decoding-related arguments have the same semantics as those of bytes.decode(). N)rrQ)r*r>rOrPrRr$r$r%rs rcCstt|S)z3 Get a Traversable resource from a package )r from_packager,r<r$r$r%rsrzContextManager[Path]cCs2tt|}|rt||Stt|t|S)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). )r8r,_path_from_readerrrrjoinpathr2)r*r>rHr$r$r%rs  rccst|}tt"t||VWddS1s80Y||}tj|j|d}|VWdn1sx0YdS)N)suffix) r2rr;r resource_pathrBrZ _tempfilerQ)rHr>Z norm_resourceZ opener_readerresr$r$r%rTs   rT)r*r#rcCsTt|}t|t|}|dur*||Stt|}||vrBdSt||S)zYTrue if 'name' is a resource inside 'package'. Directories are *not* resources. NF) r,r2r8rsetrrrSis_file)r*r#rHZpackage_contentsr$r$r%rs  rcCsdt|}t|}|dur |S|jjdup6|jjdk}|sD|jjsHdStddt| DS)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. N namespacer$css|] }|jVqdS)Nr").0itemr$r$r% zcontents..) r,r8rr r9r:listrrSiterdir)r*rHr[r$r$r%rs   r)rMrN)rMrN)4r-rr5rr contextlibrr importlibr importlib.abcr ior r pathlibr typesr typingrrrrrZ typing.iorr__all__strrPathLikerr&r,r2r6r8r=rrbytesrr TraversablerrrTboolrrr$r$r$r%sZ