a
    Sic)                     @   s~   d Z ddlZddlZddlmZmZmZmZmZ ddlm	Z	 ddl	m
Z
mZmZmZmZ ddlmZ G d	d
 d
e	je	jZdS )z
    Implements high-level operations for attributes.

    Provides the AttributeManager class, available on high-level objects
    as <obj>.attrs.
    N   )h5h5sh5th5ah5p   )base)phil	with_philEmptyis_empty_dataspaceproduct)Datatypec                   @   s   e Zd ZdZdd Zedd Zdd Zedd	 Zed
d Z	dddZ
dd Zedd Zdd Zedd Zedd ZdS )AttributeManagera  
        Allows dictionary-style access to an HDF5 object's attributes.

        These are created exclusively by the library and are available as
        a Python attribute at <object>.attrs

        Like Group objects, attributes provide a minimal dictionary-
        style interface.  Anything which can be reasonably converted to a
        Numpy array or Numpy scalar can be stored.

        Attributes are automatically created on assignment with the
        syntax <obj>.attrs[name] = value, with the HDF5 type automatically
        deduced from the value.  Existing attributes are overwritten.

        To modify an existing attribute while preserving its type, use the
        method modify().  To specify an attribute of a particular type and
        shape, use create().
    c                 C   s   |j | _dS )z Private constructor.
        N)id_id)selfparent r   J/var/www/html/django/DPS/env/lib/python3.9/site-packages/h5py/_hl/attrs.py__init__/   s    zAttributeManager.__init__c           
      C   s   t | j| |}t|r&t|jS |j}|j}t	|}|j
dur^|j
\}}|j| }|}tj||dd}|j||d t|}	|	r|	jdu rtjdd |jD |d|j}t|jdkr|d	 S |S )
z) Read the value of an attribute.
        NC)dtypeordermtypec                 S   s   g | ]}| d dqS )zutf-8surrogateescape)decode).0br   r   r   
<listcomp>R   s   z0AttributeManager.__getitem__.<locals>.<listcomp>r   r   r   )r   openr   _er   r   r   shaper   	py_createsubdtypenumpyzerosreadcheck_string_dtypelengtharrayflatreshapelen)
r   nameattrr   r%   htyper'   subshapearrstring_infor   r   r   __getitem__4   s0    






zAttributeManager.__getitem__c                 C   s   t | j| |S )z?Get a low-level AttrID object for the named attribute.
        )r   r#   r   r$   r   r1   r   r   r   get_idZ   s    zAttributeManager.get_idc                 C   s   | j ||d dS )a   Set a new attribute, overwriting any existing attribute.

        The type and shape of the attribute are determined from the data.  To
        use a specific type or shape, or to preserve the type of an attribute,
        use the methods create() and modify().
        )dataN)create)r   r1   valuer   r   r   __setitem___   s    zAttributeManager.__setitem__c                 C   s   t | j| | dS )z1 Delete an attribute (which must already exist). N)r   deleter   r$   r8   r   r   r   __delitem__i   s    zAttributeManager.__delitem__Nc              
   C   s  t n t|ts tj||d}|du r0|j}nt|tr@|f}d}t|tr\|j}|j	}n|du rl|j	}n
t
	|}|}|jdur|j\}}|t| d |krtd||f |dt|t|  }|}nL|durt
j|t
jdt
j|jt
jdkrtd||jkr||}t|ts8t
j||d}|du r\tj|dd}	t|}
n|}	d}
t|tr~ttj}n
t|}d	}t| j| |rd}t j}n|}t| j| ||	|}zzTt|ts|j||
d
 |r"t | j| | t!| j| || | W n*   |"  t | j| |  Y n0 W |"  n
|"  0 W d   n1 s|0    Y  dS )a   Create a new attribute, overwriting any existing attribute.

        name
            Name of the new attribute (required)
        data
            An array to initialize the attribute (required)
        shape
            Shape of the attribute.  Overrides data.shape if both are
            given, in which case the total number of points must be unchanged.
        dtype
            Data type of the attribute.  Overrides data.dtype if both
            are given.
        )specified_dtypeNz7Array dtype shape %s is incompatible with data shape %sr   r"   z3Shape of new attribute conflicts with shape of dataT)logicalFr   )#r
   
isinstancer   r	   array_for_new_objectr%   intr   r   r   r(   r'   r0   
ValueErrorr   	ulonglongr/   asarrayr   r&   r   r;   NULLcreate_simpler   existsr   r$   uuiduuid4hexwriter>   renameclose)r   r1   r:   r%   r   Z	use_htypeoriginal_dtyper'   r4   r3   Zhtype2spaceZattr_existstempnamer2   r   r   r   r;   n   sh    





.


	 
zAttributeManager.createc                 C   s   t  || vr|| |< nt| j| |}t|r<tdt|tj	rLdn|j
}tj|d|d}|j|jkr|jdkrt|jdkstd|| W d   n1 s0    Y  dS )aL   Change the value of an attribute while preserving its type.

        Differs from __setitem__ in that if the attribute already exists, its
        type is preserved.  This can be very useful for interacting with
        externally generated files.

        If the attribute doesn't exist, it will be automatically created.
        z"Empty attributes can't be modifiedNr   )r   r   r   z5Shape of data is incompatible with existing attribute)r
   r   r#   r   r$   r   OSErrorrB   r(   ndarrayr   rG   r%   sizer   	TypeErrorrN   )r   r1   r<   r2   dtr   r   r   modify   s    	
zAttributeManager.modifyc                 C   s   t | jS )z. Number of attributes attached to the object. )r   get_num_attrsr   r   r   r   r   __len__   s    zAttributeManager.__len__c                 #   s   t f g   fdd}j }| }|  |tj@ rDtj}ntj	}t
jj||d W d   n1 sp0    Y   D ]
}|V  q~dS )z' Iterate over the names of attributes. c                    s     |  dS )z$ Callback to gather attribute names N)append_d)r1   argsZattrlistr   r   r   iter_cb  s    z*AttributeManager.__iter__.<locals>.iter_cb)
index_typeN)r
   r   get_create_plistget_attr_creation_orderrP   r   CRT_ORDER_TRACKEDr   INDEX_CRT_ORDER
INDEX_NAMEr   iterate)r   ra   ZcplZ	crt_orderidx_typer1   r   r`   r   __iter__   s    

0zAttributeManager.__iter__c                 C   s   t | j| |S )z, Determine if an attribute exists, by name. )r   rJ   r   r$   r8   r   r   r   __contains__  s    zAttributeManager.__contains__c                 C   s   | j s
dS dt| j  S )Nz"<Attributes of closed HDF5 object>z!<Attributes of HDF5 object at %s>)r   r   r[   r   r   r   __repr__  s    zAttributeManager.__repr__)NN)__name__
__module____qualname____doc__r   r   r7   r9   r=   r?   r;   rY   r\   rj   rk   rl   r   r   r   r   r      s$   
%
	

o

r   )rp   r(   rK    r   r   r   r   r   r	   r
   r   r   r   r   datatyper   MutableMappingHDF5CommonStateObjectr   r   r   r   r   <module>
   s   