3
6ßL]·  ã               @   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   sj   | j |ƒst‚| j|ƒ}|| _|| _t|| jƒ}ttj	|ƒƒ�"}t
j|jƒ jdƒ| jd�| _W d Q R X d S )NÚascii)Úobject_hook)Úis_dict_nodeÚAssertionErrorÚ_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   ú>/tmp/pip-install-5sebvq2j/apptools/apptools/io/h5/dict_node.pyÚ__init__%   s    
zH5DictNode.__init__c             C   s
   | j | S )N)Údata)r   Úkeyr   r   r   Ú__getitem__7   s    zH5DictNode.__getitem__c             C   s   || j |< | jr| jƒ  d S )N)r   r   Úflush)r   r   Úvaluer   r   r   Ú__setitem__:   s    
zH5DictNode.__setitem__c             C   s   | j |= | jr| jƒ  d S )N)r   r   r   )r   r   r   r   r   Ú__delitem__?   s    zH5DictNode.__delitem__c             C   s
   || j kS )N)r   )r   r   r   r   r   Ú__contains__D   s    zH5DictNode.__contains__c             C   s   t | jjƒ ƒS )N)Úlistr   Úkeys)r   r   r   r   r%   G   s    zH5DictNode.keysc             C   s   | j S )N)r   )r   r   r   r   r   N   s    zH5DictNode.datac             C   s   || _ | jr| jƒ  d S )N)r   r   r   )r   Znew_data_dictr   r   r   r   R   s    c             C   s   | j ƒ  | jƒ  dS )z Write buffered data to disk. N)Ú_remove_pyobject_nodeÚ_write_pyobject_node)r   r   r   r   r   X   s    zH5DictNode.flushNc             K   s0   |j |ƒ || }| j|j||d� | |f|Ž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_h5file]   s    
zH5DictNode.add_to_h5filec             C   s:   ddl m} t||ƒr | j|ƒ}t|tƒs.dS | j|jk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)Úfiler0   Ú
isinstancer   ÚPyTablesGroupr   Ú_v_children)r)   Zpytables_noder0   r   r   r   r   p   s    


zH5DictNode.is_dict_nodec             C   sJ   x2t | jjjƒ ƒD ]}|| jkr| jj|ƒjƒ  qW | jƒ  | jjƒ  dS )zq This is called by H5File whenever a node is removed.

        All nodes in `_h5_group` will be removed.
        N)r$   r   r4   r%   r   Ú__getattr__Ú	_f_remover&   )r   Únamer   r   r   r6   †   s
    
zH5DictNode._f_removec             C   s(   t |k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ƒ}|jƒ  d S )N)r   r   r   r6   )r   Únoder   r   r   r&   �   s    z H5DictNode._remove_pyobject_nodec             C   s$   | j j}| j j}| j||| jƒ d S )N)r   Z_v_fileZ_v_pathnamer(   r   )r   Úpyt_filer+   r   r   r   r'   ¡   s    zH5DictNode._write_pyobject_nodec          
   C   s`   |d kri }| j |||ƒ}t|| jd�}ttj|f|Žƒ�}|jtj|ƒj	dƒƒ W d Q R X d S )N)Úwherer7   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   r-   r   r   r   r   ³   s    
zH5DictNode._get_pyt_groupc             C   s.   ||kr|j ||ƒ |j|||ƒ 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_arrayr8   r9   )r)   r<   r-   r   Úarrayr   r   r   Ú_array_proxy¹   s    zH5DictNode._array_proxyc       	      C   sˆ   |j |ƒ}i }xDt|jƒ ƒD ]4}|| }t|tƒrH| j||||ƒ||< q|||< qW |j}x(t|jƒ ƒD ]}||krh|j||ƒ qhW |S )N)Zget_noder$   r%   r2   r   rH   r4   rF   )	r)   r<   Z
group_pathr   r-   rD   r   r    Zpyt_childrenr   r   r   r>   É   s    

zH5DictNode._handle_array_values)T)N)N)Ú__name__Ú
__module__Ú__qualname__Ú__doc__r   r   r   r!   r"   r#   r%   Úpropertyr   Úsetterr   Úclassmethodr.   r   r6   r   r&   r'   r(   r   rH   r>   r   r   r   r   r      s.   

r   )Ú
contextlibr   r   Znumpyr   Útablesr   r3   Ztables.nodesr   r8   r9   Úobjectr   r   r   r   r   Ú<module>   s   