o
    Th                     @   sb   d dl Z d dlZd dlmZmZ ddlmZ G dd dejZG dd deZ	G d	d
 d
e	Z
dS )    N)	GeneratorIterable   )ResponseNotAcceptedc                   @   s&   e Zd ZdZdedee fddZdS )StreamWatchera}  
    A class whose subclasses may act on seen stream data from subprocesses.

    Subclasses must exhibit the following API; see `Responder` for a concrete
    example.

    * ``__init__`` is completely up to each subclass, though as usual,
      subclasses *of* subclasses should be careful to make use of `super` where
      appropriate.
    * `submit` must accept the entire current contents of the stream being
      watched, as a string, and may optionally return an iterable of strings
      (or act as a generator iterator, i.e. multiple calls to ``yield
      <string>``), which will each be written to the subprocess' standard
      input.

    .. note::
        `StreamWatcher` subclasses exist in part to enable state tracking, such
        as detecting when a submitted password didn't work & erroring (or
        prompting a user, or etc). Such bookkeeping isn't easily achievable
        with simple callback functions.

    .. note::
        `StreamWatcher` subclasses `threading.local` so that its instances can
        be used to 'watch' both subprocess stdout and stderr in separate
        threads.

    .. versionadded:: 1.0
    streamreturnc                 C   s   t )a  
        Act on ``stream`` data, potentially returning responses.

        :param str stream:
            All data read on this stream since the beginning of the session.

        :returns:
            An iterable of ``str`` (which may be empty).

        .. versionadded:: 1.0
        )NotImplementedError)selfr    r   B/var/www/html/venv/lib/python3.10/site-packages/invoke/watchers.pysubmit&   s   zStreamWatcher.submitN)__name__
__module____qualname____doc__strr   r   r   r   r   r   r      s    r   c                	   @   s`   e Zd ZdZdededdfddZdeded	edee fd
dZdedeeddf fddZ	dS )	Responderz
    A parameterizable object that submits responses to specific patterns.

    Commonly used to implement password auto-responds for things like ``sudo``.

    .. versionadded:: 1.0
    patternresponser   Nc                 C   s   || _ || _d| _dS )aY  
        Imprint this `Responder` with necessary parameters.

        :param pattern:
            A raw string (e.g. ``r"\[sudo\] password for .*:"``) which will be
            turned into a regular expression.

        :param response:
            The string to submit to the subprocess' stdin when ``pattern`` is
            detected.
        r   N)r   r   index)r
   r   r   r   r   r   __init__>   s   
zResponder.__init__r   
index_attrc                 C   sB   t | |}||d }t||tj}|rt| ||t|  |S )a  
        Generic "search for pattern in stream, using index" behavior.

        Used here and in some subclasses that want to track multiple patterns
        concurrently.

        :param str stream: The same data passed to ``submit``.
        :param str pattern: The pattern to search for.
        :param str index_attr: The name of the index attribute to use.
        :returns: An iterable of string matches.

        .. versionadded:: 1.0
        N)getattrrefindallSsetattrlen)r
   r   r   r   r   newmatchesr   r   r   pattern_matchesO   s   
zResponder.pattern_matchesc                 c   s$    |  || jdD ]}| jV  q	d S )Nr   )r!   r   r   )r
   r   _r   r   r   r   k   s   
zResponder.submit)
r   r   r   r   r   r   r   r!   r   r   r   r   r   r   r   5   s    
 r   c                       sR   e Zd ZdZdedededdf fddZd	edeeddf f fd
dZ  ZS )FailingRespondera  
    Variant of `Responder` which is capable of detecting incorrect responses.

    This class adds a ``sentinel`` parameter to ``__init__``, and its
    ``submit`` will raise `.ResponseNotAccepted` if it detects that sentinel
    value in the stream.

    .. versionadded:: 1.0
    r   r   sentinelr   Nc                    s$   t  || || _d| _d| _d S )Nr   F)superr   r$   failure_indextried)r
   r   r   r$   	__class__r   r   r   |   s   
zFailingResponder.__init__r   c                    sL   t  |}| || jd}| jr|rd| j| j}t||r$d| _|S )Nr&   z(Auto-response to r"{}" failed with {!r}!T)r%   r   r!   r$   r'   formatr   r   )r
   r   r   failederrr(   r   r   r      s   
zFailingResponder.submit)	r   r   r   r   r   r   r   r   __classcell__r   r   r(   r   r#   q   s    
(r#   )r   	threadingtypingr   r   
exceptionsr   localr   r   r#   r   r   r   r   <module>   s    -<