o
    Th"                     @   s   d dl mZmZmZmZmZmZmZ ddlm	Z	 ddl
mZ ddlmZ ddlmZmZ er@ddlmZ ddlmZ dd	l
mZ G d
d dZdS )    )TYPE_CHECKINGAnyDictListOptionalTupleUnion   )Config)ParserContext)debug)CallTask)
Collection)Result)ParseResultc                	   @   s   e Zd ZdZ		dddded ded d	dfd
dZdeeeee	ee
f f ef d	e	d fddZdeeeeee	ee
f f ef df d	ed fddZded d	ed fddZded d	ed fddZdS )Executorz
    An execution strategy for Task objects.

    Subclasses may override various extension points to change, add or remove
    behavior.

    .. versionadded:: 1.0
    N
collectionr   configr
   corer   returnc                 C   s$   || _ |dur	|nt | _|| _dS )a  
        Initialize executor with handles to necessary data structures.

        :param collection:
            A `.Collection` used to look up requested tasks (and their default
            config data, if any) by name during execution.

        :param config:
            An optional `.Config` holding configuration state. Defaults to an
            empty `.Config` if not given.

        :param core:
            An optional `.ParseResult` holding parsed core program arguments.
            Defaults to ``None``.
        N)r   r
   r   r   )selfr   r   r    r   B/var/www/html/venv/lib/python3.10/site-packages/invoke/executor.py__init__   s   
zExecutor.__init__tasks)r   r   c                 G   s  t ddd |D  | |}t d| t|}| |}z| jjj}W n ty3   d}Y nw |r;| |n|}i }|D ]G}||v oI|j	}t d| | j}	| j
|j}
|	|
 |	  t d ||	}|g|jR }|j|i |j}|rt| |||j< qA|S )a  
        Execute one or more ``tasks`` in sequence.

        :param tasks:
            An all-purpose iterable of "tasks to execute", each member of which
            may take one of the following forms:

            **A string** naming a task from the Executor's `.Collection`. This
            name may contain dotted syntax appropriate for calling namespaced
            tasks, e.g. ``subcollection.taskname``. Such tasks are executed
            without arguments.

            **A two-tuple** whose first element is a task name string (as
            above) and whose second element is a dict suitable for use as
            ``**kwargs`` when calling the named task. E.g.::

                [
                    ('task1', {}),
                    ('task2', {'arg1': 'val1'}),
                    ...
                ]

            is equivalent, roughly, to::

                task1()
                task2(arg1='val1')

            **A `.ParserContext`** instance, whose ``.name`` attribute is used
            as the task name and whose ``.as_kwargs`` attribute is used as the
            task kwargs (again following the above specifications).

            .. note::
                When called without any arguments at all (i.e. when ``*tasks``
                is empty), the default task from ``self.collection`` is used
                instead, if defined.

        :returns:
            A dict mapping task objects to their return values.

            This dict may include pre- and post-tasks if any were executed. For
            example, in a collection with a ``build`` task depending on another
            task named ``setup``, executing ``build`` will result in a dict
            with two keys, one for ``build`` and one for ``setup``.

        .. versionadded:: 1.0
        zExamining top level tasks {!r}c                 S   s   g | ]}|qS r   r   ).0xr   r   r   
<listcomp>c   s    z$Executor.execute.<locals>.<listcomp>z#Tasks (now Calls) with kwargs: {!r}TzExecuting {!r}z/Finished loading collection & shell env configs)r   format	normalizelistexpand_callsr   r   dedupeAttributeError	autoprintr   configuration	called_asload_collectionload_shell_envmake_contextargstaskkwargsprint)r   r   callsdirectexpandedr#   resultscallr%   r   collection_configcontextr+   resultr   r   r   execute1   s6   2



zExecutor.execute.r   c                 C   s   g }|D ]+}t |tr|}i }nt |tr|j}|j}n|\}}t| j| ||d}|| q|sB| jjdurBt| j| jj g}|S )z
        Transform arbitrary task list w/ various types, into `.Call` objects.

        See docstring for `~.Executor.execute` for details.

        .. versionadded:: 1.0
        )r-   r'   N)	
isinstancestrr   name	as_kwargsr   r   appenddefault)r   r   r/   r,   r:   r-   cr   r   r   r       s   

zExecutor.normalizer/   c                 C   sJ   g }t d |D ]}||vrt d| || qt d| q|S )z
        Deduplicate a list of `tasks <.Call>`.

        :param calls: An iterable of `.Call` objects representing tasks.

        :returns: A list of `.Call` objects.

        .. versionadded:: 1.0
        zDeduplicating tasks...z{!r}: no duplicates found, okz%{!r}: found in list already, skipping)r   r   r<   )r   r/   dedupedr3   r   r   r   r#      s   
zExecutor.dedupec                 C   s`   g }|D ])}t |trt|}td| || |j || || |j	 q|S )a  
        Expand a list of `.Call` objects into a near-final list of same.

        The default implementation of this method simply adds a task's
        pre/post-task list before/after the task itself, as necessary.

        Subclasses may wish to do other things in addition (or instead of) the
        above, such as multiplying the `calls <.Call>` by argument vectors or
        similar.

        .. versionadded:: 1.0
        zExpanding task-call {!r})
r8   r   r   r   r   extendr"   prer<   post)r   r/   retr3   r   r   r   r"      s   
	
zExecutor.expand_calls)NN)__name__
__module____qualname____doc__r   r   r   r9   r   r   r   r   r7   r   r    r#   r"   r   r   r   r   r      s6    

c 
r   N)typingr   r   r   r   r   r   r   r   r
   parserr   utilr   r   r   r   r   r   runnersr   r   r   r   r   r   r   <module>   s   $ 