a
    zn„fyD  ã                   @  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G dd„ dƒZej	G dd„ dƒƒZ
G dd	„ d	ƒZG d
d„ deƒZG dd„ dƒZdddœdd„ZdS )a·  
This module defines a :doc:`uproot.source.chunk.Chunk`, which is a range of bytes
requested from a file. All interaction between the "physical layer" and the
"interpretation layer" is through a :doc:`uproot.source.cursor.Cursor`'s
interpretation of a :doc:`uproot.source.chunk.Chunk`.

Also defines abstract classes for :doc:`uproot.source.chunk.Resource` and
:doc:`uproot.source.chunk.Source`, the primary types of the "physical layer."
é    )ÚannotationsNc                   @  s*   e Zd ZdZdd„ Zeddœdd„ƒZdS )	ÚResourcea  
    Abstract class for a file handle whose lifetime may be linked to threads
    in a thread pool executor.

    A :doc:`uproot.source.chunk.Resource` instance is always the first
    argument of functions evaluated by a
    :doc:`uproot.source.futures.ResourceFuture`.
    c                 C  s
   d | _ d S ©N©Ú
_file_path©Úself© r	   úV/home/yuhw/wc/wire-cell-python/wcpy/lib/python3.9/site-packages/uproot/source/chunk.pyÚ__init__"   s    zResource.__init__Ústr©Úreturnc                 C  s   | j S ©z.
        A path to the file (or URL).
        r   r   r	   r	   r
   Ú	file_path%   s    zResource.file_pathN)Ú__name__Ú
__module__Ú__qualname__Ú__doc__r   Úpropertyr   r	   r	   r	   r
   r      s   	r   c                   @  s8   e Zd ZU dZded< ded< ded< ddœdd	„Zd
S )ÚSourcePerformanceCountersz"Container for performance countersÚintÚnum_requested_bytesÚnum_requestsÚnum_requested_chunkszdict[str, int]r   c                 C  s
   t  | ¡S r   )ÚdataclassesÚasdictr   r	   r	   r
   r   5   s    z SourcePerformanceCounters.asdictN)r   r   r   r   Ú__annotations__r   r	   r	   r	   r
   r   -   s
   
r   c                   @  sÂ   e Zd ZdZdd„ Zddddœdd„Zd	d
ddœdd„Zeddœdd„ƒZeddœdd„ƒZ	eddœdd„ƒZ
eddœdd„ƒZeddœdd„ƒZeddœdd„ƒZdd„ Zed dœd!d"„ƒZd#S )$ÚSourceaa  
    Abstract class for physically reading and writing data from a file, which
    might be remote.

    In addition to the file handle, a :doc:`uproot.source.chunk.Source` might
    manage a :doc:`uproot.source.futures.ResourceThreadPoolExecutor` to read
    the file in parallel. Stopping these threads is part of the act of closing
    the file.
    c                 C  s(   d| _ d| _d| _d | _d | _d | _d S )Nr   )Ú_num_requested_bytesÚ_num_requestsÚ_num_requested_chunksr   Ú
_num_bytesÚ	_executorr   r	   r	   r
   r   D   s    zSource.__init__r   ÚChunk©ÚstartÚstopr   c                 C  s   d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).

        Request a byte range of data from the file as a
        :doc:`uproot.source.chunk.Chunk`.
        Nr	   )r   r&   r'   r	   r	   r
   ÚchunkL   s    zSource.chunkúlist[int, int]úqueue.Queueúlist[Chunk]©ÚrangesÚnotificationsr   c                 C  s   dS )aÌ  
        Args:
            ranges (list of (int, int) 2-tuples): Intervals to fetch
                as (start, stop) pairs in a single request, if possible.
            notifications (``queue.Queue``): Indicator of completed
                chunks. After each gets filled, it is ``put`` on the
                queue; a listener should ``get`` from this queue
                ``len(ranges)`` times.

        Request a set of byte ranges from the file.

        This method has two outputs:

        * The method returns a list of unfilled
          :doc:`uproot.source.chunk.Chunk` objects, which get filled
          in a background thread. If you try to read data from an
          unfilled chunk, it will wait until it is filled.
        * The method also puts the same :doc:`uproot.source.chunk.Chunk`
          objects onto the ``notifications`` queue as soon as they are
          filled.

        Reading data from chunks on the queue can be more efficient than
        reading them from the returned list. The total reading time is the
        same, but work on the filled chunks can be better parallelized if
        it is triggered by already-filled chunks, rather than waiting for
        chunks to be filled.
        Nr	   )r   r-   r.   r	   r	   r
   ÚchunksW   s    zSource.chunksr   r   c                 C  s   | j S r   r   r   r	   r	   r
   r   v   s    zSource.file_pathc                 C  s   | j S )z2
        The number of bytes in the file.
        )r"   r   r	   r	   r
   Ú	num_bytes}   s    zSource.num_bytesc                 C  s   | j S )zS
        The number of requests that have been made (performance counter).
        )r    r   r	   r	   r
   r   „   s    zSource.num_requestsc                 C  s   | j S )z€
        The number of :doc:`uproot.source.chunk.Chunk` objects that have been
        requested (performance counter).
        )r!   r   r	   r	   r
   r   ‹   s    zSource.num_requested_chunksc                 C  s   | j S )zU
        The number of bytes that have been requested (performance counter).
        )r   r   r	   r	   r
   r   “   s    zSource.num_requested_bytesr   c                 C  s   t | j| j| jƒS r   )r   r   r    r!   r   r	   r	   r
   Úperformance_countersš   s
    ýzSource.performance_countersc                 C  s   |   ddd¡ dS )zL
        Manually closes the file(s) and stops any running threads.
        N)Ú__exit__r   r	   r	   r
   Úclose¢   s    zSource.closeÚboolc                 C  s   | j du rdS | j jS )zh
        True if the associated file/connection/thread pool is closed; False
        otherwise.
        NT©r#   Úclosedr   r	   r	   r
   r6   ¨   s    
zSource.closedN)r   r   r   r   r   r(   r/   r   r   r0   r   r   r   r1   r3   r6   r	   r	   r	   r
   r   9   s&   
r   c                   @  s|   e Zd Z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œdd„ƒZ	eddœdd„ƒZ
dd„ Zdd„ ZdS )ÚMultithreadedSourcezŽ
    Abstract class for a :doc:`uproot.source.chunk.Source` that maintains a
    :doc:`uproot.source.futures.ResourceThreadPoolExecutor`.
    c              	   C  sZ   t | jƒ}t| jƒdkr.t d| jdd …  ƒ}dt| ƒj› d|› d| j› dt| ƒd›d	�	S )
Né
   z...iöÿÿÿú<ú z (z workers) at 0xZ012xú>)Úreprr   ÚlenÚtyper   Únum_workersÚid)r   Úpathr	   r	   r
   Ú__repr__º   s    
zMultithreadedSource.__repr__r   r$   r%   c                 C  s\   |  j d7  _ |  jd7  _|  j|| 7  _| j | ||¡}t| |||ƒ}| j |¡ |S )Né   )r    r!   r   ÚResourceClassÚfuturer$   r#   Úsubmit)r   r&   r'   rE   r(   r	   r	   r
   r(   À   s    zMultithreadedSource.chunkr)   r*   r+   r,   c                 C  s–   |  j d7  _ |  jt|ƒ7  _|  jtdd„ |D ƒƒ7  _g }|D ]L\}}| j | ||¡}t| |||ƒ}| t	||ƒ¡ | j
 |¡ | |¡ qD|S )NrC   c                 s  s   | ]\}}|| V  qd S r   r	   )Ú.0r&   r'   r	   r	   r
   Ú	<genexpr>Ï   ó    z-MultithreadedSource.chunks.<locals>.<genexpr>)r    r!   r=   r   ÚsumrD   rE   r$   Z_set_notifyÚnotifierr#   rF   Úappend)r   r-   r.   r/   r&   r'   rE   r(   r	   r	   r
   r/   Ê   s    zMultithreadedSource.chunksc                 C  s   | j S )zM
        The :doc:`uproot.source.futures.ResourceThreadPoolExecutor`
        )r#   r   r	   r	   r
   ÚexecutorÚ   s    zMultithreadedSource.executorr   c                 C  s   | j jS )z›
        The number of :doc:`uproot.source.futures.ResourceWorker` threads in
        the :doc:`uproot.source.futures.ResourceThreadPoolExecutor`.
        )r#   r?   r   r	   r	   r
   r?   á   s    zMultithreadedSource.num_workersr4   c                 C  s   | j jS )z—
        True if the :doc:`uproot.source.futures.ResourceThreadPoolExecutor` has
        been shut down and the file handles have been closed.
        r5   r   r	   r	   r
   r6   é   s    zMultithreadedSource.closedc                 C  s   | j  ¡  | S r   )r#   Ú	__enter__r   r	   r	   r
   rN   ñ   s    
zMultithreadedSource.__enter__c                 C  s   | j  |||¡ d S r   )r#   r2   )r   Zexception_typeZexception_valueÚ	tracebackr	   r	   r
   r2   õ   s    zMultithreadedSource.__exit__N)r   r   r   r   rB   r(   r/   r   rM   r?   r6   rN   r2   r	   r	   r	   r
   r7   ´   s   

r7   c                   @  s  e Zd ZdZe ej¡Zed1ddddœdd„ƒZ	d2dddd
dœdd„Z
dd„ Zeddœdd„ƒZeddœdd„ƒZeddœdd„ƒZedd„ ƒZed
dœdd„ƒZdd„ Zddœdd „Zd3d
d"œd#d$„Zeddœd%d&„ƒZddd'd(d)d*œd+d,„Zdd'd(d)d-œd.d/„Zd0S )4r$   aÒ  
    Args:
        source (:doc:`uproot.source.chunk.Source`): Source from which the
            data were derived.
        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).
        future (:doc:`uproot.source.futures.TrivialFuture` or :doc:`uproot.source.futures.Future`): Handle
            to the synchronous or asynchronous data. A chunk is "filled"
            when the ``future`` completes.

    A range of bytes from a :doc:`uproot.source.chunk.Source`, which may be
    synchronously or asynchronously filled.

    The following methods must wait for the
    :ref:`uproot.source.chunk.Chunk.future` to complete (to be filled):

    * :ref:`uproot.source.chunk.Chunk.wait`: Waits and nothing else.
    * :ref:`uproot.source.chunk.Chunk.raw_data`: The data as a
      ``numpy.ndarray`` of ``numpy.uint8``.
    * :ref:`uproot.source.chunk.Chunk.get`: A subinterval of the data as
      a ``numpy.ndarray`` of ``numpy.uint8``.
    * :ref:`uproot.source.chunk.Chunk.remainder`: A subinterval from the
      :doc:`uproot.source.cursor.Cursor` to the end of the
      :doc:`uproot.source.chunk.Chunk`.
    r   r   znumpy.ndarray | bytesr   )ÚsourceÚdatar&   c                 C  s$   t jj |¡}t|||t|ƒ |ƒS )aÞ  
        Args:
            source (:doc:`uproot.source.chunk.Source`): Source to attach to
                the new chunk.
            data (``numpy.ndarray`` of ``numpy.uint8``): Data for the new chunk.
            start (int): Virtual starting position for this chunk; if ``X``,
                then a :doc:`uproot.source.cursor.Cursor` is valid from ``X``
                to ``X + len(data)``.

        Manually creates a synchronous :doc:`uproot.source.chunk.Chunk`.
        )ÚuprootrP   ÚfuturesÚTrivialFuturer$   r=   )ÚclsrP   rQ   r&   rE   r	   r	   r
   Úwrap  s    z
Chunk.wrapFr4   )rP   r&   r'   Ú	is_memmapc                 C  s(   || _ || _|| _|| _d | _|| _d S r   )Ú_sourceÚ_startÚ_stopÚ_futureÚ	_raw_dataÚ
_is_memmap)r   rP   r&   r'   rE   rW   r	   r	   r
   r   '  s    zChunk.__init__c                 C  s   d| j › d| j› d�S )Nz<Chunk ú-r;   )rY   rZ   r   r	   r	   r
   rB   1  s    zChunk.__repr__r   c                 C  s   | j S )z:
        Source from which this Chunk is derived.
        )rX   r   r	   r	   r
   rP   4  s    zChunk.sourcec                 C  s   | j S )z=
        Seek position of the first byte to include.
        )rY   r   r	   r	   r
   r&   ;  s    zChunk.startc                 C  s   | j S )zq
        Seek position of the first byte to exclude (one greater than the last
        byte to include).
        )rZ   r   r	   r	   r
   r'   B  s    z
Chunk.stopc                 C  s   | j S )z|
        Handle to the synchronous or asynchronous data. A chunk is "filled"
        when the ``future`` completes.
        )r[   r   r	   r	   r
   rE   J  s    zChunk.futurec                 C  s   | j S )zÃ
        If True, the `raw_data` is or will be a view into a memmap file, which
        must be handled carefully. Accessing that data after the file is closed
        causes a segfault.
        )r]   r   r	   r	   r
   rW   R  s    zChunk.is_memmapc                 C  sˆ   | j r€| jdu r:| jdusJ ‚tjj tj| jdd�¡}n2t	| jtjjjƒsPJ ‚tjj tj| jj
dd�¡}t| j| j| j|ƒS | S dS )z¬
        Returns a Chunk (possibly this one) that is not tied to live memmap data.
        Such a Chunk can be accessed after the file is closed without segfaults.
        NT)Úcopy)r]   r[   r\   rR   rP   rS   rT   ÚnumpyÚarrayÚ
isinstanceÚ_resultr$   rX   rY   rZ   )r   rE   r	   r	   r
   Údetach_memmap[  s    
ÿÿzChunk.detach_memmapztuple[int, int])Úrangec                 C  sH   |\}}t |tjjjƒr|j}t |tjjjƒr4|j}| j|koF|| jkS r   )rb   rR   rP   ÚcursorZCursorÚindexrY   rZ   )r   re   r&   r'   r	   r	   r
   Ú__contains__p  s    zChunk.__contains__T©Úinsistc              	   C  sÒ   | j du rÎtj| j ¡ | jd�| _ |du rBt| j ƒ| j| j k}nHt	|t
jƒrdt| j ƒ|| j k}n&|du rrd}ntd|›d| jj› �ƒ‚|sÈtd| j| j › dt| j ƒ› d	t| jƒj› d| jj› �ƒ‚d| _dS )
aÖ  
        Args:
            insist (bool or int): If True, raise an OSError if ``raw_data`` does
                does not have exactly ``stop - start`` bytes. If False, do not check.
                If an integer, only raise an OSError if data up to that index can't
                be supplied (i.e. require ``len(raw_data) >= insist - start``).

        Explicitly wait until the chunk is filled (the
        :ref:`uproot.source.chunk.Chunk.future` completes).
        N)ÚdtypeTFz%insist must be a bool or an int, not z
for file path zexpected Chunk of length z,
received z bytes from )r\   r`   Z
frombufferr[   ÚresultÚ_dtyper=   rZ   rY   rb   ÚnumbersÚIntegralÚ	TypeErrorrX   r   ÚOSErrorr>   r   )r   rj   Úrequirementr	   r	   r
   Úwaitx  s0    
ÿÿÿ
ÿþÿz
Chunk.waitc                 C  s   |   ¡  | jS )zè
        Data from the Source as a ``numpy.ndarray`` of ``numpy.uint8``.

        This method will wait until the chunk is filled (the
        :ref:`uproot.source.chunk.Chunk.future` completes), if it isn't
        already.
        )rs   r\   r   r	   r	   r
   Úraw_data™  s    	zChunk.raw_datazuproot.source.cursor.CursorÚdictznumpy.ndarray)r&   r'   rf   Úcontextr   c                 C  sx   ||f| v r:| j |d� || j }|| j }| j||… S tj d|› d|› d| j› d| j› d�	| | ¡ || jj	¡‚d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).
            cursor (:doc:`uproot.source.cursor.Cursor`): A pointer to the
                current position in this chunk.
            context (dict): Auxiliary data used in deserialization.

        Returns a subinterval of the :ref:`uproot.source.chunk.Chunk.raw_data`
        as a ``numpy.ndarray`` of ``numpy.uint8``.

        Note that this ``start`` and ``stop`` are in the same coordinate
        system as the :ref:`uproot.source.chunk.Chunk.start` and
        :ref:`uproot.source.chunk.Chunk.stop`. That is, to get the whole
        chunk, use ``start=chunk.start`` and ``stop=chunk.stop``.

        This method will wait until the chunk is filled (the
        :ref:`uproot.source.chunk.Chunk.future` completes), if it isn't
        already.
        ri   zattempting to get bytes ú:ú
outside expected range ú for this ChunkN©
rs   rY   r\   rR   ZdeserializationZDeserializationErrorrZ   r_   rX   r   )r   r&   r'   rf   rv   Úlocal_startZ
local_stopr	   r	   r
   Úget¥  s     

ÿÿúz	Chunk.get)r&   rf   rv   r   c              	   C  sb   |   ¡  | j|kr*|| j }| j|d… S tj d|› d| j› d| j› d�| | ¡ || jj	¡‚dS )a!  
        Args:
            start (int): Seek position of the first byte to include.
            cursor (:doc:`uproot.source.cursor.Cursor`): A pointer to the
                current position in this chunk.
            context (dict): Auxiliary data used in deserialization.

        Returns a subinterval of the :ref:`uproot.source.chunk.Chunk.raw_data`
        as a ``numpy.ndarray`` of ``numpy.uint8`` from ``start`` to the end
        of the chunk.

        Note that this ``start`` is in the same coordinate system as the
        :ref:`uproot.source.chunk.Chunk.start`. That is, to get the whole
        chunk, use ``start=chunk.start``.

        This method will wait until the chunk is filled (the
        :ref:`uproot.source.chunk.Chunk.future` completes), if it isn't
        already.
        Nzattempting to get bytes after rx   rw   ry   rz   )r   r&   rf   rv   r{   r	   r	   r
   Ú	remainderÍ  s    

ÿÿúzChunk.remainderN)r   )F)T)r   r   r   r   r`   rk   Zuint8rm   ÚclassmethodrV   r   rB   r   rP   r&   r'   rE   rW   rd   rh   rs   rt   r|   r}   r	   r	   r	   r
   r$   ù   s0    ÿ

!(r$   r*   ©r(   r.   c                   s   d‡ ‡fdd„	}|S )ao  
    Returns a function that puts the chunk on the notifications queue when called.
    The function has a 'future' argument to be compatible with the `concurrent.futures.Future.add_done_callback` method.

    Args:
        chunk (:doc:`uproot.source.chunk.Chunk`): The chunk to put on the queue.
        notifications (``queue.Queue``): The notifications queue.
    Nc                   s   ˆ  ˆ ¡ d S r   )Úput)rE   r   r	   r
   Únotifyþ  s    znotifier.<locals>.notify)Nr	   )r(   r.   r�   r	   r   r
   rK   ô  s    
rK   )r   Ú
__future__r   r   rn   Úqueuer`   rR   r   Ú	dataclassr   r   r7   r$   rK   r	   r	   r	   r
   Ú<module>   s   
{E |