a
    ¨ˆcÌ@  ã                   @   sª   d dl mZmZ d dlmZ d dlZd dlZd dlZddl	m
Z
 ddlmZ dd„ Zd	d
„ Zddd„Zdd„ ZG dd„ deƒZG dd„ deƒZG dd„ deƒZdd„ ZdS )é    )ÚMappingÚMutableMapping)ÚpartialNé   )Ú
H5DictNode)ÚH5TableNodec                 C   s   t j t | ¡¡S )z;Return a PyTables Atom for the given dtype or dtype string.)ÚtablesZAtomZ
from_dtypeÚnpÚdtype©r
   © r   úV/home/yuhw/wc/wire-cell-python/wcpy/lib/python3.9/site-packages/apptools/io/h5/file.pyÚget_atom   s    r   c                 C   s   t dd„ | D ƒƒS )Nc                 s   s   | ]
}d V  qdS )r   Nr   )Ú.0Ú_r   r   r   Ú	<genexpr>   ó    z"iterator_length.<locals>.<genexpr>)Úsum)Úiteratorr   r   r   Úiterator_length   s    r   c                 C   s$   dj | jd�}|t |j¡ | _| S )Nz°** H5Group wrapper for H5File.{func_name}: **
Note that the first argument is a nodepath relative to the group, rather than
an absolute path. Below is the original docstring:

)Ú	func_name)ÚformatÚ__name__ÚinspectÚcleandocÚ__doc__)ÚwrappedÚoriginalZPREAMBLEr   r   r   Ú_update_wrapped_docstring   s
    úr   c                 C   s   t t| d�S )N©r   )r   r   r   r   r   r   Úh5_group_wrapper+   s    r    c                   @   sè   e Zd ZdZdZd6dd„Zd	d
„ Zdd„ Zedd„ ƒZ	edd„ ƒZ
dd„ Zdd„ Zdd„ Zdd„ Zdd„ Zdd„ Zd7dd„Zd8d d!„Zd"d#„ Zd9d$d%„Zd&d'„ Zd(d)„ Zd*d+„ Zd,d-„ Zd.d/„ Zed0d1„ ƒZed2d3„ ƒZed4d5„ ƒZdS ):ÚH5Fileah  File object for HDF5 files.

    This class wraps PyTables to provide a cleaner, but only implements an
    interface for accessing arrays.

    Parameters
    ----------
    filename : str or a `tables.File` instance
        Filename for an HDF5 file, or a PyTables `File` object.
    mode : str
        Mode to open the file:

            'r' : Read-only
            'w' : Write; create new file (an existing file would be deleted).
            'a' : Read and write to file; create if not existing
            'r+': Read and write to file; must already exist

    delete_existing : bool
        If True, an existing node will be deleted when a `create_*` method is
        called. Otherwise, a ValueError will be raise.
    auto_groups : bool
        If True, `create_array` will automatically create parent groups.
    auto_open : bool
        If True, open the file automatically on initialization. Otherwise,
        you can call `H5File.open()` explicitly after initialization.
    chunked : bool
        If True, the default behavior of `create_array` will be a chunked
        array (see PyTables `create_carray`).

    z`'{}' exists in '{}'; set `delete_existing` attribute to True to overwrite existing calculations.úr+FTNc                 C   sj   || _ || _|| _|d u r,tjdddd�| _d | _t|tjƒrT|}|j	}|j
rT|| _|| _	|rf|  ¡  d S )NZbloscé   T)ZcomplibZ	complevelÚshuffle)ÚmodeÚdelete_existingÚauto_groupsr   ZFiltersÚ	h5filtersÚ_h5Ú
isinstanceÚFileÚfilenameZisopenÚopen)Úselfr,   r%   r&   r'   Ú	auto_openr(   Zpyt_filer   r   r   Ú__init__T   s     	ÿzH5File.__init__c                 C   s   | j stj| j| jd�| _d S )N)r%   )Úis_openr   Ú	open_filer,   r%   r)   ©r.   r   r   r   r-   p   s    zH5File.openc                 C   s   | j r| j ¡  d | _d S ©N)r1   r)   Úcloser3   r   r   r   r5   t   s    
zH5File.closec                 C   s   | d S )Nú/r   r3   r   r   r   Úrooty   s    zH5File.rootc                 C   s
   | j d uS r4   ©r)   r3   r   r   r   r1   }   s    zH5File.is_openc                 C   s
   t | jƒS r4   )Ústrr)   r3   r   r   r   Ú__str__�   s    zH5File.__str__c                 C   s
   t | jƒS r4   )Úreprr)   r3   r   r   r   Ú__repr__„   s    zH5File.__repr__c                 C   s
   || j v S r4   r8   ©r.   Ú	node_pathr   r   r   Ú__contains__‡   s    zH5File.__contains__c                 C   sD   z| j  |¡}W n* tjy:   d}t| || j¡ƒ‚Y n0 t|ƒS )NzNode {0!r} not found in {1!r})r)   Zget_noder   ZNoSuchNodeErrorÚ	NameErrorr   r,   Ú
_wrap_node©r.   r>   ÚnodeÚmsgr   r   r   Ú__getitem__Š   s    zH5File.__getitem__c                 C   s   dd„ | j jdd�D ƒS )Nc                 s   s   | ]}t |ƒV  qd S r4   ©rA   )r   Únr   r   r   r   “   r   z"H5File.__iter__.<locals>.<genexpr>r6   ©Úwhere)r)   Z
iter_nodesr3   r   r   r   Ú__iter__’   s    zH5File.__iter__c                 C   s   t | ƒS r4   ©r   r3   r   r   r   Ú__len__•   s    zH5File.__len__r6   c                 c   s,   | j j|d�D ]}|j}|t|ƒfV  qdS )z3 Iterate over node paths and nodes of the h5 file. rH   N)r)   Z
walk_nodesÚ_v_pathnamerA   )r.   ÚpathrC   r>   r   r   r   Ú	iteritems˜   s    zH5File.iteritemsc                 K   s&  |   |¡ |  |¡ | j}t|tƒrF|du r8d}t|ƒ‚d}	|}|}
n|}	|	jj}|	j}
|  	|¡\}}|r´d|
dd…  }
t
|ƒ}|j||||
fd| ji|¤Ž}|	dur²| |	¡ nn|rôt
|ƒ}|j||||
fd| ji|¤Ž}|	durò|	|dd…< n.|	du �rtj|
|d�}	|j|||	fi |¤Ž}|S )aÃ  Create node to store an array.

        Parameters
        ----------
        node_path : str
            PyTable node path; e.g. '/path/to/node'.
        array_or_shape : array or shape tuple
            Array or shape tuple for an array. If given a shape tuple, the
            `dtype` parameter must also specified.
        dtype : str or numpy.dtype
            Data type of array. Only necessary if `array_or_shape` is a shape.
        chunked : bool
            Controls whether the array is chunked.
        extendable : {None | bool}
            Controls whether the array is extendable.
        kwargs : key/value pairs
            Keyword args passed to PyTables `File.create_(c|e)array`.
        Nz4`dtype` must be specified if only given array shape.)r   r   Úfiltersr   )Ú_check_nodeÚ_assert_valid_pathr)   r*   ÚtupleÚ
ValueErrorr
   ÚnameÚshapeÚ
split_pathr   Zcreate_earrayr(   ÚappendZcreate_carrayr	   ÚzerosÚcreate_array)r.   r>   Úarray_or_shaper
   ÚchunkedÚ
extendableÚkwargsÚh5rD   ÚarrayrV   rN   rU   ZatomrC   r   r   r   rZ   ž   sR    


ÿÿÿÿÿÿ
zH5File.create_arrayc                 K   s@   |   |¡ |  |¡ |  |¡\}}| jj||fi |¤Ž | | S )zïCreate group.

        Parameters
        ----------
        group_path : str
            PyTable group path; e.g. '/path/to/group'.
        kwargs : key/value pairs
            Keyword args passed to PyTables `File.create_group`.
        )rQ   rR   rW   r)   Úcreate_group)r.   Ú
group_pathr^   rN   rU   r   r   r   ra   à   s
    


zH5File.create_groupc                 K   s4   |   |¡ |  |¡ tj| |fd|i|¤Ž | | S )züCreate dict node at the specified path.

        Parameters
        ----------
        node_path : str
            Path to node where data is stored (e.g. '/path/to/my_dict')
        data : dict
            Data for initialization, if desired.
        Údata)rQ   rR   r   Úadd_to_h5file)r.   r>   rc   r^   r   r   r   Úcreate_dictð   s    


zH5File.create_dictc                 K   s2   |   |¡ |  |¡ tj| ||fi |¤Ž | | S )aÐ  Create table node at the specified path.

        Parameters
        ----------
        node_path : str
            Path to node where data is stored (e.g. '/path/to/my_dict')
        description : dict or numpy dtype object
            The description of the columns in the table. This is either a dict
            of column name -> dtype items or a numpy record array dtype. For
            more information, see the documentation for Table in pytables.
        )rQ   rR   r   rd   )r.   r>   Údescriptionr^   r   r   r   Úcreate_tableÿ   s    

zH5File.create_tablec                 C   sr   | j r|  |¡\}}|  |¡ || v rn| jrVt| | tƒrJ| j|dd� qn|  |¡ n| j 	|| j
¡}t|ƒ‚dS )z”Check if node exists and create parent groups if necessary.

        Either raise error or delete depending on `delete_existing` attribute.
        T)Ú	recursiveN)r'   rW   Ú_create_required_groupsr&   r*   ÚH5GroupÚremove_groupÚremove_nodeÚexists_errorr   r,   rT   )r.   r>   rN   rU   rD   r   r   r   rQ     s    
zH5File._check_nodec                 C   s.   || vr*|   |¡\}}|  |¡ |  |¡ d S r4   )rW   ri   ra   )r.   rN   ÚparentÚmissingr   r   r   ri   #  s    
zH5File._create_required_groupsc                 C   s2   | | }t |tƒr&d}t| |j¡ƒ‚| ¡  dS )zˆRemove node

        Parameters
        ----------
        node_path : str
            PyTable node path; e.g. '/path/to/node'.
        z:{!r} is a group. Use `remove_group` to remove group nodes.N)r*   rj   rT   r   ÚpathnameZ	_f_removerB   r   r   r   rl   *  s
    
zH5File.remove_nodec                 K   s   | | j jf i |¤Ž dS )zŒRemove group

        Parameters
        ----------
        group_path : str
            PyTable group path; e.g. '/path/to/group'.
        N)Ú	_h5_groupZ	_g_remove)r.   rb   r^   r   r   r   rk   8  s    zH5File.remove_groupc                 C   s   d|  d¡v rtdƒ‚d S )NÚattrsr6   z 'attrs' is an invalid node name.)ÚsplitrT   r=   r   r   r   rR   B  s    zH5File._assert_valid_pathc                 C   sB   |  d¡}|dkr"d|dd… fS |d|… ||d d… fS dS )zûSplit node path returning the base path and node name.

        For example: '/path/to/node' will return '/path/to' and 'node'

        Parameters
        ----------
        node_path : str
            PyTable node path; e.g. '/path/to/node'.
        r6   r   r   N)Úrfind)Úclsr>   Úir   r   r   rW   G  s    
zH5File.split_pathc                 G   s*   d  dd„ |D ƒ¡}| d¡s&d| }|S )zåJoin parts of an h5 path.

        For example, the 3 argmuments 'path', 'to', 'node' will return
        '/path/to/node'.

        Parameters
        ----------
        args : str
            Parts of path to be joined.
        r6   c                 s   s   | ]}|  d ¡V  qdS )r6   N)Ústrip)r   Úpartr   r   r   r   d  r   z#H5File.join_path.<locals>.<genexpr>)ÚjoinÚ
startswith)ru   ÚargsrN   r   r   r   Ú	join_pathX  s    
zH5File.join_path)r"   FTTN)r6   )NFF)N)r   Ú
__module__Ú__qualname__r   rm   r0   r-   r5   Úpropertyr7   r1   r:   r<   r?   rE   rJ   rL   rO   rZ   ra   re   rg   rQ   ri   rl   rk   ÚclassmethodrR   rW   r|   r   r   r   r   r!   /   sN    ÿ     ù




   ú
B



r!   c                   @   sb   e Zd ZdZdd„ Zdd„ Zdd„ Zdd	„ Zd
d„ Zdd„ Z	ddd„Z
dd„ Zdd„ Zdd„ ZdS )ÚH5AttrszéAn attributes dictionary for an h5 node.

    This intercepts `__setitem__` so that python sequences can be converted to
    numpy arrays. This helps preserve the readability of our HDF5 files by
    other (non-python) programs.
    c                 C   s
   || _ d S r4   ©Ú_node_attrs)r.   Z
node_attrsr   r   r   r0   r  s    zH5Attrs.__init__c                 C   s   | j |= d S r4   r‚   ©r.   Úkeyr   r   r   Ú__delitem__u  s    zH5Attrs.__delitem__c                 C   s
   | j | S r4   r‚   r„   r   r   r   rE   x  s    zH5Attrs.__getitem__c                 C   s   t |  ¡ ƒS r4   )ÚiterÚkeysr3   r   r   r   rJ   {  s    zH5Attrs.__iter__c                 C   s   t | j ¡ ƒS r4   )Úlenrƒ   Ú_f_listr3   r   r   r   rL   ~  s    zH5Attrs.__len__c                 C   s,   t |tƒst |tƒrt |¡}|| j|< d S r4   )r*   rS   Úlistr	   r`   rƒ   )r.   r…   Úvaluer   r   r   Ú__setitem__�  s    
zH5Attrs.__setitem__Nc                 C   s   || vr|S | | S r4   r   )r.   r…   Údefaultr   r   r   Úget†  s    zH5Attrs.getc                 C   s
   | j  ¡ S r4   )rƒ   rŠ   r3   r   r   r   rˆ   ‰  s    zH5Attrs.keysc                    s   ‡ fdd„ˆ   ¡ D ƒS )Nc                    s   g | ]}ˆ | ‘qS r   r   ©r   Úkr3   r   r   Ú
<listcomp>�  r   z"H5Attrs.values.<locals>.<listcomp>©rˆ   r3   r   r3   r   ÚvaluesŒ  s    zH5Attrs.valuesc                    s   ‡ fdd„ˆ   ¡ D ƒS )Nc                    s   g | ]}|ˆ | f‘qS r   r   r�   r3   r   r   r’   �  r   z!H5Attrs.items.<locals>.<listcomp>r“   r3   r   r3   r   Úitems�  s    zH5Attrs.items)N)r   r}   r~   r   r0   r†   rE   rJ   rL   r�   r�   rˆ   r”   r•   r   r   r   r   r�   j  s   
r�   c                   @   s  e Zd ZdZdd„ Zdd„ Zdd„ Zdd	„ Zd
d„ Zdd„ Z	dd„ Z
edd„ ƒZedd„ ƒZedd„ ƒZedd„ ƒZedd„ ƒZedd„ ƒZdd„ Zeejƒd.dd „ƒZeejƒd!d"„ ƒZeejƒd/d$d%„ƒZeejƒd&d'„ ƒZeejƒd0d(d)„ƒZeejƒd*d+„ ƒZd,d-„ Zd#S )1rj   z�A group node in an H5File.

    This is a thin wrapper around PyTables' Group object to expose attributes
    and maintain the dict interface of H5File.
    c                 C   s   || _ t| j jƒ| _d S r4   )rq   r�   Z_v_attrsrr   )r.   Zpytables_groupr   r   r   r0   š  s    zH5Group.__init__c                 C   s
   || j v S r4   ©rq   r=   r   r   r   r?   ž  s    zH5Group.__contains__c                 C   s
   t | jƒS r4   )r9   rq   r3   r   r   r   r:   ¡  s    zH5Group.__str__c                 C   s
   t | jƒS r4   )r;   rq   r3   r   r   r   r<   ¤  s    zH5Group.__repr__c                 C   sL   |  d¡}| j |d ¡}t|ƒ}t|ƒdkr2|S |d |dd … ¡ S d S )Nr6   r   r   )rs   rq   Ú__getattr__rA   r‰   ry   )r.   r>   ÚpartsrC   r   r   r   rE   §  s    
zH5Group.__getitem__c                 C   s   dd„ | j D ƒS )Nc                 s   s   | ]}t |ƒV  qd S r4   rF   )r   Úcr   r   r   r   ²  r   z#H5Group.__iter__.<locals>.<genexpr>r–   r3   r   r   r   rJ   ±  s    zH5Group.__iter__c                 C   s   t | ƒS r4   rK   r3   r   r   r   rL   ´  s    zH5Group.__len__c                 C   s   | j jS r4   )rq   rM   r3   r   r   r   rp   ·  s    zH5Group.pathnamec                 C   s   | j jS r4   )rq   Z_v_namer3   r   r   r   rU   »  s    zH5Group.namec                 C   s
   | j jjS r4   )rq   Ú_v_filer,   r3   r   r   r   r,   ¿  s    zH5Group.filenamec                 C   s   t | jjjƒS r4   )rA   rq   rš   r7   r3   r   r   r   r7   Ã  s    zH5Group.rootc                 C   s   t | jj ¡ ƒS r4   )r‹   rq   Z_v_childrenrˆ   r3   r   r   r   Úchildren_namesÇ  s    zH5Group.children_namesc                 C   s   t | jj ¡ ƒS r4   )r‹   rq   Ú	_v_groupsrˆ   r3   r   r   r   Úsubgroup_namesË  s    zH5Group.subgroup_namesc                    s   | j j‰ ‡ fdd„ˆ D ƒS )z? Iterate over `H5Group` nodes that are children of this group. c                 3   s   | ]}t ˆ | ƒV  qd S r4   rF   )r   Z
group_name©Úgroupsr   r   r   Ö  r   z&H5Group.iter_groups.<locals>.<genexpr>)rq   rœ   r3   r   rž   r   Úiter_groupsÏ  s    zH5Group.iter_groupsFc                 K   s   | j d|fd|i|¤ŽS )Nra   r&   ©Ú_delegate_to_h5file)r.   Úgroup_subpathr&   r^   r   r   r   ra   Ø  s    þýüzH5Group.create_groupc                 K   s   | j d|fi |¤ŽS )Nrk   r¡   )r.   r£   r^   r   r   r   rk   á  s
    ÿÿzH5Group.remove_groupNc                 K   s   | j d||f|||dœ|¤ŽS )NrZ   )r
   r\   r]   r¡   )r.   Únode_subpathr[   r
   r\   r]   r^   r   r   r   rZ   ç  s    
ýúùzH5Group.create_arrayc                 O   s   | j d||g|¢R i |¤ŽS )Nrg   r¡   )r.   r¤   rf   r{   r^   r   r   r   rg   û  s    ÿÿÿzH5Group.create_tablec                 K   s   | j d|fd|i|¤ŽS )Nre   rc   r¡   )r.   r¤   rc   r^   r   r   r   re     s    ÿÿÿzH5Group.create_dictc                 K   s   | j d|fi |¤ŽS )Nrl   r¡   )r.   r¤   r^   r   r   r   rl     s    zH5Group.remove_nodec           	      O   sJ   |  dd¡}t| jj|d�}| | j|¡}t||ƒ}||g|¢R i |¤ŽS )Nr&   F)r&   )Úpopr!   rq   rš   r|   rp   Úgetattr)	r.   Zfunction_namer¤   r{   r^   r&   r_   rb   Úfuncr   r   r   r¢     s
    
zH5Group._delegate_to_h5file)F)NFF)N)r   r}   r~   r   r0   r?   r:   r<   rE   rJ   rL   r   rp   rU   r,   r7   r›   r�   r    r    r!   ra   rk   rZ   rg   re   rl   r¢   r   r   r   r   rj   “  sJ   






	
   ú

rj   c                 C   s@   t | tjƒr*t | ¡r t| ƒ} q<t| ƒ} nt | ¡r<t| ƒ} | S )z* Wrap PyTables node object, if necessary. )r*   r   ÚGroupr   Zis_dict_noderj   r   Zis_table_node)rC   r   r   r   rA     s    



rA   )N)Úcollections.abcr   r   Ú	functoolsr   r   Únumpyr	   r   Z	dict_noder   Z
table_noder   r   r   r   r    r!   r�   rj   rA   r   r   r   r   Ú<module>
   s"   
  =) 