a
    zn„f�  ã                   @  sŒ   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d dl	Zd dl
Zd dlmZmZ G dd„ dejjjƒZG dd„ dejjjƒZdS )é    )ÚannotationsN)ÚCoalesceConfigÚcoalesce_requestsc                      sÈ   e Zd ZdZd+dddœ‡ fdd„Zeddd	œd
d„ƒZdd„ Z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$œd%d&„ƒZedd$œd'd(„ƒZed#d$œd)d*„ƒZ‡  ZS ),ÚFSSpecSourceaÑ  
    Args:
        file_path (str): A URL for the file to open.
        coalesce_config (struct, optional): Configuration options for read coalescing
        **kwargs (dict): any extra arguments to be forwarded to the particular
            FileSystem instance constructor. This might include S3 access keys,
            or HTTP headers, etc.

    A :doc:`uproot.source.chunk.Source` that uses FSSpec's cat_ranges feature
    to get many chunks in one request.
    NÚstrzCoalesceConfig | None)Ú	file_pathÚcoalesce_configc                   sV   t ƒ  ¡  || _tjj|fi |  |¡¤Ž\| _| _| jj	| _
d | _|  ¡  |  ¡  d S ©N)ÚsuperÚ__init__Ú_coalesce_configÚfsspecÚcoreZ	url_to_fsÚextract_fsspec_optionsÚ_fsÚ
_file_pathÚ
async_implÚ_async_implÚ_fileÚ_openÚ	__enter__)Úselfr   r   Úoptions©Ú	__class__© úW/home/yuhw/wc/wire-cell-python/wcpy/lib/python3.9/site-packages/uproot/source/fsspec.pyr      s    
ÿÿ
zFSSpecSource.__init__Údict)r   Úreturnc                   s4   t tjjjƒ‰ t ˆ fi |¤Ž}‡ fdd„| ¡ D ƒS )Nc                   s"   i | ]\}}|ˆ   ¡ vr||“qS r   )Úkeys)Ú.0ÚkÚv©Zuproot_default_optionsr   r   Ú
<dictcomp>5   s   z7FSSpecSource.extract_fsspec_options.<locals>.<dictcomp>)r   ÚuprootÚreadingÚopenÚdefaultsÚitems)Úclsr   r   r#   r   r   1   s
    
ÿz#FSSpecSource.extract_fsspec_optionsc                 C  s   t ƒ | _| j | j¡| _d S r	   )ÚFSSpecLoopExecutorÚ	_executorr   r'   r   r   ©r   r   r   r   r   9   s    zFSSpecSource._openc                 C  sR   t | jƒ}t| jƒdkr.t d| jdd …  ƒ}dt| ƒj› d|› dt| ƒd›d�S )	Né
   z...iöÿÿÿú<ú z at 0xZ012xú>)Úreprr   ÚlenÚtypeÚ__name__Úid)r   Úpathr   r   r   Ú__repr__=   s    
zFSSpecSource.__repr__c                 C  s"   t | jƒ}| d¡ | d¡ |S )Nr,   r   )r   Ú__dict__Úpop©r   Ústater   r   r   Ú__getstate__C   s    


zFSSpecSource.__getstate__c                 C  s   || _ d | _|  ¡  d S r	   )r9   r   r   r;   r   r   r   Ú__setstate__I   s    zFSSpecSource.__setstate__c                 C  s   | S r	   r   r-   r   r   r   r   N   s    zFSSpecSource.__enter__c                 C  s   | j  |||¡ | j ¡  d S r	   )r   Ú__exit__r,   Úshutdown)r   Zexception_typeZexception_valueÚ	tracebackr   r   r   r?   Q   s    zFSSpecSource.__exit__Úintzuproot.source.chunk.Chunk)ÚstartÚstopr   c                 C  sd   |  j d7  _ |  jd7  _|  j|| 7  _| jj| j||d�}tjj 	|¡}t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).

        Request a byte range of data from the file as a
        :doc:`uproot.source.chunk.Chunk`.
        é   )rC   Úend)Ú_num_requestsÚ_num_requested_chunksÚ_num_requested_bytesr   Zcat_filer   r%   ÚsourceÚfuturesZTrivialFutureÚchunkZChunk)r   rC   rD   ÚdataÚfuturer   r   r   rL   U   s    
zFSSpecSource.chunkúlist[tuple[int, int]]zqueue.Queuezlist[uproot.source.chunk.Chunk])ÚrangesÚnotificationsr   c                   s¬   ˆ j d7  _ ˆ jt|ƒ7  _ˆ jtdd„ |D ƒƒ7  _z
tj‰W n0 tyv   ddl‰ddl	‰‡‡fdd„‰Y n0 ‡fdd	„‰ d
dœ‡ ‡fdd„}t
||ˆ|ˆjd�S )aÑ  
        Args:
            ranges (list of tuple[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.
        rE   c                 s  s   | ]\}}|| V  qd S r	   r   )r    rC   rD   r   r   r   Ú	<genexpr>†   ó    z&FSSpecSource.chunks.<locals>.<genexpr>r   Nc                Ÿ  s>   t  ¡ }ˆ  ¡ }ˆj|j| g|¢R i |¤Ž}| d |¡I d H S r	   )ÚasyncioÚget_running_loopÚcopy_contextÚpartialÚrunÚrun_in_executor)ÚfuncÚargsÚkwargsÚloopÚctxÚ	func_call)ÚcontextvarsÚ	functoolsr   r   Ú	to_thread�   s    z&FSSpecSource.chunks.<locals>.to_threadc                 Ÿ  s,   t | ƒstdƒ‚ˆ | g|¢R i |¤ŽI d H S )Nzblocking_func must be callable)ÚcallableÚ	TypeError)Zblocking_funcr[   r\   )rb   r   r   Úasync_wrapper_thread•   s    z1FSSpecSource.chunks.<locals>.async_wrapper_threadrO   )Úrequest_rangesc                   sd   ˆj gt| ƒ }dd„ | D ƒ}dd„ | D ƒ}ˆjrDˆjj|||d�nˆ ˆjj|||d�}ˆj |¡S )Nc                 S  s   g | ]\}}|‘qS r   r   )r    rC   Ú_r   r   r   Ú
<listcomp>�   rS   z7FSSpecSource.chunks.<locals>.submit.<locals>.<listcomp>c                 S  s   g | ]\}}|‘qS r   r   )r    rg   rD   r   r   r   rh   ž   rS   )ÚpathsÚstartsÚends)r   r3   r   r   Z_cat_rangesZ
cat_rangesr,   Úsubmit)rf   ri   rj   rk   Ú	coroutine)re   r   r   r   rl   ›   s    ÿÿýz#FSSpecSource.chunks.<locals>.submit)Úconfig)rG   rH   r3   rI   ÚsumrT   rb   ÚAttributeErrorr`   ra   r   r   )r   rP   rQ   rl   r   )re   r`   ra   r   rb   r   Úchunksf   s    
ÿzFSSpecSource.chunksÚbool©r   c                 C  s   | j S )zH
        True if using an async loop executor; False otherwise.
        )r   r-   r   r   r   r   ­   s    zFSSpecSource.async_implc                 C  s   | j  | j¡S )z2
        The number of bytes in the file.
        )r   Úsizer   r-   r   r   r   Ú	num_bytes´   s    zFSSpecSource.num_bytesc                 C  s   dS )zh
        True if the associated file/connection/thread pool is closed; False
        otherwise.
        Fr   r-   r   r   r   Úclosed½   s    zFSSpecSource.closed)N)r5   Ú
__module__Ú__qualname__Ú__doc__r   Úclassmethodr   r   r8   r=   r>   r   r?   rL   rq   Úpropertyr   ru   rv   Ú__classcell__r   r   r   r   r      s&    ÿGr   c                   @  s,   e Zd Zeddœdd„ƒZddœdd„ZdS )	r+   zasyncio.AbstractEventLooprs   c                 C  s
   t j ¡ S r	   )r   ZasynÚget_loopr-   r   r   r   r]   Ç   s    zFSSpecLoopExecutor.loopzconcurrent.futures.Futurec                 C  s    t  |¡stdƒ‚t  || j¡S )Nz(loop executor can only submit coroutines)rT   Úiscoroutinerd   Úrun_coroutine_threadsafer]   )r   rm   r   r   r   rl   Ë   s    
zFSSpecLoopExecutor.submitN)r5   rw   rx   r{   r]   rl   r   r   r   r   r+   Æ   s   r+   )Ú
__future__r   rT   Úconcurrent.futuresÚ
concurrentÚqueuer   Zfsspec.asynr%   Zuproot.source.chunkZuproot.source.futuresZuproot.source.coalescer   r   rJ   rL   ZSourcer   rK   ÚExecutorr+   r   r   r   r   Ú<module>   s    5