B
    \0                 @   s  d Z ddlmZ ddlZddlZddlZy ddlmZmZm	Z	m
Z
mZ W n0 ek
rx   ddlmZmZm	Z	m
Z
mZ Y nX ddlmZmZ ddlmZmZmZmZmZmZmZ dd	lmZmZ d
d Zdd Zdd Zdd Ze Z e Z!dd Z"G dd de#Z$G dd de#Z%G dd de$Z&G dd de	Z'G dd de
Z(G dd deZ)G d d! d!eZ*G d"d# d#e*eZ+G d$d% d%e#Z,dS )&zJ
    Implements operations common to all high-level objects (File, etc.).
    )absolute_importN)MappingMutableMappingKeysView
ValuesView	ItemsView   )fspathfilename_encode   )h5dh5ih5rh5ph5fh5th5s)phil	with_philc          	   C   s>   t 0 tjt| } tj| r0tt| S dS Q R X dS )z@ Determine if a file is valid HDF5 (False if it doesn't exist). FN)	r   ospathabspathr	   isfiler   is_hdf5r
   )fname r   ,lib/python3.7/site-packages/h5py/_hl/base.pyr   &   s
    r   c          	   C   s|   t n t| tjr tjtjdS t| tjr:tjtjdS t| tkrRtjtdS t| t	j
krntjt	j
dS dS Q R X dS )z Attempt to guess an appropriate dtype for the object, returning None
    if nothing is appropriate (or if it should be left up the the array
    constructor to figure out)
    )ref)ZvlenN)r   
isinstancer   ZRegionReferencer   Zspecial_dtypeZ	ReferencetypebytessixZ	text_type)datar   r   r   guess_dtype0   s    r#   c              C   s2   t t j} t t j}|tj | | | S )z# Default link access property list )r   createZLINK_ACCESSZFILE_ACCESSZset_fclose_degreer   ZCLOSE_STRONGZset_elink_fapl)ZlaplZfaplr   r   r   default_laplB   s
    
r%   c              C   s   t t j} | d | S )z% Default link creation property list T)r   r$   ZLINK_CREATEZset_create_intermediate_group)lcplr   r   r   default_lcplK   s    
r'   c             C   s   |    tjkrdS dS )z) Check if an object's dataspace is empty TF)Z	get_spaceZget_simple_extent_typer   ZNULL)objr   r   r   is_empty_dataspaceU   s    r)   c               @   s:   e Zd ZdZedd Zedd ZdddZd	d
 ZdS )CommonStateObjecta  
        Mixin class that allows sharing information between objects which
        reside in the same HDF5 file.  Requires that the host class have
        a ".id" attribute which returns a low-level ObjectID subclass.

        Also implements Unicode operations.
    c             C   s   t S )zI Fetch the link access property list appropriate for this object
        )dlapl)selfr   r   r   _laplf   s    zCommonStateObject._laplc             C   s   t S )zK Fetch the link creation property list appropriate for this object
        )dlcpl)r,   r   r   r   _lcpll   s    zCommonStateObject._lcplNc                s    fdd}|dkr |rdS dS t |tr2tj}n:y|d}tj}W n$ tk
rj   |d}tj}Y nX |r||||fS |S )aG   Encode a name according to the current file settings.

        Returns name, or 2-tuple (name, lcpl) if lcpl is True

        - Binary strings are always passed as-is, h5t.CSET_ASCII
        - Unicode strings are encoded utf8, h5t.CSET_UTF8

        If name is None, returns either None or (None, None) appropriately.
        c                s    j  }||  |S )z3 Create an appropriate link creation property list )r/   copyZset_char_encoding)codingr&   )r,   r   r   get_lcpl|   s    

z&CommonStateObject._e.<locals>.get_lcplN)NNasciiutf8)r   r    r   Z
CSET_ASCIIencodeUnicodeEncodeErrorZ	CSET_UTF8)r,   namer&   r2   r1   r   )r,   r   _er   s    




zCommonStateObject._ec             C   s0   |dkrdS y
| dS  tk
r*   Y nX |S )z Decode a name according to the current file settings.

        - Try to decode utf8
        - Failing that, return the byte string

        If name is None, returns None.
        Nr4   )decodeUnicodeDecodeError)r,   r7   r   r   r   _d   s    
zCommonStateObject._d)N)	__name__
__module____qualname____doc__propertyr-   r/   r8   r;   r   r   r   r   r*   \   s
   
!r*   c               @   s0   e Zd ZdZdd Zdd Zdd Zdd	 Zd
S )_RegionProxya	  
        Proxy object which handles region references.

        To create a new region reference (datasets only), use slicing syntax:

            >>> newref = obj.regionref[0:10:2]

        To determine the target dataset shape from an existing reference:

            >>> shape = obj.regionref.shape(existingref)

        where <obj> may be any object in the file. To determine the shape of
        the selection in use on the target dataset:

            >>> selection_shape = obj.regionref.selection(existingref)
    c             C   s   |j | _ d S )N)id)r,   r(   r   r   r   __init__   s    z_RegionProxy.__init__c          	   C   s^   t | jtjstdddlm} t. |j| jj	|| jd}t
| jdt
j|jS Q R X d S )Nz.Region references can only be made to datasetsr   )
selections)Zdsid   .)r   rB   r   Z	DatasetID	TypeError rD   r   Zselectshaper   r$   ZDATASET_REGION)r,   argsrD   	selectionr   r   r   __getitem__   s    z_RegionProxy.__getitem__c          	   C   s$   t  t|| j}|jS Q R X dS )z= Get the shape of the target dataspace referred to by *ref*. N)r   r   
get_regionrB   rH   )r,   r   sidr   r   r   rH      s    z_RegionProxy.shapec          	   C   s4   ddl m} t t|| j}||S Q R X dS )zN Get the shape of the target dataspace selection referred to by *ref*
        r   )rD   N)rG   rD   r   r   rL   rB   Zguess_shape)r,   r   rD   rM   r   r   r   rJ      s    z_RegionProxy.selectionN)r<   r=   r>   r?   rC   rK   rH   rJ   r   r   r   r   rA      s
   rA   c               @   s   e Zd ZdZedd Zeedd Zeedd Zeedd	 Z	eed
d Z
e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ZdS )HLObjectz:
        Base class for high-level interface objects.
    c          	   C   s(   ddl m} t || jS Q R X dS )z4 Return a File instance associated with this object r   )filesN)rG   rO   r   ZFilerB   )r,   rO   r   r   r   file   s    zHLObject.filec             C   s   |  t| jS )z: Return the full name of this object.  None if anonymous. )r;   r   Zget_namerB   )r,   r   r   r   r7      s    zHLObject.namec             C   s$   | j dkrtd| jt| j  S )zReturn the parent group of this object.

        This is always equivalent to obj.file[posixpath.dirname(obj.name)].
        ValueError if this object is anonymous.
        Nz*Parent of an anonymous object is undefined)r7   
ValueErrorrP   	posixpathdirname)r,   r   r   r   parent   s    
zHLObject.parentc             C   s   | j S )z2 Low-level identifier appropriate for this object )_id)r,   r   r   r   rB      s    zHLObject.idc             C   s   t | jdt jS )z+ An (opaque) HDF5 reference to this object rE   )r   r$   rB   ZOBJECT)r,   r   r   r   r      s    zHLObject.refc             C   s   t | S )az  Create a region reference (Datasets only).

        The syntax is regionref[<slices>]. For example, dset.regionref[...]
        creates a region reference in which the whole dataset is selected.

        Can also be used to determine the shape of the referenced dataset
        (via .shape property), or the shape of the selection (via the
        .selection property).
        )rA   )r,   r   r   r   	regionref   s    zHLObject.regionrefc          	   C   s&   ddl m} t || S Q R X dS )z$ Attributes attached to this object r   )attrsN)rG   rW   r   ZAttributeManager)r,   rW   r   r   r   rW     s    zHLObject.attrsc             C   s
   || _ dS )z3 Setup this object, given its low-level identifier N)rU   )r,   Zoidr   r   r   rC     s    zHLObject.__init__c             C   s
   t | jS )N)hashrB   )r,   r   r   r   __hash__  s    zHLObject.__hash__c             C   s   t |dr| j|jkS dS )NrB   F)hasattrrB   )r,   otherr   r   r   __eq__  s    
zHLObject.__eq__c             C   s   |  | S )N)r\   )r,   r[   r   r   r   __ne__!  s    zHLObject.__ne__c          	   C   s   t  t| jS Q R X d S )N)r   boolrB   )r,   r   r   r   __bool__%  s    zHLObject.__bool__N)r<   r=   r>   r?   r@   rP   r   r7   rT   rB   r   rV   rW   rC   rY   r\   r]   r_   Z__nonzero__r   r   r   r   rN      s&   rN   c               @   s   e Zd Zdd ZeZdS )KeysViewHDF5c             C   s   d t| S )Nz<KeysViewHDF5 {}>)formatlist)r,   r   r   r   __str__6  s    zKeysViewHDF5.__str__N)r<   r=   r>   rc   __repr__r   r   r   r   r`   5  s   r`   c               @   s    e Zd ZdZdd Zdd ZdS )ValuesViewHDF5z
        Wraps e.g. a Group or AttributeManager to provide a value view.

        Note that __contains__ will have poor performance as it has
        to scan all the links or attributes.
    c          	   C   s8   t * x"| jD ]}|| j|krdS qW dS Q R X d S )NTF)r   _mappingget)r,   valuekeyr   r   r   __contains__D  s
    zValuesViewHDF5.__contains__c          	   c   s2   t $ x| jD ]}| j|V  qW W d Q R X d S )N)r   rf   rg   )r,   ri   r   r   r   __iter__K  s    zValuesViewHDF5.__iter__N)r<   r=   r>   r?   rj   rk   r   r   r   r   re   ;  s   re   c               @   s    e Zd ZdZdd Zdd ZdS )ItemsViewHDF5zN
        Wraps e.g. a Group or AttributeManager to provide an items view.
    c          	   C   s6   t ( |\}}|| jkr(|| j|kS dS Q R X d S )NF)r   rf   rg   )r,   itemri   valr   r   r   rj   W  s
    
zItemsViewHDF5.__contains__c          	   c   s6   t ( x | jD ]}|| j|fV  qW W d Q R X d S )N)r   rf   rg   )r,   ri   r   r   r   rk   ^  s    zItemsViewHDF5.__iter__N)r<   r=   r>   r?   rj   rk   r   r   r   r   rl   Q  s   rl   c               @   sX   e Zd ZdZejr<dd Zdd Zdd Zdd	 Z	d
d Z
ndd Zdd Zdd	 Z	dS )MappingHDF5a  
        Wraps a Group, AttributeManager or DimensionManager object to provide
        an immutable mapping interface.

        We don't inherit directly from MutableMapping because certain
        subclasses, for example DimensionManager, are read-only.
    c          	   C   s   t 
 t| S Q R X dS )z$ Get a list containing member names N)r   rb   )r,   r   r   r   keysn  s    zMappingHDF5.keysc          	      s"   t   fdd D S Q R X dS )z& Get a list containing member objects c                s   g | ]}  |qS r   )rg   ).0x)r,   r   r   
<listcomp>v  s    z&MappingHDF5.values.<locals>.<listcomp>N)r   )r,   r   )r,   r   valuess  s    zMappingHDF5.valuesc             c   s   x| D ]}|  |V  qW dS )z% Get an iterator over member objects N)rg   )r,   rr   r   r   r   
itervaluesx  s    
zMappingHDF5.itervaluesc          	      s"   t   fdd D S Q R X dS )z6 Get a list of tuples containing (name, object) pairs c                s   g | ]}|  |fqS r   )rg   )rq   rr   )r,   r   r   rs     s    z%MappingHDF5.items.<locals>.<listcomp>N)r   )r,   r   )r,   r   items}  s    zMappingHDF5.itemsc             c   s"   x| D ]}||  |fV  qW dS )z+ Get an iterator over (name, object) pairs N)rg   )r,   rr   r   r   r   	iteritems  s    
zMappingHDF5.iteritemsc             C   s   t | S )z# Get a view object on member names )r`   )r,   r   r   r   rp     s    c             C   s   t | S )z% Get a view object on member objects )re   )r,   r   r   r   rt     s    c             C   s   t | S )z# Get a view object on member items )rl   )r,   r   r   r   rv     s    N)r<   r=   r>   r?   r!   ZPY2rp   rt   ru   rv   rw   r   r   r   r   ro   d  s   
ro   c               @   s   e Zd ZdZdS )MutableMappingHDF5z
        Wraps a Group or AttributeManager object to provide a mutable
        mapping interface, in contrast to the read-only mapping of
        MappingHDF5.
    N)r<   r=   r>   r?   r   r   r   r   rx     s   rx   c               @   s0   e Zd ZdZdZdZdd Zdd Zdd ZdS )	Emptyz
        Proxy object to represent empty/null dataspaces (a.k.a H5S_NULL).

        This can have an associated dtype, but has no shape or data. This is not
        the same as an array with shape (0,).
    Nc             C   s
   || _ d S )N)dtype)r,   rz   r   r   r   rC     s    zEmpty.__init__c             C   s   t |tr| j|jkrdS dS )NTF)r   ry   rz   )r,   r[   r   r   r   r\     s    zEmpty.__eq__c             C   s   d | jS )NzEmpty(dtype={0!r}))ra   rz   )r,   r   r   r   rd     s    zEmpty.__repr__)	r<   r=   r>   r?   rH   sizerC   r\   rd   r   r   r   r   ry     s   ry   )-r?   Z
__future__r   rR   r   r!   Zcollections.abcr   r   r   r   r   ImportErrorcollectionscompatr	   r
   rG   r   r   r   r   r   r   r   Z_objectsr   r   r   r#   r%   r'   r+   r.   r)   objectr*   rA   rN   r`   re   rl   ro   rx   ry   r   r   r   r   <module>   s6    "$
	I-c1