a
    zn„f0"  ã                   @  s†   d Z ddlmZ ddlZddlZddlZddlZddlZddl	ZG dd„ dej
jjƒZG dd„ dej
jjƒZG dd	„ d	ej
jjƒZdS )
aþ  
This module defines a physical layer for local files.

Defines a :doc:`uproot.source.file.FileResource` (wrapped Python file handle)
and two sources: :doc:`uproot.source.file.MultithreadedFileSource` and
:doc:`uproot.source.file.MemmapSource`, which provide thread-safe local
file readers using many file handles or a memory-mapped file, respectively.

If the filesystem or operating system does not support memory-mapped files, the
:doc:`uproot.source.file.MultithreadedFileSource` is an automatic fallback.
é    )ÚannotationsNc                   @  st   e Zd ZdZddœdd„Zedd„ ƒZedd	œd
d„ƒZdd„ Zdd„ Z	ddddœdd„Z
eddddœdd„ƒZdS )ÚFileResourcez–
    Args:
        file_path (str): The filesystem path of the file to open.

    A :doc:`uproot.source.chunk.Resource` for a simple file handle.
    Ústr©Ú	file_pathc              
   C  sT   || _ zt| j dƒ| _W n6 tjjyN } ztj |¡|‚W Y d }~n
d }~0 0 d S )NÚrb)Ú
_file_pathÚopenÚ_fileÚuprootZ_utilZ_FileNotFoundErrorZ_file_not_found)Úselfr   Úerr© r   úU/home/yuhw/wc/wire-cell-python/wcpy/lib/python3.9/site-packages/uproot/source/file.pyÚ__init__#   s
    zFileResource.__init__c                 C  s   | j S )z)
        The Python file handle.
        ©r
   ©r   r   r   r   Úfile*   s    zFileResource.fileÚbool©Úreturnc                 C  s   | j jS ©N)r
   Úclosedr   r   r   r   r   1   s    zFileResource.closedc                 C  s   | S r   r   r   r   r   r   Ú	__enter__5   s    zFileResource.__enter__c                 C  s   | j  |||¡ d S r   )r
   Ú__exit__©r   Zexception_typeZexception_valueÚ	tracebackr   r   r   r   8   s    zFileResource.__exit__ÚintÚbytes©ÚstartÚstopr   c                 C  s   | j  |¡ | j  || ¡S )a%  
        Args:
            start (int): Seek position of the first byte to include.
            stop (int): Seek position of the first byte to exclude
                (one greater than the last byte to include).

        Returns a Python buffer of data between ``start`` and ``stop``.
        )r
   ÚseekÚread)r   r    r!   r   r   r   Úget;   s    	zFileResource.getzuproot.source.chunk.Source)Úsourcer    r!   c                   s   ‡ ‡fdd„}t jj |¡S )aß  
        Args:
            source (:doc:`uproot.source.file.MultithreadedFileSource`): The
                data source.
            start (int): Seek position of the first byte to include.
            stop (int): Seek position of the first byte to exclude
                (one greater than the last byte to include).

        Returns a :doc:`uproot.source.futures.ResourceFuture` that calls
        :ref:`uproot.source.file.FileResource.get` with ``start`` and ``stop``.
        c                   s   |   ˆ ˆ¡S r   )r$   )Úresource©r    r!   r   r   ÚtaskU   s    z!FileResource.future.<locals>.task)r   r%   ÚfuturesZResourceFuture)r%   r    r!   r(   r   r'   r   ÚfutureG   s    zFileResource.futureN)Ú__name__Ú
__module__Ú__qualname__Ú__doc__r   Úpropertyr   r   r   r   r$   Ústaticmethodr*   r   r   r   r   r      s   
r   c                   @  sº   e Zd ZdZejjjjZddœdd„Z	dd„ Z
dd	„ Zd
d„ Zdd„ Zddddœdd„Zddddœdd„Zedd„ ƒZedd„ ƒZeddœdd „ƒZd!d"„ Zd#d$„ Zeddœd%d&„ƒZd'S )(ÚMemmapSourcezÙ
    Args:
        file_path (str): The filesystem path of the file to open.
        options: Must include ``"num_fallback_workers"``.

    A :doc:`uproot.source.chunk.Source` that manages one memory-mapped file.
    r   r   c                 K  s4   |d | _ || _d| _d| _d| _|| _|  ¡  d S )NZnum_fallback_workersr   )Ú_num_fallback_workersÚ_fallback_optsÚ_num_requestsÚ_num_requested_chunksÚ_num_requested_bytesr   Ú_open©r   r   Úoptionsr   r   r   r   f   s    
zMemmapSource.__init__c                 C  sl   z t j| j| jdd�| _d | _W nF tyf   d | _t| jƒ}| j	|d< t
jjj| jfi |¤Ž| _Y n0 d S )NÚr)ZdtypeÚmodeÚnum_workers)ÚnumpyZmemmapr   Ú_dtyper
   Ú	_fallbackÚOSErrorÚdictr3   r2   r   r%   r   ÚMultithreadedFileSource)r   Úoptsr   r   r   r7   p   s    


ÿÿzMemmapSource._openc                 C  s   t | jƒ}| d¡ |S )Nr
   ©rA   Ú__dict__Úpop©r   Ústater   r   r   Ú__getstate__|   s    

zMemmapSource.__getstate__c                 C  s   || _ |  ¡  d S r   ©rE   r7   rG   r   r   r   Ú__setstate__�   s    zMemmapSource.__setstate__c                 C  sh   t | jƒ}t| jƒdkr.t d| jdd …  ƒ}d}| jd ur@d}dt| ƒj› d|› |› dt| ƒd	›d
�S )Né
   z...iöÿÿÿÚ z with fallbackú<ú z at 0xZ012xú>)Úreprr   Úlenr?   Útyper+   Úid)r   ÚpathÚfallbackr   r   r   Ú__repr__…   s    

zMemmapSource.__repr__r   zuproot.source.chunk.Chunkr   c                 C  s”   | j d u r‚| jr td| j› �ƒ‚|  jd7  _|  jd7  _|  j|| 7  _| j||… }tj	j
 |¡}tj	jj| |||dd�S | j  ||¡S d S )Númemmap is closed for file é   T©Z	is_memmap)r?   r   r@   r   r4   r5   r6   r
   r   r%   r)   ÚTrivialFutureÚchunkÚChunk)r   r    r!   Údatar*   r   r   r   r\   Ž   s    
zMemmapSource.chunkzlist[int, int]zqueue.Queuezlist[uproot.source.chunk.Chunk])ÚrangesÚnotificationsr   c           	      C  sÌ   | j d u rº| jr td| j› �ƒ‚|  jd7  _|  jt|ƒ7  _|  jtdd„ |D ƒƒ7  _g }|D ]P\}}| j	||… }t
jj |¡}t
jjj| |||dd�}| |¡ | |¡ qd|S | j  ||¡S d S )NrX   rY   c                 s  s   | ]\}}|| V  qd S r   r   )Ú.0r    r!   r   r   r   Ú	<genexpr>§   ó    z&MemmapSource.chunks.<locals>.<genexpr>TrZ   )r?   r   r@   r   r4   r5   rR   r6   Úsumr
   r   r%   r)   r[   r\   r]   ÚputÚappendÚchunks)	r   r_   r`   rg   r    r!   r^   r*   r\   r   r   r   rg   ž   s"    

ÿ
zMemmapSource.chunksc                 C  s   | j S )z2
        The ``numpy.memmap`` array/file.
        r   r   r   r   r   r   ·   s    zMemmapSource.filec                 C  s   | j S )zþ
        If None, the :ref:`uproot.source.file.MemmapSource.file` opened
        successfully and no fallback is needed.

        Otherwise, this is a :doc:`uproot.source.file.MultithreadedFileSource`
        to which all requests are forwarded.
        )r?   r   r   r   r   rV   ¾   s    	zMemmapSource.fallbackr   r   c                 C  s    | j d u r| jjjS | j jS d S r   )r?   r
   Ú_mmapr   r   r   r   r   r   É   s    

zMemmapSource.closedc                 C  s4   | j d u r&t| jjdƒr0| jj ¡  n
| j  ¡  | S )Nr   )r?   Úhasattrr
   rh   r   r   r   r   r   r   Ð   s
    

zMemmapSource.__enter__c                 C  sN   | j d u r:t| jjdƒr,| jj |||¡ qJ| jj ¡  n| j  |||¡ d S )Nr   )r?   ri   r
   rh   r   Úcloser   r   r   r   r   Ø   s
    
zMemmapSource.__exit__c                 C  s"   | j d u r| jj ¡ S | j jS d S r   )r?   r
   rh   ÚsizeÚ	num_bytesr   r   r   r   rl   á   s    
zMemmapSource.num_bytesN)r+   r,   r-   r.   r   r%   r\   r]   r>   r   r7   rI   rK   rW   rg   r/   r   rV   r   r   r   rl   r   r   r   r   r1   [   s&   
	


	r1   c                   @  s:   e Zd ZdZeZddœdd„Zdd„ Zdd	„ Zd
d„ Z	dS )rB   a  
    Args:
        file_path (str): The filesystem path of the file to open.
        options: Must include ``"num_workers"`` and ``"use_threads"``.

    A :doc:`uproot.source.chunk.MultithreadedSource` that manages many
    :doc:`uproot.source.file.FileResource` objects.
    r   r   c                 K  s8   d| _ d| _d| _|d | _|d | _|| _|  ¡  d S )Nr   Zuse_threadsr<   )r4   r5   r6   Ú_use_threadsÚ_num_workersr   r7   r8   r   r   r   r   õ   s    

z MultithreadedFileSource.__init__c                   sV   ˆ j r,tjj ‡ fdd„tˆ jƒD ƒ¡ˆ _ntjj t	ˆ j
ƒ¡ˆ _tj ˆ j
¡ˆ _d S )Nc                   s   g | ]}t ˆ jƒ‘qS r   )r   r   )ra   Úxr   r   r   Ú
<listcomp>  rc   z1MultithreadedFileSource._open.<locals>.<listcomp>)rm   r   r%   r)   ZResourceThreadPoolExecutorÚrangern   Ú	_executorZResourceTrivialExecutorr   r   ÚosrU   ÚgetsizeZ
_num_bytesr   r   r   r   r7   ÿ   s    ÿÿzMultithreadedFileSource._openc                 C  s   t | jƒ}| d¡ |S )Nrr   rD   rG   r   r   r   rI   
  s    

z$MultithreadedFileSource.__getstate__c                 C  s   || _ |  ¡  d S r   rJ   rG   r   r   r   rK     s    z$MultithreadedFileSource.__setstate__N)
r+   r,   r-   r.   r   ZResourceClassr   r7   rI   rK   r   r   r   r   rB   é   s   	
rB   )r.   Ú
__future__r   Úos.pathrs   Úqueuer=   r   Zuproot.source.chunkZuproot.source.futuresr%   r\   ÚResourcer   ZSourcer1   ZMultithreadedSourcerB   r   r   r   r   Ú<module>   s   @ 