a
    ¨ˆc   ã                   @   sT   d dl mZ d dlZd dlmZ d dlmZ d dlm	Z	 dZ
dZG dd	„ d	eƒZdS )
é    )ÚclosingN)Úndarray)ÚGroup)ÚfilenodeZ	__array__Ú	node_namec                   @   sÔ   e Zd ZdZdZd*dd„Zdd„ Zdd	„ Zd
d„ Zdd„ Z	dd„ Z
edd„ ƒZejdd„ ƒZdd„ Zed+dd„ƒZedd„ ƒZdd„ Zdd„ Zdd„ Zd d!„ Zed,d"d#„ƒZed$d%„ ƒZed&d'„ ƒZed(d)„ ƒZdS )-Ú
H5DictNodea{  Dictionary-like node interface.

    Data for the dict is stored as a JSON file in a PyTables FileNode. This
    allows easy storage of Python objects, such as dictionaries and lists of
    different data types.

    Note that this is implemented using a group-node assuming that arrays are
    valid inputs and will be stored as H5 array nodes.

    Parameters
    ----------
    h5_group : H5Group instance
        Group node which will be used as a dictionary store.
    auto_flush : bool
        If True, write data to disk whenever the dict data is altered.
        Otherwise, call `flush()` explicitly to write data to disk.
    Ú_pyobject_dataTc                 C   s~   |   |¡sJ ‚|  |¡}|| _|| _t|| jƒ}tt |¡ƒ�,}t	j
| ¡  d¡| jd�| _W d   ƒ n1 sp0    Y  d S )NÚascii)Úobject_hook)Úis_dict_nodeÚ_get_pyt_groupÚ	_h5_groupÚ
auto_flushÚgetattrÚ_pyobject_data_noder   r   Z	open_nodeÚjsonÚloadsÚreadÚdecodeÚ_object_hookr   )ÚselfZh5_groupr   Z	dict_nodeÚf© r   ú[/home/yuhw/wc/wire-cell-python/wcpy/lib/python3.9/site-packages/apptools/io/h5/dict_node.pyÚ__init__.   s    
ÿzH5DictNode.__init__c                 C   s
   | j | S ©N©Údata©r   Úkeyr   r   r   Ú__getitem__@   s    zH5DictNode.__getitem__c                 C   s   || j |< | jr|  ¡  d S r   ©r   r   Úflush)r   r   Úvaluer   r   r   Ú__setitem__C   s    
zH5DictNode.__setitem__c                 C   s   | j |= | jr|  ¡  d S r   r!   r   r   r   r   Ú__delitem__H   s    zH5DictNode.__delitem__c                 C   s
   || j v S r   r   r   r   r   r   Ú__contains__M   s    zH5DictNode.__contains__c                 C   s
   | j  ¡ S r   )r   Úkeys©r   r   r   r   r'   P   s    zH5DictNode.keysc                 C   s   | j S r   )r   r(   r   r   r   r   W   s    zH5DictNode.datac                 C   s   || _ | jr|  ¡  d S r   )r   r   r"   )r   Znew_data_dictr   r   r   r   [   s    c                 C   s   |   ¡  |  ¡  dS )z Write buffered data to disk. N)Ú_remove_pyobject_nodeÚ_write_pyobject_noder(   r   r   r   r"   a   s    zH5DictNode.flushNc                 K   s4   |  |¡ || }| j|j||d� | |fi |¤ŽS )a]  Add dict node to an H5 file at the specified path.

        Parameters
        ----------
        h5 : H5File
            The H5 file where the dictionary data will be stored.
        node_path : str
            Path to node where data is stored (e.g. '/path/to/my_dict')
        data : dict
            Data for initialization, if desired.
        r   )Zcreate_groupÚ_create_pyobject_nodeZ_h5)ÚclsÚh5Ú	node_pathr   ÚkwargsÚgroupr   r   r   Úadd_to_h5filef   s    
zH5DictNode.add_to_h5filec                 C   s:   ddl m} t||ƒr |  |¡}t|tƒs.dS | j|jv S )zÔReturn True if PyTables node looks like an H5DictNode.

        NOTE: That this returns False if the node is an `H5DictNode` instance,
        since the input node should be a normal PyTables Group node.
        é   )ÚH5GroupF)Úfiler3   Ú
isinstancer   ÚPyTablesGroupr   Ú_v_children)r,   Zpytables_noder3   r   r   r   r   y   s    


zH5DictNode.is_dict_nodec                 C   sB   | j j ¡ D ]}|| jkr| j  |¡ ¡  q|  ¡  | j  ¡  dS )zpThis is called by H5File whenever a node is removed.

        All nodes in `_h5_group` will be removed.
        N)r   r7   r'   r   Ú__getattr__Ú	_f_remover)   )r   Únamer   r   r   r9   �   s
    
zH5DictNode._f_removec                 C   s(   t |v r$|t }t| j|ƒdd… S |S )z°This gets passed object dictionaries by `json.load(s)` and if it
        finds `ARRAY_PROXY_KEY` in the object description it returns the
        proxied array object.
        N)ÚARRAY_PROXY_KEYÚNODE_KEYr   r   )r   Údctr   r   r   r   r   œ   s    zH5DictNode._object_hookc                 C   s   t | j| jƒ}| ¡  d S r   )r   r   r   r9   )r   Únoder   r   r   r)   ¦   s    z H5DictNode._remove_pyobject_nodec                 C   s$   | j j}| j j}|  ||| j¡ d S r   )r   Z_v_fileZ_v_pathnamer+   r   )r   Úpyt_filer.   r   r   r   r*   ª   s    zH5DictNode._write_pyobject_nodec                 C   sx   |d u ri }|   |||¡}t|| jd�}ttj|fi |¤Žƒ�&}| t |¡ 	d¡¡ W d   ƒ n1 sj0    Y  d S )N)Úwherer:   r	   )
Ú_handle_array_valuesÚdictr   r   r   Únew_nodeÚwriter   ÚdumpsÚencode)r,   r?   r.   r   Úout_datar/   r   r   r   r   r+   ¯   s    z H5DictNode._create_pyobject_nodec                 C   s   t |dƒr|j}|S )Nr   )Úhasattrr   )r   r0   r   r   r   r   ¼   s    
zH5DictNode._get_pyt_groupc                 C   s.   ||v r|  ||¡ | |||¡ tdt|iS )a~  Stores an array as a normal H5 node and returns the proxy object
        which will be serialized to JSON.

        `ARRAY_PROXY_KEY` marks the object dictionary as an array proxy so that
        `_object_hook` can recognize it. `NODE_KEY` stores the node name of the
        array so that `_object_hook` can load the array data when the dict node
        is deserialized.

        T)Úremove_nodeZcreate_arrayr;   r<   )r,   r?   r0   r   Úarrayr   r   r   Ú_array_proxyÂ   s    zH5DictNode._array_proxyc           
      C   s�   |  |¡}i }| ¡ D ]4}|| }t|tƒrB|  ||||¡||< q|||< q|j}g }	| ¡ D ]}||vr^|	 |¡ q^|	D ]}| ||¡ qz|S r   )Zget_noder'   r5   r   rK   r7   ÚappendrI   )
r,   r?   Z
group_pathr   r0   rG   r   r#   Zpyt_childrenZnodes_to_remover   r   r   rA   Ò   s    


zH5DictNode._handle_array_values)T)N)N)Ú__name__Ú
__module__Ú__qualname__Ú__doc__r   r   r    r$   r%   r&   r'   Úpropertyr   Úsetterr"   Úclassmethodr1   r   r9   r   r)   r*   r+   r   rK   rA   r   r   r   r   r      s:   






r   )Ú
contextlibr   r   Únumpyr   Ztablesr   r6   Ztables.nodesr   r;   r<   Úobjectr   r   r   r   r   Ú<module>
   s   