U
    aÊ¹jP%  ã                   @  sZ  d dl m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	Z	d dl
Zd dlZd dlmZ ejdk r€d dlmZ nd dlZejdddœd	d
„ƒZejd&dddœdd„ƒZdddœdd„Zdd„ ZeeeƒZejdd„ ƒZdd„ Zejejfdd„ƒZejddefdd„ƒZdd„ ZG d d!„ d!ƒZG d"d#„ d#ejej ƒZG d$d%„ d%ej ƒZ!dS )'é    )ÚannotationsN)ÚIterator)é   é   )Útarfilezstr | os.PathLikezIterator[str | os.PathLike])ÚdirÚreturnc              	   c  s.   t  ¡ }t  | ¡ z
| V  W 5 t  |¡ X dS )zµ
    >>> tmp_path = getfixture('tmp_path')
    >>> with pushd(tmp_path):
    ...     assert os.getcwd() == os.fspath(tmp_path)
    >>> assert os.getcwd() != os.fspath(tmp_path)
    N)ÚosÚgetcwdÚchdir)r   Úorig© r   ú`/var/www/html/bwave/python/venv/lib/python3.8/site-packages/setuptools/_vendor/jaraco/context.pyÚpushd   s
    	

r   zstr | os.PathLike | None)Ú
target_dirr   c              	   c  s~   |dkr$t j | ¡ dd¡ dd¡}t  |¡ z>tj 	| ¡}t
j|dd��}|j|td� W 5 Q R X |V  W 5 t |¡ X dS )až  
    Get a tarball, extract it, yield, then clean up.

    >>> import urllib.request
    >>> url = getfixture('tarfile_served')
    >>> target = getfixture('tmp_path') / 'out'
    >>> tb = tarball(url, target_dir=target)
    >>> import pathlib
    >>> with tb as extracted:
    ...     contents = pathlib.Path(extracted, 'contents.txt').read_text(encoding='utf-8')
    >>> assert not os.path.exists(extracted)
    Nz.tar.gzÚ z.tgzzr|*)ÚfileobjÚmode)ÚpathÚfilter)r	   r   ÚbasenameÚreplaceÚmkdirÚshutilÚrmtreeÚurllibÚrequestÚurlopenr   ÚopenÚ
extractallÚstrip_first_component)Úurlr   ÚreqÚtfr   r   r   Útarball'   s    

r$   ztarfile.TarInfo)Úmemberr   c                 C  s   | j  dd¡\}| _ | S )Nú/é   )ÚnameÚsplit)r%   r   Ú_r   r   r   r    G   s    r    c                  G  s   dd„ }t  |t| ƒ¡S )a…  
    Compose any number of dependent context managers into a single one.

    The last, innermost context manager may take arbitrary arguments, but
    each successive context manager should accept the result from the
    previous as a single parameter.

    Like :func:`jaraco.functools.compose`, behavior works from right to
    left, so the context manager should be indicated from outermost to
    innermost.

    Example, to create a context manager to change to a temporary
    directory:

    >>> temp_dir_as_cwd = _compose(pushd, temp_dir)
    >>> with temp_dir_as_cwd() as dir:
    ...     assert os.path.samefile(os.getcwd(), dir)
    c                   s   ‡ ‡fdd„}t  |¡S )Nc               
   ?  s4   ˆ | |Ž� }ˆ|ƒ�}|V  W 5 Q R X W 5 Q R X d S ©Nr   )ÚargsÚkwargsZsavedÚres©ÚinnerÚouterr   r   Úcomposedd   s    z/_compose.<locals>.compose_two.<locals>.composed)Ú
contextlibÚcontextmanager)r0   r1   r2   r   r/   r   Úcompose_twoc   s    z_compose.<locals>.compose_two)Ú	functoolsÚreduceÚreversed)Zcmgrsr5   r   r   r   Ú_composeO   s    r9   c               
   o  sP   t jdtdd� | dt¡}t| |Ž� }||ƒ�}|V  W 5 Q R X W 5 Q R X d S )NzBtarball_context is deprecated. Use tarball or tarball_cwd instead.é   ©Ú
stacklevelr   )ÚwarningsÚwarnÚDeprecationWarningÚpopr   r$   )r,   r-   Z	pushd_ctxZtballr   r   r   r   Útarball_contextp   s    ýrA   c                 C  s6   t jdtdd� | dd… }tdddd	�}| |d¡S )
a  
    Given a URL or filename, infer the compression code for tar.

    >>> infer_compression('http://foo/bar.tar.gz')
    'z'
    >>> infer_compression('http://foo/bar.tgz')
    'z'
    >>> infer_compression('file.bz')
    'j'
    >>> infer_compression('file.xz')
    'J'
    z3infer_compression is deprecated with no replacementr:   r;   éþÿÿÿNÚzÚjÚJ)ÚgzZbzÚxz)r=   r>   r?   ÚdictÚget)r!   Zcompression_indicatorÚmappingr   r   r   Úinfer_compression|   s    ýrK   c                 c  s"   t  ¡ }z
|V  W 5 | |ƒ X dS )a`  
    Create a temporary directory context. Pass a custom remover
    to override the removal behavior.

    >>> import pathlib
    >>> with temp_dir() as the_dir:
    ...     assert os.path.isdir(the_dir)
    ...     _ = pathlib.Path(the_dir).joinpath('somefile').write_text('contents', encoding='utf-8')
    >>> assert not os.path.exists(the_dir)
    N)ÚtempfileÚmkdtemp)ZremoverÚtemp_dirr   r   r   rN   •   s    
rN   Tc           	   	   c  sr   d| krdnd}|ƒ �R}|d| |g}|r6|  d|g¡ ttjjdƒ}|rL|nd}tj||d� |V  W 5 Q R X dS )z 
    Check out the repo indicated by url.

    If dest_ctx is supplied, it should be a context manager
    to yield the target directory for the check out.
    ÚgitÚhgÚclonez--branchÚwN)Ústdout)Úextendr   r	   r   ÚdevnullÚ
subprocessÚ
check_call)	r!   ÚbranchÚquietZdest_ctxÚexeÚrepo_dirÚcmdrU   rS   r   r   r   Úrepo_context¨   s    r]   c                   C  s   t jdtdd� t ¡ S )aD  
    A null context suitable to stand in for a meaningful context.

    >>> with null() as value:
    ...     assert value is None

    This context is most useful when dealing with two or more code
    branches but only some need a context. Wrap the others in a null
    context to provide symmetry across all options.
    z.null is deprecated. Use contextlib.nullcontextr:   r;   )r=   r>   r?   r3   Únullcontextr   r   r   r   Únull»   s    ýr_   c                   @  st   e Zd ZdZdZeffdd„Zdd„ Zedd„ ƒZ	ed	d
„ ƒZ
edd„ ƒZdd„ Zdd„ Zedœdd„Zdd„ ZdS )ÚExceptionTrapa©  
    A context manager that will catch certain exceptions and provide an
    indication they occurred.

    >>> with ExceptionTrap() as trap:
    ...     raise Exception()
    >>> bool(trap)
    True

    >>> with ExceptionTrap() as trap:
    ...     pass
    >>> bool(trap)
    False

    >>> with ExceptionTrap(ValueError) as trap:
    ...     raise ValueError("1 + 1 is not 3")
    >>> bool(trap)
    True
    >>> trap.value
    ValueError('1 + 1 is not 3')
    >>> trap.tb
    <traceback object at ...>

    >>> with ExceptionTrap(ValueError) as trap:
    ...     raise Exception()
    Traceback (most recent call last):
    ...
    Exception

    >>> bool(trap)
    False
    )NNNc                 C  s
   || _ d S r+   )Ú
exceptions)Úselfra   r   r   r   Ú__init__ò   s    zExceptionTrap.__init__c                 C  s   | S r+   r   ©rb   r   r   r   Ú	__enter__õ   s    zExceptionTrap.__enter__c                 C  s
   | j d S ©Nr   ©Úexc_inford   r   r   r   Útypeø   s    zExceptionTrap.typec                 C  s
   | j d S )Nr'   rg   rd   r   r   r   Úvalueü   s    zExceptionTrap.valuec                 C  s
   | j d S )Nr:   rg   rd   r   r   r   Útb   s    zExceptionTrap.tbc                 G  s&   |d }|ot || jƒ}|r"|| _|S rf   )Ú
issubclassra   rh   )rb   rh   ri   Úmatchesr   r   r   Ú__exit__  s
    zExceptionTrap.__exit__c                 C  s
   t | jƒS r+   )Úboolri   rd   r   r   r   Ú__bool__  s    zExceptionTrap.__bool__©Ú_testc                  s   t  ˆ¡‡ ‡‡fdd„ƒ}|S )a±  
        Wrap func and replace the result with the truth
        value of the trap (True if an exception occurred).

        First, give the decorator an alias to support Python 3.8
        Syntax.

        >>> raises = ExceptionTrap(ValueError).raises

        Now decorate a function that always fails.

        >>> @raises
        ... def fail():
        ...     raise ValueError('failed')
        >>> fail()
        True
        c               	     s(   t ˆjƒ�}ˆ| |Ž W 5 Q R X ˆ |ƒS r+   )r`   ra   )r,   r-   Ztrap©rr   Úfuncrb   r   r   Úwrapper!  s    z%ExceptionTrap.raises.<locals>.wrapper)r6   Úwraps)rb   rt   rr   ru   r   rs   r   Úraises  s    zExceptionTrap.raisesc                 C  s   | j |tjd�S )aª  
        Wrap func and replace the result with the truth
        value of the trap (True if no exception).

        First, give the decorator an alias to support Python 3.8
        Syntax.

        >>> passes = ExceptionTrap(ValueError).passes

        Now decorate a function that always fails.

        >>> @passes
        ... def fail():
        ...     raise ValueError('failed')

        >>> fail()
        False
        rq   )rw   ÚoperatorÚnot_)rb   rt   r   r   r   Úpasses)  s    zExceptionTrap.passesN)Ú__name__Ú
__module__Ú__qualname__Ú__doc__rh   Ú	Exceptionrc   re   Úpropertyri   rj   rk   rn   rp   ro   rw   rz   r   r   r   r   r`   Î   s   !


r`   c                   @  s   e Zd ZdZdS )ÚsuppresszŸ
    A version of contextlib.suppress with decorator support.

    >>> @suppress(KeyError)
    ... def key_error():
    ...     {}['']
    >>> key_error()
    N)r{   r|   r}   r~   r   r   r   r   r�   ?  s   r�   c                   @  s*   e Zd ZdZddd„Zdd„ Zdd	„ Zd
S )Úon_interrupta  
    Replace a KeyboardInterrupt with SystemExit(1)

    >>> def do_interrupt():
    ...     raise KeyboardInterrupt()
    >>> on_interrupt('error')(do_interrupt)()
    Traceback (most recent call last):
    ...
    SystemExit: 1
    >>> on_interrupt('error', code=255)(do_interrupt)()
    Traceback (most recent call last):
    ...
    SystemExit: 255
    >>> on_interrupt('suppress')(do_interrupt)()
    >>> with __import__('pytest').raises(KeyboardInterrupt):
    ...     on_interrupt('ignore')(do_interrupt)()
    Úerrorr'   c                C  s   || _ || _d S r+   )ÚactionÚcode)rb   r„   r…   r   r   r   rc   ]  s    zon_interrupt.__init__c                 C  s   | S r+   r   rd   r   r   r   re   a  s    zon_interrupt.__enter__c                 C  s6   |t k	s| jdkrd S | jdkr,t| jƒ|‚| jdkS )NÚignorerƒ   r�   )ÚKeyboardInterruptr„   Ú
SystemExitr…   )rb   ÚexctypeÚexcinstÚexctbr   r   r   rn   d  s
    
zon_interrupt.__exit__N)rƒ   r'   )r{   r|   r}   r~   rc   re   rn   r   r   r   r   r‚   J  s   
r‚   )N)"Ú
__future__r   r3   r6   rx   r	   r   rV   ÚsysrL   Úurllib.requestr   r=   Útypingr   Úversion_infoZ	backportsr   r4   r   r$   r    r9   Ztarball_cwdrA   rK   r   rN   r]   r_   r`   r�   ÚContextDecoratorr‚   r   r   r   r   Ú<module>   sB   
 ÿ

q