o
    ThM                     @   s  d 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	m
Z
mZmZmZmZmZmZmZmZmZmZ ddlmZ ddlmZmZ erPdd	lmZ dd
lmZ ede
dZG dd dee Zde	de	de
fddZG dd dZ ddde	de	ddfddZ!dS )zb
This module contains the core `.Task` class & convenience decorators used to
generate new tasks.
    N)deepcopy)update_wrapper)TYPE_CHECKINGAnyCallableDictListGenericIterableOptionalSetTupleTypeTypeVarUnion   Context)Argumenttranslate_underscores)	Signature)ConfigT)boundc                   @   s  e Zd ZdZ												d0dedee dee d	eee  d
ee dededee	ee
f  deeee ef  deeee ef  dedeee  deee  ddfddZedefddZdefddZdedefddZdefddZde
d e
defd!d"Zedefd#d$Zdedd%fd&d'Zd	eee  dee fd(d)Zdeded*ee de	ee
f fd+d,Z	d1d-ee dee fd.d/ZdS )2TaskaW  
    Core object representing an executable task & its argument specification.

    For the most part, this object is a clearinghouse for all of the data that
    may be supplied to the `@task <invoke.tasks.task>` decorator, such as
    ``name``, ``aliases``, ``positional`` etc, which appear as attributes.

    In addition, instantiation copies some introspection/documentation friendly
    metadata off of the supplied ``body`` object, such as ``__doc__``,
    ``__name__`` and ``__module__``, allowing it to "appear as" ``body`` for
    most intents and purposes.

    .. versionadded:: 1.0
    N FTbodynamealiases
positionaloptionaldefaultauto_shortflagshelpprepost	autoprintiterableincrementablereturnc                 C   s   || _ t| | j  t|dd| _t|dd| _t|dd| _|| _|| _|| _| 	|| _
t|| _|p5g | _|p:g | _|| _|pBi  | _|	pIg | _|
pNg | _d| _|| _d S )N__doc__ __name__
__module__r   )r   r   getattrr*   r,   r-   _namer   
is_defaultfill_implicit_positionalsr   tupler    r'   r(   r"   copyr#   r$   r%   times_calledr&   )selfr   r   r   r   r    r!   r"   r#   r$   r%   r&   r'   r(   r   r   ?/var/www/html/venv/lib/python3.10/site-packages/invoke/tasks.py__init__;   s$   





zTask.__init__c                 C   s   | j p| jS N)r/   r,   r5   r   r   r6   r   f   s   z	Task.namec                 C   s*   d}| j rdd| j }d| j|S )Nr+   z ({})z, z<Task {!r}{}>)r   formatjoinr   )r5   r   r   r   r6   __repr__j   s   zTask.__repr__otherc                 C   sR   t |tr| j|jkrdS | j|jkrdS z	| jj|jjkW S  ty(   Y dS w )NFT)
isinstancer   r   r   __code__AttributeError)r5   r=   r   r   r6   __eq__p   s   zTask.__eq__c                 C   s   t | jt | j S r8   )hashr   r   r9   r   r   r6   __hash__~   s   zTask.__hash__argskwargsc                 O   sJ   t |d tsd}t|t|d | j|i |}|  jd7  _|S )Nr   z9Task expected a Context as its first arg, got {} instead!r   )r>   r   	TypeErrorr:   typer   r4   )r5   rD   rE   errresultr   r   r6   __call__   s   zTask.__call__c                 C   s
   | j dkS )Nr   )r4   r9   r   r   r6   called   s   
zTask.calledr   c                 C   sR   t |tjr|n|j}t|}t|j }t	|st
d|j|dd dS )a   
        Returns a modified `inspect.Signature` based on that of ``body``.

        :returns:
            an `inspect.Signature` matching that of ``body``, but with the
            initial context argument removed.
        :raises TypeError:
            if the task lacks an initial positional `.Context` argument.

        .. versionadded:: 1.0
        .. versionchanged:: 2.0
            Changed from returning a two-tuple of ``(arg_names, spec_dict)`` to
            returning an `inspect.Signature`.
        z,Tasks must have an initial Context argument!r   N)
parameters)r>   typesFunctionTyperJ   inspect	signaturelistrL   valueslenrF   replace)r5   r   funcsigparamsr   r   r6   argspec   s   

zTask.argspecc                 C   s(   |d u rdd |  | jj D }|S )Nc                 S   s    g | ]}|j tjju r|jqS r   )r!   rO   r   emptyr   ).0xr   r   r6   
<listcomp>   s
    z2Task.fill_implicit_positionals.<locals>.<listcomp>)rX   r   rL   rR   )r5   r   r   r   r6   r1      s
   zTask.fill_implicit_positionalstaken_namesc           
      C   s  i }|| j v |d< || jv |d< || jv r#t|d< |d ur|ng |d< || jv r,d|d< |}d|v r:||d< t|}|g}| jrT|D ]}||ksS||v sS||  nqB||d	< |d tj	j
fvrtt|}|d rl|tu sp||d< ||d< ||fD ]}	|	| jv r| j|	|d
<  |S qx|S )Nr   r    kindr!   Tr(   _	attr_namenamesr#   )r   r    r'   rQ   r(   r   r"   appendrO   r   rY   rG   boolr#   pop)
r5   r   r!   r]   optsoriginal_namera   charr^   possibilityr   r   r6   arg_opts   s@   



zTask.arg_optsignore_unknown_helpc           
   	   C   s   |  | j}t|j }g }|j D ]}tdi | |j|j	|}|
| |t|j q| jrC|sCtdt| j tt| jD ]}t|D ]\}}	|	j|krd|d||  nqPqJ|S )as  
        Return a list of Argument objects representing this task's signature.

        :param bool ignore_unknown_help:
            Controls whether unknown help flags cause errors. See the config
            option by the same name for details.

        .. versionadded:: 1.0
        .. versionchanged:: 1.7
            Added the ``ignore_unknown_help`` kwarg.
        z4Help field was set for param(s) that don't exist: {}r   Nr   )rX   r   setrL   keysrR   r   ri   r   r!   rb   updatera   r#   
ValueErrorr:   rQ   reversedr   	enumerateinsertrd   )
r5   rj   rV   r]   rD   paramnew_argposargiargr   r   r6   get_arguments   s.   


zTask.get_arguments)Nr   Nr   FTNNNFNNr8   )r,   r-   __qualname__r*   r   r   strr
   rc   r   r   r   r   r7   propertyr   r<   objectrA   intrC   r   rJ   rK   rX   r1   r   ri   r   rw   r   r   r   r6   r   $   s    
	



+




2r   rD   rE   r)   c                     s    dt t| dkr#t| d r#t| d ts# | d fi S | r1dv r-td| d< dtdtt f fdd	}|S )
a  
    Marks wrapped callable object as a valid Invoke task.

    May be called without any parentheses if no extra options need to be
    specified. Otherwise, the following keyword arguments are allowed in the
    parenthese'd form:

    * ``name``: Default name to use when binding to a `.Collection`. Useful for
      avoiding Python namespace issues (i.e. when the desired CLI level name
      can't or shouldn't be used as the Python level name.)
    * ``aliases``: Specify one or more aliases for this task, allowing it to be
      invoked as multiple different names. For example, a task named ``mytask``
      with a simple ``@task`` wrapper may only be invoked as ``"mytask"``.
      Changing the decorator to be ``@task(aliases=['myothertask'])`` allows
      invocation as ``"mytask"`` *or* ``"myothertask"``.
    * ``positional``: Iterable overriding the parser's automatic "args with no
      default value are considered positional" behavior. If a list of arg
      names, no args besides those named in this iterable will be considered
      positional. (This means that an empty list will force all arguments to be
      given as explicit flags.)
    * ``optional``: Iterable of argument names, declaring those args to
      have :ref:`optional values <optional-values>`. Such arguments may be
      given as value-taking options (e.g. ``--my-arg=myvalue``, wherein the
      task is given ``"myvalue"``) or as Boolean flags (``--my-arg``, resulting
      in ``True``).
    * ``iterable``: Iterable of argument names, declaring them to :ref:`build
      iterable values <iterable-flag-values>`.
    * ``incrementable``: Iterable of argument names, declaring them to
      :ref:`increment their values <incrementable-flag-values>`.
    * ``default``: Boolean option specifying whether this task should be its
      collection's default task (i.e. called if the collection's own name is
      given.)
    * ``auto_shortflags``: Whether or not to automatically create short
      flags from task options; defaults to True.
    * ``help``: Dict mapping argument names to their help strings. Will be
      displayed in ``--help`` output. For arguments containing underscores
      (which are transformed into dashes on the CLI by default), either the
      dashed or underscored version may be supplied here.
    * ``pre``, ``post``: Lists of task objects to execute prior to, or after,
      the wrapped task whenever it is executed.
    * ``autoprint``: Boolean determining whether to automatically print this
      task's return value to standard output when invoked directly via the CLI.
      Defaults to False.
    * ``klass``: Class to instantiate/return. Defaults to `.Task`.

    If any non-keyword arguments are given, they are taken as the value of the
    ``pre`` kwarg for convenience's sake. (It is an error to give both
    ``*args`` and ``pre`` at the same time.)

    .. versionadded:: 1.0
    .. versionchanged:: 1.1
        Added the ``klass`` keyword argument.
    klassr   r   r$   z2May not give *args and 'pre' kwarg simultaneously!r   r)   c                    s    | fi }|S r8   r   )r   _taskr}   rE   r   r6   innerc  s   ztask.<locals>.inner)rd   r   rS   callabler>   rF   r   r   )rD   rE   r   r   r   r6   task!  s   6&r   c                   @   s   e Zd ZdZ			d!dddee deeedf  deeeef  d	df
d
dZ	ded	efddZ
ded	d fddZd	efddZded	efddZddd	efddZd	eeef fddZ		d"deed   deeeef  d	d fdd ZdS )#Calla  
    Represents a call/execution of a `.Task` with given (kw)args.

    Similar to `~functools.partial` with some added functionality (such as the
    delegation to the inner task, and optional tracking of the name it's being
    called by.)

    .. versionadded:: 1.0
    Nr   r   	called_asrD   .rE   r)   c                 C   s(   || _ || _|p
t | _|pt | _dS )a  
        Create a new `.Call` object.

        :param task: The `.Task` object to be executed.

        :param str called_as:
            The name the task is being called as, e.g. if it was called by an
            alias or other rebinding. Defaults to ``None``, aka, the task was
            referred to by its default name.

        :param tuple args:
            Positional arguments to call with, if any. Default: ``None``.

        :param dict kwargs:
            Keyword arguments to call with, if any. Default: ``None``.
        N)r   r   r2   rD   dictrE   )r5   r   r   rD   rE   r   r   r6   r7   v  s   zCall.__init__r   c                 C   s   t | j|S r8   )r.   r   )r5   r   r   r   r6   __getattr__  s   zCall.__getattr__memoc                 C   s   |   S r8   )clone)r5   r   r   r   r6   __deepcopy__  s   zCall.__deepcopy__c                 C   sF   d}| j d ur| j | jjkrd| j }d| jj| jj|| j| jS )Nr+   z (called as: {!r})z%<{} {!r}{}, args: {!r}, kwargs: {!r}>)r   r   r   r:   	__class__r,   rD   rE   )r5   akar   r   r6   r<     s   zCall.__repr__r=   c                 C   s,   d  D ]}t| |t||kr dS qdS )Nztask args kwargsFT)splitr.   )r5   r=   attrr   r   r6   rA     s
   zCall.__eq__configr   c                 C   s
   t |dS )zt
        Generate a `.Context` appropriate for this call, with given config.

        .. versionadded:: 1.0
        )r   r   )r5   r   r   r   r6   make_context  s   
zCall.make_contextc                 C   s    t | j| jt| jt| jdS )zq
        Return keyword args suitable for cloning this call into another.

        .. versionadded:: 1.1
        )r   r   rD   rE   )r   r   r   r   rD   rE   r9   r   r   r6   
clone_data  s   zCall.clone_dataintowith_c                 C   s:   |dur|n| j }|  }|dur|| |di |S )a  
        Return a standalone copy of this Call.

        Useful when parameterizing task executions.

        :param into:
            A subclass to generate instead of the current class. Optional.

        :param dict with_:
            A dict of additional keyword arguments to use when creating the new
            clone; typically used when cloning ``into`` a subclass that has
            extra args on top of the base class. Optional.

            .. note::
                This dict is used to ``.update()`` the original object's data
                (the return value from its `clone_data`), so in the event of
                a conflict, values in ``with_`` will win out.

        .. versionadded:: 1.0
        .. versionchanged:: 1.1
            Added the ``with_`` kwarg.
        Nr   )r   r   rm   )r5   r   r   r}   datar   r   r6   r     s
   
z
Call.clone)NNN)NN)r,   r-   rx   r*   r   ry   r   r   r   r7   r   r{   r   r<   rc   rA   r   r   r   r   r   r   r   r   r6   r   k  s>    


r   c                 O   s   t | ||dS )ai  
    Describes execution of a `.Task`, typically with pre-supplied arguments.

    Useful for setting up :ref:`pre/post task invocations
    <parameterizing-pre-post-tasks>`. It's actually just a convenient wrapper
    around the `.Call` class, which may be used directly instead if desired.

    For example, here's two build-like tasks that both refer to a ``setup``
    pre-task, one with no baked-in argument values (and thus no need to use
    `.call`), and one that toggles a boolean flag::

        @task
        def setup(c, clean=False):
            if clean:
                c.run("rm -rf target")
            # ... setup things here ...
            c.run("tar czvf target.tgz target")

        @task(pre=[setup])
        def build(c):
            c.run("build, accounting for leftover files...")

        @task(pre=[call(setup, clean=True)])
        def clean_build(c):
            c.run("build, assuming clean slate...")

    Please see the constructor docs for `.Call` for details - this function's
    ``args`` and ``kwargs`` map directly to the same arguments as in that
    method.

    .. versionadded:: 1.0
    )rD   rE   )r   )r   rD   rE   r   r   r6   call  s   !r   )"r*   rO   rM   r3   r   	functoolsr   typingr   r   r   r   r   r	   r
   r   r   r   r   r   r   contextr   parserr   r   r   r   r   r   r   r   r   r   r   r   r   r6   <module>   s"    < ~J{