
    `i                     j    d Z ddlmZ  G d de          Z edefi           Z G d de          ZdS )a
  
Part of the astor library for Python AST manipulation.

License: 3-clause BSD

Copyright 2012 (c) Patrick Maupin
Copyright 2013 (c) Berker Peksag

This file contains a TreeWalk class that views a node tree
as a unified whole and allows several modes of traversal.

   )	iter_nodec                       e Zd ZdZd ZdS )MetaFlattenzThis metaclass is used to flatten classes to remove
    class hierarchy.

    This makes it easier to manipulate classes (find
    attributes in a single dict, etc.)

    c                 B   t           f}i }t          |          D ](}||vr"|                    t          |                     )|                    |           |                    dd            |                    dd            t
                              | |||          S )N__dict____weakref__)objectreversedupdatevarspoptype__new__)clstypenamebasesclsdictnewbasesnewdictbases          c/home/jaya/work/projects/VOICE-AGENT/VIET/agent-env/lib/python3.11/site-packages/astor/tree_walk.pyr   zMetaFlatten.__new__   s    9UOO 	+ 	+D8##tDzz***wJ%%%M4(((||GT8W===    N)__name__
__module____qualname____doc__r    r   r   r   r      s-         > > > > >r   r   c                   b    e Zd ZdZd
dZd ZdeeefdZ	e
d             Ze
d             Zd	 ZdS )TreeWalka  The TreeWalk class can be used as a superclass in order
    to walk an AST or similar tree.

    Unlike other treewalkers, this class can walk a tree either
    recursively or non-recursively.  Subclasses can define
    methods with the following signatures::

        def pre_xxx(self):
            pass

        def post_xxx(self):
            pass

        def init_xxx(self):
            pass

    Where 'xxx' is one of:

      - A class name
      - An attribute member name concatenated with '_name'
        For example, 'pre_targets_name' will process nodes
        that are referenced by the name 'targets' in their
        parent's node.
      - An attribute member name concatenated with '_item'
        For example, 'pre_targets_item'  will process nodes
        that are in a list that is the targets attribute
        of some node.

    pre_xxx will process a node before processing any of its subnodes.
    if the return value from pre_xxx evalates to true, then walk
    will not process any of the subnodes.  Those can be manually
    processed, if desired, by calling self.walk(node) on the subnodes
    before returning True.

    post_xxx will process a node after processing all its subnodes.

    init_xxx methods can decorate the class instance with subclass-specific
    information.  A single init_whatever method could be written, but to
    make it easy to keep initialization with use, any number of init_xxx
    methods can be written.  They will be called in alphabetical order.

    Nc                 n    g | _         |                                  ||                     |           d S d S N)	nodestacksetupwalk)selfnodes     r   __init__zTreeWalk.__init__W   s7    

IIdOOOOO r   c                    i x| _         }i x| _        }t          t          t	          |                               D ]}|                    d          r t          | |                       0|                    d          rt          | |          ||dd         <   a|                    d          rt          | |          ||dd         <   dS )zYAll the node-specific handlers are setup at
        object initialization time.

        init_pre_   Npost_   )pre_handlerspost_handlerssortedr   r   
startswithgetattr)r%   r.   r/   r   s       r   r#   zTreeWalk.setup]   s    
 ,.-L-//]4T

++,, 	> 	>Dw'' >#d##%%%%(( >)0t)<)<T!""X&&)) >*1$*=*=d122h'	> 	>r    c                    | j         j        }| j        j        }| j        } ||          }	|j        |j        }}
 |
|| |t          ||dz                       dg            ||          |	k    rL|d         \  }}}}| ||          k    r | ||          j                  p ||dz             }| |             `|| _        || _	         |             |o|d         }|o	|d         |u }|r%|d          ||d                   k    r
 |             |dz   |d         d<   |dk     rL | ||          j                  p ||dz             }|"|| _        || _	         |            r
 |             n4||         \  }} |
|| |t          ||dz                       dg            ||          |	k    JdS dS )zUWalk the tree starting at a given node.

        Maintain a stack of nodes.

        _item_nameN    r   )
r.   getr/   r"   appendr   r   r   cur_nodecur_name)r%   r&   r   listlenr   r.   r/   r"   
emptystackr;   r   subnodesindexhandlercurrentpopstacks                    r   r$   zTreeWalk.walkl   s2    (,*.N	S^^
&	dDD4!@!@AA2FGGGc)nnz))*3B-'D$%H%%(=d)<== 9(=88 ?CEEE $ $			#5	""9wqzT'9 ss72;/?/? ? ?CEEE %	IbM"qyy'<T

(;<< 8'<w77 &$(DM$(DMwyy %e_
ddDD4)H)H$I$I2NOOO7 c)nnz))))))r   c                 V    | j         }t          |          dk     rdS |d         d         S )z+Return the parent node of the current node.   Nr9   r8   r"   r?   r%   r"   s     r   parentzTreeWalk.parent   s0     N	y>>A4}Qr   c                 Z    | j         }t          |          dk     rdS |d         dd         S )z Return the parent node and name.rG   Nr9   rH   rI   s     r   parent_namezTreeWalk.parent_name   s4     N	y>>A4}RaR  r   c                 R   | j         }| j        }|                                }|d         }|d         dz
  }|d         |         \  }}|d         |cxu r|u sn J |d         ||d         |f            |d         }	t          |	t                    r||	|<   dS t          |	||           dS )z<Replace a node after first checking integrity of node stack.r6   r   r9   r8   N)r<   r"   r   
isinstancer>   setattr)
r%   new_noder<   r"   curprevrB   oldnoder   rJ   s
             r   replacezTreeWalk.replace   s    =N	mmoo}R1R1v,,,,W,,,,,s1vxb/4/6,,,afd## 	,$F5MMMFD(+++++r   r!   )r   r   r   r   r'   r#   r>   r?   r   r$   propertyrJ   rL   rT   r   r   r   r   r   +   s        ) )V   > > > !t4 'P 'P 'P 'PR     X  ! ! X!, , , , ,r   r   N)r   	node_utilr   r   r   r	   r   r   r   r   <module>rW      s     !          > > > > >$ > > >, k-&B77H, H, H, H, H,{ H, H, H, H, H,r   