
    Cj5                    8   % S r SSKJr  SSKrSSKrSSKrSSKJr  SSKJ	r	  SSK
Jr  SSKJrJrJrJrJrJrJrJr  SSKrSSKJr  SS	KJr  \(       a  SS
KJr  SSKJr  \" S5      rS\ S'     " S S\!5      r" " S S\!5      r# " S S\#5      r$ " S S\#5      r% " S S\#5      r&S$S jr' " S S\RP                  5      r)\" S5      r*\\/ \SS\*4   4   \/ \*4   \\*   4   r+S\ S'     " S S 5      r,\RZ                  R]                  S!S"9 " S# S\\*   5      5       r/g)%zx
This module contains the `Worker` class and related objects.

See the guide for how to use [workers](/guide/workers).

    )annotationsN)
ContextVar)Event)	monotonic)TYPE_CHECKING	AwaitableCallable	CoroutineGenericTypeVarUnioncast)	TypeAlias)Message)App)DOMNodeactive_workerzContextVar[Worker]c                      \ rS rSrSrSrg)NoActiveWorker)   zThere is no active worker. N__name__
__module____qualname____firstlineno____doc____static_attributes__r       `/home/rurouni/.local/share/pipx/venvs/strix-agent/lib/python3.13/site-packages/textual/worker.pyr   r   )   s    $r   r   c                      \ rS rSrSrSrg)WorkerError-   zA worker related error.r   Nr   r   r   r    r"   r"   -   s    !r   r"   c                  0   ^  \ rS rSrSrSU 4S jjrSrU =r$ )WorkerFailed1   z4The worker raised an exception and did not complete.c                8   > Xl         [        TU ]	  SU< 35        g )NzWorker raised exception: )errorsuper__init__)selfr(   	__class__s     r    r*   WorkerFailed.__init__4   s    
4UI>?r   )r(   )r(   BaseExceptionreturnNone)r   r   r   r   r   r*   r   __classcell__r,   s   @r    r%   r%   1   s    >@ @r   r%   c                      \ rS rSrSrSrg)DeadlockError9   z)The operation would result in a deadlock.r   Nr   r   r   r    r4   r4   9   s    3r   r4   c                      \ rS rSrSrSrg)WorkerCancelled=   z.The worker was cancelled and did not complete.r   Nr   r   r   r    r7   r7   =   s    8r   r7   Workerc                 `     [         R                  5       $ ! [         a    [        S5      Sef = f)z}Get the currently active worker.

Raises:
    NoActiveWorker: If there is no active worker.

Returns:
    A Worker instance.
z1There is no active worker in this task or thread.N)r   getLookupErrorr   r   r   r    get_current_workerr=   A   s8      "" ?
	s    -c                  4    \ rS rSrSrSr Sr Sr Sr Sr	Sr
g	)
WorkerStateR   z,A description of the worker's current state.               r   N)r   r   r   r   r   PENDINGRUNNING	CANCELLEDERRORSUCCESSr   r   r   r    r?   r?   R   s-    6G1GI3E:G<r   r?   
ResultTyper   WorkTypec                  ,    \ rS rSrSrSS jrSS jrSrg)		_ReprTextl   z-Shim to insert a word into the Worker's repr.c                    Xl         g Ntext)r+   rS   s     r    r*   _ReprText.__init__o   s    	r   c                    U R                   $ rQ   rR   r+   s    r    __repr___ReprText.__repr__r   s    yyr   rR   N)rS   strr/   r0   )r/   rY   )r   r   r   r   r   r*   rW   r   r   r   r    rN   rN   l   s    7r   rN   T)angularc            	         \ rS rSrSr\R                  R                   " S S\SSS95       r	SS	SS
SS.               S$S jjr
S%S jr\S&S j5       r\S'S j5       r\R                  S(S j5       r\S)S j5       r\S)S j5       r\S)S j5       r\S*S j5       r\S+S j5       r\S,S j5       r\S-S j5       r\S.S j5       r S/     S0S jjrS1S2S jjrS3S jrS3S jrS3S jrS4S jr S5     S6S  jjrS7S! jrS3S" jr S#r!g)8r9   v   z>A class to manage concurrent work (either a task or a thread).c                  :   ^  \ rS rSrSrSU 4S jjrSS jrSrU =r$ )Worker.StateChangedz   zThe worker state changed.c                :   > Xl         X l        [        TU ]  5         g)zaInitialize the StateChanged message.

Args:
    worker: The worker object.
    state: New state.
N)workerstater)   r*   )r+   ra   rb   r,   s      r    r*   Worker.StateChanged.__init__~   s     !KJGr   c              #  D   #    U R                   v   U R                  v   g 7frQ   )ra   rb   rV   s    r    __rich_repr__!Worker.StateChanged.__rich_repr__   s     ++**s    )rb   ra   )ra   r9   rb   r?   r/   r0   r/   zrich.repr.Result)	r   r   r   r   r   r*   re   r   r1   r2   s   @r    StateChangedr^   z   s    '			 	r   rh   Fra   )bubble	namespace defaultT)namegroupdescriptionexit_on_errorthreadc                  Xl         X l        X0l        X@l        [	        U5      S::  a  UOUSS S-   U l        X`l        [        5       U l         Xpl	        [        R                  U l        U R                  U l        SU l        SU l        SU l        SU l        [%        5       U l        SU l        SU l        U R                   R-                  U R/                  X R                  5      5        g)a  Initialize a Worker.

Args:
    node: The widget, screen, or App that initiated the work.
    work: A callable, coroutine, or other awaitable object to run in the worker.
    name: Name of the worker (short string to help identify when debugging).
    group: The worker group.
    description: Description of the worker (longer string with more details).
    exit_on_error: Exit the app if the worker raises an error. Set to `False` to suppress exceptions.
    thread: Mark the worker as a thread worker.
i  Nz...r   F)_node_workrm   rn   lenro   rp   r   cancelled_event_thread_workerr?   rF   _staterb   _error_completed_steps_total_steps
_cancelledr   _created_time_result_taskpost_messagerh   )r+   nodeworkrm   rn   ro   rp   rq   s           r    r*   Worker.__init__   s    , 

	
{+t3KUd9Ke9S 	 +&+gA$!))[[
,0%&(, %&[*.*.


 1 1$ DEr   c              #     #    [        U R                  R                  5      v   SU R                  S4v   SU R                  S4v   SU R                  S4v   S[        U R                  S5      S4v   g 7f)	Nrm   rk   rn   rl   ro   progressrA           )rN   rb   rm   rn   ro   roundr   rV   s    r    re   Worker.__rich_repr__   se     

((dii##tzz9,,T--r11%q1366s   A1A3c                    U R                   $ )z(The node where this worker was run from.)rs   rV   s    r    r   Worker.node   s     zzr   c                    U R                   $ )z The current state of the worker.)rx   rV   s    r    rb   Worker.state        {{r   c                    XR                   :g  nXl         U(       a+  U R                  R                  U R                  X5      5        gg)z"Set the state, and send a message.N)rx   rs   r   rh   )r+   rb   changeds      r    rb   r      s:     ;;&JJ##D$5$5d$BC r   c                    U R                   $ )zMHas the work been cancelled?

Note that cancelled work may still be running.
)r|   rV   s    r    is_cancelledWorker.is_cancelled   s     r   c                <    U R                   [        R                  :H  $ )zIs the task running?)rb   r?   rG   rV   s    r    
is_runningWorker.is_running   s     zz[0000r   c                z    U R                   [        R                  [        R                  [        R                  4;   $ )z5Has the task finished (cancelled, error, or success)?)rb   r?   rH   rI   rJ   rV   s    r    is_finishedWorker.is_finished   s5     zz!!
 
 	
r   c                    U R                   $ )zThe number of completed steps.rz   rV   s    r    completed_stepsWorker.completed_steps   s     $$$r   c                    U R                   $ )z4The number of total steps, or None if indeterminate.)r{   rV   s    r    total_stepsWorker.total_steps   s        r   c                    U R                   (       d  g[        S[        SU R                  U R                   -  S-  5      5      $ )zProgress as a percentage.

If the total steps is None, then this will return 0. The percentage will be clamped between 0 and 100.
r   r   d   g      Y@)r{   maxminrz   rV   s    r    r   Worker.progress   s:       1c# 5 58I8I IURSTTr   c                    U R                   $ )z:The result of the worker, or `None` if there is no result.)r~   rV   s    r    resultWorker.result   s     ||r   c                    U R                   $ )zDThe exception raised by the worker, or `None` if there was no error.)ry   rV   s    r    r(   Worker.error  r   r   Nc                n    Ub  U =R                   U-  sl         US:w  a  Uc  SO[        SU5      U l        gg)zUpdate the number of completed steps.

Args:
    completed_steps: The number of completed seps, or `None` to not change.
    total_steps: The total number of steps, `None` for indeterminate, or -1 to leave unchanged.
Nr   )rz   r   r{   )r+   r   r   s      r    updateWorker.update  s>     &!!_4!"(3(;QATD r   c                .    U =R                   U-  sl         g)zUAdvance the number of completed steps.

Args:
    steps: Number of steps to advance.
Nr   )r+   stepss     r    advanceWorker.advance  s     	&r   c                L  ^ ^#    SU 4S jjm    SU4S jjnS	U 4S jjn[         R                  " T R                  5      (       dJ  [        T R                  S5      (       a2  [         R                  " T R                  R                  5      (       a  UnOP[         R
                  " T R                  5      (       a  TnO([        T R                  5      (       a  UnO[        S5      e[        R                  " 5       nUc   eUR                  SUT R                  5      I Sh  vN $  N7f)
z?Run a threaded worker.

Returns:
    Return value of the work.
c                N   >^  SUU 4S jjn[         R                  " U" 5       5      $ )z.Set the active worker and await the awaitable.c                 N   >#    [         R                  T 5        TI S h  vN $  N7frQ   r   set)r+   r   s   r    do_work<Worker._run_threaded.<locals>.run_awaitable.<locals>.do_work&  s     !!$'!z!zs   %#%r/   rK   )asynciorun)r   r   r+   s   ` r    run_awaitable+Worker._run_threaded.<locals>.run_awaitable#  s     " " ;;wy))r   c                   > T" U " 5       5      $ )z*Set the active worker and await coroutine.r   )r   r   s    r    run_coroutine+Worker._run_threaded.<locals>.run_coroutine,  s     !((r   c                <   > [         R                  T5        U " 5       $ )z-Set the active worker, and call the callable.r   )r   r+   s    r    run_callable*Worker._run_threaded.<locals>.run_callable2  s    d#6Mr   funcz*Unsupported attempt to run a thread workerN)r   zAwaitable[ResultType]r/   rK   )r   z/Callable[[], Coroutine[None, None, ResultType]]r/   rK   )r   zCallable[[], ResultType]r/   rK   )inspectiscoroutinefunctionrt   hasattrr   isawaitablecallabler"   r   get_running_looprun_in_executor)r+   r   r   runnerloopr   s   `    @r    _run_threadedWorker._run_threaded  s     	*	)A	)	)	 ''

33tzz6**++DJJOO<<"F  ,,"Fdjj!!!FJKK'')))$

CCCCs   DD$D"D$c                  #    [         R                  " U R                  5      (       dJ  [        U R                  S5      (       aG  [         R                  " U R                  R                  5      (       a  U R                  5       I Sh  vN $ [         R
                  " U R                  5      (       a  U R                  I Sh  vN $ [        U R                  5      (       a  [        S5      e[        S5      e Nm N67f)z=Run an async worker.

Returns:
    Return value of the work.
r   Nz6Request to run a non-async function as an async workerz*Unsupported attempt to run an async worker)r   r   rt   r   r   r   r   r"   rV   s    r    
_run_asyncWorker._run_asyncH  s      ''

33tzz6**++DJJOO<<%%  ,,##djj!!VWWFGG &#s$   BC7C38C7>C5?5C75C7c                   #    U R                   (       a  U R                  5       OU R                  5       I Sh  vN $  N7f)zRun the work.

Implement this method in a subclass, or pass a callable to the constructor.

Returns:
    Return value of the work.
N)rw   r   r   rV   s    r    r   
Worker.runZ  s3      %)$7$7D T__=N
 	
 
s   5><>c           	       #    UR                  5          [        R                  U 5        [        R                  U l        UR                  R                  U 5         U R                  5       I Sh  vN U l	        [        R                  U l        UR                  R                  U 5        SSS5        g NB! [        R                   a@  n[        R                  U l        X l        UR                  R                  U 5         SnANZSnAf[         a  n[        R                   U l        X l        UR                  R                  U S[#        U5      5        SSKJn  UR                  R                  U" 5       5        U R(                  (       a&  [+        U R                  5      nUR-                  U5         SnAGNSnAff = f! , (       d  f       g= f7f)z.Run the worker.

Args:
    app: App instance.
Nfailedr   )	Traceback)_contextr   r   r?   rG   rb   logra   r   r~   rJ   r   CancelledErrorrH   ry   	ExceptionrI   reprrich.tracebackr   rp   r%   _handle_exception)r+   appr(   r   worker_faileds        r    _runWorker._runf  s+     \\^d#$,,DJGGNN4 %%)XXZ/  )00
t$/ ^  0)) %(22
#t$$ 	9(..
#tXtE{;4y{+%%$0$=M))-8	9 ^sl   GAF:B2-B0.	B270F:'	G0B22F76D<F:F7BF2,F:2F77F::
GGc                   ^ ^ T R                   b  g[        R                  T l        [        R
                  " T R                  U5      5      T l         SUU 4S jjnT R                   R                  U5        g)zpStart the worker.

Args:
    app: An app instance.
    done_callback: A callback to call when the task is done.
Nc                   > Tb	  T" T5        gg)zURun the callback.

Called by `Task.add_done_callback`.

Args:
    The worker's task.
Nr   )r   done_callbackr+   s    r    task_done_callback)Worker._start.<locals>.task_done_callback  s     (d# )r   )r   zasyncio.Taskr/   r0   )r   r?   rG   rb   r   create_taskr   add_done_callback)r+   r   r   r   s   ` ` r    _startWorker._start  sX     ::! ((
((38
		$ 		$ 	

$$%78r   c                    SU l         U R                  b  U R                  R                  5         U R                  R	                  5         g)zCancel the task.TN)r|   r   cancelrv   r   rV   s    r    r   Worker.cancel  s5    ::!JJ  "r   c                  #     [         R                  5       U L a  [        S5      e U R                  [
        R                  :X  a  [        S5      eU R                  b   U R                  I Sh  vN   U R                  [
        R                  :X  a$  U R                  c   e[        U R                  5      eU R                  [
        R                  :X  a  [        S5      e[!        SU R"                  5      $ ! [         a     Nf = f N! [        R                   a%  n[
        R                  U l        Xl         SnANSnAff = f7f)zWait for the work to complete.

Raises:
    WorkerFailed: If the Worker raised an exception.
    WorkerCancelled: If the Worker was cancelled before it completed.

Returns:
    The return value of the work.
z7Can't call worker.wait from within the worker function!z2Worker must be started before calling this method.Nz+Worker was cancelled, and did not complete.rK   )r   r;   r4   r<   rb   r?   rF   r"   r   r   r   rH   ry   rI   r%   r7   r   r~   )r+   r(   s     r    waitWorker.wait  s    	  "d*#M  + ::,,,RSS::!$jj   ::***;;***t{{++ZZ;000!"OPPL$,,//#  		 !)) $(22
#$sd   E"C3 7ED -D.D 2BE3
D =E?D  ED D>D94E9D>>E)r|   rz   r}   ry   rs   r~   rx   r   rw   r{   rt   rv   ro   rp   rn   rm   rb   )r   r   r   rL   rm   rY   rn   rY   ro   rY   rp   boolrq   r   r/   r0   rg   )r/   r   )r/   r?   )rb   r?   r/   r0   )r/   r   )r/   int)r/   
int | None)r/   float)r/   zResultType | None)r/   zBaseException | None)Nr   )r   r   r   r   r/   r0   )rA   )r   r   r/   r0   r   )r   r   r/   r0   rQ   )r   r   r   zCallable[[Worker], None] | Noner/   r0   )r/   r0   )"r   r   r   r   r   richr   autor   rh   r*   re   propertyr   rb   setterr   r   r   r   r   r   r   r(   r   r   r   r   r   r   r   r   r   r   r   r   r    r9   r9   v   s   H	YY^^wu  . "*F*F *F
 *F *F *F *F *F 
*FX7     \\D D   1 1 
 
 % % ! ! U U    
 MOU)U?IU	U'*DXH$

%@ JN99'F9	96# 0r   )r/   r9   )0r   
__future__r   r   enumr   contextvarsr   	threadingr   timer   typingr   r   r	   r
   r   r   r   r   	rich.reprr   typing_extensionsr   textual.messager   textual.appr   textual.domr   r   __annotations__r   r   r"   r%   r4   r7   r=   Enumr?   rK   rL   rN   r   r   r9   r   r   r    <module>r     s>   #    "  	 	 	  ' ## %/$?! ? *%Y %") "@; @4K 49k 9"=$)) = \"
 R4z1223R^j) 
 0  P0WZ  P0 P0r   