
    AHjF                       d Z ddlmZ ddlmZ ddlmZ ddlmZm	Z	m
Z
mZmZmZmZmZmZmZ ddlZddlmZ ddlmZ dd	lmZmZmZmZmZ erdd
lmZ eZ  ed      Z! edd      Z" G d d      Z# G d de$      Z% G d de%      Z& G d dee!         Z'd"dZ(	 	 	 	 	 	 	 	 	 	 d#dZ)ejT                  jV                   G d dee!                Z, G d de,e!         Z- G d de,e!         Z.dd 	 	 	 	 	 	 	 	 	 	 	 d$d!Z/y)%z_

This module contains the `Reactive` class which implements [reactivity](/guide/reactivity/).
    )annotations)partial)isawaitable)
TYPE_CHECKINGAny	AwaitableCallableClassVarGenericTypeTypeVarcastoverloadN)events)count_parameters)MessageTargetWatchCallbackBothValuesTypeWatchCallbackNewValueTypeWatchCallbackNoArgsTypeWatchCallbackType)DOMNodeReactiveTypeReactableTyper   )boundc                      e Zd ZdZddZy)_Mutatedz*A wrapper to indicate a value was mutated.c                    || _         y N)value)selfr   s     H/root/tools/cai/cai_env/lib/python3.12/site-packages/textual/reactive.py__init__z_Mutated.__init__/   s	    
    N)r   r   returnNone)__name__
__module____qualname____doc__r"    r#   r!   r   r   ,   s
    4r#   r   c                      e Zd ZdZy)ReactiveErrorzBase class for reactive errors.Nr&   r'   r(   r)   r*   r#   r!   r,   r,   3   s    )r#   r,   c                      e Zd ZdZy)TooManyComputesErrorz@Raised when an attribute has public and private compute methods.Nr-   r*   r#   r!   r/   r/   7   s    Jr#   r/   c                       e Zd ZdZddZddZy)
Initializea  Initialize a reactive by calling a method parent object.

    Example:
        ```python
            class InitializeApp(App):

                def get_names(self) -> list[str]:
                    return ["foo", "bar", "baz"]

                # The `names` property will call `get_names` to get its default when first referenced.
                names = reactive(Initialize(get_names))
        ```

    c                    || _         y r   callback)r    r4   s     r!   r"   zInitialize.__init__K   s	     r#   c                $    | j                  |      S r   r3   )r    objs     r!   __call__zInitialize.__call__N   s    }}S!!r#   N)r4   z'Callable[[ReactableType], ReactiveType]r$   r%   )r6   r   r$   r   )r&   r'   r(   r)   r"   r7   r*   r#   r!   r1   r1   ;   s    !"r#   r1   c                   K   d}| d{    | j                  t        j                  t        t        j
                  |                    y7 Bw)z7Coroutine to await an awaitable returned from a watcherTNr3   )post_messager   Callbackr   Reactive_compute)r6   	awaitable_rich_traceback_omits      r!   await_watcherr?   R   s<     
OOV__gh6G6G.MNO s   AAAAc                Z   d}t        |      }| j                         5  |dk(  r t        t        |      ||      }n1|dk(  r t        t        |      |      }n t        t
        |             }t        |      r | j                  t        t        | |             ddd       y# 1 sw Y   yxY w)a  Invoke a watch function.

    Args:
        watcher_object: The object watching for the changes.
        watch_function: A watch function, which may be sync or async.
        old_value: The old value of the attribute.
        value: The new value of the attribute.
    T      N)
r   _contextr   r   r   r   r   	call_nextr   r?   )watcher_objectwatch_function	old_valuer   r>   param_countwatch_results          r!   invoke_watcherrJ   Z   s      ">2K		 	 	" !L4 ;^L5L AJ4 9>J5QLH4 7HJL|$$$~|D  s   A:B!!B*c            	      X   e Zd ZU dZi Zded<   ddddddddd	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 ddZdd	Zedd
       Z	e
dd       ZddZedd       Zedd       ZddZer"e	 	 	 	 	 	 	 	 dd       Ze	 	 	 	 	 	 	 	 dd       Z	 	 	 	 	 	 	 	 d dZd!d"dZd#dZed$d       Zedd       Zy)%r;   ax  Reactive descriptor.

    Args:
        default: A default value or callable that returns a default.
        layout: Perform a layout on change.
        repaint: Perform a repaint on change.
        init: Call watchers on initialize (post mount).
        always_update: Call watchers even when the new value equals the old value.
        compute: Run compute methods when attribute is changed.
        recompose: Compose the widget again when the attribute changes.
        bindings: Refresh bindings when the reactive changes.
        toggle_class: An optional TCSS classname(s) to toggle based on the truthiness of the value.
    zClassVar[dict[str, object]]
_reactivesFTN)layoutrepaintinitalways_updatecompute	recomposebindingstoggle_classc                   || _         || _        || _        || _        || _        || _        || _        || _        |	| _        d | _	        |  y r   )
_default_layout_repaint_init_always_update_run_compute
_recompose	_bindings_toggle_class_owner)
r    defaultrM   rN   rO   rP   rQ   rR   rS   rT   s
             r!   r"   zReactive.__init__   sP      
+##!)26r#   c              #  >  K   d | j                   f d| j                  df d| j                  df d| j                  df d| j                  df d| j
                  df d| j                  df d	| j                  df d
t        | d
d       d f y w)NrM   FrN   TrO   rP   rQ   rR   rS   name)	rV   rW   rX   rY   rZ   r[   r\   r]   getattrr    s    r!   __rich_repr__zReactive.__rich_repr__   s     DMM!!e++,,djj%''t22E99**D004??E11$..%//gdFD1477s   BBc                X    	 t        |d      j                          y# t        $ r Y yw xY w)zaClear any watchers on a given object.

        Args:
            obj: A reactive object.
        
__watchersN)rc   clearAttributeErrorclsr6   s     r!   _clear_watcherszReactive._clear_watchers   s,    	C&,,. 		s    	))c                6    | j                   J | j                   S )z2The owner (class) where the reactive was declared.)r_   rd   s    r!   ownerzReactive.owner   s     {{&&&{{r#   c                   d}d| }t        ||      ryt        || j                  d      }|| j                  r |       }n8| j                  }t        |      rt        |t              r ||      n |       n|}t        |||       | j                  x}* |j                  t        |      g|j                           | j                  r| j                  |||       yy)zInitialized a reactive attribute on an object.

        Args:
            obj: An object with reactive attributes.
            name: Name of attribute.
        T
_reactive_N)hasattrrc   compute_namerY   rV   callable
isinstancer1   setattrr^   	set_classboolsplit_check_watchers)	r    r6   rb   r>   internal_namecompute_methodr`   default_or_callablerT   s	            r!   _initialize_reactivezReactive._initialize_reactive   s      $$TF+3& d&7&7>%$**$&G"&-- /0 ""5zB (,,. )  	]G, ...L;CMM$w-?,*<*<*>?::  dG4 r#   c                p    d}|j                   j                         D ]  \  }}|j                  ||        y)zSet defaults and call any watchers / computes for the first time.

        Args:
            obj: An object with Reactive descriptors
        TN)rL   itemsr}   )rk   r6   r>   rb   reactives        r!   _initialize_objectzReactive._initialize_object   s:      $!nn224 	5ND())#t4	5r#   c                p    t        |di       j                          t        |dg       j                          y)z|Reset reactive structures on object (to avoid reference cycles).

        Args:
            obj: A reactive object.
        rg   
__computesN)rc   rh   rj   s     r!   _reset_objectzReactive._reset_object   s.     	\2&,,.\2&,,.r#   c                >   || _         d| }d| }t        ||      r|n|}t        ||      r	 t        |d      }|j                  |       || _        d| | _        || _        | j                  }t	        |d| |       y # t        $ r g }t	        |d|       Y `w xY w)Ncompute_	_compute_r   rp   	_default_)
r_   rq   rc   ri   ru   appendrb   rz   rr   rV   )r    rn   rb   public_computeprivate_computerr   computesr`   s           r!   __set_name__zReactive.__set_name__   s    #D6*%dV,&uo>ON 	 5,'7"5,7 OOD! 	)$0(--4&)73 " 7|X67s   B BBc                     y r   r*   r    r6   obj_types      r!   __get__zReactive.__get__  s    
 r#   c                     y r   r*   r   s      r!   r   zReactive.__get__  s     &)r#   c                   d}|| S t        |d      s#t        d|j                  j                   d      t        || j                  x}      s| j                  || j                         t        || j                        rSt        ||      } t        || j                               }t        |||       | j                  || j                  |       |S t        ||      S )NTidINode is missing data; Check you are calling super().__init__(...) in the z)() constructor, before getting reactives.)rq   r,   	__class__r&   rz   r}   rb   rr   rc   ru   ry   )r    r6   r   r>   rz   rG   r   s          r!   r   zReactive.__get__"  s    
  $;KsD![\_\i\i\r\r[s  t]  ^  sT-?-??M@%%c49953))*]3I3GC!2!235EC.  dii;L3..r#   c                   d}t        |d      s#t        d|j                  j                   d      t	        |t
              r|j                  }d}| j                  || j                         t        || j                        rt        d| d| j                  d      | j                  }t        ||      }t        |d| d       }t        |      r ||      }t        |d	| d       }t        |      r ||      }| j                  x}	* |j                  t        |      g|	j!                           |s| j"                  s||k7  rt%        || j&                  |       | j)                  |||       | j*                  r| j-                  |       | j.                  r|j1                          | j2                  s| j4                  s| j6                  r3|j9                  | j4                  | j2                  | j6                  
       y y y )NT_idr   z)() constructor, before setting reactives.z
Can't set .z9; reactive attributes with a compute method are read-only
_validate_	validate_)rN   rM   rR   )rq   r,   r   r&   rt   r   r   r}   rb   rr   ri   rc   rs   r^   rv   rw   rx   rZ   ru   rz   ry   r[   r<   r]   refresh_bindingsrW   rX   r\   refresh)
r    r6   r   alwaysr>   rb   current_valueprivate_validate_functionpublic_validate_functionrT   s
             r!   _setzReactive._set<  s   #sE"[\_\i\i\r\r[s  t]  ^  eX&KKEF!!#tyy13))* SE499-/hi  yyT*$+C:dV1Dd$K!-.-e4E#*3)D60BD#I ,-,U3E !...L;CMM$u+=(:(:(<= T((MU,BC++U3   dM:  c"~~$$& ||t}} MM<<"oo   1@ -Cr#   c                ,    d}| j                  ||       y )NT)r   )r    r6   r   r>   s       r!   __set__zReactive.__set__s  s    #		#ur#   c                   d}d| }t        ||      }t        |d| d      }t        |      rt        ||||       t        |d| d      }t        |      rt        ||||       t        |di       j                  |g       }	|	rd|	D 
cg c]  \  }
}|
j                  s|
|f c}}
|	dd |	D ]8  \  }
} |
j
                  |j                  d    5  t        |
|||       ddd       : yyc c}}
w # 1 sw Y   LxY w)zCheck watchers, and call watch methods / computes

        Args:
            obj: The reactable object.
            name: Attribute name.
            old_value: The old (previous) value of the attribute.
        Trp   _watch_Nwatch_rg   )rc   rs   rJ   get_closingprevent_prevent_message_types_stack)rk   r6   rb   rG   r>   rz   r   private_watch_functionpublic_watch_functionwatchers	reactabler4   s               r!   ry   zReactive._check_watchersx  s7     $$TF+]+!(v.>!E*+3 6	5I 'vdV_d C)*3 5y%H 3b155dB? ,4'Ix )) H%HQK
 (0 J#	8&Y&&(H(H(LM J"9h	5IJ JJ J Js   
C*C00C9	c           
     |   d}|j                   j                         |j                  z  D ]`  }	 t        |d|       }t        |d| t        |d| d            } |       }t        |d| |       ||k7  sN| j                  |||       b y# t        $ r$ 	 t        |d|       }n# t        $ r Y Y w xY wY }w xY w)zOInvoke all computes.

        Args:
            obj: Reactable object.
        Tr   r   rp   r   N)rL   keys	_computesrc   ri   ru   ry   )rk   r6   _rich_traceback_guardrQ   r{   r   r   s          r!   r<   zReactive._compute  s     !%~~**,s}}< 	AG!(	.B!C $z'+WSIgY:OQU-VM #$EC:gY/7%##C-@	A " %,SIgY2G%HN%  #s5   B	B;B('B;(	B51B;4B55B;:B;)r`   DReactiveType | Callable[[], ReactiveType] | Initialize[ReactiveType]rM   rw   rN   rw   rO   rw   rP   rw   rQ   rw   rR   rw   rS   rw   rT   
str | Noner$   r%   )r$   zrich.repr.Result)r6   	Reactabler$   r%   )r$   Type[MessageTarget])r6   r   rb   strr$   r%   )r6   objectr$   r%   )rn   r   rb   r   r$   r%   )r    Reactive[ReactiveType]r6   r   r   type[ReactableType]r$   r   )r    r   r6   r%   r   r   r$   r   )r    r   r6   zReactable | Noner   r   r$   z%Reactive[ReactiveType] | ReactiveType)F)r6   r   r   r   r   rw   r$   r%   )r6   r   r   r   r$   r%   )r6   r   rb   r   rG   r   r$   r%   )r&   r'   r(   r)   rL   __annotations__r"   re   classmethodrl   propertyrn   r}   r   r   r   r   r   r   r   r   ry   r<   r*   r#   r!   r;   r;   |   s    /1J+0 ##'U 	
       ! 
2	8 	 	  
 5D 5 5 / /42 		(		 *	 		 
	 
	)(	)/3	)?R	)#	) 
	)/$// &/ 
/	/45n
 !J !JF A Ar#   r;   c                  X     e Zd ZdZdddddddd	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 d fdZ xZS )r   a@  Create a reactive attribute.

    Args:
        default: A default value or callable that returns a default.
        layout: Perform a layout on change.
        repaint: Perform a repaint on change.
        init: Call watchers on initialize (post mount).
        always_update: Call watchers even when the new value equals the old value.
        recompose: Compose the widget again when the attribute changes.
        bindings: Refresh bindings when the reactive changes.
        toggle_class: An optional TCSS classname(s) to toggle based on the truthiness of the value.
    FTNrM   rN   rO   rP   rR   rS   rT   c          
     4    t         	|   ||||||||       y )Nr   superr"   )
r    r`   rM   rN   rO   rP   rR   rS   rT   r   s
            r!   r"   zreactive.__init__  s/     	'% 	 		
r#   )r`   r   rM   rw   rN   rw   rO   rw   rP   rw   rR   rw   rS   rw   rT   r   r$   r%   r&   r'   r(   r)   r"   __classcell__r   s   @r!   r   r     s    " ##'
U
 	

 
 
 
 
 
 !
 

 
r#   r   c                  D     e Zd ZdZ	 	 	 	 d	 	 	 	 	 	 	 	 	 	 	 d fdZ xZS )vara  Create a reactive attribute (with no auto-refresh).

    Args:
        default: A default value or callable that returns a default.
        init: Call watchers on initialize (post mount).
        always_update: Call watchers even when the new value equals the old value.
        bindings: Refresh bindings when the reactive changes.
        toggle_class: An optional TCSS classname(s) to toggle based on the truthiness of the value.
    c           	     2    t         |   |dd||||       y )NF)rM   rN   rO   rP   rS   rT   r   )r    r`   rO   rP   rS   rT   r   s         r!   r"   zvar.__init__  s,     	'% 	 	
r#   )TFFN)r`   r   rO   rw   rP   rw   rS   rw   rT   r   r$   r%   r   r   s   @r!   r   r     sZ     ##'
U
 
 	

 
 !
 

 
r#   r   T)rO   c                   t        |d      st        |di        t        |d      }|j                  |g       }t	        fd|D              ry|rt        ||d      }t        |||       |j                  | f       y)a,  Watch a reactive variable on an object.

    Args:
        node: The node that created the watcher.
        obj: The parent object.
        attribute_name: The attribute to watch.
        callback: A callable to call when the attribute changes.
        init: True to call watcher initialization.
    rg   c              3  .   K   | ]  \  }}|k(    y wr   r*   ).0_callback_from_listr4   s      r!   	<genexpr>z_watch.<locals>.<genexpr>  s     
T.Ca1C8))
Ts   N)rq   ru   rc   
setdefaultanyrJ   r   )noder6   attribute_namer4   rO   r   watcher_listr   s      `    r!   _watchr     s|    " 3%\2&sL)H&&~r:L

T|
TT^T:sHm]Cx()r#   )r6   r   r=   zAwaitable[object]r$   r%   )
rE   r   rF   r   rG   r   r   r   r$   r%   )r   r   r6   r   r   r   r4   r   rO   rw   r$   r%   )0r)   
__future__r   	functoolsr   inspectr   typingr   r   r   r	   r
   r   r   r   r   r   	rich.reprrichtextualr   textual._callbackr   textual._typesr   r   r   r   r   textual.domr   r   r   r   r   	Exceptionr,   r/   r1   r?   rJ   reprautor;   r   r   r   r*   r#   r!   <module>r      s`  
 #       .  #I~&y9 *I *K= K"& ".P%  	
 
D uAw|$ uA uAp	#
x% #
L
(<
  
H *
*	* *  	* * 
*r#   