
    e-                     >    d dl mZ d dlmZ  G d d          ZdgZdS )   )util)PrettyPrinterc                   (    e Zd ZdZg Zej        Zed             Z	d Z
ddZed             Zed             Zed	             Zej        d
             Zd Zd Zd Zd Zd Zd Zd Z fdZ fdZd Zd Zd ZddZd Zd Zd Z ddZ!d Z" xZ#S )AttrTreea  
    An AttrTree offers convenient, multi-level attribute access for
    collections of objects. AttrTree objects may also be combined
    together using the update method or merge classmethod. Here is an
    example of adding a ViewableElement to an AttrTree and accessing it:

    >>> t = AttrTree()
    >>> t.Example.Path = 1
    >>> t.Example.Path                             #doctest: +ELLIPSIS
    1
    c                 J    |d         }|D ]}|                     |           |S )z9
        Merge a collection of AttrTree objects.
            )update)clstreesfirsttrees       3lib/python3.11/site-packages/holoviews/core/tree.pymergezAttrTree.merge   s6    
 a 	 	DLL    c                     | j                                         }| j         d         dk    r| j         d         S t          t          |                     t	          |          z   S )z
        The _dir_mode may be set to 'default' or 'user' in which case
        only the child nodes added by the user are listed.
        	_dir_modeuserchildren)__dict__keysdirtypelist)self	dict_keyss     r   __dir__zAttrTree.__dir__    sS    
 M&&((	=%//=,,tDzz??T)__44r   Ndefaultc                    || j         d<   t          |                               |d          | j         d<   g | j         d<   d| j         d<   || j         d<   d}|| j         d	<   i | j         d
<   t          |t                    r|                                n|}|rt          |          n|}|sg n|}|D ]\  }}|                     ||           dS )aW  
        identifier: A string identifier for the current node (if any)
        parent:     The parent node (if any)
        items:      Items as (path, value) pairs to construct
                    (sub)tree down to given leaf values.

        Note that the root node does not have a parent and does not
        require an identifier.
        parentFescape
identifierr   _fixedr   zJNo attribute %r in this AttrTree, and none can be added because fixed=True_fixed_errordataN)r   r   
_sanitizer
isinstancedictitemsr   set_path)r   r)   r"   r   dir_modefixed_errorpathitems           r   __init__zAttrTree.__init__+   s     #)h&*4jj&;&;Ju&;&U&Ul#$&j!"'h%-k"b(3n% "f!+E4!8!8Ce$/U%*U 	& 	&JD$MM$%%%%	& 	&r   c                 4    | }|j         |j         }|j         |S N)r   )r   roots     r   r2   zAttrTree.rootE   s%    k%;D k%r   c                     | j         r3d                    | j         j        t          | j                  g          S | j        r| j        n| j        j        S )z5Returns the path up to the root for the current node..)r   joinr-   strr"   	__class____name__r   s    r   r-   zAttrTree.pathL   sM     ; 	S88T[-s4?/C/CDEEE&*oR4??4>;RRr   c                     | j         d         S )z:If fixed, no new paths can be created via attribute accessr#   r   r9   s    r   fixedzAttrTree.fixedU   s     }X&&r   c                     || j         d<   d S )Nr#   r;   )r   vals     r   r<   zAttrTree.fixedZ   s    "%hr   c                 .   t          |t                    st          d          | j        |j        f}d\  | _        |_        |                                D ]/\  }}|| j        vr|| |<   | |                             |           0|\  | _        |_        dS )zn
        Updated the contents of the current AttrTree with the
        contents of a second AttrTree.
        z+Can only update with another AttrTree type.)FFN)r'   r   	Exceptionr<   r)   r%   r	   )r   otherfixed_statusr"   elements        r   r	   zAttrTree.update_   s    
 %** 	KIJJJ
EK0$2!U[#(;;== 	1 	1J**#*Z  Z ''0000$0!U[[[r   c                      t          |t                    r"t          |                    d                    nt          |          } fd|D             }t	          |          r/t          dd                    d |D                       z            t          |          dk    r;                     |d                   }|	                    |dd         |           dS  
                    |d         |           dS )	z
        Set the given value at the supplied path where path is either
        a tuple of strings or a string in A.B.C format.
        r4   c                 b    g | ]+}t                    j                            |          )|,S  )r   r&   	allowable).0pr   s     r   
<listcomp>z%AttrTree.set_path.<locals>.<listcomp>w   s5    PPPAT$ZZ-B-L-LQ-O-OPaPPPr   zCAttribute strings in path elements cannot be correctly escaped : %s,c              3   4   K   | ]}t          |          V  d S r1   )repr)rH   els     r   	<genexpr>z$AttrTree.set_path.<locals>.<genexpr>z   s(      ?^?^RR?^?^?^?^?^?^r   r   r   N)r'   r6   tuplesplitanyr@   r5   len__getattr__r*   __setattr__)r   r-   r>   
disallowedattrtrees   `    r   r*   zAttrTree.set_pathp   s   
 *4D3)?)?PuTZZ__%%%U4[[PPPPPPP
z?? 	` 57:xx?^?^S]?^?^?^7^7^_ ` ` `t99q==''Q00Hd122h,,,,,T!Wc*****r   c                     |s| S d |D             }|                                  }| j                                        D ]6\  }t          fd|D                       r|                    |           7|S )zN
        Filters the loaded AttrTree using the supplied path_filters.
        c                     g | ];}t          |t                    s"t          |                    d                     n|<S )r4   )r'   rP   rQ   )rH   pfs     r   rJ   z#AttrTree.filter.<locals>.<listcomp>   sQ     8 8 8$& 5?r54I4I  bhhsmm,,,8 8 8r   c                 F    g | ]}t          fd |D                       S )c                     g | ]}|v S rF   rF   )rH   subpathr-   s     r   rJ   z.AttrTree.filter.<locals>.<listcomp>.<listcomp>   s    ;;;WD;;;r   )all)rH   rZ   r-   s     r   rJ   z#AttrTree.filter.<locals>.<listcomp>   s6    TTTC;;;;;;;<<TTTr   )r7   r%   r)   rR   r*   )r   path_filtersnew_attrtreer.   r-   s       @r   filterzAttrTree.filter   s     (D[8 8*68 8 8 ~~'')//++ 	2 	2JD$TTTT|TTTUU 2%%dD111r   c                    |dk    rL| j         v r	| j         = nDfd| j                                         D             }t          |          | _         n
|| j         <   | j        &| j                            | j        fz   |           dS dS )z:
        Propagate the value up to the root node.
        _DELETEc           	      l    g | ]0\  }}t          d  t          |          D                       ,||f1S )c              3   (   K   | ]\  }}||k    V  d S r1   rF   )rH   krI   s      r   rO   z1AttrTree._propagate.<locals>.<listcomp>.<genexpr>   s*      #D#DTQAqD#D#D#D#D#D#Dr   )r^   zip)rH   keyvr-   s      r   rJ   z'AttrTree._propagate.<locals>.<listcomp>   s[     F F Ffc1 ##D#DSd^^#D#D#D D DF#q F F Fr   N)r%   r)   r(   r   
_propagater"   )r   r-   r>   r)   s    `  r   rj   zAttrTree._propagate   s     )ty  IdOOF F F F	0A0A F F F KK		!DIdO;"K""DO#5d#:C@@@@@ #"r   c                    t          |t                    rd|vr|                     ||           dS t          |t                    r?| j        8|                     t          |                    d                    |           dS t          |t
                    r| j        |                     ||           dS t          d          )z
        Set a value at a child node with given identifier. If at a root
        node, multi-level path specifications is allowed (i.e. 'A.B.C'
        format or tuple format) in which case the behaviour matches
        that of set_path.
        r4   Nz5Multi-level item setting only allowed from root node.)r'   r6   rU   r   r*   rP   rQ   r@   )r   r"   r>   s      r   __setitem__zAttrTree.__setitem__   s     j#&& 	U3j+@+@Z-----
C(( 	UT[-@MM%
 0 0 5 566<<<<<
E** 	Ut{/BMM*c*****STTTr   c                 0   t          |t                    r"t          |                    d                    nt          |          }t	          |          dk    r-|d         }|| j        v r| j        |         S t          |          | }|D ]
}||         }|S )z
        For a given non-root node, access a child element by identifier.

        If the node is a root node, you may also access elements using
        either tuple format or the 'A.B.C' string format.
        r4   r   r   )r'   r6   rP   rQ   rS   r   r   KeyErrorr   r"   split_label	path_items       r   __getitem__zAttrTree.__getitem__   s     %Z55MuZ--c22333;@;L;L 	{q  $QJT]**}Z00z***	% 	. 	.J!*-IIr   c                    t          |t                    r"t          |                    d                    nt          |          }t	          |          dk    rs|d         }|| j        v r;| j        |= | j                            | j                            |                     nt          |          | 
                    |d           d S | }|d d         D ]
}||         }||d         = d S )Nr4   r   r   rc   )r'   r6   rP   rQ   rS   r   r   popindexrn   rj   ro   s       r   __delitem__zAttrTree.__delitem__   s    $Z55MuZ--c22333;@;L;L 	{q  $QJT]**M*-!!$-"5"5j"A"ABBBBz***OOK33333I)#2#. 2 2
%j1		+b/***r   c                    | j         d u p| j         j         d u }t          j        |          r | j        r|rt	          | j        |z            t                                          ||           t          j        |          r<|| j        vr| j        	                    |           | 
                    |f|           d S d S r1   )r   r   tree_attributer<   AttributeErrorr$   superrU   r   appendrj   )r   r"   r>   shallowr7   s       r   rU   zAttrTree.__setattr__   s    ;$&D$+*<*Dz** 	Atz 	Ag 	A !2Z!?@@@J,,,z** 	0..$$Z000OOZM3/////	0 	0r   c                 6   	 t                                                    S # t          $ r Y nw xY w                    dt	          |           j        z   df          rt          d d          | j        dk    rt          | j        z            t          fdt	          |           j	        D                       s%t	          |           
                    d          }n}|| j        v r| j        |         S |                    d          s_t          j                  rK| j                            |           | j        d	         }|                     || |
          }|| j        |<   |S t          t	          |           j        d d          )zz
        Access a identifier from the AttrTree or generate a new AttrTree
        with the chosen attribute path.
        ___z
Attribute z not found.Tc              3   B   K   | ]}                     |          V  d S r1   )
startswith)rH   prefixr"   s     r   rO   z'AttrTree.__getattr__.<locals>.<genexpr>   sM       @ @ ((00 @ @ @ @ @ @r   Fr    r   )r"   r   r+   z object has no attribute r4   )r{   rT   rz   r   r   r8   r<   r$   rR   _disabled_prefixesr&   r   r   r   ry   r|   r7   )r   r"   	sanitizedr+   
child_treer7   s    `   r   rT   zAttrTree.__getattr__   s   
	77&&z222 	 	 	D	   #T

(;";T!BCC 	A !Ej!E!E!EFFFZ !2Z!?@@@  @ @ @ @!%d!>@ @ @ @ @ 	#T

--j-GGII"I%%=++ ##C(( 	cT-@-L-L 	cM  +++}[1H9/3h ( H HJ'1DM)$ DJJ$7!a!aT^!a!a!abbbs    % 
22c                 N    t          | j                                                  S r1   )iterr%   valuesr9   s    r   __iter__zAttrTree.__iter__  s    DI$$&&'''r   c                 &    || j         v p|| j        v S r1   )r   r%   )r   names     r   __contains__zAttrTree.__contains__  s    t}$9	(99r   c                 *    t          | j                  S r1   )rS   r%   r9   s    r   __len__zAttrTree.__len__  s    49~~r   c                 P   t          |t                    r"t          |                    d                    nt          |          }t	          |          dk    r#|d         }| j                            ||          S | }|D ]$}||k    s||c S |                    ||          }%|S )zGet a node of the AttrTree using its path string.

        Args:
            identifier: Path string of the node to return
            default: Value to return if no node is found

        Returns:
            The indexed node of the AttrTree
        r4   r   r   )r'   r6   rP   rQ   rS   r   get)r   r"   r   rp   rq   s        r   r   zAttrTree.get  s     %Z55MuZ--c22333;@;L;L 	{q  $QJ=$$Z999	% 	; 	;JG##y'8!j'::IIr   c                 N    t          | j                                                  S )zKeys of nodes in the AttrTree)r   r%   r   r9   s    r   r   zAttrTree.keys5  s    DINN$$%%%r   c                 N    t          | j                                                  S )zKeys and nodes of the AttrTree)r   r%   r)   r9   s    r   r)   zAttrTree.items:  s    DIOO%%&&&r   c                 N    t          | j                                                  S )zNodes of the AttrTree)r   r%   r   r9   s    r   r   zAttrTree.values?  s    DI$$&&'''r   c                 V    || j         v r| |         }|                     |           |S |S )a  Pop a node of the AttrTree using its path string.

        Args:
            identifier: Path string of the node to return
            default: Value to return if no node is found

        Returns:
            The node that was removed from the AttrTree
        )r   rw   )r   r"   r   r.   s       r   ru   zAttrTree.popD  s9     &&
#DZ(((KNr   c                 *    t          j        |           S r1   )r   pprintr9   s    r   __repr__zAttrTree.__repr__V  s    #D)))r   )NNNr   r1   )$r8   
__module____qualname____doc__r   r   sanitize_identifierr&   classmethodr   r   r/   propertyr2   r-   r<   setterr	   r*   ra   rj   rl   rr   rw   rU   rT   r   r   r   r   r   r)   r   ru   r   __classcell__)r7   s   @r   r   r      s       
 
 )J  [	5 	5 	5& & & &4   X S S XS ' ' X' \& & \&1 1 1"+ + +$  &A A A"U U U"  *+ + +$0 0 0 0 0#c #c #c #c #cL( ( (: : :     ,& & &
' ' '
( ( (
   $* * * * * * *r   r   N) r   r   r   r   __all__rF   r   r   <module>r      sg          ! ! ! ! ! !Q* Q* Q* Q* Q* Q* Q* Q*h
 ,r   