U
    ?h                     @   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 mZ ddlm	Z	m
Z
 ddlmZ ddlmZ d dlmZ G d	d
 d
eZd2ddZd3d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eZdS )4    N   )ResourceReader   )import_helper	os_helper)zip)_path)
ModuleSpecc                   @   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   P/var/www/html/venv/lib/python3.8/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   contents1   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__)loaderr1   namemodulespecr   r   r   create_package_from_loader7   s    
r:   r   c                 C   s   t t| ||d|S )N)r   r   r)   )r:   r
   )r   r   r1   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   executeL   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_nameS   s    z!CommonTestsBase.test_package_namec                 C   s   |  | jd dS )z?
        Passing in the package itself should succeed.
        r>   Nr=   r?   r+   r   r   r   test_package_objectY   s    z#CommonTestsBase.test_package_objectc                 C   s   d}|  | j| dS )zB
        Passing in a string for the path should succeed.
        r>   NrA   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=   r?   r   r   r   r   test_pathlib_pathf   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modulesr?   r-   r=   r+   r   r   r   $test_importing_module_as_side_effectm   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=   assertEqualr5   r   )r   
bytes_datar   r   r   r   test_missing_patht   s    
z!CommonTestsBase.test_missing_pathc                 C   s:   t d}t}t||d}| |d | |jjd d S )NrJ   rK   r>   )rL   rM   __file__r;   r=   rO   r5   r   )r   rP   r   r   r   r   r   test_extant_path   s
    
z CommonTestsBase.test_extant_pathc              	   C   s6   t t t d}| t | |d W 5 Q R X d S )NrK   r>   )r;   rN   ZassertRaisesr=   r   r   r   r   test_useless_loader   s    z#CommonTestsBase.test_useless_loaderN)r-   r.   r/   __doc__abcabstractmethodr=   r@   rB   rC   rF   rI   rQ   rS   rT   r   r   r   r   r<   G   s   
r<   )	metaclass    zHello, UTF-16 world!
zutf-16zHello, UTF-8 world!
z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twor_   r]         )r]   r^   r>   r_   )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MODULEr?   r+   r   r   r   setUp   s    
zModuleSetup.setUpc                 C   s   |  |t| i t|S r   )tree_on_pathrh   	importlibimport_module)r   r8   r   r   r   rk      s    zModuleSetup.load_fixtureN)r-   r.   r/   rm   rk   r   r   r   r   re      s   re   c                   @   s   e Zd ZdZdd ZdS )ZipSetuprd   c              	   C   s@   | j t }t|d }| j ttt	
|| d S )Nzzipped modules.zip)rh   rj   r   temp_dirrD   Pathr   DirsOnSysPathstrzip_Zmake_zip_file)r   r9   rr   rH   r   r   r   rn      s
    zZipSetup.tree_on_pathNr-   r.   r/   rl   rn   r   r   r   r   rq      s   rq   c                   @   s   e Zd ZdZdd ZdS )	DiskSetuprd   c                 C   s8   | j t }t|t| | j t	| d S r   )
rh   rj   r   rr   r   buildrD   rs   r   rt   )r   r9   rr   r   r   r   rn      s    zDiskSetup.tree_on_pathNrw   r   r   r   r   rx      s   rx   c                   @   s   e Zd ZdS )CommonTestsN)r-   r.   r/   r   r   r   r   rz      s   rz   )T)NNTr   )rV   ro   rL   rG   r2   rD   rf   r   Zcompat.py39r   r   rY   r   rv   r   Zimportlib.machineryr	   r
   r:   r;   ABCMetar<   dictbytesrangeencoderh   re   rq   rx   rz   r   r   r   r   <module>   sR   %
	
I


 	