a
    ijL&                     @   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/ghrups/robot-bench/.venv/lib/python3.9/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   s    zReader.get_resource_readerc                 C   s   || _ t| jtr| j| jS r   )r   
isinstancefile	Exceptionr   pathr   r   r   open_resource   s    zReader.open_resourcec                 C   s   || _ t| jtr| j| jS r   )r   r   r   r   )r   path_r   r   r   resource_path   s    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)   s    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   r,   r.   r   r   r   r   r      s   r   Tc                 C   s.   d}t |}t|| d|d}||_| |_|S )NZtestingpackagezdoes-not-exist)origin
is_package)types
ModuleTyper   __spec__
__loader__)loaderr3   namemodulespecr   r   r   create_package_from_loader6   s    
r<   r   c                 C   s   t t| ||d|S )N)r   r   r+   )r<   r   )r   r   r3   r.   r   r   r   create_package?   s    r=   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)r?   datar/   r-   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.
        r@   Nr?   rA   r-   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.
        r@   NrC   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.
        r@   N)pathlibPurePathr?   rA   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.
        r@   N)sysmodulesrA   r/   r?   r-   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   r@   N)ioBytesIOr=   FileNotFoundErrorr?   assertEqualr7   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 )NrL   rM   r@   )rN   rO   __file__r=   r?   rQ   r7   r   )r   rR   r   r   r   r   r   test_extant_path~   s
    
z CommonTestsBase.test_extant_pathc                 C   sJ   t t t d}| t | |d W d    n1 s<0    Y  d S )NrM   r@   )r=   rP   ZassertRaisesr?   r   r   r   r   test_useless_loader   s    z#CommonTestsBase.test_useless_loaderN)r/   r0   r1   __doc__abcabstractmethodr?   rB   rD   rE   rH   rK   rS   rU   rV   r   r   r   r   r>   F   s   
r>   )	metaclass    u   ﻿Hello, UTF-16 world!
z	utf-16-lezHello, UTF-8 world!
utf-8   )__init__.pybinary.file)r_   r`   utf-16.filer@   subdirectoryzone resource)r_   zresource1.txtztwo resource)r_   zresource2.txtZ	subsubdirzresource.txtz
a resource)r_   onetworb   r`         )r`   ra   r@   rb   )data01Zdata02Znamespacedata01c                   @   s   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fixturesZ
addCleanupcloseenter_contextr
   Zisolated_modulesload_fixtureMODULErA   r-   r   r   r   setUp   s    
zModuleSetup.setUpc                 C   s   |  |t| i t|S r   )tree_on_pathrk   	importlibimport_moduler   r:   r   r   r   rn      s    zModuleSetup.load_fixtureN)r/   r0   r1   rp   rn   r   r   r   r   rh      s   rh   c                   @   s   e Zd ZdZdd ZdS )ZipSetuprg   c              	   C   s@   | j t }t|d }| j ttt	
|| d S )Nzzipped modules.zip)rk   rm   r   temp_dirrF   Pathr
   DirsOnSysPathstrzip_Zmake_zip_file)r   r;   rv   rJ   r   r   r   rq      s
    zZipSetup.tree_on_pathNr/   r0   r1   ro   rq   r   r   r   r   ru      s   ru   c                   @   s   e Zd ZdZdd ZdS )	DiskSetuprg   c                 C   s8   | j t }t|t| | j t	| d S r   )
rk   rm   r   rv   r   buildrF   rw   r
   rx   )r   r;   rv   r   r   r   rq      s    zDiskSetup.tree_on_pathNr{   r   r   r   r   r|      s   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.rg   c                 C   s   | j | | t|S r   )rk   rm   augment_sys_metapathrr   rs   rt   r   r   r   rn      s    zMemorySetup.load_fixturec                 c   s,   |  |}tj| d V  tj| d S r   )MemoryFinderrI   	meta_pathappendremove)r   r:   Z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   s
   || _ d S r   _modulert   r   r   r   r      s    z!MemorySetup.MemoryFinder.__init__Nc                 C   s(   || j krd S tjj|t| j ddS )NT)r9   r8   r3   )r   rr   	machineryr   r~   MemoryLoader)r   fullnamer   targetr   r   r   	find_spec   s    

z"MemorySetup.MemoryFinder.find_spec)N)r/   r0   r1   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   s
   || _ d S r   r   rt   r   r   r   r      s    z!MemorySetup.MemoryLoader.__init__c                 C   s   d S r   r   rt   r   r   r   exec_module   s    z$MemorySetup.MemoryLoader.exec_modulec                 C   s   t | j|S r   )r~   MemoryTraversableResourcesr   )r   r   r   r   r   r      s    z,MemorySetup.MemoryLoader.get_resource_readerN)r/   r0   r1   r   r   r   r   r   r   r   r      s   r   c                   @   s   e Zd Zdd Zdd ZdS )z&MemorySetup.MemoryTraversableResourcesc                 C   s   || _ || _d S r   r   	_fullnamer   r:   r   r   r   r   r      s    z/MemorySetup.MemoryTraversableResources.__init__c                 C   s   t | j| jS r   )r~   MemoryTraversabler   r   r-   r   r   r   files   s    z,MemorySetup.MemoryTraversableResources.filesN)r/   r0   r1   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edd	d
Zed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   s   || _ || _d S r   r   r   r   r   r   r     s    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>      z8MemorySetup.MemoryTraversable._resolve.<locals>.<lambda>)rF   PurePosixPathr   	functoolsreducer&   rk   r   r   r   r   _resolve  s    z&MemorySetup.MemoryTraversable._resolvec                 c   sL   |   }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   r-   r   r   r   is_dir  s    z$MemorySetup.MemoryTraversable.is_dirc                 C   s
   |    S r   )r   r-   r   r   r   is_file  s    z%MemorySetup.MemoryTraversable.is_filerNc                 O   s\   |   }t|tr"t| j dt|tr6|d}t|}d|v rL|S tj	|||dS )Nz is a directoryr]   b)encodingerrors)
r   r   r   r   r   ry   encoderN   rO   TextIOWrapper)r   moder   r   ___r.   resultr   r   r   open!  s    



z"MemorySetup.MemoryTraversable.openc                 C   s   t | jjS r   )rF   r   r   r9   r-   r   r   r   r9   .  s    z"MemorySetup.MemoryTraversable.name)r   NN)r/   r0   r1   rW   r   r   r   boolr   r   r   propertyr9   r   r   r   r   r      s   


r   N)r/   r0   r1   rW   ro   rn   ri   contextmanagerr   rr   rX   MetaPathFinderr   Loaderr   r   r   r   r   r   r   r   r   r~      s   

r~   c                   @   s   e Zd ZdS )CommonTestsN)r/   r0   r1   r   r   r   r   r   3  s   r   )T)NNTr   )#rX   ri   r   rr   rN   rF   rI   r4   importlib.machineryr   r   r   r   r[   r   r	   rz   Zcompat.py39r
   r   r   r<   r=   ABCMetar>   r   bytesranger   rk   rh   ru   r|   r~   r   r   r   r   r   <module>   sT   %
	
I


	g