a
    SicW                     @   s
  d Z ddlZddlZddlm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Ze jZe jZejdd ZdZee jkrdZejejdZe dd e! D Z"edkre#ej$ej%d e"#ej$dej%di edkr<e#dej&i e"#ej&di edkrfe#dej'i e"#ej'di dd Z(dd Z)dd dd d d d!d e(e)d"d d#Z*erd$d e*d%< erd&d e*d'< d(d) Z+d*d+ Z,d,d- Z-d.d/ Z.d6d0d1Z/d7d2d3Z0G d4d5 d5eZ1dS )8z:
    Implements high-level support for HDF5 file objects.
    N)warn   )filename_decodefilename_encode)phil	with_phil)Group   )h5h5fh5ph5ih5fd_objects)version   FT)earliestlatestc                 c   s   | ]\}}||fV  qd S N ).0xyr   r   J/var/www/html/django/DPS/env/lib/python3.9/site-packages/h5py/_hl/files.py	<genexpr>$       r   )r   
   r	   )v108v110r   r   )r         Zv112)r      r   Zv114c                 K   s:   t stdddl}|d|j  | jf i | 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
setdefaultMPIInfoZset_fapl_mpio)plistkwargsZmpi4pyr   r   r   _set_fapl_mpio2   s
    r*   c                 K   s   |  tj|d dS )z@Set the Python file object driver in a file access property listfileobjN)set_fileobj_driverr   fileobj_drivergetr(   r)   r   r   r   _set_fapl_fileobj<   s    r0   c                 K   s   | j f i |S r   )set_fapl_sec2r/   r   r   r   <lambda>B   r   r2   c                 K   s   | j f i |S r   )set_fapl_stdior/   r   r   r   r2   C   r   c                 K   s   | j f i |S r   )set_fapl_corer/   r   r   r   r2   D   r   c                 K   s   | j f d|  i|S )N	memb_fapl)set_fapl_familycopyr/   r   r   r   r2   E   s   c                 K   s   | j f i |S r   )set_fapl_splitr/   r   r   r   r2   K   r   )sec2stdiocorefamilympior+   splitc                 K   s   | j f i |S r   )Zset_fapl_ros3r/   r   r   r   r2   O   r   ros3c                 K   s   | j f i |S r   )Zset_fapl_directr/   r   r   r   r2   R   r   directc                 C   s   |t | < dS )zRegister 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_driverU   s    
rE   c                 C   s
   t | = dS )znUnregister a custom driver.

    Parameters
    ----------
    name : str
        The name of the driver.
    NrA   )rC   r   r   r   unregister_driverb   s    rF   c                   C   s   t tS )zFReturn a frozenset of the names of all of the registered drivers.
    )	frozensetrB   r   r   r   r   registered_driversm   s    rH   c                 K   s  t t j}|dur@|tv r,t| }tj}qNdd |D \}}ntjtj }}||| ||	|
 t	|
 }|dur||d< |dur||d< |dur||d< |j|  |r|t|t|t| |dur`tdk rtdd dkstd d	k rtd
|dv r|jddd nB|dv r8|jddd n(|dkrR|jddd ntd| | du s| dkrtjdkr|rdjtt|d}t||S zt|  }W n  ty   td|  Y n0 ||fi | |S )z$ Set up a file access property list Nc                 s   s   | ]}t | V  qd S r   )libver_dictr   r   r   r   r   r   ~   r   zmake_fapl.<locals>.<genexpr>r   r	   r   r      r   r   r      zDHDF version >= 1.12.1 or 1.10.x >= 1.10.7 required for file locking.)falseFF)ignore_when_disabled)trueTTzbest-effortzUnsupported locking value: windowswin32z8'{key}' is an invalid keyword argument for this function)keyzUnknown driver type "%s")r   createFILE_ACCESSrI   r   LIBVER_LATESTLIBVER_EARLIESTset_libver_boundsset_alignmentlist	get_cache	set_cacheset_page_buffer_sizeinthdf5_versionr$   set_file_lockingsysplatformformatnextiter	TypeErrorrB   KeyError)driverlibverrdcc_nslotsrdcc_nbytesrdcc_w0lockingpage_buf_sizemin_meta_keepmin_raw_keepalignment_thresholdalignment_intervalkwdsr(   lowhighZcache_settingsmsgrD   r   r   r   	make_fapls   s\    

(


 
rx   c                 C   s   | s|rt t j}| r<|t jt jB  |t jt jB  |rtjtj	tj
tjd}||d}|dkrrtd|||| |r|dkr|t| nd}|S )z& Set up a file creation property list )Zfsmpage	aggregatenonez Invalid file space strategy typery   N)r   rU   FILE_CREATEset_link_creation_orderCRT_ORDER_TRACKEDCRT_ORDER_INDEXEDset_attr_creation_orderr   FSPACE_STRATEGY_FSM_AGGRFSPACE_STRATEGY_PAGEFSPACE_STRATEGY_AGGRFSPACE_STRATEGY_NONEr.   r$   set_file_space_strategyset_file_space_page_sizer_   )track_orderfs_strategy
fs_persistfs_thresholdfs_page_sizer(   Z
strategiesZfs_strat_numr   r   r   	make_fcpl   s.    

r   c           
      C   s  |durb|dv rt dzt|}W n tt fyB   t dY n0 |du rXttj}|| |dkrtj}|rt	r|tj
O }tj| ||d}n|dkrtj| tj|d}n|dv rtj| tj||d	}n|d
krtj| tj||d	}n|dkrvztj| tj|d}W nf | tjtr&tjndtjtjtjtr@tjndfv rNtntyr   tj| tj||d	}Y n0 nt dz8|dur| }| |krt d|| f W n2 ty }	 z|  |	W Y d}	~	n
d}	~	0 0 |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 integerr   )faplr   )w-r   )r   fcplwar|   z/Invalid mode; must be one of r, r+, w, w-, x, azGRequested userblock size (%d) does not match that of existing file (%d))r$   r_   rg   r   rU   r}   set_userblockr   
ACC_RDONLYswmr_supportACC_SWMR_READopenACC_RDWRACC_EXCL	ACC_TRUNC
get_driverr   SEC2
direct_vfdDIRECTFAMILYWINDOWSr-   r?   ROS3DFileNotFoundErrorOSErrorget_create_plistget_userblock	Exceptionclose)
rC   modeuserblock_sizer   r   swmrflagsfidZexisting_fcpler   r   r   make_fid   sZ    




r   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redkre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   sB   ddl m} t || d W  d   S 1 s40    Y  dS )z$ Attributes attached to this object r   )attrs/N) r   r   AttributeManager)selfr   r   r   r   r     s    z
File.attrsc                 C   s   t t| jS )zFile name on disk)r   r   get_nameidr   r   r   r   filename  s    zFile.filenamec                 C   sf   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}t	rBd	|t j
< trPd
|t j< || j  dS )z,Low-level HDF5 file driver used to open filer9   r:   r;   r<   rR   r=   Zmpiposixr+   r?   r@   unknown)r   r   STDIOCOREr   r   MPIOMPIPOSIXr-   r?   r   r   r   r.   r   get_access_plistr   )r   Zdriversr   r   r   ri   !  s    

zFile.driverc                 C   s*   t j}tr|t jO }| j |@ r&dS dS )z Python mode used to open file r   r   )r   r   r   ACC_SWMR_WRITEr   
get_intent)r   Zwrite_intentr   r   r   r   3  s    
z	File.modec                 C   s    | j   }tdd |D S )z/File format version bounds (2-tuple: low, high)c                 s   s   | ]}t | V  qd S r   )libver_dict_rrJ   r   r   r   r   A  r   zFile.libver.<locals>.<genexpr>)r   r   get_libver_boundstuple)r   boundsr   r   r   rj   <  s    zFile.libverc                 C   s   | j  }| S )z User block size (in bytes) )r   r   r   )r   r   r   r   r   r   C  s    
zFile.userblock_size)r      	   c                 C   s
   | j  S )z( Set/get MPI-IO atomic mode
            )r   Zget_mpi_atomicityr   r   r   r   atomicL  s    zFile.atomicc                 C   s   | j | d S r   )r   Zset_mpi_atomicityr   valuer   r   r   r   S  s    c                 C   s   t ot| j tjtjB @ S )z- Controls single-writer multiple-reader mode )r   boolr   r   r   r   r   r   r   r   r   	swmr_modeY  s    zFile.swmr_modec                 C   s0   t r|r| 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 {}.{}.{}.)r   r   start_swmr_writer$   RuntimeErrorrd   r`   r   r   r   r   r   _  s
    
r   NFr   r   c                    s  |s|rt dk rtd|r(ts(td|dkr<ts<td|durpt dk rpt dd d	ksht d d
k rptdt|tjr|rtdt t	|}W d   n1 s0    Y  nBt
|drt
|dr|dvrtdd}|d||krtd|j|d t|dd}nt|}|
du r8t j}
|rP|dvrPtd|rl|dkrltddd t\ t|||||	||||f	||d|}t|
||||d}t||||||d}W d   n1 s0    Y  t|tr|| _n
|df| _t | dS )aW  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', 'direct', 'stdio', 'mpio', 'ros3'.
        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_page_size
            File space page size in bytes. Only used when fs_strategy="page". If
            None use the HDF5 default (4096 bytes).
        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.
        page_buf_size
            Page buffer size in bytes. Only allowed for HDF5 files created with
            fs_strategy="page". Must be a power of two value and greater or
            equal than the file space page size when creating the file. It is
            not used by default.
        min_meta_keep
            Minimum percentage of metadata to keep in the page buffer before
            allowing pages containing metadata to be evicted. Applicable only if
            page_buf_size is set. Default value is zero.
        min_raw_keep
            Minimum percentage of raw data to keep in the page buffer before
            allowing pages containing raw data to be evicted. Applicable only if
            page_buf_size is set. Default value is zero.
        locking
            The file locking behavior. Defined as:
            False (or "false")  Disable file locking
            True (or "true")    Enable file locking
            "best-effort"       Enable file locking but ignore some errors
            None                Use HDF5 defaults
            Warning: The HDF5_USE_FILE_LOCKING environment variable can override
            this parameter.
            Only available with HDF5 >= 1.12.1 or 1.10.x >= 1.10.7.

        alignment_threshold
            Together with ``alignment_interval``, this property ensures that
            any file object greater than or equal in size to the alignement
            threshold (in bytes) will be aligned on an address which is a
            multiple of alignment interval.

        alignment_interval
            This property should be used in conjunction with
            ``alignment_threshold``. See the description above. For more
            details, see
            https://portal.hdfgroup.org/display/HDF5/H5P_SET_ALIGNMENT

        Additional keywords
            Passed on to the selected file driver.
        )r   r   r   zYHDF version 1.10.1 or greater required for file space strategy or page buffering support.zEThe SWMR feature is not available in this version of the HDF5 libraryr?   z:h5py was built without ROS3 support, can't use ros3 driverNrK   r	   rM   rN   zLHDF version >= 1.12.1 or 1.10.x >= 1.10.7 required for file locking options.z5Unable to set file space strategy of an existing filereadseek)Nr+   z;Driver must be 'fileobj' for file-like object if specified.r+   zQInvalid value of 'fileobj' argument; must equal to file-like object if specified.)r+   ASCIIreplace)r   r   r   r   zkswmr=True only affects read ('r') mode. For swmr write mode, set f.swmr_mode = True after opening the file.)
stacklevel)rr   rs   )r   r   r   r   r   )r   r   )r`   r$   r   r?   
isinstancer   ObjectIDr   r   get_file_idhasattrr.   updatereprencoder   r
   
get_configr   r   rx   r   r   r   Z_libversuper__init__)r   rC   r   ri   rj   r   r   rk   rl   rm   r   r   r   r   r   ro   rp   rq   rn   rr   rs   rt   r   r   r   	__class__r   r   r   k  sn    s
,

4
zFile.__init__c                 C   sl   t T | jjrJ| jtjtj B  | jtjtjB  | j  t	  W d   n1 s^0    Y  dS )z2 Close the file.  All open objects become invalid N)
r   r   valid_close_open_objectsr   	OBJ_LOCALOBJ_FILEr   r   nonlocal_closer   r   r   r   r     s    
z
File.closec                 C   s4   t  t| j W d   n1 s&0    Y  dS )z5 Tell the HDF5 library to flush its buffers.
        N)r   r   flushr   r   r   r   r   r   -  s    z
File.flushc                 C   s   | S r   r   r   r   r   r   	__enter__3  s    zFile.__enter__c                 G   s   | j r|   d S r   )r   r   )r   argsr   r   r   __exit__7  s    zFile.__exit__c                 C   sH   | j sd}n8| j}t|tr(|dd}dtj| d| j d}|S )Nz<Closed HDF5 file>utf8r   z<HDF5 file "z" (mode z)>)	r   r   r   bytesdecodeospathbasenamer   )r   r   r   r   r   r   __repr__<  s    
zFile.__repr__)r   NNNFNNNNNFr   NNr   r   Nr   r   )__name__
__module____qualname____doc__propertyr   r   r   ri   r   rj   r   r#   r`   r   setterr   r   r   r   r   r   r   __classcell__r   r   r   r   r     s\   

      4

r   )FNFr   N)NF)2r   rb   r   warningsr   compatr   r   baser   r   groupr   r   r
   r   r   r   r   r   r   r   r#   r?   r   hdf5_version_tupler`   r   swmr_min_hdf5_versionrX   rW   rI   dictitemsr   r   
LIBVER_V18LIBVER_V110LIBVER_V112ZLIBVER_V114r*   r0   rB   rE   rF   rH   rx   r   r   r   r   r   r   r   <module>
   sb    






>  

>