3
AAd]O>  ã               @   s¢   d dl mZmZ d dlm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jtj| ƒƒS )zA Return a PyTables Atom for the given dtype or dtype string.
    )ÚtablesZAtomZ
from_dtypeÚnpÚdtype)r
   © r   úo/home/yuhw/wc/wire-cell-python/wcpy/lib/python3.6/site-packages/apptools-4.4.0-py3.6.egg/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>   s    z"iterator_length.<locals>.<genexpr>)Úsum)Úiteratorr   r   r   Úiterator_length   s    r   c             C   s   dj | jd�}||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__Ú__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 kr,tjdddd�| _d | _t|tjƒrT|}|j	}|j
rT|| _|| _	|rf| jƒ  d S )NZbloscé   T)ZcomplibZ	complevelÚshuffle)ÚmodeÚdelete_existingÚauto_groupsr   ZFiltersÚ	h5filtersÚ_h5Ú
isinstanceZFileÚfilenameZisopenÚopen)Úselfr&   r    r!   r"   Ú	auto_openr#   Zpyt_filer   r   r   Ú__init__F   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'   Z   s    zH5File.openc             C   s   | j r| jjƒ  d | _d S )N)r+   r$   Úclose)r(   r   r   r   r-   ^   s    
zH5File.closec             C   s   | d S )Nú/r   )r(   r   r   r   Úrootc   s    zH5File.rootc             C   s
   | j d k	S )N)r$   )r(   r   r   r   r+   g   s    zH5File.is_openc             C   s
   t | jƒS )N)Ústrr$   )r(   r   r   r   Ú__str__k   s    zH5File.__str__c             C   s
   t | jƒS )N)Úreprr$   )r(   r   r   r   Ú__repr__n   s    zH5File.__repr__c             C   s
   || j kS )N)r$   )r(   Ú	node_pathr   r   r   Ú__contains__q   s    zH5File.__contains__c             C   sF   y| j j|ƒ}W n, tjk
r<   d}t|j|| jƒƒ‚Y nX t|ƒS )NzNode {0!r} not found in {1!r})r$   Zget_noder   ZNoSuchNodeErrorÚ	NameErrorr   r&   Ú
_wrap_node)r(   r4   ÚnodeÚmsgr   r   r   Ú__getitem__t   s    zH5File.__getitem__c             C   s   dd„ | j jdd�D ƒS )Nc             s   s   | ]}t |ƒV  qd S )N)r7   )r   Únr   r   r   r   }   s    z"H5File.__iter__.<locals>.<genexpr>r.   )Úwhere)r$   Z
iter_nodes)r(   r   r   r   Ú__iter__|   s    zH5File.__iter__c             C   s   t | ƒS )N)r   )r(   r   r   r   Ú__len__   s    zH5File.__len__r.   c             c   s0   x*| j j|d�D ]}|j}|t|ƒfV  qW dS )z3 Iterate over node paths and nodes of the h5 file. )r<   N)r$   Z
walk_nodesÚ_v_pathnamer7   )r(   Úpathr8   r4   r   r   r   Ú	iteritems‚   s    zH5File.iteritemsc             K   s"  | j |ƒ | j|ƒ | j}t|tƒrF|dkr8d}t|ƒ‚d}	|}|}
n|}	|	jj}|	j}
| j	|ƒ\}}|r´d|
dd…  }
t
|ƒ}|j||||
fd| ji|—Ž}|	dk	r²|j|	ƒ nj|rôt
|ƒ}|j||||
fd| ji|—Ž}|	dk	rò|	|dd…< n*|	dk�rtj|
|d�}	|j|||	f|Ž}|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   Úfilters)r
   )r   )Ú_check_nodeÚ_assert_valid_pathr$   r%   ÚtupleÚ
ValueErrorr
   ÚnameÚshapeÚ
split_pathr   Zcreate_earrayr#   ÚappendZcreate_carrayr	   ÚzerosÚcreate_array)r(   r4   Úarray_or_shaper
   ÚchunkedÚ
extendableÚkwargsÚh5r9   ÚarrayrH   r@   rG   Úatomr8   r   r   r   rL   ˆ   s>    



zH5File.create_arrayc             K   s<   | j |ƒ | j|ƒ | j|ƒ\}}| jj||f|Ž | | 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`.
        )rC   rD   rI   r$   Úcreate_group)r(   Ú
group_pathrP   r@   rG   r   r   r   rT   Á   s
    


zH5File.create_groupc             K   s4   | j |ƒ | j|ƒ 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)rC   rD   r   Úadd_to_h5file)r(   r4   rV   rP   r   r   r   Úcreate_dictÑ   s    


zH5File.create_dictc             K   s.   | j |ƒ | j|ƒ tj| ||f|Ž | | 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.
        )rC   rD   r   rW   )r(   r4   ÚdescriptionrP   r   r   r   Úcreate_tableà   s    

zH5File.create_tablec             C   sr   | j r| j|ƒ\}}| j|ƒ || krn| jrVt| | tƒrJ| j|dd� qn| j|ƒ n| j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"   rI   Ú_create_required_groupsr!   r%   ÚH5GroupÚremove_groupÚremove_nodeÚexists_errorr   r&   rF   )r(   r4   r@   rG   r9   r   r   r   rC   ñ   s    
zH5File._check_nodec             C   s.   || kr*| j |ƒ\}}| j|ƒ | j|ƒ d S )N)rI   r\   rT   )r(   r@   ÚparentÚmissingr   r   r   r\     s    
zH5File._create_required_groupsc             C   s2   | | }t |tƒr&d}t|j|jƒƒ‚|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%   r]   rF   r   ÚpathnameZ	_f_remove)r(   r4   r8   r9   r   r   r   r_     s
    
zH5File.remove_nodec             K   s   | | j jf |Ž dS )zŒRemove group

        Parameters
        ----------
        group_path : str
            PyTable group path; e.g. '/path/to/group'.
        N)Ú	_h5_groupZ	_g_remove)r(   rU   rP   r   r   r   r^     s    zH5File.remove_groupc             C   s   d|j dƒkrtdƒ‚d S )NÚattrsr.   z 'attrs' is an invalid node name.)ÚsplitrF   )r(   r4   r   r   r   rD   #  s    zH5File._assert_valid_pathc             C   sB   |j 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'.
        r.   r   r   N)Úrfind)Úclsr4   Úir   r   r   rI   (  s    
zH5File.split_pathc             G   s*   dj dd„ |D ƒƒ}|j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.
        r.   c             s   s   | ]}|j d ƒV  qdS )r.   N)Ústrip)r   Úpartr   r   r   r   E  s    z#H5File.join_path.<locals>.<genexpr>)ÚjoinÚ
startswith)rh   Úargsr@   r   r   r   Ú	join_path9  s    
zH5File.join_path)r   FTTN)r.   )NFF)N)r   Ú
__module__Ú__qualname__r   r`   r*   r'   r-   Úpropertyr/   r+   r1   r3   r5   r:   r=   r>   rA   rL   rT   rX   rZ   rC   r\   r_   r^   ÚclassmethodrD   rI   ro   r   r   r   r   r   $   s6    

 
8

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 )N)Ú_node_attrs)r(   Z
node_attrsr   r   r   r*   S  s    zH5Attrs.__init__c             C   s   | j |= d S )N)ru   )r(   Úkeyr   r   r   Ú__delitem__V  s    zH5Attrs.__delitem__c             C   s
   | j | S )N)ru   )r(   rv   r   r   r   r:   Y  s    zH5Attrs.__getitem__c             C   s   t t| jƒ ƒƒS )N)ÚiterÚlistÚkeys)r(   r   r   r   r=   \  s    zH5Attrs.__iter__c             C   s   t | jjƒ ƒS )N)Úlenru   Ú_f_list)r(   r   r   r   r>   _  s    zH5Attrs.__len__c             C   s,   t |tƒst |tƒrtj|ƒ}|| j|< d S )N)r%   rE   ry   r	   rR   ru   )r(   rv   Úvaluer   r   r   Ú__setitem__b  s    
zH5Attrs.__setitem__Nc             C   s   || kr|S | | S )Nr   )r(   rv   Údefaultr   r   r   Úgetg  s    zH5Attrs.getc             C   s
   | j jƒ S )N)ru   r|   )r(   r   r   r   rz   j  s    zH5Attrs.keysc                s   ‡ fdd„t ˆ jƒ ƒD ƒS )Nc                s   g | ]}ˆ | ‘qS r   r   )r   Úk)r(   r   r   ú
<listcomp>n  s    z"H5Attrs.values.<locals>.<listcomp>)ry   rz   )r(   r   )r(   r   Úvaluesm  s    zH5Attrs.valuesc                s   ‡ fdd„t ˆ jƒ ƒD ƒS )Nc                s   g | ]}|ˆ | f‘qS r   r   )r   r�   )r(   r   r   r‚   q  s    z!H5Attrs.items.<locals>.<listcomp>)ry   rz   )r(   r   )r(   r   Úitemsp  s    zH5Attrs.items)N)r   rp   rq   r   r*   rw   r:   r=   r>   r~   r€   rz   rƒ   r„   r   r   r   r   rt   K  s   
rt   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 )1r]   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 )N)rd   rt   Z_v_attrsre   )r(   Zpytables_groupr   r   r   r*   {  s    zH5Group.__init__c             C   s
   || j kS )N)rd   )r(   r4   r   r   r   r5     s    zH5Group.__contains__c             C   s
   t | jƒS )N)r0   rd   )r(   r   r   r   r1   ‚  s    zH5Group.__str__c             C   s
   t | jƒS )N)r2   rd   )r(   r   r   r   r3   …  s    zH5Group.__repr__c             C   sL   |j dƒ}| jj|d ƒ}t|ƒ}t|ƒdkr2|S |dj|dd … ƒ S d S )Nr.   r   r   )rf   rd   Ú__getattr__r7   r{   rl   )r(   r4   Úpartsr8   r   r   r   r:   ˆ  s    
zH5Group.__getitem__c             C   s   dd„ | j D ƒS )Nc             s   s   | ]}t |ƒV  qd S )N)r7   )r   Úcr   r   r   r   “  s    z#H5Group.__iter__.<locals>.<genexpr>)rd   )r(   r   r   r   r=   ’  s    zH5Group.__iter__c             C   s   t | ƒS )N)r   )r(   r   r   r   r>   •  s    zH5Group.__len__c             C   s   | j jS )N)rd   r?   )r(   r   r   r   rc   ˜  s    zH5Group.pathnamec             C   s   | j jS )N)rd   Z_v_name)r(   r   r   r   rG   œ  s    zH5Group.namec             C   s
   | j jjS )N)rd   Ú_v_filer&   )r(   r   r   r   r&      s    zH5Group.filenamec             C   s   t | jjjƒS )N)r7   rd   rˆ   r/   )r(   r   r   r   r/   ¤  s    zH5Group.rootc             C   s   t | jjjƒ ƒS )N)ry   rd   Z_v_childrenrz   )r(   r   r   r   Úchildren_names¨  s    zH5Group.children_namesc             C   s   t | jjjƒ ƒS )N)ry   rd   Ú	_v_groupsrz   )r(   r   r   r   Úsubgroup_names¬  s    zH5Group.subgroup_namesc             C   s   dd„ | j jjƒ D ƒS )z? Iterate over `H5Group` nodes that are children of this group. c             s   s   | ]}t |ƒV  qd S )N)r7   )r   Úgr   r   r   r   ²  s    z&H5Group.iter_groups.<locals>.<genexpr>)rd   rŠ   rƒ   )r(   r   r   r   Úiter_groups°  s    zH5Group.iter_groupsFc             K   s   | j d|fd|i|—ŽS )NrT   r!   )Ú_delegate_to_h5file)r(   Úgroup_subpathr!   rP   r   r   r   rT   ´  s    zH5Group.create_groupc             K   s   | j d|f|ŽS )Nr^   )rŽ   )r(   r�   rP   r   r   r   r^   º  s    
zH5Group.remove_groupNc             K   s   | j d||f|||dœ|—ŽS )NrL   )r
   rN   rO   )rŽ   )r(   Únode_subpathrM   r
   rN   rO   rP   r   r   r   rL   ¿  s    zH5Group.create_arrayc             O   s   | j d||f|ž|ŽS )NrZ   )rŽ   )r(   r�   rY   rn   rP   r   r   r   rZ   Ç  s    zH5Group.create_tablec             K   s   | j d|fd|i|—ŽS )NrX   rV   )rŽ   )r(   r�   rV   rP   r   r   r   rX   Ì  s    zH5Group.create_dictc             K   s   | j d|f|ŽS )Nr_   )rŽ   )r(   r�   rP   r   r   r   r_   Ñ  s    zH5Group.remove_nodec       	      O   sD   |j ddƒ}t| jj|d�}|j| j|ƒ}t||ƒ}||f|ž|ŽS )Nr!   F)r!   )Úpopr   rd   rˆ   ro   rc   Úgetattr)	r(   Zfunction_namer�   rn   rP   r!   rQ   rU   Úfuncr   r   r   rŽ   Õ  s
    
zH5Group._delegate_to_h5file)F)NFF)N)r   rp   rq   r   r*   r5   r1   r3   r:   r=   r>   rr   rc   rG   r&   r/   r‰   r‹   r�   r   r   rT   r^   rL   rZ   rX   r_   rŽ   r   r   r   r   r]   t  s4   
 r]   c             C   s@   t | tjƒr*tj| ƒr t| ƒ} q<t| ƒ} ntj| ƒr<t| ƒ} | S )z* Wrap PyTables node object, if necessary. )r%   r   ÚGroupr   Zis_dict_noder]   r   Zis_table_node)r8   r   r   r   r7   Þ  s    



r7   )N)Úcollectionsr   r   Ú	functoolsr   Únumpyr	   r   Z	dict_noder   Z
table_noder   r   r   r   r   r   rt   r]   r7   r   r   r   r   Ú<module>   s   
  ))j