
    ^j)?                     @    d dl mZ d dlmZ ddlmZmZ  G d d      Zy)    )
ASSERTIONS)PreOrderIter   )	LoopError	TreeErrorc                      e Zd ZdZddgZdZed        Zej                  d        Zd Z	d Z
d	 Zed
        Zed        Zed        Zej                  d        Zej                   d        Zd Zd Zd Zd Zed        Zd Zed        Zed        Zed        Zed        Zed        Zed        Zed        Zed        Zed        Zed        Z ed        Z!d  Z"d! Z#d" Z$d# Z%y$)%LightNodeMixinu	  
    The :any:`LightNodeMixin` behaves identical to :any:`NodeMixin`, but uses `__slots__`.

    There are some minor differences in the object behaviour.
    See slots_ for any details.

    .. _slots: https://docs.python.org/3/reference/datamodel.html#slots

    The only tree relevant information is the `parent` attribute.
    If `None` the :any:`LightNodeMixin` is root node.
    If set to another node, the :any:`LightNodeMixin` becomes the child of it.

    The `children` attribute can be used likewise.
    If `None` the :any:`LightNodeMixin` has no children.
    The `children` attribute can be set to any iterable of :any:`LightNodeMixin` instances.
    These instances become children of the node.

    >>> from anytree import LightNodeMixin, RenderTree
    >>> class MyBaseClass():  # Just an example of a base class
    ...     __slots__ = []
    >>> class MyClass(MyBaseClass, LightNodeMixin):  # Add Node feature
    ...     __slots__ = ['name', 'length', 'width']
    ...     def __init__(self, name, length, width, parent=None, children=None):
    ...         super().__init__()
    ...         self.name = name
    ...         self.length = length
    ...         self.width = width
    ...         self.parent = parent
    ...         if children:
    ...             self.children = children

    Construction via `parent`:

    >>> my0 = MyClass('my0', 0, 0)
    >>> my1 = MyClass('my1', 1, 0, parent=my0)
    >>> my2 = MyClass('my2', 0, 2, parent=my0)

    >>> for pre, _, node in RenderTree(my0):
    ...     treestr = u"%s%s" % (pre, node.name)
    ...     print(treestr.ljust(8), node.length, node.width)
    my0      0 0
    ├── my1  1 0
    └── my2  0 2

    Construction via `children`:

    >>> my0 = MyClass('my0', 0, 0, children=[
    ...     MyClass('my1', 1, 0),
    ...     MyClass('my2', 0, 2),
    ... ])

    >>> for pre, _, node in RenderTree(my0):
    ...     treestr = u"%s%s" % (pre, node.name)
    ...     print(treestr.ljust(8), node.length, node.width)
    my0      0 0
    ├── my1  1 0
    └── my2  0 2

    Both approaches can be mixed:

    >>> my0 = MyClass('my0', 0, 0, children=[
    ...     MyClass('my1', 1, 0),
    ... ])
    >>> my2 = MyClass('my2', 0, 2, parent=my0)

    >>> for pre, _, node in RenderTree(my0):
    ...     treestr = u"%s%s" % (pre, node.name)
    ...     print(treestr.ljust(8), node.length, node.width)
    my0      0 0
    ├── my1  1 0
    └── my2  0 2
    
__children__parent/c                 4    t        | d      r| j                  S y)uD  
        Parent Node.

        On set, the node is detached from any previous parent node and attached
        to the new node.

        >>> from anytree import Node, RenderTree
        >>> udo = Node("Udo")
        >>> marc = Node("Marc")
        >>> lian = Node("Lian", parent=marc)
        >>> print(RenderTree(udo))
        Node('/Udo')
        >>> print(RenderTree(marc))
        Node('/Marc')
        └── Node('/Marc/Lian')

        **Attach**

        >>> marc.parent = udo
        >>> print(RenderTree(udo))
        Node('/Udo')
        └── Node('/Udo/Marc')
            └── Node('/Udo/Marc/Lian')

        **Detach**

        To make a node to a root node, just set this attribute to `None`.

        >>> marc.is_root
        False
        >>> marc.parent = None
        >>> marc.is_root
        True
        _LightNodeMixin__parentN)hasattrr   selfs    T/opt/ringagent/.cad-venv/lib/python3.12/site-packages/anytree/node/lightnodemixin.pyparentzLightNodeMixin.parentU   s    H 423==     c                     t        | d      r| j                  }nd }||ur4| j                  |       | j                  |       | j	                  |       y y )Nr   )r   r   _LightNodeMixin__check_loop_LightNodeMixin__detach_LightNodeMixin__attach)r   valuer   s      r   r   zLightNodeMixin.parent}   sN    423]]FFe$MM&!MM%  r   c                      |J| u rd}t        | fz        t         fd|j                         D              rd}t        | |fz        y y )Nz1Cannot set parent. %r cannot be parent of itself.c              3   &   K   | ]  }|u  
 y wN .0childr   s     r   	<genexpr>z.LightNodeMixin.__check_loop.<locals>.<genexpr>   s     GU5D=G   z&Cannot set parent. %r is parent of %r.)r   anyiter_path_reverse)r   nodemsgs   `  r   __check_loopzLightNodeMixin.__check_loop   sa    t|Itg..Gd.D.D.FGG>tTl 233 H	 r   c                      |q j                  |       |j                  }t        rt         fd|D              sJ d       |D cg c]	  }| us| c}|_        d  _         j                  |       y y c c}w )Nc              3   &   K   | ]  }|u  
 y wr   r   r   s     r   r!   z*LightNodeMixin.__detach.<locals>.<genexpr>   s     EU5D=Er"   Tree is corrupt.)_pre_detach"_LightNodeMixin__children_or_emptyr   r#   _LightNodeMixin__childrenr   _post_detach)r   r   parentchildrenr    s   `   r   __detachzLightNodeMixin.__detach   sz    V$#77NEnEEYGYYE4B X5eSWFW XF DMf%  !Ys   	A6A6c                      |h j                  |       |j                  }t        rt         fd|D              rJ d       |j	                          | _         j                  |       y y )Nc              3   &   K   | ]  }|u  
 y wr   r   r   s     r   r!   z*LightNodeMixin.__attach.<locals>.<genexpr>   s     Iu}Ir"   r*   )_pre_attachr,   r   r#   appendr   _post_attach)r   r   r/   s   `  r   __attachzLightNodeMixin.__attach   sg    V$#77NI.II]K]]I!!$'"DMf% r   c                 @    t        | d      sg | _        | j                  S )Nr-   )r   r-   r   s    r   __children_or_emptyz"LightNodeMixin.__children_or_empty   s    t89 DOr   c                 ,    t        | j                        S )a  
        All child nodes.

        >>> from anytree import Node
        >>> n = Node("n")
        >>> a = Node("a", parent=n)
        >>> b = Node("b", parent=n)
        >>> c = Node("c", parent=n)
        >>> n.children
        (Node('/n/a'), Node('/n/b'), Node('/n/c'))

        Modifying the children attribute modifies the tree.

        **Detach**

        The children attribute can be updated by setting to an iterable.

        >>> n.children = [a, b]
        >>> n.children
        (Node('/n/a'), Node('/n/b'))

        Node `c` is removed from the tree.
        In case of an existing reference, the node `c` does not vanish and is the root of its own tree.

        >>> c
        Node('/c')

        **Attach**

        >>> d = Node("d")
        >>> d
        Node('/d')
        >>> n.children = [a, b, d]
        >>> n.children
        (Node('/n/a'), Node('/n/b'), Node('/n/d'))
        >>> d
        Node('/n/d')

        **Duplicate**

        A node can just be the children once. Duplicates cause a :any:`TreeError`:

        >>> n.children = [a, b, d, a]
        Traceback (most recent call last):
            ...
        anytree.node.exceptions.TreeError: Cannot add node Node('/n/a') multiple times as child.
        )tupler,   r   s    r   childrenzLightNodeMixin.children   s    b T--..r   c                     t               }| D ]3  }t        |      }||vr|j                  |       $d|d}t        |       y )NzCannot add node z multiple times as child.)setidaddr   )r;   seenr    childidr&   s        r   __check_childrenzLightNodeMixin.__check_children   sN    u 	%EiGd"!(	1JKn$	%r   c                 @   t        |      }t        j                  |       | j                  }| `	 | j	                  |       |D ]	  }| |_         | j                  |       t        r$t        | j                        t        |      k(  sJ y y # t        $ r	 || _         w xY wr   )
r:   r	   _LightNodeMixin__check_childrenr;   _pre_attach_childrenr   _post_attach_childrenr   len	Exception)r   r;   old_childrenr    s       r   r;   zLightNodeMixin.children   s     ?''1}}M		%%h/! $#$&&x04==)S]:::  	(DM	s   AB Bc                     | j                   }| j                  |       | j                   D ]	  }d |_         t        rt	        | j                         dk(  sJ | j                  |       y )Nr   )r;   _pre_detach_childrenr   r   rG   _post_detach_children)r   r;   r    s      r   r;   zLightNodeMixin.children  s[    ==!!(+]] 	 EEL	 t}}%***""8,r   c                      y)z(Method call before detaching `children`.Nr   r   r;   s     r   rK   z#LightNodeMixin._pre_detach_children      r   c                      y)z'Method call after detaching `children`.Nr   rN   s     r   rL   z$LightNodeMixin._post_detach_children  rO   r   c                      y)z(Method call before attaching `children`.Nr   rN   s     r   rE   z#LightNodeMixin._pre_attach_children  rO   r   c                      y)z'Method call after attaching `children`.Nr   rN   s     r   rF   z$LightNodeMixin._post_attach_children  rO   r   c                     | j                   S )a  
        Path from root node down to this `Node`.

        >>> from anytree import Node
        >>> udo = Node("Udo")
        >>> marc = Node("Marc", parent=udo)
        >>> lian = Node("Lian", parent=marc)
        >>> udo.path
        (Node('/Udo'),)
        >>> marc.path
        (Node('/Udo'), Node('/Udo/Marc'))
        >>> lian.path
        (Node('/Udo'), Node('/Udo/Marc'), Node('/Udo/Marc/Lian'))
        )_pathr   s    r   pathzLightNodeMixin.path  s      zzr   c              #   <   K   | }|| |j                   }|yyw)ae  
        Iterate up the tree from the current node to the root node.

        >>> from anytree import Node
        >>> udo = Node("Udo")
        >>> marc = Node("Marc", parent=udo)
        >>> lian = Node("Lian", parent=marc)
        >>> for node in udo.iter_path_reverse():
        ...     print(node)
        Node('/Udo')
        >>> for node in marc.iter_path_reverse():
        ...     print(node)
        Node('/Udo/Marc')
        Node('/Udo')
        >>> for node in lian.iter_path_reverse():
        ...     print(node)
        Node('/Udo/Marc/Lian')
        Node('/Udo/Marc')
        Node('/Udo')
        Nr   r   r%   s     r   r$   z LightNodeMixin.iter_path_reverse+  s)     * J;;D s   c                 X    t        t        t        | j                                           S r   )r:   reversedlistr$   r   s    r   rT   zLightNodeMixin._pathE  s     Xd4#9#9#;<=>>r   c                 H    | j                   y| j                   j                  S )at  
        All parent nodes and their parent nodes.

        >>> from anytree import Node
        >>> udo = Node("Udo")
        >>> marc = Node("Marc", parent=udo)
        >>> lian = Node("Lian", parent=marc)
        >>> udo.ancestors
        ()
        >>> marc.ancestors
        (Node('/Udo'),)
        >>> lian.ancestors
        (Node('/Udo'), Node('/Udo/Marc'))
        r   )r   rU   r   s    r   	ancestorszLightNodeMixin.ancestorsI  s!      ;;{{r   c                 0    t        t        |             dd S )aj  
        All child nodes and all their child nodes.

        >>> from anytree import Node
        >>> udo = Node("Udo")
        >>> marc = Node("Marc", parent=udo)
        >>> lian = Node("Lian", parent=marc)
        >>> loui = Node("Loui", parent=marc)
        >>> soe = Node("Soe", parent=lian)
        >>> udo.descendants
        (Node('/Udo/Marc'), Node('/Udo/Marc/Lian'), Node('/Udo/Marc/Lian/Soe'), Node('/Udo/Marc/Loui'))
        >>> marc.descendants
        (Node('/Udo/Marc/Lian'), Node('/Udo/Marc/Lian/Soe'), Node('/Udo/Marc/Loui'))
        >>> lian.descendants
        (Node('/Udo/Marc/Lian/Soe'),)
        r   Nr:   r   r   s    r   descendantszLightNodeMixin.descendants]  s    $ \$'(,,r   c                 T    | }|j                   |j                   }|j                   |S )a>  
        Tree Root Node.

        >>> from anytree import Node
        >>> udo = Node("Udo")
        >>> marc = Node("Marc", parent=udo)
        >>> lian = Node("Lian", parent=marc)
        >>> udo.root
        Node('/Udo')
        >>> marc.root
        Node('/Udo')
        >>> lian.root
        Node('/Udo')
        rW   rX   s     r   rootzLightNodeMixin.rootq  s-      kk%;;D kk%r   c                 ^      j                   }|yt         fd|j                  D              S )a  
        Tuple of nodes with the same parent.

        >>> from anytree import Node
        >>> udo = Node("Udo")
        >>> marc = Node("Marc", parent=udo)
        >>> lian = Node("Lian", parent=marc)
        >>> loui = Node("Loui", parent=marc)
        >>> lazy = Node("Lazy", parent=marc)
        >>> udo.siblings
        ()
        >>> marc.siblings
        ()
        >>> lian.siblings
        (Node('/Udo/Marc/Loui'), Node('/Udo/Marc/Lazy'))
        >>> loui.siblings
        (Node('/Udo/Marc/Lian'), Node('/Udo/Marc/Lazy'))
        r   c              3   ,   K   | ]  }|us|  y wr   r   )r   r%   r   s     r   r!   z*LightNodeMixin.siblings.<locals>.<genexpr>  s     JdT9ITJs   	)r   r:   r;   r   r   s   ` r   siblingszLightNodeMixin.siblings  s+    ( >JfooJJJr   c                 0    t        t        | d             S )a  
        Tuple of all leaf nodes.

        >>> from anytree import Node
        >>> udo = Node("Udo")
        >>> marc = Node("Marc", parent=udo)
        >>> lian = Node("Lian", parent=marc)
        >>> loui = Node("Loui", parent=marc)
        >>> lazy = Node("Lazy", parent=marc)
        >>> udo.leaves
        (Node('/Udo/Marc/Lian'), Node('/Udo/Marc/Loui'), Node('/Udo/Marc/Lazy'))
        >>> marc.leaves
        (Node('/Udo/Marc/Lian'), Node('/Udo/Marc/Loui'), Node('/Udo/Marc/Lazy'))
        c                     | j                   S r   )is_leaf)r%   s    r   <lambda>z'LightNodeMixin.leaves.<locals>.<lambda>  s
    T\\ r   )filter_r_   r   s    r   leaveszLightNodeMixin.leaves  s      \$0IJKKr   c                 2    t        | j                        dk(  S )aI  
        `Node` has no children (External Node).

        >>> from anytree import Node
        >>> udo = Node("Udo")
        >>> marc = Node("Marc", parent=udo)
        >>> lian = Node("Lian", parent=marc)
        >>> udo.is_leaf
        False
        >>> marc.is_leaf
        False
        >>> lian.is_leaf
        True
        r   )rG   r,   r   s    r   ri   zLightNodeMixin.is_leaf  s      4++,11r   c                     | j                   du S )a6  
        `Node` is tree root.

        >>> from anytree import Node
        >>> udo = Node("Udo")
        >>> marc = Node("Marc", parent=udo)
        >>> lian = Node("Lian", parent=marc)
        >>> udo.is_root
        True
        >>> marc.is_root
        False
        >>> lian.is_root
        False
        NrW   r   s    r   is_rootzLightNodeMixin.is_root  s      {{d""r   c                 J    | j                   }|rt        d |D              dz   S y)aI  
        Number of edges on the longest path to a leaf `Node`.

        >>> from anytree import Node
        >>> udo = Node("Udo")
        >>> marc = Node("Marc", parent=udo)
        >>> lian = Node("Lian", parent=marc)
        >>> udo.height
        2
        >>> marc.height
        1
        >>> lian.height
        0
        c              3   4   K   | ]  }|j                     y wr   )height)r   r    s     r   r!   z(LightNodeMixin.height.<locals>.<genexpr>  s     :u||:s   r   r   )r,   maxrN   s     r   rr   zLightNodeMixin.height  s+      ++:::Q>>r   c                 H    t        | j                               D ]  \  }} S )a4  
        Number of edges to the root `Node`.

        >>> from anytree import Node
        >>> udo = Node("Udo")
        >>> marc = Node("Marc", parent=udo)
        >>> lian = Node("Lian", parent=marc)
        >>> udo.depth
        0
        >>> marc.depth
        1
        >>> lian.depth
        2
        )	enumerater$   )r   depth_s      r   rv   zLightNodeMixin.depth  s-    $ "$"8"8":; 	HE1	r   c                 @    t        t        |       d      D ]  \  }} S )a  
        Tree size --- the number of nodes in tree starting at this node.

        >>> from anytree import Node
        >>> udo = Node("Udo")
        >>> marc = Node("Marc", parent=udo)
        >>> lian = Node("Lian", parent=marc)
        >>> loui = Node("Loui", parent=marc)
        >>> soe = Node("Soe", parent=lian)
        >>> udo.size
        5
        >>> marc.size
        4
        >>> lian.size
        2
        >>> loui.size
        1
        r   )ru   r   )r   sizerw   s      r   ry   zLightNodeMixin.size   s+    , !d!3Q7 	GD!	r   c                      y)z+Method call before detaching from `parent`.Nr   re   s     r   r+   zLightNodeMixin._pre_detach  rO   r   c                      y)z*Method call after detaching from `parent`.Nr   re   s     r   r.   zLightNodeMixin._post_detach  rO   r   c                      y)z)Method call before attaching to `parent`.Nr   re   s     r   r3   zLightNodeMixin._pre_attach   rO   r   c                      y)z(Method call after attaching to `parent`.Nr   re   s     r   r5   zLightNodeMixin._post_attach#  rO   r   N)&__name__
__module____qualname____doc__	__slots__	separatorpropertyr   setterr   r   r   r,   r;   staticmethodrD   deleterrK   rL   rE   rF   rU   r$   rT   r]   r`   rb   rf   rl   ri   ro   rr   rv   ry   r+   r.   r3   r5   r   r   r   r	   r	      s   GR z*II% %N ]]! !4&&  
 0/ 0/d % % __ & - -7676  "4 ? ?    & - -&  ( K K0 L L" 2 2" # #"  (  *  2:987r   r	   N)anytree.configr   anytree.iteratorsr   
exceptionsr   r   r	   r   r   r   <module>r      s    % * ,]7 ]7r   