gL&|ddlZddlZddlZddlZddlZddlZddlZddlZddlm Z ddlm Z m Z m Z ddl mZddl mZddlmZmZGd d e Zd0d Zd1d ZGd dej.Zedeeddj9ddj9ddeeddddddddddddddiid eeddj9ddj9dd!eed"d#id$%ZGd&d'ZGd(d)eZGd*d+eZ Gd,d-eZ!Gd.d/e eZ"y)2N) ModuleSpec)ResourceReader TraversableTraversableResources)_path)zip) import_helper os_helperc0eZdZdZdZdZdZdZdZy)Readerc 8t|j|yN)varsupdate)selfkwargss M/opt/hc_python/lib/python3.12/site-packages/importlib_resources/tests/util.py__init__zReader.__init__s T &!c|Srrpackages rget_resource_readerzReader.get_resource_readers rct||_t|jtr |j|jSr)r isinstancefile Exceptionrpaths r open_resourcezReader.open_resources* dii +))Oyyrct||_t|jtr |j|jSr)r rr"r )rpath_s r resource_pathzReader.resource_paths* dii +))Oyyrc|_t|jtr |jd}t fdt ||j DS)Nc$|jdS)N/)split)entrys rpartz Reader.is_resource..part)s;;s# #rc3NK|]}t|dk(xr|dk(yw)rrN)len).0partsr%s r z%Reader.is_resource..,s/ ?XeCJ!O 1aE 1 1?Xs"%)r rr"r anymap _contents)rr%r,s ` r is_resourcezReader.is_resource$sM dii +))O $ ?B4?X   rc#Kt|jtr |j|jEd{y7wr)rr"r r4rs rcontentszReader.contents0s* dii +))O>>!!s6A>AN) __name__ __module__ __qualname__rrr#r&r5r8rrrrrs "   "rrcnd}tj|}t||d|}||_||_|S)Ntestingpackagezdoes-not-exist)origin is_package)types ModuleTyper__spec__ __loader__)loaderr?namemodulespecs rcreate_package_from_loaderrH6s< D   d #F dF+; SDFOF Mrc2tt||||S)N)rr"r4)rHr)rr"r?r8s rcreate_packagerJ?s %Dtx8 rcdeZdZdZej dZdZdZdZ dZ dZ dZ d Z d Zy ) CommonTestsBasez> Tests shared by test_open, test_path, and test_read. cy)zl Call the pertinent legacy API function (e.g. open_text, path) on package and path. Nr)rrr"s rexecutezCommonTestsBase.executeKsrcP|j|jjdy)z= Passing in the package name should succeed. utf-8.fileN)rNdatar9r7s rtest_package_namez!CommonTestsBase.test_package_nameRs TYY''6rc<|j|jdy)z? Passing in the package itself should succeed. rPNrNrQr7s rtest_package_objectz#CommonTestsBase.test_package_objectXs TYY -rc@d}|j|j|y)zB Passing in a string for the path should succeed. rPNrTr!s rtest_string_pathz CommonTestsBase.test_string_path^s TYY%rcftjd}|j|j|y)zS Passing in a pathlib.PurePath object for the path should succeed. rPN)pathlibPurePathrNrQr!s rtest_pathlib_pathz!CommonTestsBase.test_pathlib_pathes& - TYY%rctj|jj=|j |jjdy)z= The anchor package can already be imported. rPN)sysmodulesrQr9rNr7s r$test_importing_module_as_side_effectz4CommonTestsBase.test_importing_module_as_side_effectls2 KK ** + TYY''6rctjd}t|t}|j |d|j |j jdy)z Attempting to open or read or request the path for a non-existent path should succeed if open_resource can return a viable data stream. Hello, world!rr"rPN)ioBytesIOrJFileNotFoundErrorrN assertEqualrCr )r bytes_datars rtest_missing_pathz!CommonTestsBase.test_missing_pathssM ZZ 01  j7H7JK Wl+ ++11<@rctjd}t}t||}|j |d|j |j jdy)NrarbrP)rcrd__file__rJrNrfrCr )rrgr"rs rtest_extant_pathz CommonTestsBase.test_extant_path~sNZZ 01  jt< Wl+ ++11<@rcttt}|jt5|j|ddddy#1swYyxYw)NrbrP)rJre assertRaisesrNrs rtest_useless_loaderz#CommonTestsBase.test_useless_loaders? &7&9@Q@ST   0 1 LL, /2 1 1s AAN)r9r:r;__doc__abcabstractmethodrNrRrUrWr[r_rhrkrnrrrrLrLFsK   7 . &&7 A A0rrL) metaclassuHello, UTF-16 world! z utf-16-lezHello, UTF-8 world! utf-8) __init__.py binary.file)rwrx utf-16.filerP subdirectoryz one resource)rwz resource1.txtz two resource)rwz resource2.txt subsubdirz resource.txtz a resource)rwonetworzrx )rxryrPrz)data01data02namespacedata01ceZdZdZdZy) ModuleSetupctj|_|j|jj|jj t j|j|j|_ yr) contextlib ExitStackfixtures addCleanupclose enter_contextr isolated_modules load_fixtureMODULErQr7s rsetUpzModuleSetup.setUpsY",,.   ++, ##M$B$B$DE%%dkk2 rc`|j|t|itj|Sr) tree_on_pathr importlib import_modulerrFs rrzModuleSetup.load_fixtures+ 68F#345&&v..rN)r9r:r;rrrrrrrs 3/rrceZdZdZdZy)ZipSetuprc $|jjtj}t j |dz }|jjt jttj||y)Nzzipped modules.zip) rrr temp_dirrYPathr DirsOnSysPathstrzip_ make_zip_file)rrGrr^s rrzZipSetup.tree_on_pathse==..y/A/A/CD,,x(+?? ##  ' 'D,>,>tW,M(N O rNr9r:r;rrrrrrrs F rrceZdZdZdZy) DiskSetuprc |jjtj}t j |t j||jjtj|yr) rrr rr buildrYrr r)rrGrs rrzDiskSetup.tree_on_pathsS==..y/A/A/CD D',,x01 ##M$?$?$IJrNrrrrrrs FKrrceZdZdZdZdZejdZGdde jjZ Gdde jjZGd d eZGd d eZy ) MemorySetupz#Support loading a module in memory.rc|jj|j|tj|Sr)rraugment_sys_metapathrrrs rrzMemorySetup.load_fixtures0 ##D$=$=f$EF&&v..rc#K|j|}tjj|dtjj |ywr) MemoryFinderr] meta_pathappendremove)rrFfinder_instances rrz MemorySetup.augment_sys_metapaths=++F3 _-  _-sAAceZdZdZddZy)MemorySetup.MemoryFinderc||_yr_modulers rrz!MemorySetup.MemoryFinder.__init__ !DLrNc||jk7rytjj|tj |jdS)NT)rErDr?)rr machineryrr MemoryLoader)rfullnamer"targets r find_specz"MemorySetup.MemoryFinder.find_specsG4<<'&&11"// =2 rr)r9r:r;rrrrrrrs  " rrceZdZdZdZdZy)MemorySetup.MemoryLoaderc||_yrrrs rrz!MemorySetup.MemoryLoader.__init__rrcyrrrs r exec_modulez$MemorySetup.MemoryLoader.exec_modules rcBtj|j|Sr)rMemoryTraversableResourcesr)rrs rrz,MemorySetup.MemoryLoader.get_resource_readers99$,,Q QrN)r9r:r;rrrrrrrrs "  RrrceZdZdZdZy)&MemorySetup.MemoryTraversableResourcesc ||_||_yrr _fullnamerrFrs rrz/MemorySetup.MemoryTraversableResources.__init__!DL%DNrcVtj|j|jSr)rMemoryTraversablerrr7s rfilesz,MemorySetup.MemoryTraversableResources.filess00t~~N NrN)r9r:r;rrrrrrrs  & OrrcReZdZdZdZdZdZdefdZdefdZ d d Z e d Z y) MemorySetup.MemoryTraversablezImplement only the abstract methods of `Traversable`. Besides `.__init__()`, no other methods may be implemented or overridden. This is critical for validating the concrete `Traversable` implementations. c ||_||_yrrrs rrz&MemorySetup.MemoryTraversable.__init__rrctj|j}tjd|j t S)z Fully traverse the `fixtures` dictionary. This should be wrapped in a `try/except KeyError` but it is not currently needed and lowers the code coverage numbers. c ||Srr)dps rz8MemorySetup.MemoryTraversable._resolve..s1r)rY PurePosixPathr functoolsreducer0rr!s r_resolvez&MemorySetup.MemoryTraversable._resolves2((8D##$5tzz8L Lrc#K|j}t|tst|jd|D]3}t j |j|jd|5yw)Nz is not a directoryr))rrdictOSErrorrrrr)r directoryr"s riterdirz%MemorySetup.MemoryTraversable.iterdirsi Ii. 00CDEE!!33LLT^^$4AdV"<"sA2A4returnc>t|jtSr)rrrr7s ris_dirz$MemorySetup.MemoryTraversable.is_dirsdmmot4 4rc$|j Sr)rr7s ris_filez%MemorySetup.MemoryTraversable.is_files{{}$ $rNc|j}t|trt|jdt|t r|j d}tj|}d|vr|Stj|||S)Nz is a directoryrub)encodingerrors) rrrrrrencodercrd TextIOWrapper)rmoderr___r8results ropenz"MemorySetup.MemoryTraversable.open!sw}}H(D) 0@AA(C(#??73ZZ)Fd{ ##FXfM MrcTtj|jjSr)rYrrrEr7s rrEz"MemorySetup.MemoryTraversable.name.s((8== =r)rNN) r9r:r;rorrrboolrrrpropertyrErrrrrsI  & M  5D 5 %T % N  >  >rrN)r9r:r;rorrrcontextmanagerrrrpMetaPathFinderrLoaderrrrrrrrrrrsr- F/.. y}}33 Ry}}++RO%9O4>K4>rrc eZdZy) CommonTestsN)r9r:r;rrrrr3srr)T)NNTr)#rprrrrcrYr]r@importlib.machineryrrrrrsr r r compat.py39r r rrHrJABCMetarLrbytesrangerrrrrrrrrrrsU   *CC1""^""JF0 F0R U1X5<<[I-44W= q!- !NC!NC$~|&DE  U1X5<<[I-44W= 5r2/ # 8 / / { K Kd>+d>N )_ r