o
    ^e                     @   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mZ ddlmZ ddl m	Z	 ddl
mZ d dlmZ G d	d
 d
e	ZdddZdddZG dd de jdZG dd dZG dd deZdS )    N   )data01)	zipdata01   )ResourceReader)import_helper)
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   ND:\Projects\ConvertPro\env\Lib\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      || _ t| jtr| j| jS r
   )_path
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)   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 )CommonTestsz>
    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CommonTests.executec                 C   s   |  tjd dS )z=
        Passing in the package name should succeed.
        
utf-8.fileN)r?   r   r/   r-   r   r   r   test_package_nameR   s   zCommonTests.test_package_namec                 C   s   |  td dS )z?
        Passing in the package itself should succeed.
        r@   Nr?   r   r-   r   r   r   test_package_objectX   s   zCommonTests.test_package_objectc                 C   s   d}|  t| dS )zB
        Passing in a string for the path should succeed.
        r@   NrB   r   r   r   r   test_string_path^   s   zCommonTests.test_string_pathc                 C   s   t d}| t| 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_pathe   s   
zCommonTests.test_pathlib_pathc                 C   s   t jtj= | t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_effectl   s   
z0CommonTests.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CommonTests.test_missing_pathc                 C   s:   t d}t}t||d}| |d | |jjd d S )NrK   rL   r@   )rM   rN   __file__r=   r?   rP   r7   r   )r   rQ   r   r   r   r   r   test_extant_path~   s
   
zCommonTests.test_extant_pathc                 C   sL   t t t d}| t | |d W d    d S 1 sw   Y  d S )NrL   r@   )r=   rO   assertRaisesr?   r   r   r   r   test_useless_loader   s   "zCommonTests.test_useless_loaderN)r/   r0   r1   __doc__abcabstractmethodr?   rA   rC   rD   rG   rJ   rR   rT   rV   r   r   r   r   r>   F   s    
r>   )	metaclassc                   @   s0   e Zd ZdZedd Zedd Zdd ZdS )ZipSetupBaseNc                 C   s@   t | jj}|j}t|d | _tj	| j t
d| _d S )Nzziptestdata.zipZziptestdata)rE   Path
ZIP_MODULErS   parentstr	_zip_pathrH   r   append	importlibimport_moduledata)clsZ	data_pathdata_dirr   r   r   
setUpClass   s
   zZipSetupBase.setUpClassc                 C   sx   z	t j| j W n	 ty   Y nw zt j| j= t j| jj= W n	 t	y)   Y nw z| `| `W d S  t
y;   Y d S w r
   )rH   r   remover`   
ValueErrorpath_importer_cacherI   rd   r/   KeyErrorAttributeError)re   r   r   r   tearDownClass   s"   

zZipSetupBase.tearDownClassc                 C   s    t  }| jt jg|R   d S r
   )r   Zmodules_setupZ
addCleanupZmodules_cleanup)r   rI   r   r   r   setUp   s   zZipSetupBase.setUp)r/   r0   r1   r]   classmethodrg   rm   rn   r   r   r   r   r[      s    

r[   c                   @   s   e Zd ZeZdS )ZipSetupN)r/   r0   r1   r   r]   r   r   r   r   rp      s    rp   )T)NNTr   )rX   rb   rM   rH   r4   rE    r   r   r   Z_compatr   importlib.machineryr   r	   r<   r=   ABCMetar>   r[   rp   r   r   r   r   <module>   s"    
%
	I#