
    AHjt=                       d Z ddlmZ ddl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 ddlmZ dd	lmZ dd
lmZmZ erddlmZ ddlmZ  G d de      Z  G d de       Z! G d de       Z" G d de       Z# G d de       Z$ edd      Z%	  ed      Z&	 ejN                  jQ                  d       G d dee%                Z)y)a  
This module contains the `DOMQuery` class and related objects.

A DOMQuery is a set of DOM nodes returned by [query][textual.dom.DOMNode.query].

The set of nodes may be further refined with [filter][textual.css.query.DOMQuery.filter] and [exclude][textual.css.query.DOMQuery.exclude].
Additional methods apply actions to all nodes in the query.

!!! info

    If this sounds like JQuery, a (once) popular JS library, it is no coincidence.
    )annotations)TYPE_CHECKINGGenericIterableIteratorTypeVarcastoverloadN)
active_app)AwaitRemove)DeclarationError
TokenErrormatch)SelectorSet)parse_declarationsparse_selectors)DOMNodeWidgetc                      e Zd ZdZy)
QueryErrorz%Base class for a query related error.N__name__
__module____qualname____doc__     I/root/tools/cai/cai_env/lib/python3.12/site-packages/textual/css/query.pyr   r       s    /r   r   c                      e Zd ZdZy)InvalidQueryFormatzQuery did not parse correctly.Nr   r   r   r    r"   r"   $   s    (r   r"   c                      e Zd ZdZy)	NoMatcheszNo nodes matched the query.Nr   r   r   r    r$   r$   (   s    %r   r$   c                      e Zd ZdZy)TooManyMatchesz!Too many nodes matched the query.Nr   r   r   r    r&   r&   ,   s    +r   r&   c                      e Zd ZdZy)	WrongTypez)Query result was not of the correct type.Nr   r   r   r    r(   r(   0   s    3r   r(   	QueryTyper   )bound
ExpectTypeT)angularc                  H   e Zd Zg dZddddd	 	 	 	 	 	 	 	 	 	 	 d+dZed,d       Zed-d       Zd.dZd/d	Z	d0d
Z
d0dZered1d       Zed2d       Zd3dZd4dZd5dZd5dZered6d       Zed7d       Z	 d8	 	 	 d9dZered6d       Zed7d       Z	 d8	 	 	 d9dZered6d       Zed7d       Z	 d8	 	 	 d9dZered0d       Zed:d       Z	 d8	 	 	 d;dZd<dZd=dZd>d Zd>d!Zd>d"Zd?d#Z	 d8	 	 	 d@d$Zdd%d%d&	 	 	 	 	 	 	 dAd'ZdBd(ZdBd)Z	 	 	 	 dC	 	 	 	 	 	 	 	 	 dDd*Z y)EDOMQuery)_node_nodes_filters	_excludes_deepNT)filterexcludedeepparentc                  d}|| _         d| _        |r|j                  j                         ng | _        |r|j                  j                         ng | _        || _        |%	 | j                  j                  t        |             |&	 | j                  j                  t        |             yy# t        $ r t        d|d      w xY w# t        $ r t        d|d      w xY w)aE  Initialize a query object.

        !!! warning

            You won't need to construct this manually, as `DOMQuery` objects are returned by [query][textual.dom.DOMNode.query].

        Args:
            node: A DOM node.
            filter: Query to filter children in the node.
            exclude: Query to exclude children in the node.
            deep: Query should be deep, i.e. recursive.
            parent: The parent query, if this is the result of filtering another query.

        Raises:
            InvalidQueryFormat: If the format of the query is invalid.
        TNzUnable to parse filter z	 as query)
r/   r0   r1   copyr2   r3   appendr   r   r"   )selfnoder4   r5   r6   r7   _rich_traceback_omits          r    __init__zDOMQuery.__init__>   s    2  $
.2&,FOO  "" 	 (.F!!#2 	 
X$$_V%<=
 X%%og&>? 	  X(+B6*I)VWWX  X(+B6*I)VWWXs   !$B. $C
 .C
C#c                    | j                   S )zThe node being queried.)r/   r;   s    r    r<   zDOMQuery.noden   s     zzr   c                   ddl m} | j                  t        | j                  r| j
                  j                  |      n| j
                  j                        }|D cg c]"  t        fd| j                  D              r$ }}|D cg c]"  t        fd| j                  D              s$ }}t        d|      | _        | j                  S c c}w c c}w )zLazily evaluate nodes.r   r   c              3  6   K   | ]  }t        |        y wNr   .0selector_setr<   s     r    	<genexpr>z!DOMQuery.nodes.<locals>.<genexpr>   s     S\u\40S   c              3  6   K   | ]  }t        |        y wrC   r   rD   s     r    rG   z!DOMQuery.nodes.<locals>.<genexpr>   s     X5t4XrH   list[QueryType])textual.widgetr   r0   listr3   r/   walk_childrenallr1   anyr2   r	   )r;   r   initial_nodesr<   nodess      ` r    rQ   zDOMQuery.nodess   s     	*;; 48JJ

((0DJJDUDUM
 *ST]]SS E  "XXX E 
 0%8DK{{
s   'C'Cc                ,    t        | j                        S rC   )lenrQ   r@   s    r    __len__zDOMQuery.__len__   s    4::r   c                ,    t        | j                        S )z#True if non-empty, otherwise False.)boolrQ   r@   s    r    __bool__zDOMQuery.__bool__   s    DJJr   c                ,    t        | j                        S rC   )iterrQ   r@   s    r    __iter__zDOMQuery.__iter__   s    DJJr   c                ,    t        | j                        S rC   )reversedrQ   r@   s    r    __reversed__zDOMQuery.__reversed__   s    

##r   c                     y rC   r   r;   indexs     r    __getitem__zDOMQuery.__getitem__   s    8;r   c                     y rC   r   r_   s     r    ra   zDOMQuery.__getitem__   s    @Cr   c                     | j                   |   S rC   )rQ   r_   s     r    ra   zDOMQuery.__getitem__   s    zz%  r   c              #     K   	 | j                   r&ddj                  d | j                   D              f | j                  r'ddj                  d | j                  D              f y y # t        $ r Y y w xY ww)Nqueryz AND c              3  L   K   | ]  }d j                  d |D                yw),c              3  4   K   | ]  }|j                     y wrC   cssrE   selectors     r    rG   z3DOMQuery.__rich_repr__.<locals>.<genexpr>.<genexpr>         H( H   NjoinrE   	selectorss     r    rG   z)DOMQuery.__rich_repr__.<locals>.<genexpr>   s'      !%  Hi HH!   "$r5   z OR c              3  L   K   | ]  }d j                  d |D                yw)rg   c              3  4   K   | ]  }|j                     y wrC   ri   rk   s     r    rG   z3DOMQuery.__rich_repr__.<locals>.<genexpr>.<genexpr>   rm   rn   Nro   rq   s     r    rG   z)DOMQuery.__rich_repr__.<locals>.<genexpr>   s'       %  Hi HH rs   )r1   rp   r2   AttributeErrorr@   s    r    __rich_repr__zDOMQuery.__rich_repr__   s     	}}LL !)-!   ~~KK  )-      		s)   A9A$A* (A9*	A63A95A66A9c                H    t        | j                  || j                  |       S )zFilter this set by the given CSS selector.

        Args:
            selector: A CSS selector.

        Returns:
            New DOM Query.
        )r4   r6   r7   r.   r<   r3   r;   rl   s     r    r4   zDOMQuery.filter   s%     II	
 	
r   c                H    t        | j                  || j                  |       S )zExclude nodes that match a given selector.

        Args:
            selector: A CSS selector.

        Returns:
            New DOM query.
        )r5   r6   r7   ry   rz   s     r    r5   zDOMQuery.exclude   s%     II	
 	
r   c                     y rC   r   r@   s    r    firstzDOMQuery.first   s    &)r   c                     y rC   r   r;   expect_types     r    r}   zDOMQuery.first   s    FIr   c                    d}| j                   r:| j                   d   }|'t        ||      st        d|j                  d|       |S t	        d| d| j
                        )aW  Get the *first* matching node.

        Args:
            expect_type: Require matched node is of this type,
                or None for any type.

        Raises:
            WrongType: If the wrong type was found.
            NoMatches: If there are no matching nodes in the query.

        Returns:
            The matching Widget.
        Tr   -Query value is the wrong type; expected type , found No nodes match z on )rQ   
isinstancer(   r   r$   r<   )r;   r   r=   r}   s       r    r}   zDOMQuery.first   sx       $::JJqME&!%5#GH\H\G__ghmgno  LodXT$))GHHr   c                     y rC   r   r@   s    r    only_onezDOMQuery.only_one   s    ),r   c                     y rC   r   r   s     r    r   zDOMQuery.only_one   s    ILr   c                    d}|| j                  |      n| j                         }	 | j                  d   }t        d      # t        $ r Y |S w xY w)a  Get the *only* matching node.

        Args:
            expect_type: Require matched node is of this type,
                or None for any type.

        Raises:
            WrongType: If the wrong type was found.
            NoMatches: If no node matches the query.
            TooManyMatches: If there is more than one matching node in the query.

        Returns:
            The matching Widget.
        T   z7Call to only_one resulted in more than one matched node)r}   rQ   r&   
IndexError)r;   r   r=   the_one_s        r    r   zDOMQuery.only_one  si    "  $ (3'>DJJ{#DJJL 		 

1A I   	 		s   A 	AAc                     y rC   r   r@   s    r    lastzDOMQuery.last,  s    %(r   c                     y rC   r   r   s     r    r   zDOMQuery.last/  s    EHr   c                    | j                   st        d| d| j                        | j                   d   }|'t        ||      st	        d|j
                  d|       |S )aV  Get the *last* matching node.

        Args:
            expect_type: Require matched node is of this type,
                or None for any type.

        Raises:
            WrongType: If the wrong type was found.
            NoMatches: If there are no matching nodes in the query.

        Returns:
            The matching Widget.
        r   z on domr   r   )rQ   r$   r<   r   r(   r   )r;   r   r   s      r    r   zDOMQuery.last2  sq      zzodXWTYYMJKKzz"~":dK+H?@T@T?WW_`d_ef  r   c                     y rC   r   r@   s    r    resultszDOMQuery.resultsM  s    25r   c                     y rC   r   )r;   filter_types     r    r   zDOMQuery.resultsP  s    RUr   c              #  Z   K   || E d{    y| D ]  }t        ||      s|  y7 w)a  Get query results, optionally filtered by a given type.

        Args:
            filter_type: A Widget class to filter results,
                or None for no filter.

        Yields:
            Iterator[Widget | ExpectType]: An iterator of Widget instances.
        N)r   )r;   r   r<   s      r    r   zDOMQuery.resultsS  s8      OO dK0J s   +)++c                :    | D ]  } |j                   |g|   | S )zSet the given class name(s) according to a condition.

        Args:
            add: Add the classes if True, otherwise remove them.

        Returns:
            Self.
        )	set_class)r;   addclass_namesr<   s       r    r   zDOMQuery.set_classf  s+      	.DDNN3--	.r   c                    t        |t              r| D ]  }|j                  |        | S t        |      }| D ]  }|j                  |        | S )zSet the classes on nodes to exactly the given set.

        Args:
            classes: A string of space separated classes, or an iterable of class names.

        Returns:
            Self.
        )r   strset_classesrL   )r;   classesr<   r   s       r    r   zDOMQuery.set_classess  s_     gs# *  )*  w-K .  -.r   c                2    | D ]  } |j                   |   | S )z%Add the given class name(s) to nodes.)	add_classr;   r   r<   s      r    r   zDOMQuery.add_class  s$     	)DDNNK(	)r   c                2    | D ]  } |j                   |   | S )z,Remove the given class names from the nodes.)remove_classr   s      r    r   zDOMQuery.remove_class  &     	,DD{+	,r   c                2    | D ]  } |j                   |   | S )z0Toggle the given class names from matched nodes.)toggle_classr   s      r    r   zDOMQuery.toggle_class  r   r   c                v    t        j                         } |j                  | j                  d| j                  iS )zRemove matched nodes from the DOM.

        Returns:
            An awaitable object that waits for the widgets to be removed.
        r7   )r   get_prunerQ   r/   )r;   apps     r    removezDOMQuery.remove  s.     nnszz4::9djj99r   c                >   d}| D ]  } |j                   di |  |B	 t        |d      }| D ]/  }|j                  j                  |       |j                  d       1 | S # t        $ r1}t        |j                  |j                  |j
                        dd}~ww xY w)zjSet styles on matched nodes.

        Args:
            css: CSS declarations to parser, or None.
        TN)
set_styles )	read_from)layoutr   )	r   r   r   nametokenmessage_inline_stylesmergerefresh)r;   rj   update_stylesr=   r<   
new_styleserrors          r    r   zDOMQuery.set_styles  s      $ 	-DDOO,m,	-?Y/?QR
  *##))*5D)*  $ Y&uzz5;;NTXXYs   A" "	B+,BBFrepaintr   	recomposec               <    | D ]  }|j                  |||        | S )zRefresh matched nodes.

        Args:
            repaint: Repaint node(s).
            layout: Layout node(s).
            recompose: Recompose node(s).

        Returns:
            Query for chaining.
        r   )r   )r;   r   r   r   r<   s        r    r   zDOMQuery.refresh  s,      	NDLL9LM	Nr   c                Z    | D ]%  }|j                         s|j                           | S  | S )zlFocus the first matching node that permits focus.

        Returns:
            Query for chaining.
        )allow_focusfocus)r;   r<   s     r    r   zDOMQuery.focus  s7      	D!

		 r   c                    | j                   j                  j                  }|6t        |       }||v r'| j                   j                  j	                  ||       | S )zhBlur the first matching node that is focused.

        Returns:
            Query for chaining.
        )avoiding)r/   screenfocusedrL   _reset_focus)r;   r   rQ   s      r    blurzDOMQuery.blur  sR     **##++"&t*E%

!!..w.Gr   c                ^    | D ]'  }|||_         |||_        |||_        |!||_        ) | S )a  Sets common attributes on matched nodes.

        Args:
            display: Set `display` attribute on nodes, or `None` for no change.
            visible: Set `visible` attribute on nodes, or `None` for no change.
            disabled: Set `disabled` attribute on nodes, or `None` for no change.
            loading: Set `loading` attribute on nodes, or `None` for no change.

        Returns:
            Query for chaining.
        )displayvisibledisabledloading)r;   r   r   r   r   r<   s         r    setzDOMQuery.set  sN    $  	'D"&"&# ("&	' r   )r<   r   r4   
str | Noner5   r   r6   rV   r7   zDOMQuery | NonereturnNone)r   r   )r   rJ   )r   int)r   rV   )r   zIterator[QueryType])r`   r   r   r)   )r`   slicer   rJ   )r`   zint | slicer   zQueryType | list[QueryType])r   zrich.repr.Result)rl   r   r   DOMQuery[QueryType])r   r)   )r   type[ExpectType]r   r+   rC   )r   type[ExpectType] | Noner   zQueryType | ExpectType)r   r   r   zIterator[ExpectType])r   r   r   z Iterator[QueryType | ExpectType])r   rV   r   r   r   r   )r   zstr | Iterable[str]r   r   )r   r   r   r   )r   r   )rj   r   r   r   )r   rV   r   rV   r   rV   r   r   )r   r   )NNNN)
r   bool | Noner   r   r   r   r   r   r   r   )!r   r   r   	__slots__r>   propertyr<   rQ   rT   rW   rZ   r]   r   r
   ra   rw   r4   r5   r}   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r    r.   r.   :   sp   EI """&.X.X 	.X
 .X .X  .X 
.X`    *  $ 	; 
;	C 
C!*
"
  	) 
)	I 
I 6:I2I	I8 	, 
,	L 
L 6:%2%	%N 	( 
(	H 
H 6:2	2 	5 
5	U 
U 6:2	)&&: !%	. "&eu/3HL	"
  $# $#  	
  
r   r.   )*r   
__future__r   typingr   r   r   r   r   r	   r
   	rich.reprrichtextual._contextr   textual.await_remover   textual.css.errorsr   r   textual.css.matchr   textual.css.modelr   textual.css.parser   r   textual.domr   rK   r   	Exceptionr   r"   r$   r&   r(   r)   r+   reprautor.   r   r   r    <module>r      s    # V V V  ' , ; # ) A#%0 0) )&
 &,Z ,4
 4 Kx0	 1\"
 5 Awy! A Ar   