U
    i2e                     @   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	 ddl
mZ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   )ResourceReader)import_helper	os_helper)zip)
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   B/tmp/pip-unpacked-wheel-bn52x7jx/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   )_path
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__)loaderr2   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   r2   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   executeL   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_nameS   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_objectY   s    zCommonTests.test_package_objectc                 C   s   d}|  t| dS )zB
        Passing in a string for the path should succeed.
        r?   NrA   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_pathf   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_effectm   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>   assertEqualr6   r   )r   
bytes_datar   r   r   r   test_missing_patht   s    
zCommonTests.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   r6   r   )r   rP   r   r   r   r   r   test_extant_path   s
    
zCommonTests.test_extant_pathc              	   C   s6   t t t d}| t | |d W 5 Q R X d S )NrK   r?   )r<   rN   assertRaisesr>   r   r   r   r   test_useless_loader   s    zCommonTests.test_useless_loaderN)r.   r/   r0   __doc__abcabstractmethodr>   r@   rB   rC   rF   rI   rQ   rS   rU   r   r   r   r   r=   G   s   
r=   )	metaclassc                   @   s   e Zd ZdZdd ZdS )ZipSetupBaser   c              	   C   s   t  | _| | jj t }| jtjf|  | jt	
 }t|d }ttj| j}| jttt|| t| j| _d S )Nzzipped modules.zip)
contextlib	ExitStackZfixturesZ
addCleanupcloser   Zmodules_setupZmodules_cleanupenter_contextr   temp_dirrD   PathrR   parentjoinpath
ZIP_MODULEZDirsOnSysPathstrzip_Zmake_zip_file	importlibimport_moduledata)r   rH   r_   Zsrc_pathr   r   r   setUp   s    
zZipSetupBase.setUpN)r.   r/   r0   rc   ri   r   r   r   r   rZ      s   rZ   c                   @   s   e Zd ZdS )ZipSetupN)r.   r/   r0   r   r   r   r   rj      s   rj   )T)NNTr   )rW   rf   rL   rG   r3   rD   r[    r   r   _compatr   r   r   re   Zimportlib.machineryr	   r
   r;   r<   ABCMetar=   rZ   rj   r   r   r   r   <module>   s"   %
	
I