
    s
ed                    
   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          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!          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*d*e+d+<    e(            Z,d,e+d-<   dS ).zAIntegration with worker framework to make API calls asynchronous.    )annotations)TaskCanceledErrorCallDuration
TaskStatus
TaskResult
TaskWorker
TaskThreadTaskContextTaskModifierInsertArgumentTaskCancelContextAddCancelContextN)QtCore   )
singletons.c                      e Zd ZdZddZdS )r   z&Task was canceled during its workflow.returnstrc                    dS )z.Prepare string representation of the instance.z'Task was cancelled during its execution selfs    ?lib/python3.11/site-packages/navigator_updater/utils/workers.py__str__zTaskCanceledError.__str__   s    88    Nr   r   )__name__
__module____qualname____doc__r   r   r   r   r   r      s.        009 9 9 9 9 9r   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'   r)   r+   s       r   __init__zCall.__init__-   s1     <D>F2:AEGMr   c                    | j         S )zFunction to call.)r0   r   s    r   r'   zCall.function=        r   typing.Sequence[typing.Any]c                    | j         S )z-Positional arguments to call `function` with.)r1   r   s    r   r)   z	Call.argsB        {r   typing.Mapping[str, typing.Any]c                    | j         S )z*Keyword arguments to call `function` with.)r2   r   s    r   r+   zCall.kwargsG        }r   
typing.Anyc                0     | j         | j        i | j        S )zCExecute stored function with both positional and keyword arguments.r/   r   s    r   __call__zCall.__call__L   s    t=t}===r   NN)r'   r(   r)   r*   r+   r,   r   r-   r   r(   )r   r6   )r   r9   r   r<   )r   r    r!   r"   	__slots__r3   propertyr'   r)   r+   r>   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   r-   c                b    |t           j                                         }|| _        || _        dS )z+Initialize new :class:`~Duration` instance.N)datetimenow_Duration__start_Duration__stop)r   rG   rI   s      r   r3   zDuration.__init__`   s/     =%))++E8=:>r   datetime.datetimec                    | j         S )zStart of the tracked time span.)rM   r   s    r   rG   zDuration.startl        |r   c                    | j         S )zStop of the tracked time span.)rN   r   s    r   rI   zDuration.stopq   r8   r   typing.Optional[float]c                V    | j         dS | j         | j        z
                                  S )z*Total number of seconds in this time span.N)rN   rM   total_secondsr   s    r   totalzDuration.totalv   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)rN   	TypeErrorrK   rL   )r   rI   s     r   finishzDuration.finish}   s?     ;"8999<$((**Dr   r?   )rG   rH   rI   rH   r   r-   )r   rO   )r   rH   )r   rS   N)rI   rH   r   r-   )r   r    r!   r"   rB   r3   rC   rG   rI   rV   rY   r   r   r   r   r   Q   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   resultr<   durationtyping.Optional[Duration]r   r-   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   rY   r   r_   r   
isinstancerX   r`   BaseException_TaskResult__call_TaskResult__status_TaskResult__result_TaskResult__duration)r   rf   rg   rh   ri   s        r   r3   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.)rn   r   s    r   rf   zTaskResult.call   r8   r   r   c                    | j         S )zTask execution duration.)rq   r   s    r   ri   zTaskResult.duration   r5   r   typing.Optional[BaseException]c                >    | j         t          j        k    rdS | j        S )z+Exception, that was raised by the function.Nro   r   r^   rp   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.
        rv   r   s    r   rh   zTaskResult.result   s$     =J000= mr   c                    | j         S )z!Resulted status of the execution.)ro   r   s    r   rg   zTaskResult.status   r;   r   r?   )
rf   r   rg   r   rh   r<   ri   rj   r   r-   r   r   )r   r   )r   rt   rA   )r   r   )r   r    r!   r"   rB   r3   rC   rf   ri   rw   rh   rg   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 it's execution.

    .. py:attribute:: sig_succeeded

        Task finished its execution successfully.

    .. py:attribute:: sig_canceled

        Task was canceled during it's 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.
    Nrf   r   canceltyping.Optional[ACTION]r   r-   c                    t                                                       t                      | _        || _        || _        d| _        d| _        dS )z-Initialize new :class:`~TaskWorker` instance.FN)superr3   r|   _TaskWorker__signals_TaskWorker__call_TaskWorker__cancel_TaskWorker__canceled_TaskWorker__result)r   rf   r   	__class__s      r   r3   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   rf   zTaskWorker.call  r8   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   rh   zTaskWorker.result(  r;   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   rX   r   r   r   s    r   r   zTaskWorker.cancel-  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)rf   rg   rh   ri   )MANAGERinstance	_registerr   r~   emitr   r   r   r   r   r^   r_   	Exceptionr`   rY   r   r   r   r   r   r   )r   ri   responserg   rw   s        r   runzTaskWorker.run9  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   rG   r   s    r   rG   zTaskWorker.starte  s    ""4(((((r   'TaskThread'c                N    t          |           }|                                 |S )z)Start a manageable thread with this task.)parent)r
   rG   )r   rh   s     r   threadzTaskWorker.threadi  s$    't444r   rZ   )rf   r   r   r   r   r-   rz   )r   r|   r   r   r   r-   )r   r   )r   r    r!   r"   r3   rC   rf   r   rh   r   r   rG   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   r-   c                V    t                                                       || _        dS )z-Initialize new :class:`~TaskThread` instance.N)r   r3   _TaskThread__parent)r   r   r   s     r   r3   zTaskThread.__init__w  s$    28r   r   c                    | j         j        S r   )r   rf   r   s    r   rf   zTaskThread.call|  s     }!!r   r   c                    | j         j        S r   )r   rh   r   s    r   rh   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   r-   rz   r   r   )r   r    r!   r"   r3   rC   rf   rh   r   r   r   r   s   @r   r
   r
   p  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(   r)   ,typing.Optional[typing.Iterable[typing.Any]]r+   r,   r   r   r   r-   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'   r)   r+   r   s        r   r3   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   r)   zTaskContext.args  r8   r   r   c                D    t          | j        | j        | j                  S )z8New :class:`~Call` signature from the collected context.r'   r)   r+   )r   r'   r)   r+   r   s    r   rf   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  r;   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  r5   r   typing.Dict[str, typing.Any]c                    | j         S )zKCollection of keyword arguments to call :attr:`~TaskContext.function` with.)r   r   s    r   r+   zTaskContext.kwargs  r;   r   r	   c                8    t          | j        | j                  S )z4New :class:`~TaskWorker` from the collected context.)rf   r   )r	   rf   r   r   s    r   workerzTaskContext.worker  s     ty====r   )NNN)
r'   r(   r)   r   r+   r,   r   r   r   r-   )r   r   rz   )r   r   )r   r   r   r-   r@   )r   r   )r   r	   )r   r    r!   r"   rB   r3   rC   r)   rf   r   setterr'   r+   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   r-   c                    dS z9Apply modification to the :class:`~TaskContext` instance.Nr   r   r   s     r   applyzTaskModifier.apply  s      r   Nr   r   r   r-   )r   r    r!   r"   rB   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   r<   targettyping.Union[int, str]r   r-   c                "    || _         || _        dS )z1Initialize new :class:`~InsertArgument` instance.N)_InsertArgument__value_InsertArgument__target)r   r   r   s      r   r3   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  rQ   r   r   r   c                    t          | j        t                    r'|j                            | j        | j                   dS | j        |j        | j        <   dS r   )rl   r   intr)   insertr   r+   r   s     r   r   zInsertArgument.apply  sN    dk3'' 	5LTZ88888*.*GN4;'''r   N)r   )r   r<   r   r   r   r-   )r   r   rA   r   )
r   r    r!   r"   rB   r3   rC   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                  t    e Zd ZdZ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$   __modifiersr)   $typing.Union[FUNCTION, TaskModifier]r   r-   c                    d}g }|D ]@}t          |t                    r|                    |           -||}2t          d          || _        t          |          | _        dS )z'Initialize new :class:`~Task` instance.Nz=Task arguments must be TaskModifiers and at most one function)rl   r   appendrX   _Task__functiontuple_Task__modifiers)r   r)   r'   	modifiersargs        r   r3   zTask.__init__%  s    .2/1	  	a 	aC#|,, a  %%%%! _```5=JOPYJZJZr   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.function6  s#     ?" !?@@@r   typing.Tuple[TaskModifier, ...]c                    | j         S )z4Collection of modifiers for a :class:`~TaskContext`.)r   r   s    r   r   zTask.modifiers=  s     r   Nr   r+   r,   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   r)   r+   rh   modifiers        r   	__contextzTask.__contextB  sI     -8UYbh,i,i,i 	+ 	+HNN6N****r   r<   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   r)   r+   )r   lenrX   _Task__contextrf   r   r)   r+   s      r   r>   zTask.__call__N  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.workere  s    ~~4~77>>r   )r)   r   r   r-   r@   )r   r   r?   )r)   r   r+   r,   r   r   )r)   r<   r+   r<   r   r<   )r   r<   r   r<   r   r   )r)   r<   r+   r<   r   r	   )r   r    r!   r"   rB   r3   rC   r'   r   r   r>   r   r   r   r   r   r   r     s          .I[ [ [ ["    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   r-   c                |    t                                                       d| _        t                      | _        dS )z.Initialize new :class:`~TaskManager` instance.FN)r   r3   _TaskManager__cancelset_TaskManager__workers)r   r   s    r   r3   zTaskManager.__init__u  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suppressrX   r   r~   r   r   r   s     r   rE   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 rZ   )_TaskManager__succeeded_r   r   s    r   <lambda>z'TaskManager._register.<locals>.<lambda>  s    t7G7G7O7O r   c                .                                   S rZ   )_TaskManager__canceledr  s    r   r  z'TaskManager._register.<locals>.<lambda>  s    doof6M6M r   c                .                                   S rZ   )_TaskManager__failedr  s    r   r  z'TaskManager._register.<locals>.<lambda>  s    DMM&4I4I r   c                .                                   S rZ   )_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  rX   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   r-   )r   r    r!   r"   r   r}   r  r	   r~   r   r   r   r   r3   r  r  r  r  r  r   r!  r#  r   r   s   @r   r   r   j  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   r-   c                    || _         dS )z0Initialize new :class:`~CancelContext` instance.N_CancelContext__canceled)r   r%  s     r   r3   zCancelContext.__init__  s     (r   c                    | j         S )zTask was canceled.r(  r   s    r   r%  zCancelContext.canceled  r5   r   c                0    | j         rt                      dS )z-Break execution of a task if it was canceled.N)r)  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   r-   )r   r&  r   )
r   r    r!   r"   rB   r3   rC   r%  r,  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   r-   c                    || _         dS )z3Initialize new :class:`~AddCancelContext` instance.N_AddCancelContext__argument)r   r0  s     r   r3   zAddCancelContext.__init__  s    -5r   c                    | j         S )z4Name of the argument to which context will be added.r2  r   s    r   r0  zAddCancelContext.argument  r5   r   r   c                    | j         |j        vrt                      |j        | j         <   |j        | j                  j        |_        dS r   )r0  r+   r   r   r   s     r   r   zAddCancelContext.apply   s:    =..,9OOGN4=) 6=r   Nr   )r0  r   r   r-   r   r   )	r   r    r!   r"   rB   r3   rC   r0  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._prepare  s    !###r   r-   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   r8  r   )r   r    r!   r"   rB   r;  r?  r   r   r   r7  r7    sG        DDI$ $ $ $$ $ $ $ $ $r   r7  z/typing.Final[singletons.Singleton[TaskManager]]r   z6typing.Final[singletons.Singleton[QtCore.QThreadPool]]r   )-r"   
__future__r   __all__r   r  rK   r\   typingqtpyr    r   CallableAnyACTIONr(   r   r   r   r   Enumr   r   QObjectr|   	QRunnabler	   QThreadr
   r   ABCMetar   r   r   r   r   r   	Singletonr:  r7  SingleInstanceOfr   __annotations__r   r   r   r   <module>rP     s   H G G " " " " " "   


                    
VZ	(?3
?+9 9 9 9 9	 9 9 9,> ,> ,> ,> ,> ,> ,> ,>^: : : : : : : :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 $5XR? R? R? R? R? R? R? R?j\ \ \ \ \&. \ \ \~       8> > > > >| > > >D$ $ $ $ $*.v/AB $ $ $ <W:;VWb;c;c c c c cFYFYF[F[ [ [ [ [ [ [r   