
    .Ph$                    $   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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
            Zej        	 d#d$d            Zd%dZd Z eee          Z	 d Zd Zej        ej        fd            Z ej        e e                      Zej        ddefd&d            Z  G d d          Z! G d dej"        ej#                  Z" G d  d!ej#                  Z$dS )'    )annotationsN)Iterator)      )tarfiledirstr | os.PathLikereturnIterator[str | os.PathLike]c              #     K   t          j                    }t          j        |            	 | V  t          j        |           dS # t          j        |           w xY w)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   origs     W/var/www/html/test/jupyter/venv/lib/python3.11/site-packages/jaraco/context/__init__.pypushdr      sQ       9;;DHSMMM			
s   A A
target_dirstr | os.PathLike | Nonec              #    K   |Gt           j                            |                               dd                              dd          }t          j        |           	 t
          j                            |           }t          j	        |d          5 }|
                    |t                     ddd           n# 1 swxY w Y   |V  t          j        |           dS # t          j        |           w xY w)au  
    Get a URL to a tarball, download, extract, yield, then clean up.

    Assumes everything in the tarball is prefixed with a common
    directory. That common path is stripped and the contents
    are extracted to ``target_dir``, similar to passing
    ``-C {target} --strip-components 1`` to the ``tar`` command.

    Uses the streaming protocol to extract the contents from a
    stream in a single pass without loading the whole file into
    memory.

    >>> 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)

    If the target is not specified, contents are extracted to a
    directory relative to the current working directory named after
    the name of the file as extracted from the URL.

    >>> target = getfixture('tmp_path')
    >>> with pushd(target), tarball(url):
    ...     target.joinpath('served').is_dir()
    True
    Nz.tar.gz z.tgzzr|*)fileobjmode)pathfilter)r   r   basenamereplacemkdirurllibrequesturlopenr   open
extractallstrip_first_componentshutilrmtree)urlr   reqtfs       r   tarballr)   )   s5     D W%%c**229bAAII&RTUU
HZ"n$$S))\#E222 	IbMMz2GMHHH	I 	I 	I 	I 	I 	I 	I 	I 	I 	I 	I 	I 	I 	I 	Ij!!!!!j!!!!s0   !5C$ B?3C$ ?CC$ CC$ $C:membertarfile.TarInfoc                L    | j                             dd          \  }| _         | S )N/   )namesplit)r*   r   _s      r   r#   r#   W   s&     [&&sA..NAv{M    c                 L    d }t          j        |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                :      fd}t          j        |          S )Nc               ?     K    | i |5 } |          5 }|V  d d d            n# 1 swxY w Y   d d d            d S # 1 swxY w Y   d S N )argskwargssavedresinnerouters       r   composedz/_compose.<locals>.compose_two.<locals>.composedt   s      ''' 5%%,, #			                                s,   A)A-	A-	AAA)
contextlibcontextmanager)r<   r=   r>   s   `` r   compose_twoz_compose.<locals>.compose_twos   s5    	 	 	 	 	 	 (222r2   )	functoolsreducereversed)cmgrsrA   s     r   _composerF   _   s,    (3 3 3 K%999r2   c                   |\  }}}| t           j        t           j        t           j        fv r[|j        t          j        k    rFt          j        |t          j        t          j	        z  t          j
        z              | |           dS  )z>
    Add support for removing read-only files on Windows.
    N)r   rmdirremoveunlinkerrnoEACCESchmodstatS_IRWXUS_IRWXGS_IRWXO)funcr   exc_infor1   excs        r   remove_readonlyrU      sk     IAsA")RY///CI4M4M
t|dl2T\ABBBT




r2   c                     t          j                    dk    r%t          j        t          j        t                    nt          j        S )NWindows)onerror)platformsystemrB   partialr$   r%   rU   r7   r2   r   robust_removerr\      s;     ?	)) 	&-AAAA]r2   c              #  p   K   t          j                    }	 |V   | |           dS #  | |           w xY w)z
    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)
    >>> assert not os.path.exists(the_dir)
    N)tempfilemkdtemp)removertemp_dirs     r   ra   ra      sP       !!Hs   ( 5)r`   Tbranch
str | Nonequietboolc              #    K   d| v rdnd} |            5 }|d| |g}|                     d|gt          |          z             |rt          j        nd}t          j        |||           |V  ddd           dS # 1 swxY w Y   dS )aT  
    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.

    >>> repo = repo_context('https://github.com/jaraco/jaraco.context')
    >>> with repo as dest:
    ...     listing = os.listdir(dest)
    >>> 'README.rst' in listing
    True
    githgclonez--branchN)stdoutstderr)extendre   
subprocessDEVNULL
check_call)r&   rb   rd   dest_ctxexerepo_dircmdstreams           r   repo_contextru      s        C<<%%TC	 xGS(+

J'$v,,6777',6##$c&@@@@                 s   AA;;A?A?c                      e Zd ZdZdZeffdZd Zed             Z	ed             Z
ed             Zd Zd	 Zed
dZ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                    || _         d S r6   )
exceptions)selfry   s     r   __init__zExceptionTrap.__init__   s    $r2   c                    | S r6   r7   rz   s    r   	__enter__zExceptionTrap.__enter__       r2   c                    | j         d         S Nr   rS   r}   s    r   typezExceptionTrap.type       }Qr2   c                    | j         d         S )Nr.   r   r}   s    r   valuezExceptionTrap.value   r   r2   c                    | j         d         S )N   r   r}   s    r   tbzExceptionTrap.tb   r   r2   c                V    |d         }|ot          || j                  }|r|| _        |S r   )
issubclassry   rS   )rz   rS   r   matchess       r   __exit__zExceptionTrap.__exit__   s5    {<:dDO<< 	%$DMr2   c                *    t          | j                  S r6   )re   r   r}   s    r   __bool__zExceptionTrap.__bool__  s    DIr2   _testc               N     t          j                   f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                     t          j                  5 } | i | d d d            n# 1 swxY w Y    |          S r6   )rw   ry   )r8   r9   trapr   rR   rz   s      r   wrapperz%ExceptionTrap.raises.<locals>.wrapper  s    t// &4d%f%%%& & & & & & & & & & & & & & &5;;s   	+//)rB   wraps)rz   rR   r   r   s   ``` r   raiseszExceptionTrap.raises  sF    & 
			 	 	 	 	 	 
		
 r2   c                D    |                      |t          j                  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
        r   )r   operatornot_)rz   rR   s     r   passeszExceptionTrap.passes"  s    & {{4x}{555r2   N)__name__
__module____qualname____doc__rS   	Exceptionr{   r~   propertyr   r   r   r   r   re   r   r   r7   r2   r   rw   rw      s         B  H#,, % % % %       X      X      X      %)     66 6 6 6 6r2   rw   c                      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   r7   r2   r   r   r   8  s           r2   r   c                  &    e Zd ZdZddZd Zd ZdS )	on_interruptaF  
    Replace a KeyboardInterrupt with SystemExit(1).

    Useful in conjunction with console entry point functions.

    >>> 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               "    || _         || _        d S r6   )actioncode)rz   r   r   s      r   r{   zon_interrupt.__init__X  s    			r2   c                    | S r6   r7   r}   s    r   r~   zon_interrupt.__enter__\  r   r2   c                    |t           us| j        dk    rd S | j        dk    rt          | j                  || j        dk    S )Nignorer   r   )KeyboardInterruptr   
SystemExitr   )rz   exctypeexcinstexctbs       r   r   zon_interrupt.__exit___  sK    +++t{h/F/FF[G##TY''W4{j((r2   N)r   r.   )r   r   r   r   r{   r~   r   r7   r2   r   r   r   C  sP         (     ) ) ) ) )r2   r   )r   r	   r
   r   r6   )r   r   r
   r   )r*   r+   r
   r+   )rb   rc   rd   re   )%
__future__r   r?   rK   rB   r   r   rY   r$   rN   rm   sysr^   urllib.requestr   typingr   version_info	backportsr   r@   r   r)   r#   rF   tarball_cwdrU   r\   r%   ra   r[   robust_temp_dirru   rw   r   ContextDecoratorr   r7   r2   r   <module>r      s   " " " " " "           				        



            g!!!!!!!NNN      04*" *" *" *" *"Z   : : :< hug&&
     ]    " $)#Hnn6F6FGGG "$    0n6 n6 n6 n6 n6 n6 n6 n6b    z"J$?   !) !) !) !) !):. !) !) !) !) !)r2   