o
    3IhL&                     @   s  d dl Z d dlZd dlZd dlZd dlZd dlZd dlZd dlZd dlm	Z	 ddl m
Z
mZmZ ddlmZ ddlmZ ddlmZmZ G d	d
 d
e
Zd4ddZd5ddZG dd de jdZedeeddddddeeddddddddddd d!d"d#iid$eedddddd%eed&d'id(d)ZG d*d+ d+ZG d,d- d-eZG d.d/ d/eZ G d0d1 d1eZ!G d2d3 d3e eZ"dS )6    N)
ModuleSpec   )ResourceReaderTraversableTraversableResources   )_path)zip)import_helper	os_helperc                   @   s<   e Zd Zdd Zdd Zdd Zdd Zd	d
 Zdd ZdS )Readerc                 K   s   t | | d S N)varsupdate)selfkwargs r   ^/home/air/sanwanet/gpt-api/venv/lib/python3.10/site-packages/importlib_resources/tests/util.py__init__   s   zReader.__init__c                 C   s   | S r   r   r   packager   r   r   get_resource_reader      zReader.get_resource_readerc                 C      || _ t| jtr| j| jS r   )r   
isinstancefile	Exceptionr   pathr   r   r   open_resource      zReader.open_resourcec                 C   r   r   )r   r   r   r   )r   path_r   r   r   resource_path   r    zReader.resource_pathc                    s>    | _ t| jtr| jdd }t fddt|| jD S )Nc                 S   s
   |  dS )N/)split)entryr   r   r   part)      
z Reader.is_resource.<locals>.partc                 3   s(    | ]}t |d ko|d  kV  qdS )r   r   N)len).0partsr!   r   r   	<genexpr>,   s    
z%Reader.is_resource.<locals>.<genexpr>)r   r   r   r   anymap	_contents)r   r!   r&   r   r+   r   is_resource$   s   
zReader.is_resourcec                 c   s$    t | jtr
| j| jE d H  d S r   )r   r   r   r/   r   r   r   r   contents0   s   zReader.contentsN)	__name__
__module____qualname__r   r   r   r"   r0   r2   r   r   r   r   r      s    r   Tc                 C   s.   d}t |}t|| d|d}||_| |_|S )Ntestingpackagezdoes-not-exist)origin
is_package)types
ModuleTyper   __spec__
__loader__)loaderr8   namemodulespecr   r   r   create_package_from_loader6   s   
rA   r   c                 C   s   t t| ||d|S )N)r   r   r/   )rA   r   )r   r   r8   r2   r   r   r   create_package?   s   rB   c                   @   s^   e Zd ZdZejdd Zdd Zdd Zdd	 Z	d
d Z
dd Zdd Zdd Zdd ZdS )CommonTestsBasez>
    Tests shared by test_open, test_path, and test_read.
    c                 C   s   dS )zl
        Call the pertinent legacy API function (e.g. open_text, path)
        on package and path.
        Nr   )r   r   r   r   r   r   executeK   s    zCommonTestsBase.executec                 C   s   |  | jjd dS )z=
        Passing in the package name should succeed.
        
utf-8.fileN)rD   datar3   r1   r   r   r   test_package_nameR   s   z!CommonTestsBase.test_package_namec                 C   s   |  | jd dS )z?
        Passing in the package itself should succeed.
        rE   NrD   rF   r1   r   r   r   test_package_objectX   s   z#CommonTestsBase.test_package_objectc                 C   s   d}|  | j| dS )zB
        Passing in a string for the path should succeed.
        rE   NrH   r   r   r   r   test_string_path^   s   z CommonTestsBase.test_string_pathc                 C   s   t d}| | j| dS )zS
        Passing in a pathlib.PurePath object for the path should succeed.
        rE   N)pathlibPurePathrD   rF   r   r   r   r   test_pathlib_pathe   s   
z!CommonTestsBase.test_pathlib_pathc                 C   s    t j| jj= | | jjd dS )z=
        The anchor package can already be imported.
        rE   N)sysmodulesrF   r3   rD   r1   r   r   r   $test_importing_module_as_side_effectl   s   z4CommonTestsBase.test_importing_module_as_side_effectc                 C   s8   t d}t|t d}| |d | |jjd dS )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!r   r   rE   N)ioBytesIOrB   FileNotFoundErrorrD   assertEqualr<   r   )r   
bytes_datar   r   r   r   test_missing_paths   s   
z!CommonTestsBase.test_missing_pathc                 C   s:   t d}t}t||d}| |d | |jjd d S )NrQ   rR   rE   )rS   rT   __file__rB   rD   rV   r<   r   )r   rW   r   r   r   r   r   test_extant_path~   s
   
z CommonTestsBase.test_extant_pathc                 C   sL   t t t d}| t | |d W d    d S 1 sw   Y  d S )NrR   rE   )rB   rU   assertRaisesrD   r   r   r   r   test_useless_loader   s   "z#CommonTestsBase.test_useless_loaderN)r3   r4   r5   __doc__abcabstractmethodrD   rG   rI   rJ   rM   rP   rX   rZ   r\   r   r   r   r   rC   F   s    
rC   )	metaclass    u   ﻿Hello, UTF-16 world!
z	utf-16-lezHello, UTF-8 world!
utf-8   )__init__.pybinary.file)re   rf   utf-16.filerE   subdirectoryzone resource)re   zresource1.txtztwo resource)re   zresource2.txt	subsubdirzresource.txtz
a resource)re   onetworh   rf         )rf   rg   rE   rh   )data01data02namespacedata01c                   @      e Zd Zdd Zdd ZdS )ModuleSetupc                 C   s:   t  | _| | jj | jt  | | j	| _
d S r   )
contextlib	ExitStackfixtures
addCleanupcloseenter_contextr
   isolated_modulesload_fixtureMODULErF   r1   r   r   r   setUp   s   
zModuleSetup.setUpc                 C   s   |  |t| i t|S r   )tree_on_pathru   	importlibimport_moduler   r?   r   r   r   rz         
zModuleSetup.load_fixtureN)r3   r4   r5   r|   rz   r   r   r   r   rr      s    rr   c                   @      e Zd ZdZdd ZdS )ZipSetuprn   c              	   C   s@   | j t }t|d }| j ttt	
|| d S )Nzzipped modules.zip)ru   rx   r   temp_dirrK   Pathr
   DirsOnSysPathstrzip_make_zip_file)r   r@   r   rO   r   r   r   r}      s
   zZipSetup.tree_on_pathNr3   r4   r5   r{   r}   r   r   r   r   r          r   c                   @   r   )	DiskSetuprn   c                 C   s8   | j t }t|t| | j t	| d S r   )
ru   rx   r   r   r   buildrK   r   r
   r   )r   r@   r   r   r   r   r}      s   zDiskSetup.tree_on_pathNr   r   r   r   r   r      r   r   c                   @   sr   e Zd ZdZdZdd Zejdd ZG dd de	j
jZG d	d
 d
e	j
jZG dd deZG dd deZdS )MemorySetupz#Support loading a module in memory.rn   c                 C   s   | j | | t|S r   )ru   rx   augment_sys_metapathr~   r   r   r   r   r   rz      r   zMemorySetup.load_fixturec                 c   s.    |  |}tj| d V  tj| d S r   )MemoryFinderrN   	meta_pathappendremove)r   r?   finder_instancer   r   r   r      s
   
z MemorySetup.augment_sys_metapathc                   @   s   e Zd Zdd ZdddZdS )zMemorySetup.MemoryFinderc                 C   
   || _ d S r   _moduler   r   r   r   r      r'   z!MemorySetup.MemoryFinder.__init__Nc                 C   s(   || j krd S tjj|t| j ddS )NT)r>   r=   r8   )r   r~   	machineryr   r   MemoryLoader)r   fullnamer   targetr   r   r   	find_spec   s   

z"MemorySetup.MemoryFinder.find_specr   )r3   r4   r5   r   r   r   r   r   r   r      s    r   c                   @   s$   e Zd Zdd Zdd Zdd ZdS )zMemorySetup.MemoryLoaderc                 C   r   r   r   r   r   r   r   r      r'   z!MemorySetup.MemoryLoader.__init__c                 C   s   d S r   r   r   r   r   r   exec_module   r   z$MemorySetup.MemoryLoader.exec_modulec                 C   s   t | j|S r   )r   MemoryTraversableResourcesr   )r   r   r   r   r   r         z,MemorySetup.MemoryLoader.get_resource_readerN)r3   r4   r5   r   r   r   r   r   r   r   r      s    r   c                   @   rq   )z&MemorySetup.MemoryTraversableResourcesc                 C      || _ || _d S r   r   	_fullnamer   r?   r   r   r   r   r         
z/MemorySetup.MemoryTraversableResources.__init__c                 C   s   t | j| jS r   )r   MemoryTraversabler   r   r1   r   r   r   files   s   z,MemorySetup.MemoryTraversableResources.filesN)r3   r4   r5   r   r   r   r   r   r   r      s    r   c                   @   sZ   e Zd ZdZdd Zdd Zdd Zdefd	d
ZdefddZ	dddZ
edd ZdS )z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                 C   r   r   r   r   r   r   r   r     r   z&MemorySetup.MemoryTraversable.__init__c                 C   s    t | j}tdd |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                 S   s   | | S r   r   )dpr   r   r   <lambda>  s    z8MemorySetup.MemoryTraversable._resolve.<locals>.<lambda>)rK   PurePosixPathr   	functoolsreducer*   ru   r   r   r   r   _resolve  s   z&MemorySetup.MemoryTraversable._resolvec                 c   sN    |   }t|tst| j d|D ]}t| j| j d| V  qd S )Nz is not a directoryr#   )r   r   dictOSErrorr   r   r   r   )r   	directoryr   r   r   r   iterdir  s   
z%MemorySetup.MemoryTraversable.iterdirreturnc                 C   s   t |  tS r   )r   r   r   r1   r   r   r   is_dir  r   z$MemorySetup.MemoryTraversable.is_dirc                 C   s
   |    S r   )r   r1   r   r   r   is_file  r'   z%MemorySetup.MemoryTraversable.is_filerNc                 O   s\   |   }t|trt| j dt|tr|d}t|}d|v r&|S tj	|||dS )Nz is a directoryrc   b)encodingerrors)
r   r   r   r   r   r   encoderS   rT   TextIOWrapper)r   moder   r   ___r2   resultr   r   r   open!  s   



z"MemorySetup.MemoryTraversable.openc                 C   s   t | jjS r   )rK   r   r   r>   r1   r   r   r   r>   .  s   z"MemorySetup.MemoryTraversable.name)r   NN)r3   r4   r5   r]   r   r   r   boolr   r   r   propertyr>   r   r   r   r   r      s    


r   N)r3   r4   r5   r]   r{   rz   rs   contextmanagerr   r~   r^   MetaPathFinderr   Loaderr   r   r   r   r   r   r   r   r   r      s    

r   c                   @   s   e Zd ZdS )CommonTestsN)r3   r4   r5   r   r   r   r   r   3  s    r   )T)NNTr   )#r^   rs   r   r~   rS   rK   rN   r9   importlib.machineryr   r   r   r   ra   r   r	   r   compat.py39r
   r   r   rA   rB   ABCMetarC   r   bytesranger   ru   rr   r   r   r   r   r   r   r   r   <module>   sV    
%
	I


	g