3
_½µ_¬E  ã               @   sš  d Z ddlZddlZddlmZmZ ddlmZmZ ddl	m
Z
 ddlmZmZmZmZmZmZ dd	lmZ ejƒ jZejdd
… ZdZeejƒ jkršdZejejdœZedd„ ejƒ D ƒƒZed0krðejej ej!dœƒ ejej dej!diƒ ed1k�rejdej"iƒ ejej"diƒ dd„ Z#dd„ Z$dd„ dd„ dd„ dd„ e#e$d d„ d!œZ%d"d#„ Z&d$d%„ Z'd&d'„ Z(d(d)„ Z)d2d*d+„Z*d3d,d-„Z+G d.d/„ d/e
ƒZ,dS )4z:
    Implements high-level support for HDF5 file objects.
é    Né   )Úfilename_decodeÚfilename_encode)ÚphilÚ	with_phil)ÚGroupé   )Úh5Úh5fÚh5pÚh5iÚh5fdÚ_objects)Úversioné   FT)ZearliestÚlatestc             c   s   | ]\}}||fV  qd S )N© )Ú.0ÚxÚyr   r   ú0/tmp/pip-install-xpigntgx/h5py/h5py/_hl/files.pyú	<genexpr>!   s    r   é
   )Úv108Úv110r   r   é   é   Zv112c             K   s6   t stdƒ‚ddl}|jd|jjƒ ƒ | jf |Ž dS )z-Set file access property list for mpio driverz9h5py was built without MPI support, can't use mpio driverr   NÚinfo)ÚmpiÚ
ValueErrorZ
mpi4py.MPIÚ
setdefaultZMPIZInfoZset_fapl_mpio)ÚplistÚkwargsZmpi4pyr   r   r   Ú_set_fapl_mpio+   s
    r#   c             K   s   | j tj|jdƒƒ dS )z@Set the Python file object driver in a file access property listÚfileobjN)Zset_fileobj_driverr   Úfileobj_driverÚget)r!   r"   r   r   r   Ú_set_fapl_fileobj5   s    r'   c             K   s   | j f |ŽS )N)Zset_fapl_sec2)r!   r"   r   r   r   Ú<lambda>;   s    r(   c             K   s   | j f |ŽS )N)Zset_fapl_stdio)r!   r"   r   r   r   r(   <   s    c             K   s   | j f |ŽS )N)Zset_fapl_core)r!   r"   r   r   r   r(   =   s    c             K   s   | j f d| jƒ i|—ŽS )NZ	memb_fapl)Zset_fapl_familyÚcopy)r!   r"   r   r   r   r(   >   s   c             K   s   | j f |ŽS )N)Zset_fapl_split)r!   r"   r   r   r   r(   D   s    )Úsec2ÚstdioÚcoreÚfamilyÚmpior$   Úsplitc             C   s   |t | < dS )zäRegister a custom driver.

    Parameters
    ----------
    name : str
        The name of the driver.
    set_fapl : callable[PropFAID, **kwargs] -> NoneType
        The function to set the fapl to use your custom driver.
    N)Ú_drivers)ÚnameÚset_faplr   r   r   Úregister_driverH   s    
r3   c             C   s
   t | = dS )znUnregister a custom driver.

    Parameters
    ----------
    name : str
        The name of the driver.
    N)r0   )r1   r   r   r   Úunregister_driverU   s    r4   c               C   s   t tƒS )zFReturn a frozenset of the names of all of the registered drivers.
    )Ú	frozensetr0   r   r   r   r   Úregistered_drivers`   s    r6   c             K   s  t jt jƒ}|dk	r@|tkr,t| }tj}qNdd„ |D ƒ\}}ntjtj }}|j||ƒ t|j	ƒ ƒ}	|dk	rv||	d< |dk	r†||	d< |dk	r–||	d< |j
|	Ž  | dksº| dkrÞtjdkrÞ|rÚd	jtt|ƒƒd
�}
t|
ƒ‚|S yt|  }W n" tk
�r   td|  ƒ‚Y nX ||f|Ž |S )z$ Set up a file access property list Nc             s   s   | ]}t | V  qd S )N)Úlibver_dict)r   r   r   r   r   r   o   s    zmake_fapl.<locals>.<genexpr>r   r   r   ÚwindowsÚwin32z8'{key}' is an invalid keyword argument for this function)ÚkeyzUnknown driver type "%s")r   ÚcreateZFILE_ACCESSr7   r
   ÚLIBVER_LATESTÚLIBVER_EARLIESTZset_libver_boundsÚlistZ	get_cacheZ	set_cacheÚsysÚplatformÚformatÚnextÚiterÚ	TypeErrorr0   ÚKeyErrorr   )ÚdriverÚlibverÚrdcc_nslotsÚrdcc_nbytesÚrdcc_w0Úkwdsr!   ÚlowÚhighZcache_settingsÚmsgr2   r   r   r   Ú	make_faplf   s8    
rO   c             C   sŠ   | s|r‚t jt jƒ}| r<|jt jt jB ƒ |jt jt jB ƒ |r†tjtj	tj
tjdœ}|j|dƒ}|dkrrtdƒ‚|j|||ƒ nd}|S )z& Set up a file creation property list )ZfsmÚpageZ	aggregateÚnoner   z Invalid file space strategy typeNéÿÿÿÿrR   )r   r;   ÚFILE_CREATEZset_link_creation_orderZCRT_ORDER_TRACKEDZCRT_ORDER_INDEXEDZset_attr_creation_orderr
   ZFSPACE_STRATEGY_FSM_AGGRZFSPACE_STRATEGY_PAGEZFSPACE_STRATEGY_AGGRZFSPACE_STRATEGY_NONEr&   r   Zset_file_space_strategy)Útrack_orderÚfs_strategyÚ
fs_persistÚfs_thresholdr!   Z
strategiesZfs_strat_numr   r   r   Ú	make_fcpl�   s$    
rX   c       	      C   s˜  |dk	rd|dkrt dƒ‚yt|ƒ}W n  tt fk
rD   t dƒ‚Y nX |dkrZtjtjƒ}|j|ƒ |dkr–tj}|r„t	r„|tj
O }tj| ||d�}n®|dkr²tj| tj|d�}n’|dkrÐtj| tj||d	�}nt|d
krîtj| tj||d	�}nV|dk�r<ytj| tj|d�}W n* tk
�r8   tj| tj||d	�}Y nX nt dƒ‚y8|dk	�rz|jƒ }|jƒ |k�rzt d||jƒ f ƒ‚W n   |jƒ  ‚ Y nX |S )zR Get a new FileID by opening or creating a file.
    Also validates mode argument.NÚrúr+z5User block may only be specified when creating a filez"User block size must be an integer)Úfaplúw-r   )r[   ÚfcplÚwÚaz/Invalid mode; must be one of r, r+, w, w-, x, azGRequested userblock size (%d) does not match that of existing file (%d))rY   rZ   )r\   r   )r   ÚintrD   r   r;   rS   Zset_userblockr
   Z
ACC_RDONLYÚswmr_supportÚACC_SWMR_READÚopenÚACC_RDWRZACC_EXCLZ	ACC_TRUNCÚIOErrorÚget_create_plistÚget_userblockÚclose)	r1   ÚmodeÚuserblock_sizer[   r]   ÚswmrÚflagsÚfidZexisting_fcplr   r   r   Úmake_fidª   sH    



rn   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r–ed%kr–eedd„ ƒƒZejedd„ ƒƒZeedd„ ƒƒZejedd„ ƒƒZd&‡ fdd„	Zdd„ Zdd„ Zedd „ ƒZed!d"„ ƒZed#d$„ ƒZ‡  ZS )'ÚFilez&
        Represents an HDF5 file.
    c          
   C   s*   ddl m} t� |j| d ƒS Q R X dS )z$ Attributes attached to this object r   )Úattrsú/N)Ú rp   r   ZAttributeManager)Úselfrp   r   r   r   rp   â   s    z
File.attrsc             C   s   t tj| jƒƒS )zFile name on disk)r   r
   Úget_nameÚid)rs   r   r   r   Úfilenameë   s    zFile.filenamec             C   sJ   t jdt jdt jdt jdt jdt jdt jdt jdi}|j	| j
jƒ jƒ d	ƒS )
z,Low-level HDF5 file driver used to open filer*   r+   r,   r-   r8   r.   Zmpiposixr$   Úunknown)r   ZSEC2ZSTDIOZCOREZFAMILYÚWINDOWSZMPIOZMPIPOSIXr%   r&   ru   Úget_access_plistZ
get_driver)rs   Zdriversr   r   r   rF   ñ   s    
zFile.driverc             C   s*   t j}tr|t jO }| jjƒ |@ r&dS dS )z Python mode used to open file zr+rY   )r
   rd   ra   ÚACC_SWMR_WRITEru   Ú
get_intent)rs   Zwrite_intentr   r   r   ri   ÿ   s    
z	File.modec             C   s    | j jƒ jƒ }tdd„ |D ƒƒS )z/File format version bounds (2-tuple: low, high)c             s   s   | ]}t | V  qd S )N)Úlibver_dict_r)r   r   r   r   r   r     s    zFile.libver.<locals>.<genexpr>)ru   ry   Zget_libver_boundsÚtuple)rs   Zboundsr   r   r   rG     s    zFile.libverc             C   s   | j jƒ }|jƒ S )z User block size (in bytes) )ru   rf   rg   )rs   r]   r   r   r   rj     s    
zFile.userblock_sizer   é   é	   c             C   s
   | j jƒ S )z( Set/get MPI-IO atomic mode
            )ru   Zget_mpi_atomicity)rs   r   r   r   Úatomic  s    zFile.atomicc             C   s   | j j|ƒ d S )N)ru   Zset_mpi_atomicity)rs   Úvaluer   r   r   r€     s    c             C   s   t ot| jjƒ tjtjB @ ƒS )z- Controls single-writer multiple-reader mode )ra   Úboolru   r{   r
   rb   rz   )rs   r   r   r   Ú	swmr_mode%  s    zFile.swmr_modec             C   s0   t r|r| jjƒ  q,tdƒ‚ntdjtŽ ƒ‚d S )Nz4It is not possible to forcibly switch SWMR mode off.z7SWMR support is not available in HDF5 version {}.{}.{}.)ra   ru   Zstart_swmr_writer   ÚRuntimeErrorrA   Úhdf5_version)rs   r�   r   r   r   rƒ   +  s
    
NFc                st  |rt dk rtdƒ‚|r&t r&tdƒ‚t|tjƒr\|r>tdƒ‚t� tj|ƒ}W dQ R X �nt	|dƒrºt	|dƒrº|dkr€td
ƒ‚d	}|j
d	|ƒ|krœtdƒ‚|j|d� t|ƒjddƒ}nt|ƒ}|
dkrÔtjƒ j}
|dkrætjƒ j}|� rþ|dk� rþtdƒ‚t�: t|||||	f|Ž}t||||t|
|||d�|d�}W dQ R X t|tƒ�rV|| _n
|df| _tt| ƒj|ƒ dS )a©  Create a new file object.

        See the h5py user guide for a detailed explanation of the options.

        name
            Name of the file on disk, or file-like object.  Note: for files
            created with the 'core' driver, HDF5 still requires this be
            non-empty.
        mode
            r        Readonly, file must exist (default)
            r+       Read/write, file must exist
            w        Create file, truncate if exists
            w- or x  Create file, fail if exists
            a        Read/write if exists, create otherwise
        driver
            Name of the driver to use.  Legal values are None (default,
            recommended), 'core', 'sec2', 'stdio', 'mpio'.
        libver
            Library version bounds.  Supported values: 'earliest', 'v108',
            'v110', 'v112'  and 'latest'. The 'v108', 'v110' and 'v112'
            options can only be specified with the HDF5 1.10.2 library or later.
        userblock_size
            Desired size of user block.  Only allowed when creating a new
            file (mode w, w- or x).
        swmr
            Open the file in SWMR read mode. Only used when mode = 'r'.
        rdcc_nbytes
            Total size of the raw data chunk cache in bytes. The default size
            is 1024**2 (1 MB) per dataset.
        rdcc_w0
            The chunk preemption policy for all datasets.  This must be
            between 0 and 1 inclusive and indicates the weighting according to
            which chunks which have been fully read or written are penalized
            when determining which chunks to flush from cache.  A value of 0
            means fully read or written chunks are treated no differently than
            other chunks (the preemption is strictly LRU) while a value of 1
            means fully read or written chunks are always preempted before
            other chunks.  If your application only reads or writes data once,
            this can be safely set to 1.  Otherwise, this should be set lower
            depending on how often you re-read or re-write the same data.  The
            default value is 0.75.
        rdcc_nslots
            The number of chunk slots in the raw data chunk cache for this
            file. Increasing this value reduces the number of cache collisions,
            but slightly increases the memory used. Due to the hashing
            strategy, this value should ideally be a prime number. As a rule of
            thumb, this value should be at least 10 times the number of chunks
            that can fit in rdcc_nbytes bytes. For maximum performance, this
            value should be set approximately 100 times that number of
            chunks. The default value is 521.
        track_order
            Track dataset/group/attribute creation order under root group
            if True. If None use global default h5.get_config().track_order.
        fs_strategy
            The file space handling strategy to be used.  Only allowed when
            creating a new file (mode w, w- or x).  Defined as:
            "fsm"        FSM, Aggregators, VFD
            "page"       Paged FSM, VFD
            "aggregate"  Aggregators, VFD
            "none"       VFD
            If None use HDF5 defaults.
        fs_persist
            A boolean value to indicate whether free space should be persistent
            or not.  Only allowed when creating a new file.  The default value
            is False.
        fs_threshold
            The smallest free-space section size that the free space manager
            will track.  Only allowed when creating a new file.  The default
            value is 1.
        Additional keywords
            Passed on to the selected file driver.

        r   r   zGHDF version 1.10.1 or greater required for file space strategy support.zEThe SWMR feature is not available in this version of the HDF5 libraryz5Unable to set file space strategy of an existing fileNÚreadÚseekr$   z;Driver must be 'fileobj' for file-like object if specified.zQInvalid value of 'fileobj' argument; must equal to file-like object if specified.)r$   ÚASCIIÚreplacer^   úw-r   )rT   rU   rV   rW   )r]   rk   r   )r   r   r   )Nr$   )r^   rŠ   r   )r…   r   ra   Ú
isinstancer   ZObjectIDr   r   Úget_file_idÚhasattrr&   ÚupdateÚreprÚencoder   r	   Ú
get_configrT   Zdefault_file_moderO   rn   rX   r}   Z_libverÚsuperro   Ú__init__)rs   r1   ri   rF   rG   rj   rk   rH   rI   rJ   rT   rU   rV   rW   rK   rm   r[   )Ú	__class__r   r   r“   7  sD    N



zFile.__init__c          
      s¼   t �® ˆ jjr®tjˆ jtj ƒ}tjˆ jtjƒ}‡ fdd„|D ƒ}‡ fdd„|D ƒ}x"|D ]}x|jrrtj|ƒ q`W qZW x"|D ]}x|jr–tj|ƒ q„W q~W ˆ jjƒ  t	j
ƒ  W dQ R X dS )z2 Close the file.  All open objects become invalid c                s$   g | ]}t j|ƒjˆ jjkr|‘qS r   )r   rŒ   ru   )r   r   )rs   r   r   ú
<listcomp>À  s    zFile.close.<locals>.<listcomp>c                s$   g | ]}t j|ƒjˆ jjkr|‘qS r   )r   rŒ   ru   )r   r   )rs   r   r   r•   Á  s    N)r   ru   Zvalidr
   Zget_obj_idsZOBJ_FILEr   Zdec_refrh   r   Znonlocal_close)rs   Zid_listÚ	file_listZid_r   )rs   r   rh   ´  s    


z
File.closec          	   C   s    t � tj| jƒ W dQ R X dS )z5 Tell the HDF5 library to flush its buffers.
        N)r   r
   Úflushru   )rs   r   r   r   r—   Î  s    z
File.flushc             C   s   | S )Nr   )rs   r   r   r   Ú	__enter__Ô  s    zFile.__enter__c             G   s   | j r| jƒ  d S )N)ru   rh   )rs   Úargsr   r   r   Ú__exit__Ø  s    zFile.__exit__c             C   sB   | j sd}n2| j}t|tƒr(|jddƒ}dtjj|ƒ| jf }|S )Nz<Closed HDF5 file>Úutf8r‰   z<HDF5 file "%s" (mode %s)>)	ru   rv   r‹   ÚbytesÚdecodeÚosÚpathÚbasenameri   )rs   rY   rv   r   r   r   Ú__repr__Ý  s    

zFile.__repr__)r   r~   r   )NNNNFNNNNNFr   )Ú__name__Ú
__module__Ú__qualname__Ú__doc__Úpropertyrp   r   rv   rF   ri   rG   rj   r   r…   r€   Úsetterrƒ   r“   rh   r—   r˜   rš   r¡   Ú__classcell__r   r   )r”   r   ro   Ü   s<   	   zro   )r   r   r   )r   r   r   )FNFr   )NF)-r¥   r?   rž   Úcompatr   r   Úbaser   r   Úgroupr   rr   r	   r
   r   r   r   r   r   r‘   r   Zhdf5_version_tupler…   ra   Zswmr_min_hdf5_versionr=   r<   r7   ÚdictÚitemsr|   rŽ   Z
LIBVER_V18ZLIBVER_V110ZLIBVER_V112r#   r'   r0   r3   r4   r6   rO   rX   rn   ro   r   r   r   r   Ú<module>   sH    


*

2