B
    i\p=              
   @   s   d Z ddlmZ ddlmZ ddlZddl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 Zd	d
 ZdddZG dd deZdddZd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.

    )absolute_import)print_functionN)defaultdictdeque)zipc           	   C   s  t jdd} | jddtddd | jdd	td
dd | jddddd | jdddd | jddddd | jdddd | jddddd | jdddd | jddddd | jdddd | jd dd!d"d | jd#dd!d$d | jd%d&d'd( | j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`.
        )Z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-sizeZ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strZset_defaults)parser r   4lib/python3.7/site-packages/tables/scripts/pttree.py_get_parser   sT    


r   c           	   C   s   t  } |  }|jddd}t|dkr>|d d }}n|\}}|dkrRd}t|d}t||f|j}t	| W d Q R X d S )Nr   :r   r   / r)
r   
parse_args__dict__poprsplitlentables	open_fileget_tree_strprint)r   argsr   filenamenodenamefZtree_strr   r   r   maind   s    r1   r!   TFc       0   
   C   s  |  |}|  |j}|dk r(tjj}t|
}d}d}d}tdd }tdd }tdd }tdd }tdd }tdd }td	d }t|}t }x^|r|	 }t
|tjjr|| q|j}| \}}||  d
7  < || ||< |||< t
|tjr|| d
kry`|j}|j}||  |7  < ||  |7  < ||  d
7  < ||7 }||7 }|d
7 }|||< W n0 tk
r }  zt| j W dd} ~ X Y nX || qt
|tjr|| d
kr|t|j  |||< q|| qW i }!|}x
|r|	 }|j}|j}"|"j}#||#  || 7  < ||#  || 7  < ||#  || 7  < |j| }$|$|ks|j}%|r|%d|jj  7 }%g }&d||  | }'|||  d
kr|%d|| || |||  f 7 }%t
|tjjr|&d|j!  n*|| d
kr,|&d|||    nt
|tj"tj#fr|r~d||| ||| f }(|rt|(d|' 7 }(|&|( |r|&dt$|j%  |r.|j&j'})|j&j(}*|*rd|)|*f }+nd}+|&d|+  nX|$|kr.d||  },|r|,d||| ||| f 7 },|r$|,d|' 7 },|&|, ||!krH|!)|t* i |%|!| _+|&|!| _,|	dkrt|' |!| _-nH|	dkr|j|!| _-n0|dkrd|!| _-nt|"j .||!| _-|dk	r|#|!kr|!)|#t* i |!|# /|!|  ||k	r|"|kr|0|" qW d}-|-t1|!|j d 7 }-|rt2|| }.t3| j4j5}/|-d7 }-|-d| 7 }-|-d ||||f 7 }-|-d!|. 7 }-|-d"||/ 7 }-|-d7 }-|-S )#zl
    Generate the ASCII string representing the tree structure, and the summary
    info (if requested)
    r   c               S   s   dS )Ng        r   r   r   r   r   <lambda>   s    zget_tree_str.<locals>.<lambda>c               S   s   dS )Ng        r   r   r   r   r   r3      s    c               S   s   dS )Nr   r   r   r   r   r   r3      s    c               S   s   dS )Nr   r   r   r   r   r   r3      s    c               S   s   dS )Nr   r   r   r   r   r   r3      s    c               S   s   d S )Nr   r   r   r   r   r3      s    c               S   s   dS )Nr"   r   r   r   r   r   r3      s    r   Nz (%s)d   z, addr=%i, ref=%i/%izsoftlink --> %szhardlink --> %szmem=%s, disk=%sz
 [%4.1f%%]zshape=%sz%s(%i)Nonezcompression=%sz... %i leavesz, mem=%s, disk=%sr   namer!   z?
------------------------------------------------------------

z

z=------------------------------------------------------------
zTotal branch leaves:    %i
z1Total branch size:      %s in memory, %s on disk
zMean compression ratio: %.2f
zHDF5 file size:         %s
)6Zget_nodeZ_g_check_openZ_v_depthossysZmaxintbytes2humanr   r   r&   
isinstancer)   linkZLinkappendZ_v_pathnameZ_get_obj_infoZLeafZsize_in_memoryZsize_on_diskNotImplementedErrorwarningswarnmessageZGroupextendlistZ_v_childrenvaluesZ	_v_parentZ_v_name	__class____name__targetZArrayZTablereprshapefilterscomplib	complevelupdate
PrettyTreer6   labelsr   index	add_child
appendleftr   floatstatr.   st_size)0r0   wherer   Zprint_classr   r   r   r   Zprint_totalr   r   rootZstart_depthb2hZtotal_in_memZtotal_on_diskZtotal_itemsZin_memZon_diskZ
leaf_countZ	ref_countZref_idxZhl_addressesZ
hl_targetsstackZleavesZnodepathZaddrZrcmdeZprettyparentparent_pathZdepthr6   rN   ZpctZsizestrliblevelZcompstrZitemstrZout_strZ	avg_ratioZfsizer   r   r   r+   z   s   	



	













r+   c               @   s:   e Zd ZdZdddZdd Zdd Zd	d
 Zdd ZdS )rM   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             C   s4   |d krg }|d krg }|| _ || _|| _|| _d S )N)r6   childrenrN   r   )selfr6   ra   rN   r   r   r   r   __init__~  s    zPrettyTree.__init__c             C   s4   || k	st | |jkst || jkr0| j| d S )N)AssertionErrorra   r<   )rb   childr   r   r   rP     s    
zPrettyTree.add_childc             c   s   | j V  x| jD ]}d| V  qW t| jdd d}|r@|d nd }xH|D ]@}||krZdnd}x*| D ]}|| V  ||krdnd}qhW qJW d S )	Nz   c             S   s   | j S )N)r   )cr   r   r   r3     s    z'PrettyTree.tree_lines.<locals>.<lambda>)keyr2   z`--z+--z|  )r6   rN   sortedra   
tree_lines)rb   Zlabelra   Zlastre   prefixliner   r   r   ri     s    

zPrettyTree.tree_linesc             C   s   d |  S )N
)joinri   )rb   r   r   r   __str__  s    zPrettyTree.__str__c             C   s   d| j jtt| f S )Nz
<%s at %s>)rD   rE   hexid)rb   r   r   r   __repr__  s    zPrettyTree.__repr__)NNNN)	rE   
__module____qualname____doc__rc   rP   ri   rn   rq   r   r   r   r   rM   h  s   
rM   c                s(   | rd dnd d fdd}|S )N)ZTBZGBZMBZkBB)g   mBg    eAg    .Ag     @@r   )ZTiBZGiBZMiBZKiBru   )l        i   @i   i   r   c                s:   x,t  D ]\}}t| | }|dkrP qW d||f S )Nr   z%.1f%s)r   rR   )nbytesrj   valueZscaled)prefixesrC   r   r   rW     s
    zbytes2human.<locals>.b2hr   )r   rW   r   )rx   rC   r   r9     s    	r9   /tmpc             C   s   t tj| dd}|dd}||d}||d}t jddd	}xH||fD ]<}|j|d
tj	dtj
d|d |j|dtjd|d qRW |dd}||dd}||dd}	||dd}
|S )Nztest_pttree.hdf5wr!   Zgroup1Zgroup1aZgroup1b   Zbzip2)rK   rJ   Z	zeros128b    )Zdtype)objrI   Z
random128bZgroup2Zsoftlink_g1_z128z/group1/group1a/zeros128bZhardlink_g1a_z128Zhardlink_g1az/group1/group1a)r)   r*   r7   rY   rm   Zcreate_groupZFiltersZcreate_carraynpZzerosZfloat64ZrandomZrandZcreate_soft_linkZcreate_hard_link)rj   r0   Zg1Zg1aZg1brI   ZggZg2ZsoftlinkZhardlinkZhlgroupr   r   r   make_test_file  s"    r   )
r!   r2   TTTFFTNF)F)ry   )rt   Z
__future__r   r   r)   Znumpyr~   r7   r   collectionsr   r   r>   Z	six.movesr   r   r1   r+   objectrM   r9   r   r   r   r   r   <module>   s&   F   
 l@
