
    cei                    f   U d Z ddlmZ g d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
 ddlmZ ej        g ej        f         Zej        dej        f         Z G d	 d
e          Z G d de          Z G d de          Z G d d          Z G d d          Z G d dej                  Z G d d          Z G d de
j                  Z G d de
j                  Z G d de
j                  Z G d d          Z  G d d ej!        !          Z" G d" d#e"          Z# G d$ d%e"          Z$ G d& d'          Z% G d( d)e
j                  Z& G d* d+          Z' G d, d-e"          Z( G d. d/ej)        e
j*                           Z+ ej,        e&          Z-d0e.d1<    e+            Z/d2e.d3<   d7d6Z0dS )8zAIntegration with worker framework to make API calls asynchronous.    )annotations)TaskCanceledErrorTaskFailedErrorCallDuration
TaskStatus
TaskResult
TaskWorker
TaskThreadTaskContextTaskModifierInsertArgumentTaskCancelContextAddCancelContextN)QtCore   )
singletons.c                      e Zd ZdZdS )	TaskErrorzGeneral issue running a task.N)__name__
__module____qualname____doc__     @lib/python3.11/site-packages/anaconda_navigator/utils/workers.pyr   r      s        ''''r   r   c                      e Zd ZdZddZdS )r   z&Task was canceled during its workflow.returnstrc                    dS ).Prepare string representation of the instance.z'Task was cancelled during its executionr   selfs    r   __str__zTaskCanceledError.__str__!   s    88r   Nr   r    r   r   r   r   r%   r   r   r   r   r      s.        009 9 9 9 9 9r   r   c                      e Zd ZdZddZdS )r   z Task failed during its workflow.r   r    c                    dS )r"   z Task failed during its executionr   r#   s    r   r%   zTaskFailedError.__str__)   s    11r   Nr&   r'   r   r   r   r   r   &   s.        **2 2 2 2 2 2r   r   c                  t    e Zd ZdZdZ	 	 dddZedd            Zedd            Zedd            Z	ddZ
dS )r   z
    Signature of a function call.

    :param function: Function to call.
    :param args: Positional arguments for the `function`.
    :param kwargs: Keyword arguments for the `function`.
    )
__function__args__kwargsNfunctionFUNCTIONargs,typing.Optional[typing.Sequence[typing.Any]]kwargs0typing.Optional[typing.Mapping[str, typing.Any]]r   Nonec                @    |d}|i }|| _         || _        || _        dS )z'Initialize new :class:`~Call` instance.Nr   _Call__function_Call__args_Call__kwargs)r$   r.   r0   r2   s       r   __init__zCall.__init__9   s1     <D>F2:AEGMr   c                    | j         S )zFunction to call.)r7   r#   s    r   r.   zCall.functionI        r   typing.Sequence[typing.Any]c                    | j         S )z-Positional arguments to call `function` with.)r8   r#   s    r   r0   z	Call.argsN        {r   typing.Mapping[str, typing.Any]c                    | j         S )z*Keyword arguments to call `function` with.)r9   r#   s    r   r2   zCall.kwargsS        }r   
typing.Anyc                0     | j         | j        i | j        S )zCExecute stored function with both positional and keyword arguments.r6   r#   s    r   __call__zCall.__call__X   s    t=t}===r   NN)r.   r/   r0   r1   r2   r3   r   r4   r   r/   )r   r=   )r   r@   r   rC   )r   r   r   r   	__slots__r:   propertyr.   r0   r2   rE   r   r   r   r   r   .   s          5I
 BFGK	N N N N N     X    X    X> > > > > >r   r   c                  v    e Zd ZdZdZ	 	 ddd	Zedd            Zedd            Zedd            Z	dddZ
dS )r   aU  
    Simple helper to track execution duration.

    :param start: Optional start of the tracked time span.

                  If it is not provided - current timestamp will be used.

    :param stop: Optional end of the tracked time span.

                 Might be omitted on initialization and then set with :meth:`~Duration.finish`.
    )__start__stopNstart"typing.Optional[datetime.datetime]stopr   r4   c                b    |t           j                                         }|| _        || _        dS )z+Initialize new :class:`~Duration` instance.N)datetimenow_Duration__start_Duration__stop)r$   rN   rP   s      r   r:   zDuration.__init__l   s/     =%))++E8=:>r   datetime.datetimec                    | j         S )zStart of the tracked time span.)rT   r#   s    r   rN   zDuration.startx        |r   c                    | j         S )zStop of the tracked time span.)rU   r#   s    r   rP   zDuration.stop}   r?   r   typing.Optional[float]c                V    | j         dS | j         | j        z
                                  S )z*Total number of seconds in this time span.N)rU   rT   total_secondsr#   s    r   totalzDuration.total   s,     ;4dl*99;;;r   c                    | j         t          d          |t          j                                        }|| _         dS )a  
        Set the stop value for this duration.

        Might be set only once (either with this method or on initialization).

        :param stop: Value to set stop to.

                     If it is not provided - current timestamp will be used.
        NzDuration is already closed)rU   	TypeErrorrR   rS   )r$   rP   s     r   finishzDuration.finish   s?     ;"8999<$((**Dr   rF   )rN   rO   rP   rO   r   r4   )r   rV   )r   rO   )r   rZ   N)rP   rO   r   r4   )r   r   r   r   rI   r:   rJ   rN   rP   r]   r`   r   r   r   r   r   ]   s        
 
 &I 9=7;
? 
? 
? 
? 
?    X    X < < < X<      r   r   c                  l    e Zd ZdZ ej                    Z ej                    Z ej                    ZdS )r   z0Possible states of the :class:`~Task` execution.N)	r   r   r   r   enumauto	SUCCEEDEDCANCELEDFAILEDr   r   r   r   r      s;        ::	Ity{{HTY[[FFFr   r   c                      e Zd ZdZdZ	 	 dddZedd            Zedd            Zedd            Z	edd            Z
edd            ZdS )r	   a  
    Details of task execution.

    Instances of this class are provided through the task signals.

    :param call: Signature of the function that was called.
    :param status: Resulted status of the execution.
    :param result: Function output.

                   Must be an exact value for succeeded executions, and exception for failed executions.
    :param duration: Optional information about task execution duration.
    )__call__status__result
__durationNcallr   statusr   resultrC   durationtyping.Optional[Duration]r   r4   c                z   |"t                      }|                                 |t          j        k    r6|t	                      }nYt          |t                    st          d          n4|t          j        k    r$t          |t                    st          d          || _	        || _
        || _        || _        dS )z-Initialize new :class:`~TaskResult` instance.Nz=result must be a TaskCanceledError instance if task is failedz-result must be an exception if task is failed)r   r`   r   rf   r   
isinstancer_   rg   BaseException_TaskResult__call_TaskResult__status_TaskResult__result_TaskResult__duration)r$   rm   rn   ro   rp   s        r   r:   zTaskResult.__init__   s     zzHOOZ(((~*,,(9:: a _```a
)))Jv}4U4U)KLLL*.28282:r   c                    | j         S )z*Signature of the function that was called.)ru   r#   s    r   rm   zTaskResult.call   r?   r   r   c                    | j         S )zTask execution duration.)rx   r#   s    r   rp   zTaskResult.duration   r<   r   typing.Optional[BaseException]c                >    | j         t          j        k    rdS | j        S )z+Exception, that was raised by the function.Nrv   r   re   rw   r#   s    r   	exceptionzTaskResult.exception   s!     =J0004}r   c                H    | j         t          j        k    r| j        S | j        )zw
        Function output.

        If function raised an error - this property will also raise the same error.
        r}   r#   s    r   ro   zTaskResult.result   s$     =J000= mr   c                    | j         S )z!Resulted status of the execution.)rv   r#   s    r   rn   zTaskResult.status   rB   r   rF   )
rm   r   rn   r   ro   rC   rp   rq   r   r4   r   r   )r   r   )r   r{   rH   )r   r   )r   r   r   r   rI   r:   rJ   rm   rp   r~   ro   rn   r   r   r   r	   r	      s          AI "&26; ; ; ; ;2    X    X    X    X    X  r   r	   c                      e Zd ZdZ ej                    Z ej        e          Z ej        e          Z	 ej        e          Z
 ej        e          ZdS )TaskSignalsa  
    Collection of signals for :class:`~TaskWorker`.

    .. py:attribute:: sig_start

        Task started its execution.

    .. py:attribute:: sig_succeeded

        Task finished its execution successfully.

    .. py:attribute:: sig_canceled

        Task was canceled during its execution.

    .. py:attribute:: sig_failed

        Task raised an error.

    .. py:attribute:: sig_done

        Task finished its execution.

        This signal is called regardless of execution result.

    N)r   r   r   r   r   Signal	sig_startr	   sig_succeededsig_canceled
sig_failedsig_doner   r   r   r   r      sk         6 I!FM*--M 6=,,Lz**Jv}Z((HHHr   r   c                       e Zd ZdZdd fd	Zedd
            Zedd            Zedd            ZddZ	ddZ
ddZddZ xZS )r
   a  
    Executor for tasks.

    :param call: Signature of the function to call with the worker.
    :param cancel: Optional cancel function.

                   This function might be called to change `function` execution context, so it would be aware that it
                   should be canceled.

                   If it is not provided - `function` execution will still be finished gracefully, but result will
                   contain only information about cancelled function call, unless it will raise an exception.
    Nrm   r   canceltyping.Optional[ACTION]r   r4   c                    t                                                       t                      | _        || _        || _        d| _        d| _        dS )z-Initialize new :class:`~TaskWorker` instance.FN)superr:   r   _TaskWorker__signals_TaskWorker__call_TaskWorker__cancel_TaskWorker__canceled_TaskWorker__result)r$   rm   r   	__class__s      r   r:   zTaskWorker.__init__   sD    4?MM*.?E %59r   c                    | j         S z8Signature of the function that is executed in this task.)r   r#   s    r   rm   zTaskWorker.call*  r?   r   r   c                    | j         S )z4Collection of signals to track task execution state.)r   r#   s    r   signalszTaskWorker.signals/  s     ~r   typing.Optional[TaskResult]c                    | j         S zTask execution result.)r   r#   s    r   ro   zTaskWorker.result4  rB   r   c                    | j         t          d          | j        rt          d          d| _        | j        |                                  dS dS )!Cancel execution of current task.NzAlready finishedzAlready canceledT)r   r_   r   r   r#   s    r   r   zTaskWorker.cancel9  sZ    =$.///? 	0.///=$MMOOOOO %$r   c                   t           j                            |            | j        j                                         t                      }	 | j        rt                      | 	                                }t          j        }nE# t          $ r}|}t          j        }Y d}~n(d}~wt          $ r}|}t          j        }Y d}~nd}~ww xY w|                                 | j        rt                      }t          j        }n:# |                                 | j        rt                      }t          j        }w xY wt!          | j	        |||          | _        t          j        | j        j        t          j        | j        j        t          j        | j        j        i|                             | j                   | j        j                            | j                   dS )zl
        Execute task.

        This method is called internally by the Qt.

        :meta private:
        N)rm   rn   ro   rp   )MANAGERinstance	_registerr   r   emitr   r   r   r   r   re   rf   	Exceptionrg   r`   r	   r   r   r   r   r   )r$   rp   responsern   r~   s        r   runzTaskWorker.runE  s    	""4(((##%%% ,4::	- *'))){{}}H)FF  	) 	) 	) H(FFFFFF 	' 	' 	' H&FFFFFF	' OO -,..#, OO -,..#,,,,,"	
 
 
  $,"<!:t|6	
 		 $t}%%%""4=11111sB   5B C> 
CB C>  C-C ;C>  CC> >7D5c                D    t           j                            |            dS )zLaunch this task.N)THREAD_POOLr   rN   r#   s    r   rN   zTaskWorker.startq  s    ""4(((((r   'TaskThread'c                N    t          |           }|                                 |S )z)Start a manageable thread with this task.)parent)r   rN   )r$   ro   s     r   threadzTaskWorker.threadu  s$    't444r   ra   )rm   r   r   r   r   r4   r   )r   r   r   r   r   r4   )r   r   )r   r   r   r   r:   rJ   rm   r   ro   r   r   rN   r   __classcell__r   s   @r   r
   r
     s         : : : : : : :    X    X    X
 
 
 
*2 *2 *2 *2X) ) ) )       r   r
   c                  d     e Zd ZdZd fdZedd            Zedd
            ZddZddZ	 xZ
S )r   z
    Wrapper for a :class:`~TaskWorker`, that provides a manageable :class:`~qtpy.QtCore.QThread` interface.

    Should be initialized from :class:`~TaskWorker` instance via :meth:`~TaskWorker.thread`.
    r   r
   r   r4   c                V    t                                                       || _        dS )z-Initialize new :class:`~TaskThread` instance.N)r   r:   _TaskThread__parent)r$   r   r   s     r   r:   zTaskThread.__init__  s$    28r   r   c                    | j         j        S r   )r   rm   r#   s    r   rm   zTaskThread.call  s     }!!r   r   c                    | j         j        S r   )r   ro   r#   s    r   ro   zTaskThread.result  s     }##r   c                8    | j                                          dS )r   N)r   r   r#   s    r   r   zTaskThread.cancel  s    r   c                8    | j                                          dS )zr
        Execute the thread.

        This method is called internally by the Qt.

        :meta private:
        N)r   r   r#   s    r   r   zTaskThread.run  s     	r   )r   r
   r   r4   r   r   r   )r   r   r   r   r:   rJ   rm   ro   r   r   r   r   s   @r   r   r   |  s         9 9 9 9 9 9
 " " " X" $ $ $ X$          r   r   c                      e Zd ZdZdZ	 	 	 dddZedd            Zedd            Zedd            Z	e	j
        d d            Z	ed!d            Zed"d            Zed#d            ZdS )$r   a)  
    Context for task signature.

    This is used to prepare contexts for both function :class:`~Call` and/or :class:`~TaskWorker`.

    :param function: Function that should be called.
    :param args: Initial collection of positional arguments to call function with.
    :param kwargs: Initial collection of keyword arguments to call function with.
    :param cancel: Initial value for cancel function, which may be used by :class:`~TaskWorker`.

    Values of `args`, `kwargs` and `cancel` might be modified by :class:`~TaskModifier` instances.
    )r+   r,   r-   __cancelNr.   r/   r0   ,typing.Optional[typing.Iterable[typing.Any]]r2   r3   r   r   r   r4   c                    |g }|i }|| _         t          |          | _        t          |          | _        || _        dS )z.Initialize new :class:`~TaskContext` instance.N)_TaskContext__functionlist_TaskContext__argsdict_TaskContext__kwargs_TaskContext__cancel)r$   r.   r0   r2   r   s        r   r:   zTaskContext.__init__  sD     <D>F2:=A$ZZDHLL17r   typing.List[typing.Any]c                    | j         S )zNCollection of positional arguments to call :attr:`~TaskContext.function` with.)r   r#   s    r   r0   zTaskContext.args  r?   r   r   c                D    t          | j        | j        | j                  S )z8New :class:`~Call` signature from the collected context.r.   r0   r2   )r   r.   r0   r2   r#   s    r   rm   zTaskContext.call  s     T]4;OOOOr   c                    | j         S )z9Function that can cancel function call in a graceful way.r   r#   s    r   r   zTaskContext.cancel  rB   r   valuec                    || _         dS )zUpdate `cancel` value.Nr   )r$   r   s     r   r   zTaskContext.cancel  s     r   c                    | j         S )z#Function to call from this context.)r   r#   s    r   r.   zTaskContext.function  r<   r   typing.Dict[str, typing.Any]c                    | j         S )zKCollection of keyword arguments to call :attr:`~TaskContext.function` with.)r   r#   s    r   r2   zTaskContext.kwargs  rB   r   r
   c                8    t          | j        | j                  S )z4New :class:`~TaskWorker` from the collected context.)rm   r   )r
   rm   r   r#   s    r   workerzTaskContext.worker  s     ty====r   )NNN)
r.   r/   r0   r   r2   r3   r   r   r   r4   )r   r   r   )r   r   )r   r   r   r4   rG   )r   r   )r   r
   )r   r   r   r   rI   r:   rJ   r0   rm   r   setterr.   r2   r   r   r   r   r   r     s2         AI
 BFGK.28 8 8 8 8$    X P P P XP    X ]   ]    X    X > > > X> > >r   r   c                  8    e Zd ZdZdZej        d	d            ZdS )
r   z
    Common interface for all :class:`~TaskContext` modifiers.

    This is used to separate context modifiers from the actual function in :class:`~Task`.
    r   contextr   r   r4   c                    dS z9Apply modification to the :class:`~TaskContext` instance.Nr   r$   r   s     r   applyzTaskModifier.apply        r   Nr   r   r   r4   )r   r   r   r   rI   abcabstractmethodr   r   r   r   r   r     sM          IH H H H H Hr   r   )	metaclassc                  X    e Zd ZdZdZddd
Zedd            Zedd            ZddZ	dS )r   a	  
    Insert new positional argument.

    Places `value` at `target` in :attr:`~TaskContext.args` or :attr:`~TaskContext.kwargs`.

    :param value: Value of the argument to add.
    :param target: Index of the positional or Key of the keyword argument to add.
    )__value__targetr   r   rC   targettyping.Union[int, str]r   r4   c                "    || _         || _        dS )z1Initialize new :class:`~InsertArgument` instance.N)_InsertArgument__value_InsertArgument__target)r$   r   r   s      r   r:   zInsertArgument.__init__  s    16>Dr   c                    | j         S )z
        Location for the new argument.

        Might be :code:`int` index for positional arguments, or :code:`str` key for keyword arguments.
        )r   r#   s    r   r   zInsertArgument.target  s     }r   c                    | j         S )zValue of the new argument.)r   r#   s    r   r   zInsertArgument.value  rX   r   r   r   c                    t          | j        t                    r'|j                            | j        | j                   dS | j        |j        | j        <   dS r   )rs   r   intr0   insertr   r2   r   s     r   r   zInsertArgument.apply  sN    dk3'' 	5LTZ88888*.*GN4;'''r   N)r   )r   rC   r   r   r   r4   )r   r   rH   r   )
r   r   r   r   rI   r:   rJ   r   r   r   r   r   r   r   r     s          (IE E E E E
    X    X5 5 5 5 5 5r   r   c                  >    e Zd ZdZdZddZedd            ZddZdS )AddCancelCallbackzg
    Add new callback to use for canceling a task.

    :param callback: Callback function to use.
    )
__callbackcallbackACTIONr   r4   c                    || _         dS )z4Initialize new :class:`~AddCancelCallback` instance.N_AddCancelCallback__callback)r$   r   s     r   r:   zAddCancelCallback.__init__%  s    08r   c                    | j         S )z.Callback function to use for canceling a task.r   r#   s    r   r   zAddCancelCallback.callback)  r<   r   r   r   c                    | j         |_        dS r   )r   r   r   s     r   r   zAddCancelCallback.apply.  s    r   N)r   r   r   r4   )r   r   r   )	r   r   r   r   rI   r:   rJ   r   r   r   r   r   r   r     sm           I9 9 9 9    X' ' ' ' ' 'r   r   c                      e Zd ZdZdZej        dd            Zej        dd
            Zd dZed!d            Z	ed"d            Z
	 	 d#d$dZd%dZd&dZd'dZdS )(r   a  
    Decorator for functions and methods to convert them to tasks.

    After decoration - functions and methods might still be called usual way. :meth:`~Task.worker` might be used to
    create a worker for a task.

    :param args: :class:`~TaskModifier` instances to modify a :class:`~TaskContext` before execution.

    .. note::

        Decorator can be called in both :code:`@Task` and :code:`@Task(...)` ways. Both will work.
    )r+   __modifiersr0   r   r   r4   c                    dS 'Initialize new :class:`~Task` instance.Nr   )r$   r0   s     r   r:   zTask.__init__H  r   r   r.   r/   c                   dS r   r   )r$   r.   r0   s      r   r:   zTask.__init__L  r   r   $typing.Union[FUNCTION, TaskModifier]c                    d}g }|D ]@}t          |t                    r|                    |           -||}2t          d          || _        t          |          | _        dS )r   Nz=Task arguments must be TaskModifiers and at most one function)rs   r   appendr_   _Task__functiontuple_Task__modifiers)r$   r0   r.   	modifiersargs        r   r:   zTask.__init__P  s    .2/1	  	a 	aC#|,, a  %%%%! _```5=JOPYJZJZr   c                <    | j         t          d          | j         S )z$Function that is used for this task.Nzfunction was not wrapped yet)r   AttributeErrorr#   s    r   r.   zTask.functiona  s#     ?" !?@@@r   typing.Tuple[TaskModifier, ...]c                    | j         S )z4Collection of modifiers for a :class:`~TaskContext`.)r   r#   s    r   r   zTask.modifiersh  s     r   Nr   r2   r3   r   c                t    t          | j        ||          }| j        D ]}|                    |           |S )z:Prepare a context for execution or converting into a task.r   r   )r   r.   r   r   )r$   r0   r2   ro   modifiers        r   	__contextzTask.__contextm  sI     -8UYbh,i,i,i 	+ 	+HNN6N****r   rC   c                    | j         3t          |          dk    s|rt          d          |d         | _         | S |                     ||                                          S )z
        Execute a function in a usual way.

        .. note::

            This method might be also called when the decorator is called in a :code:`@Task(...)` way to finalize
            function/method wrapping.
        Nr   z9function must be the only provided argument for wrapping.r   r0   r2   )r   lenr_   _Task__contextrm   r$   r0   r2   s      r   rE   zTask.__call__y  s`     ?"D		Q6 [\\\"1gDOK~~4~77<<>>>r   r   owner'Task'c                V    || S t          | j        g| j        t          |          R  S )zCPrepare a bound task for a methods called for a specific instances.)r   r.   r   r   )r$   r   r  s      r   __get__zTask.__get__  s4    KDMMDNMN84L4LMMMMr   r
   c                :    |                      ||          j        S )z$Prepare worker to execute this task.r  )r
  r   r  s      r   r   zTask.worker  s    ~~4~77>>r   )r0   r   r   r4   )r.   r/   r0   r   r   r4   )r0   r   r   r4   rG   )r   r  rF   )r0   r   r2   r3   r   r   )r0   rC   r2   rC   r   rC   )r   rC   r  rC   r   r  )r0   rC   r2   rC   r   r
   )r   r   r   r   rI   typingoverloadr:   rJ   r.   r   r
  rE   r  r   r   r   r   r   r   8  s"         .I_6 6 6 _6 _6 6 6 _6[ [ [ ["    X       X  BFGK
 
 
 
 
? ? ? ?"N N N N? ? ? ? ? ?r   r   c                  "    e Zd ZdZ ej                    Z ej        e          Z ej        e          Z	 ej        e          Z
 ej        e          Z ej        e          Zd fdZddZddZdd	Zdd
ZddZddZddZddZ xZS )TaskManagerz$Central manager for executing tasks.r   r4   c                |    t                                                       d| _        t                      | _        dS )z.Initialize new :class:`~TaskManager` instance.FN)r   r:   _TaskManager__cancelset_TaskManager__workers)r$   r   s    r   r:   zTaskManager.__init__  s/    #?Buur   r   r
   c                   | j                             |           | j        rEt          j        t
                    5  |                                 ddd           n# 1 swxY w Y   | j                            |           dS )zProcess start of a worker.N)	r  addr  
contextlibsuppressr_   r   r   r   r$   r   s     r   rL   zTaskManager.__start  s    6"""= 	 $Y//                                  	F#####s   AA #A c                P    || j         vrdS | j                            |           dS )zProcess success of a worker.N)r  r   r   r  s     r   __succeededzTaskManager.__succeeded  s1    ''F'''''r   c                P    || j         vrdS | j                            |           dS )zProcess cancel of a worker.N)r  r   r   r  s     r   
__canceledzTaskManager.__canceled  s1    ''Fv&&&&&r   c                P    || j         vrdS | j                            |           dS )zProcess fail of a worker.N)r  r   r   r  s     r   __failedzTaskManager.__failed  s0    ''FV$$$$$r   c                    	 | j                             |           n# t          $ r Y dS w xY w| j                            |           | j         s| j                                         dS dS )z"Process worker finished execution.N)r  removeLookupErrorr   r   	sig_emptyr  s     r   __donezTaskManager.__done  s    	N!!&)))) 	 	 	FF	 	6"""~ 	"N!!!!!	" 	"s    
++c                b     j         v rdS                                 j        j                             fd           j        j                             fd           j        j                             fd           j        j                             fd           dS )z-Register worker when it starts its execution.Nc                .                                   S ra   )_TaskManager__succeeded_r$   r   s    r   <lambda>z'TaskManager._register.<locals>.<lambda>  s    t7G7G7O7O r   c                .                                   S ra   )_TaskManager__canceledr,  s    r   r.  z'TaskManager._register.<locals>.<lambda>  s    doof6M6M r   c                .                                   S ra   )_TaskManager__failedr,  s    r   r.  z'TaskManager._register.<locals>.<lambda>  s    DMM&4I4I r   c                .                                   S ra   )_TaskManager__doner,  s    r   r.  z'TaskManager._register.<locals>.<lambda>  s    $++f2E2E r   )r  _TaskManager__startr   r   connectr   r   r   r  s   ``r   r   zTaskManager._register  s    T^##FV$,,-O-O-O-O-OPPP#++,M,M,M,M,MNNN!))*I*I*I*I*IJJJ''(E(E(E(E(EFFFFFr   c                
   d| _         t          | j                  }|s| j                                         dS |D ]G}t          j        t                    5  |                                 ddd           n# 1 swxY w Y   HdS )z&Cancel all running and future workers.TN)	r  r   r  r'  r   r  r  r_   r   )r$   workersr   s      r   
cancel_allzTaskManager.cancel_all  s    /3DN/C/C 	N!!!F  	  	 F$Y//                                 	  	 s   A77A;	>A;	c                    d| _         dS )z
        Reset task processing.

        This might be useful to allow executing new tasks after calling :meth:`~TaskManager.cancel_all`.
        FN)r  r#   s    r   resetzTaskManager.reset  s     r   r   )r   r
   r   r4   )r   r   r   r   r   r   r'  r
   r   r   r   r   r   r:   r5  r+  r0  r2  r4  r   r9  r;  r   r   s   @r   r  r    sO       ..Ij))I!FM*--M 6=,,Lz**Jv}Z((HE E E E E E$ $ $ $( ( ( (' ' ' '% % % %
" 
" 
" 
"	G 	G 	G 	G              r   r  c                  H    e Zd ZdZdZdddZedd	            Zdd
ZddZ	dS )r   z`
    Simplistic context with a canceled flag.

    :param canceled: Initial canceled state.
    )r!  Fcanceledboolr   r4   c                    || _         dS )z0Initialize new :class:`~CancelContext` instance.N_CancelContext__canceled)r$   r=  s     r   r:   zCancelContext.__init__  s     (r   c                    | j         S )zTask was canceled.r@  r#   s    r   r=  zCancelContext.canceled  r<   r   c                0    | j         rt                      dS )z-Break execution of a task if it was canceled.N)rA  r   r#   s    r   abort_if_canceledzCancelContext.abort_if_canceled  s"    ? 	&#%%%	& 	&r   c                    d| _         dS )zCancel task execution.TNr@  r#   s    r   r   zCancelContext.cancel  s    r   N)F)r=  r>  r   r4   )r   r>  r   )
r   r   r   r   rI   r:   rJ   r=  rD  r   r   r   r   r   r     s           I) ) ) ) )    X& & & &
     r   r   c                  @    e Zd ZdZdZdddZedd	            ZddZdS )r   a  
    Add new :class:`~CancelContext` to the :class:`~TaskContext`.

    This adds new :class:`~CancelContext` instance to the function kwargs and modifies :attr:`~TaskContext.cancel` to
    use the context.

    :param argument: Name of the keyword argument to add context to.

    .. warning::

        Context will be provided if only it was not provided before (with a regular keyword argument).

        Otherwise - it will reuse provided value.
    )
__argumentr   argumentr    r   r4   c                    || _         dS )z3Initialize new :class:`~AddCancelContext` instance.N_AddCancelContext__argument)r$   rH  s     r   r:   zAddCancelContext.__init__"  s    -5r   c                    | j         S )z4Name of the argument to which context will be added.rJ  r#   s    r   rH  zAddCancelContext.argument&  r<   r   r   c                    | j         |j        vrt                      |j        | j         <   |j        | j                  j        |_        dS r   )rH  r2   r   r   r   s     r   r   zAddCancelContext.apply+  s:    =..,9OOGN4=) 6=r   Nr  )rH  r    r   r4   r&   r   )	r   r   r   r   rI   r:   rJ   rH  r   r   r   r   r   r     sr           I6 6 6 6 6    X> > > > > >r   r   c                  &    e Zd ZdZdZd	dZd
dZdS )ThreadPoolSingletonz:Singleton instance of a :class:`~qtpy.QtCore.QThreadPool`.r   r   QtCore.QThreadPoolc                (    t          j                    S )zInitialize singleton instance.)r   QThreadPoolr#   s    r   _preparezThreadPoolSingleton._prepare7  s    !###r   r4   c                j    | j                                          | j                                          dS )zCDestroy singleton instance when :meth:`~Singleton.reset` is called.N)r   clearwaitForDoner#   s    r   _releasezThreadPoolSingleton._release;  s0    !!#####r   N)r   rP  r   )r   r   r   r   rI   rS  rW  r   r   r   rO  rO  2  sG        DDI$ $ $ $$ $ $ $ $ $r   rO  z/typing.Final[singletons.Singleton[TaskManager]]r   z6typing.Final[singletons.Singleton[QtCore.QThreadPool]]r   r   r4   c                 t    t           j                                         t                                           dS )zFinish all background workers.N)r   r   r9  r   r;  r   r   r   teardownrY  E  s/    !!!r   r   )1r   
__future__r   __all__r   r  rR   rc   r  qtpyr    r   CallableAnyr   r/   r   r   r   r   r   r   Enumr   r	   QObjectr   	QRunnabler
   QThreadr   r   ABCMetar   r   r   r   r  r   r   	SingletonrR  rO  SingleInstanceOfr   __annotations__r   rY  r   r   r   <module>rh     su   H G G " " " " " "   


                    
VZ	(?3
?+( ( ( ( (	 ( ( (9 9 9 9 9	 9 9 92 2 2 2 2i 2 2 2,> ,> ,> ,> ,> ,> ,> ,>^: : : : : : : :z       H H H H H H H HV") ") ") ") ")&. ") ") ")Jg g g g g! g g gT" " " " " " " "JC> C> C> C> C> C> C> C>LH H H H HS[ H H H H$5 $5 $5 $5 $5\ $5 $5 $5N' ' ' ' ' ' ' '8Z? Z? Z? Z? Z? Z? Z? Z?z\ \ \ \ \&. \ \ \~       8> > > > >| > > >D$ $ $ $ $*.v/AB $ $ $ <W:;VWb;c;c c c c cFYFYF[F[ [ [ [ [     r   