
    d}                         d Z ddlZddlZddlmZmZ ddlmZmZm	Z	m
Z
 ddlmZmZmZ ddlmZ ddlmZ dd	lmZmZ d
Z	 d Z G d de          Z G d de          Z G d d          ZdS )zPyTables nodes.    N   )class_name_dictclass_id_dict)ClosedNodeError	NodeErrorUndoRedoWarningPerformanceWarning)	join_path
split_pathisvisiblepath)lazyattr)move_to_shadow)AttributeSetNotLoggedAttributeSetreStructuredTextc                 F     t          j                    fd            }|S )zDecorate string representation method to handle closed nodes.

    If the node is closed, a string like this is returned::

      <closed MODULE.CLASS at ADDRESS>

    instead of calling `oldmethod` and returning its result.

    c                     | j         s.d| j        j         d| j        j         dt	          |           ddS  |           S )Nz<closed .z at 0xx>)	_v_isopen	__class__
__module____name__id)self	oldmethods    +lib/python3.11/site-packages/tables/node.py	newmethodz_closedrepr.<locals>.newmethod   sm    ~ 	EDt~8 D D~.D D68hhCD D D Ey    )	functoolswraps)r   r   s   ` r   _closedreprr#      s;     _Y      r    c                   (     e Zd ZdZd Z fdZ xZS )MetaNodea  Node metaclass.

    This metaclass ensures that their instance classes get registered
    into several dictionaries (namely the `tables.utils.class_name_dict`
    class name dictionary and the `tables.utils.class_id_dict` class
    identifier dictionary).

    It also adds sanity checks to some methods:

      * Check that the node is open when calling string representation
        and provide a default string if so.

    c                 ~    dD ]}||v rt          ||                   ||<   t                              | |||          S )N)__str____repr__)r#   type__new__)mcsnamebasesdict_mnames        r   r*   zMetaNode.__new__7   sH    , 	9 	9E~~*5<88e||Cue444r    c                     t                                          |||           | t          | j        <   t	          | dd           }|+|D ]}t	          |dd           }||k    r d S | t
          |<   d S d S )N
_c_classid)super__init__r   r   getattrr   )clsr,   r-   r.   cidbasepcidr   s          r   r3   zMetaNode.__init__?   s    ue,,, ),% c<..? ) )t\4883;;EE  &)c""" ?r    )r   r   __qualname____doc__r*   r3   __classcell__)r   s   @r   r%   r%   (   sQ         5 5 5) ) ) ) ) ) ) ) )r    r%   c                   @   e Zd ZdZeZd Z ee          Ze	d             Z
d Zd Z eee          ZdZ	 d(dZd	 Zd
 Zd Zd Zd Zd Zd Zd Zd Zd Zd Zd Zd Zd)dZd Zd Zd*dZ 	 	 d+dZ!d Z"d(dZ#d Z$	 	 d,dZ%d  Z&d! Z'd" Z(d# Z)d$ Z*d% Z+d& Z,d' Z-dS )-Nodea  Abstract base class for all PyTables nodes.

    This is the base class for *all* nodes in a PyTables hierarchy. It is an
    abstract class, i.e. it may not be directly instantiated; however, every
    node in the hierarchy is an instance of this class.

    A PyTables node is always hosted in a PyTables *file*, under a *parent
    group*, at a certain *depth* in the node hierarchy. A node knows its own
    *name* in the parent group and its own *path name* in the file.

    All the previous information is location-dependent, i.e. it may change when
    moving or renaming a node in the hierarchy. A node also has
    location-independent information, such as its *HDF5 object identifier* and
    its *attribute set*.

    This class gathers the operations and attributes (both location-dependent
    and independent) which are common to all PyTables nodes, whatever their
    type is. Nonetheless, due to natural naming restrictions, the names of all
    of these members start with a reserved prefix (see the Group class
    in :ref:`GroupClassDescr`).

    Sub-classes with no children (e.g. *leaf nodes*) may define new methods,
    attributes and properties to avoid natural naming restrictions. For
    instance, _v_attrs may be shortened to attrs and _f_rename to
    rename. However, the original methods and attributes should still be
    available.

    .. rubric:: Node attributes

    .. attribute:: _v_depth

        The depth of this node in the tree (an non-negative integer value).

    .. attribute:: _v_file

        The hosting File instance (see :ref:`FileClassDescr`).

    .. attribute:: _v_name

        The name of this node in its parent group (a string).

    .. attribute:: _v_pathname

        The path of this node in the tree (a string).

    .. attribute:: _v_objectid

        A node identifier (may change from run to run).

        .. versionchanged:: 3.0
           The *_v_objectID* attribute has been renamed into *_v_object_id*.

    c                 d    t          | j                  \  }}| j                            |          S )z"The parent :class:`Group` instance)r   _v_pathname_v_file	_get_node)r   
parentpathnodenames      r   _g_getparentzNode._g_getparent   s-    !+D,<!=!=X|%%j111r    c                 ,    |                      |           S )zThe associated `AttributeSet` instance.

        See Also
        --------
        tables.attributeset.AttributeSet : container for the HDF5 attributes

        )_AttributeSetr   s    r   _v_attrszNode._v_attrs   s     !!$'''r    c                 H    t          | j        d          r| j        j        S dS )z<A description of this node. A shorthand for TITLE attribute.TITLE )hasattrrH   rJ   rG   s    r   _g_gettitlezNode._g_gettitle   s&    4='** 	=&&2r    c                     || j         _        d S N)rH   rJ   )r   titles     r   _g_settitlezNode._g_settitle   s    #r    FTc                 b   t          |t          d                   r|                                }d | _        	 d| _        	 d | _        	 d | _        	 d | _        	 |j        j        d         | _	        	 d| _
        	 d | _        	 | j        x}}|                     |           |                                 |j        }|r|                                 |r|                    | ||           |                     ||           	 |                     ||d           |r|                                 | _        n|                                 | _        |r*|r(|                                r|                                  |                                  d S # t0          $ r |                                   w xY w)NSoftLinkFMAX_TREE_DEPTHTinit)
isinstancer   dereferencer@   r   r?   _v_name_v_depthparams_v_maxtreedepth_v__deleting_v_objectid_v_new_g_check_group_g_check_open_check_writable
_g_refnode_g_set_location_g_new	_g_create_g_openis_undo_enabled_g_log_create_g_post_init_hook	Exception_f_close)r   
parentnoder,   _logvalidatenewfile_s          r   r3   zNode.__init__   s   
 j/*"=>> 	2#//11JD/;C	  *189IJ	 "(	 $3 	J'''  """"  	$!!###  	8 !!$h777Z...	 KK
DtK444 2#'>>#3#3  #'<<>>   %t % 5 5 7 7 %""$$$ ""$$$$$ 	 	 	
 MMOOO	s   ?BF  F.c                 F    | j                             d| j                   d S )NCREATEr@   rn   r?   rG   s    r   ri   zNode._g_log_create  s#    (D$455555r    c                    | j         sd S d| _        	 | j        j        }|                    | d           | j         rd| _        |                                  d S d S # | j         rd| _        |                                  w w xY w)NTF)check_unregistered)r   r]   r@   _node_manager	drop_noderl   )r   node_managers     r   __del__zNode.__del__  s    $ ~ 	F 		 <5L""4E"BBB
 ~  $(!   t~  $(! s   #A %B c                     dS )z*Code to be called before killing the node.N rG   s    r   _g_pre_kill_hookzNode._g_pre_kill_hook1      r    c                     t           )z8Create a new HDF5 node and return its object identifier.NotImplementedErrorrG   s    r   rf   zNode._g_create5      !!r    c                     t           )z<Open an existing HDF5 node and return its object identifier.r   rG   s    r   rg   zNode._g_open9  r   r    c                 ^    | j         st          d          | j        j        s
J d            dS )zeCheck that the node is open.

        If the node is closed, a `ClosedNodeError` is raised.

        zthe node object is closedz#found an open node in a closed fileN)r   r   r@   isopenrG   s    r   ra   zNode._g_check_open=  s=     ~ 	?!"=>>>|"II$IIIIIIr    c                    |j         }|j        }|| _         d| _        |j        }|                    |          r}|dk    sJ |dk    r|| _        n|t          |          d         | _        t          |          \  }| _        |	                    d          |	                    d          z
  dz   | _        n+|| _        t          |j        |          | _        |dz   | _        || j        k    r)t          j        d| j        | j        fz  t                     | j        dk    r"|j                            | | j                   dS dS )a  Set location-dependent attributes.

        Sets the location-dependent attributes of this node to reflect
        that it is placed under the specified `parentnode`, with the
        specified `name`.

        This also triggers the insertion of file references to this
        node.  If the maximum recommended tree depth is exceeded, a
        `PerformanceWarning` is issued.

        Tr   /Nr   znode ``%s`` is exceeding the recommended maximum depth (%d);be ready to see PyTables asking for *lots* of memory and possibly slow I/O)r@   rZ   r   root_uep
startswithr?   lenr   rY   countr
   r\   warningswarnr	   rw   
cache_node)r   rm   r,   rq   parentdepthr   _s          r   rd   zNode._g_set_locationH  s]    " )>??8$$ 	,!####3#'  #'H#7 (..OAt| JJsOOhnnS.A.AAAEDMM  DL()?FFD'!ODM $...M N "-t/CDE -	. . . s""**41ABBBBB #"r    c                 L   | j         }t          || j                  }|                    d          }|| _         || _        || j        k    r#t          j        d| j        fz  t                     | j	        j
        }|                    ||           |                                  dS )a  Update location-dependent attributes.

        Updates location data when an ancestor node has changed its
        location in the hierarchy to `newparentpath`.  In fact, this
        method is expected to be called by an ancestor of this node.

        This also triggers the update of file references to this node.
        If the maximum recommended node depth is exceeded, a
        `PerformanceWarning` is issued.  This warning is assured to be
        unique.

        r   zmoved descendent node is exceeding the recommended maximum depth (%d);be ready to see PyTables asking for *lots* of memory and possibly slow I/ON)r?   r
   rY   r   rZ   r\   r   r   r	   r@   rw   rename_node_g_update_dependent)r   newparentpatholdpathnewpathnewdepthry   s         r   _g_update_locationzNode._g_update_locationv  s     "M4<88==%%"  d***M N "134 6HI I I
 |1  '222 	  """""r    c                     | j         j        }| j        }| j        s0|                    |           |j                            |d           d| _         d| _        d| _        d| _        d| _	        dS )zwClear location-dependent attributes.

        This also triggers the removal of file references to this node.

        NF)
r@   rw   r?   r]   drop_from_cacheregistrypopr   rY   rZ   )r   ry   pathnames      r   _g_del_locationzNode._g_del_location  sx     |1#  	6((222 !%%h555r    c                     dS )z?Code to be run after node creation and before creation logging.Nr|   rG   s    r   rj   zNode._g_post_init_hook  r~   r    c                 P    d| j         v r| j                            |            dS dS )zUpdate dependent objects after a location change.

        All dependent objects (but not nodes!) referencing this node
        must be updated here.

        rH   N)__dict__rH   _g_update_node_locationrG   s    r   r   zNode._g_update_dependent  s5     &&M11$77777 '&r    c                     | j         sdS | j        }d|v r| j                                         |                                  |                                 d| _         dS )ac  Close this node in the tree.

        This releases all resources held by the node, so it should not
        be used again.  On nodes with data, it may be flushed to disk.

        You should not need to close nodes manually because they are
        automatically opened/closed when they are loaded/evicted from
        the integrated LRU cache.

        NrH   F)r   r   rH   _g_closer   clear)r   myDicts     r   rl   zNode._f_close  sk    ( ~ 	F M""$$$ 	 	 r    c                     | j         }|                    | j                   |                                  |                     |           dS )a|  Remove this node from the hierarchy.

        If the node has children, recursive removal must be stated by
        giving `recursive` a true value; otherwise, a `NodeError` will
        be raised.

        If `force` is set to true, the node will be removed no matter it
        has children or not (useful for deleting hard links).

        It does not log the change.

        N)	_v_parent_g_unrefnoderY   rl   	_g_delete)r   	recursiveforceparents       r   	_g_removezNode._g_remove  sH     DL))) 	vr    c                     |                                   | j        }|                                 |                                r|                     ||           dS |                     ||           dS )a]  Remove this node from the hierarchy.

        If the node has children, recursive removal must be stated by giving
        recursive a true value; otherwise, a NodeError will be raised.

        If the node is a link to a Group object, and you are sure that you want
        to delete it, you can do this by setting the force flag to true.

        N)ra   r@   rb   rh   _g_remove_and_logr   )r   r   r   rq   s       r   	_f_removezNode._f_remove  sy     	  "" 	-""9e44444NN9e,,,,,r    c                 n    | j         }| j        }|                    d|           t          ||           d S )NREMOVE)r@   r?   rn   r   )r   r   r   rq   oldpathnames        r   r   zNode._g_remove_and_log  s;    &

8[)))uk*****r    c                    | j         }| j        }| j        }|                    | |           |                    |           |                                  |                     ||           |                     || j        d           | j                             |j	        ||j	        ||| j                   | 
                                 dS )zMove this node in the hierarchy.

        Moves the node into the given `newparent`, with the given
        `newname`.

        It does not log the change.

        FrU   N)r   rY   r?   rc   r   r   rd   re   _g_move_noder^   r   )r   	newparentnewname	oldparentoldnamer   s         r   _g_movezNode._g_move  s     N	,& 	T7+++w''' 	Y000 	It|%888 	##I$97$-$97$/1A	C 	C 	C
 	  """""r    c                 4    |                      ||           dS )zRename this node in place.

        Changes the name of a node to *newname* (a string).  If a node with the
        same newname already exists and overwrite is true, recursively remove
        it before renaming.

        )r   	overwriteN)_f_move)r   r   r   s      r   	_f_renamezNode._f_rename:  s      	W	:::::r    Nc                    |                                   | j        }| j        }| j        }||t	          d          ||}||}t          |d          r|j        }|j        }	n't          |d          r|}|}	nt          d|          ||urt	          d          |                                 |j        }
|	|
k    r||k    rdS | 	                    |	           |
                    ||          }|                     |           |                     |||           | j        }|                     ||           |                                r|                     |           dS dS )a  Move or rename this node.

        Moves a node into a new parent group, or changes the name of the
        node. newparent can be a Group object (see :ref:`GroupClassDescr`) or a
        pathname in string form. If it is not specified or None, the current
        parent group is chosen as the new parent.  newname must be a string
        with a new name. If it is not specified or None, the current name is
        chosen as the new name. If createparents is true, the needed groups for
        the given new parent group path to exist will be created.

        Moving a node across databases is not allowed, nor it is moving a node
        *into* itself. These result in a NodeError. However, moving a node
        *over* itself is allowed and simply does nothing. Moving over another
        existing node is similarly not allowed, unless the optional overwrite
        argument is true, in which case that node is recursively removed before
        moving.

        Usually, only the first argument will be used, effectively moving the
        node to a new location without changing its name.  Using only the
        second argument is equivalent to renaming the node in place.

        NFyou should specify at least a ``newparent`` or a ``newname`` parameterr@   r   %new parent is not a node nor a path: zGnodes can not be moved across databases; please make a copy of the node)ra   r@   r   rY   r   rL   r?   	TypeErrorrb   _g_check_not_contains_get_or_create_pathr`   _g_maybe_remover   rh   _g_log_move)r   r   r   r   createparentsrq   r   r   newfiler   r   r   s               r   r   zNode._f_moveE  s   2 	N	,  I J J J!I?G 9i(( 	,'G+GGY-- 	,GGG)(y+ , , ,
 % = > > > 	 'g'W"4"4 F 	""7+++ --iGG	I&&& 	Y;;; &Y(((   "" 	*[)))))	* 	*r    c                 H    | j                             d|| j                   d S )NMOVErt   r   r   s     r   r   zNode._g_log_move  s%    &+t/?@@@@@r    c                     t           )a  Copy this node and return the new one.

        Creates and returns a copy of the node in the given `newparent`,
        with the given `newname`.  If `recursive` copy is stated, all
        descendents are copied as well.  Additional keyword argumens may
        affect the way that the copy is made.  Unknown arguments must be
        ignored.  On recursive copies, all keyword arguments must be
        passed on to the children invocation of this method.

        If `_log` is false, the change is not logged.  This is *only*
        intended to be used by ``_g_copy_as_child()`` as a means of
        optimising sub-tree copies.

        r   )r   r   r   r   rn   kwargss         r   _g_copyzNode._g_copy  s
      "!r    c                 0     | j         || j        fddd|S )zCopy this node as a child of another group.

        Copies just this node into `newparent`, not recursing children
        nor overwriting nodes nor logging the copy.  This is intended to
        be used when copying whole sub-trees.

        F)r   rn   )r   rY   )r   r   r   s      r   _g_copy_as_childzNode._g_copy_as_child  s=     t|It| C&+%C C;AC C 	Cr    c                    |                                   | j        }| j        }| j        }	|}
|}|
|t	          d          |
|}
||	}t          |
d          r|
j        }|
j        }n't          |
d          r|}|
}nt          d|
          ||u rA|j        }||k    r||	k    rt	          d| j        z            |r|                     |           |	                    |
|          }
| 
                    |
           ||ur.|                                rt          j        dt                     |                     |
||            | j        |
||fi |S )a]  Copy this node and return the new node.

        Creates and returns a copy of the node, maybe in a different place in
        the hierarchy. newparent can be a Group object (see
        :ref:`GroupClassDescr`) or a pathname in string form. If it is not
        specified or None, the current parent group is chosen as the new
        parent.  newname must be a string with a new name. If it is not
        specified or None, the current name is chosen as the new name. If
        recursive copy is stated, all descendants are copied as well. If
        createparents is true, the needed groups for the given new parent group
        path to exist will be created.

        Copying a node across databases is supported but can not be
        undone. Copying a node over itself is not allowed, nor it is
        recursively copying a node into itself. These result in a
        NodeError. Copying over another existing node is similarly not allowed,
        unless the optional overwrite argument is true, in which case that node
        is recursively removed before copying.

        Additional keyword arguments may be passed to customize the copying
        process. For instance, title and filters may be changed, user
        attributes may be or may not be copied, data may be sub-sampled, stats
        may be collected, etc. See the documentation for the particular node
        type.

        Using only the first argument is equivalent to copying the node to a
        new location without changing its name. Using only the second argument
        is equivalent to making a copy of the node in the same group.

        Nr   r@   r   r   z6source and destination nodes are the same node: ``%s``zHcopying across databases can not be undone nor redone from this database)ra   r@   r   rY   r   rL   r?   r   r   r   r`   rh   r   r   r   r   r   )r   r   r   r   r   r   r   srcfile	srcparentsrcname	dstparentdstnamedstfiledstpathsrcpaths                  r   _f_copyzNode._f_copy  s   D 	,N	,	  I J J J!I?G 9i(( 	,'G+GGY-- 	,GGG)(y+ , , , g+G'!!g&8&8L&'( ( (
  4**7333 //	=II	I&&& '!!g&=&=&?&?!M :)+ + +
 	Y;;; t|Iw	DDVDDDr    c                 R    |                                   t          | j                  S )zIs this node visible?)ra   r   r?   rG   s    r   _f_isvisiblezNode._f_isvisible  s'     	T-...r    c                     t          |t          d                   st          d|j        z            t          |t          d                   st          d|j        z            d S )Nr=   z'new parent is not a registered node: %sGroupz%new parent node ``%s`` is not a group)rW   r   r   r?   )r   nodes     r   r`   zNode._g_check_group$  s|     $ 788 	0E"./ 0 0 0$ 899 	0C"./ 0 0 0	0 	0r    c                     | j         }|dk    s||k    s|                    |dz             rt          d|z            d S )Nr   z8can not move or recursively copy node ``%s`` into itself)r?   r   r   )r   r   
mypathnames      r   r   zNode._g_check_not_contains/  sb    %
#*$$!!*s"233 % *,67 8 8 8 %$r    c                     ||v rG|st          d|j         d| d          |                    |                              d           d S d S )Nzdestination group ``z`` already has a node named ``z2``; you may want to use the ``overwrite`` argumentT)r   r?   _f_get_childr   )r   r   r,   r   s       r   r   zNode._g_maybe_remove8  s|    6>> /.6+= . .*.. . ./ / / %%//55555 >r    c                 T    |                     d          rt          d|z            dS )zCheck validity of name for this particular kind of node.

        This is invoked once the standard HDF5 and natural naming checks
        have successfully passed.

        _i_z1node name starts with reserved prefix ``_i_``: %sN)r   
ValueErrorr   r,   s     r   _g_check_namezNode._g_check_nameA  sB     ??5!! 	LCdJL L L	L 	Lr    c                 ,    t          | j        |          S )zGet a PyTables attribute from this node.

        If the named attribute does not exist, an AttributeError is
        raised.

        )r4   rH   r   s     r   
_f_getattrzNode._f_getattrN  s     t}d+++r    c                 2    t          | j        ||           dS )zSet a PyTables attribute for this node.

        If the node already has a large number of attributes, a
        PerformanceWarning is issued.

        N)setattrrH   )r   r,   values      r   
_f_setattrzNode._f_setattrX  s     	tU+++++r    c                 0    t          | j        |           dS )zDelete a PyTables attribute from this node.

        If the named attribute does not exist, an AttributeError is
        raised.

        N)delattrrH   r   s     r   
_f_delattrzNode._f_delattrb  s     	t$$$$$r    )T)FF)F)NNFF)NNFFF).r   r   r9   r:   r   rF   rD   propertyr   r   rH   rM   rQ   _v_titler   r3   ri   rz   r}   rf   rg   ra   rd   r   r   rj   r   rl   r   r   r   r   r   r   r   r   r   r   r   r`   r   r   r   r   r   r   r|   r    r   r=   r=   Q   s       4 4n !M2 2 2
 &&I 	( 	( X	(  $ $ $ x[11H I+S S S Sj6 6 6!  !  ! F  " " "" " "	J 	J 	J,C ,C ,C\ #  #  #D  ,  	8 	8 	8% % %N  .- - - -(+ + +"# "# "#H	; 	; 	; 	; /3/4V* V* V* V*pA A A" " " "$
C 
C 
C /3@E^E ^E ^E ^E@/ / /	0 	0 	08 8 86 6 6L L L, , ,, , ,% % % % %r    r=   )	metaclassc                   $    e Zd ZeZd Zd Zd ZdS )NotLoggedMixinc                     d S rO   r|   rG   s    r   ri   zNotLoggedMixin._g_log_createu      r    c                     d S rO   r|   r   s     r   r   zNotLoggedMixin._g_log_movex  r   r    c                 2    |                      ||           d S rO   )r   )r   r   r   s      r   r   z NotLoggedMixin._g_remove_and_log{  s    y%(((((r    N)r   r   r9   r   rF   ri   r   r   r|   r    r   r   r   o  sH         *M    ) ) ) ) )r    r   )r:   r   r!   r   r   r   
exceptionsr   r   r   r	   pathr
   r   r   utilsr   undoredor   attributesetr   r   __docformat__r#   r)   r%   r=   r   r|   r    r   <module>r     s}          4 4 4 4 4 4 4 4- - - - - - - - - - - - 6 6 6 6 6 6 6 6 6 6       $ $ $ $ $ $ = = = = = = = = # 9  *&) &) &) &) &)t &) &) &)RY% Y% Y% Y% Y%X Y% Y% Y% Y%|) ) ) ) ) ) ) ) ) )r    