
    ꢩe;                         d Z ddlZddlZddlmZmZ ddlZddlmZ ddl	Z
ddlZd Zd Z	 	 	 	 ddZ G d d          ZddZddZdS )zjThis utility prints the contents of an HDF5 file as a tree.

Pass the flag -h to this for help on usage.

    N)defaultdictdeque)Pathc            	          t          j        d          } |                     ddt          dd           |                     dd	t          d
d           |                     dddd           |                     ddd           |                     dddd           |                     ddd           |                     dddd           |                     ddd           |                     dddd           |                     ddd           |                     d dd!d"           |                     d#dd!d$           |                     d%d&d'(           |                     d)d*d+d+d,d,d,-           | S ).Na  
        `pttree` is designed to give a quick overview of the contents of a
        PyTables HDF5 file by printing a depth-indented list of nodes, similar
        to the output of the Unix `tree` function.

        It can also display the size, shape and compression states of
        individual nodes, as well as summary information for the whole file.

        For a more verbose output (including metadata), see `ptdump`.
        )descriptionz-Lz--max-level	max_depthz8maximum branch depth of tree to display (-1 == no limit))typedesthelpz-Sz	--sort-bysort_byz@artificially order nodes, can be either "size", "name" or "none"z--print-size
store_true
print_sizezprint size of each node/branch)actionr
   r   z--no-print-sizestore_false)r   r
   z--print-shapeprint_shapezprint shape of each nodez--no-print-shapez--print-compressionprint_compressionz9print compression library(level) for each compressed nodez--no-print-compressionz--print-percentprint_percentz>print size of each node as a %% of the total tree size on diskz--no-print-percentz--use-si-unitsuse_si_unitsz)report sizes in SI units (1 MB == 10^6 B)z--use-binary-unitsz.report sizes in binary units (1 MiB == 2^20 B)srczfilename[:nodepath]z&path to the root of the tree structure)metavarr      sizeTF)r   r   r   r   r   r   r   )argparseArgumentParseradd_argumentintstrset_defaults)parsers    5lib/python3.11/site-packages/tables/scripts/pttree.py_get_parserr!      sh   $	
 
 
F m#KG     k)O     |,-     -l     ='     =}     l9LH            ,_M     ]     N8     ]=    
 '<E  G G G !V&**/e  E E E M    c                     t                      } |                                 }|j                            d                              dd          }t          |          dk    r|d         d}}n|\  }}|dk    rd}t          j        |d          5 }t          ||fi |j        }t          |           d d d            n# 1 swxY w Y   d S )Nr   :r   r   / r)
r!   
parse_args__dict__poprsplitlentb	open_fileget_tree_strprint)r   argsr   filenamenodenameftree_strs          r    mainr6   W   s   ]]FD -

E
"
"
)
)#q
1
1C
3xx1}} VS( (r>>H	h	$	$ 8==t}==h               	Ds   #C  CCr%   TFc                    |                      |          }|                                 |j        }|dk     rt          j        j        }t          |
          }d}d}d}t          d           }t          d           }t          d           }t          d           }t          d           }t          d           }t          d           }t          |          }t                      }|r|	                                }t          |t          j        j                  r|                    |           L|j        }|                                \  }}||xx         d	z  cc<   ||         ||<   |||<   t          |t          j                  r|                    |           nBt          |t          j                  r||         d	k    r	 |j        }|j        }||xx         |z  cc<   ||xx         |z  cc<   ||xx         d	z  cc<   ||z  }||z  }|d	z  }|||<   n8# t*          $ r+} t-          j        t1          |                      Y d
} ~ nd
} ~ ww xY w|                    |           nzt          |t          j                  r`||         d	k    r?|                    t7          |j                                                             |||<   n|                    |           |i }!|}|r|	                                }|j        }|j        }"|"j        }#||#xx         ||         z  cc<   ||#xx         ||         z  cc<   ||#xx         ||         z  cc<   |j        |z
  }$|$|k    s5|j        }%|r|%d|j         j!        z  z  }%g }&||         |z  }'|||                  d	k    r#|%d||         ||         |||                  fz  z  }%t          |t          j        j                  r|&                    d|j"        z             n||         d	k    r&|&                    d|||                  z             nSt          |t          j#        t          j$        f          r|rUd%                     |||                    |||                             }(|r
|(d|'ddz  }(|&                    |(           |r*|&                    dtM          |j'                  z             |r<|j(        j)        })|j(        j*        }*|*rd|)|*fz  }+nd}+|&                    d|+z             nk|$|k    red||         z  },|r7|,d%                     |||                    |||                             z  },|r
|,d|'ddz  },|&                    |,           ||!vr#|!+                    |tY                      i           |%|!|         _-        |&|!|         _.        |	dk    r|' |!|         _/        nq|	dk    r|j        |!|         _/        nX|dk    rd|!|         _/        nDt7          |"j                                                  0                    |          |!|         _/        |dk    rH|#|!vr#|!+                    |#tY                      i           |!|#         1                    |!|                    ||ur|"|vr|2                    |"           |d}-|-t1          |!|j                           dz   z  }-|r||z  }.tg          | j4                  5                                j6        }/|-dz  }-|-d|z  z  }-|-d %                     ||           ||                    z  }-|-d!|.z  z  }-|-d" ||/          z  z  }-|-dz  }-|-S )#zl
    Generate the ASCII string representing the tree structure, and the summary
    info (if requested)
    r   c                      dS Nr    r;   r"   r    <lambda>zget_tree_str.<locals>.<lambda>   s     r"   c                      dS r:   r;   r;   r"   r    r<   zget_tree_str.<locals>.<lambda>       ! r"   c                      dS r:   r;   r;   r"   r    r<   zget_tree_str.<locals>.<lambda>   s    Q r"   c                      dS r:   r;   r;   r"   r    r<   zget_tree_str.<locals>.<lambda>   s    A r"   c                      dS r:   r;   r;   r"   r    r<   zget_tree_str.<locals>.<lambda>   r>   r"   c                      d S Nr;   r;   r"   r    r<   zget_tree_str.<locals>.<lambda>   s    t r"   c                      dS )Nr&   r;   r;   r"   r    r<   zget_tree_str.<locals>.<lambda>   s    R r"   r   Nz (%s)z, addr=%i, ref=%i/%izsoftlink --> %szhardlink --> %szmem={}, disk={}z [z5.1%]zshape=%sz%s(%i)Nonezcompression=%sz... %i leavesz, mem={}, disk={}r   namer%   z?
------------------------------------------------------------

z

z=------------------------------------------------------------
zTotal branch leaves:    %i
z1Total branch size:      {} in memory, {} on disk
zMean compression ratio: %.2f
zHDF5 file size:         %s
)7get_node_g_check_open_v_depthossysmaxintbytes2humanr   r   r*   
isinstancer-   linkLinkappend_v_pathname_get_obj_infoUnImplementedLeafsize_in_memorysize_on_diskNotImplementedErrorwarningswarnr   Groupextendlist_v_childrenvalues	_v_parent_v_name	__class____name__targetArrayTableformatreprshapefilterscomplib	complevelupdate
PrettyTreerG   labelsr   index	add_child
appendleftr   r2   statst_size)0r4   wherer   print_classr   r   r   r   print_totalr   r   rootstart_depthb2htotal_in_memtotal_on_disktotal_itemsin_memon_disk
leaf_count	ref_countref_idxhl_addresses
hl_targetsstackleavesnodepathaddrrcmdeprettyparentparent_pathdepthrG   rp   ratiosizestrliblevelcompstritemstrout_str	avg_ratiofsizes0                                                   r    r/   r/   m   s    ::eD-K1}}FM	
l
#
#C LMK ##F))$$GYY''J II&&I))$$G||,,LZZ((J$KKEWWF
 <$yy{{dBGL)) 	 MM$%%''b$1!$!TdB,-- ,	$MM$bg&& )	$ !##*+A)A 4LLLA%LLLDMMMQ&MMMt$$$)$$$ !A%L!Q&M1$K (,Jt$$* * * *M#a&&))))))))*
 MM$bh'' 
	$ !##T$"2"9"9";";<<===#'
4  
 d###y  <$H FE
 k%yy{{( 	{vd|+-;:d#33+ y   <D :$."999FDMM1E d+,q00. &l4012  
 $-- %'/$+=>>>>""/ *<+= >? @ @ @ @ D28RX"677 ' +/66F4L))33wt}+=+=? ?G$ 6#5#5#5#5#55MM'*** AMM*tDJ/?/?"?@@@$ >,.C L2E )"*c5\"9"(MM"2W"<=== )##)Jt,<< ?299F4L))33wt}+=+= ?  ? ?G  21E11111Gg&&& 6!!tZ\\2333 $F4L"(F4L&  (-vt$$F""'+|t$$ 3;;+,F4L((+/0B0I0I 1B 1B ,C ,CCH5;; 4L(
 s{{ f,,MM;
"=>>> {#--fTl;;;te 3 3 V$$$W  k%Z )Gs6$*+,,x77G 
#!L0	QZ  %%''/?"1K??GNNCss=113 3 	33i??1CCJJ>>?"Ns   AH 
I)!IIc                   2    e Zd ZdZddZd Zd Zd Zd ZdS )	ro   a  

    A pretty ASCII representation of a recursive tree structure. Each node can
    have multiple labels, given as a list of strings.

    Example:
    --------

        A = PrettyTree('A', labels=['wow'])
        B = PrettyTree('B', labels=['such tree'])
        C = PrettyTree('C', children=[A, B])
        D = PrettyTree('D', labels=['so recursive'])
        root = PrettyTree('root', labels=['many nodes'], children=[C, D])
        print root

    Credit to Andrew Cooke's blog:
    <http://www.acooke.org/cute/ASCIIDispl0.html>

    Nc                 N    |g }|g }|| _         || _        || _        || _        d S rC   )rG   childrenrp   r   )selfrG   r   rp   r   s        r    __init__zPrettyTree.__init__s  s9     H>F	 r"   c                 r    || usJ | |j         vsJ || j         vr| j                             |           d S d S rC   )r   rR   )r   childs     r    rr   zPrettyTree.add_child  sW    D    5>))))%%M  ''''' &%r"   c              #      K   | j         V  | j        D ]	}d|z   V  
t          | j        d           }|r|d         nd }|D ]0}||u rdnd}|                                D ]}||z   V  ||u rdnd}1d S )Nz   c                     | j         S rC   )r   )cs    r    r<   z'PrettyTree.tree_lines.<locals>.<lambda>  s    	 r"   )keyr7   z`--z+--z|  )rG   rp   sortedr   
tree_lines)r   labelr   lastr   prefixlines          r    r   zPrettyTree.tree_lines  s      i[ 	  	 E%-$-.A.ACCC'1x||T 	; 	;E#tmmUUF((** ; ;tm###"'4--U;	; 	;r"   c                 P    d                     |                                           S )N
)joinr   r   s    r    __str__zPrettyTree.__str__  s    yy**+++r"   c                 D    d| j         j         dt          |           ddS )N<z at 0xx>)rc   rd   idr   s    r    __repr__zPrettyTree.__repr__  s)    ?4>*??"T((?????r"   )NNNN)	rd   
__module____qualname____doc__r   rr   r   r   r   r;   r"   r    ro   ro   ]  ss         (    ( ( (
; 
; 
;, , ,@ @ @ @ @r"   ro   c                 ,    | rddnddfd}|S )N)TBGBMBkBB)g   mBg    eAg    .Ag     @@r   )TiBGiBMiBKiBr   )l        i   @i   i   r   c                 X    t                    D ]\  }}| |z  }|dk    r n|d| S )Nr   z.1f)zip)nbytesr   valuescaledprefixesr`   s       r    r{   zbytes2human.<locals>.b2h  sR    "8V44 	 	OVUe^F{{  &&f&&&r"   r;   )r   r{   r   r`   s     @@r    rN   rN     sG     7.'26' ' ' ' ' ' Jr"   /tmpc           	         t          j        t          t          |           dz            d          }|                    dd          }|                    |d          }|                    |d          }t          j        dd	          }||fD ]o}|                    |d
t          j        dt          j	                  |           |                    |dt          j
                            d          |           p|                    dd          }|                    |dd           |                    |dd           |                    |dd           |S )Nztest_pttree.hdf5wr%   group1group1agroup1b   bzip2)rm   rl   	zeros128b    )dtype)objrk   
random128bgroup2softlink_g1_z128z/group1/group1a/zeros128bhardlink_g1a_z128hardlink_g1az/group1/group1a)r-   r.   r   r   create_groupFilterscreate_carraynpzerosfloat64randomrandcreate_soft_linkcreate_hard_link)r   r4   g1g1ag1brk   ggg2s           r    make_test_filer     sZ   
Sf(::;;SAAA	
X	&	&B
..Y
'
'C
..Y
'
'Cj1g666G3h ) )	KRXb
-K-K-K ' 	 	) 	) 	)	LbinnR.@.@ ' 	 	) 	) 	) 	) 
X	&	&Br-/JKKKr.0KLLLr>+<===Hr"   )
r%   r7   TTTFFTNF)F)r   )r   rK   r   collectionsr   r   rZ   pathlibr   numpyr   tablesr-   r!   r6   r/   ro   rN   r   r;   r"   r    <module>r      s    
			  * * * * * * * *               C C CL	 	 	, :>BGDH#m m m m`=@ =@ =@ =@ =@ =@ =@ =@@   *     r"   