a
    zn„f$C  ã                   @  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dd„ Z	ej
dd„ ƒ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 remote files, accessed via the XRootD protocol.

Defines a :doc:`uproot.source.xrootd.XRootDResource` (``XRootD.File``) and two
sources: :doc:`uproot.source.xrootd.MultithreadedXRootDSource` and
:doc:`uproot.source.xrootd.XRootDSource`. The latter requires the server to
support vector-read requests; if not, it automatically falls back to
:doc:`uproot.source.xrootd.MultithreadedXRootDSource`.
é    )ÚannotationsNc           	      C  sÚ   d}d}t j ¡ }|  d¡}| |¡}|jdkrB|jdkrB||fS |  d¡}|dkrXt‚| |¡}|j› d|j› d	�}| |¡}| 	|j
jjd
¡\}}|jr¼|jdv r²t|jƒ‚n
t|jƒ‚tt| dd¡ƒ\}}||fS )a‚  
    Args:
        file (``XRootD.client.File``): An XRootD file object.

    Query a XRootD server for its configuration.

    Returns a 2-tuple of integers:

    * ``readv_iov_max``: The maximum number of elements that can be
      requested in a single vector read.
    * ``readv_ior_max``: The maximum number of bytes that can be requested
      per *element* in a vector read.
    i   iðÿ ZLastURLÚfileÚ	localhostZ
DataServerÚ z://ú/zreadv_iov_max readv_ior_max©éÎ   ó   
é   )ÚuprootÚextrasÚXRootD_clientZget_propertyÚURLÚprotocolZhostidÚNotImplementedErrorZ
FileSystemÚqueryÚflagsZ	QueryCodeZCONFIGÚerrorÚcodeÚTimeoutErrorÚmessageÚOSErrorÚmapÚintÚsplit)	r   Zreadv_iov_maxZreadv_ior_maxr   Zlast_urlZdata_serverÚfsÚstatusÚresult© r   úW/home/yuhw/wc/wire-cell-python/wcpy/lib/python3.9/site-packages/uproot/source/xrootd.pyÚget_server_config   s,    






ÿ

r    c                   c  s
   d V  d S ©Nr   r   r   r   r   Útrivial_resourceM   s    r"   c                   @  sð   e Zd ZdZdddœdd„Zdd„ Zd	d
„ Zdd„ Zddœdd„Zdd„ Z	e
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dd d!œd"d#„Zed$ddd%œd&d'„ƒZeddd(œd)d*„ƒZed+d,„ ƒZd-d.„ Zd/S )0ÚXRootDResourcezÇ
    Args:
        file_path (str): A URL of the file to open.
        timeout (None or float): An optional timeout in seconds.

    A :doc:`uproot.source.chunk.Resource` for XRootD connections.
    Ústrúfloat | None)Ú	file_pathÚtimeoutc                 C  s   || _ || _|  ¡  d S r!   )Ú
_file_pathÚ_timeoutÚ_open)Úselfr&   r'   r   r   r   Ú__init__[   s    zXRootDResource.__init__c                 C  sB   t j ¡ }| ¡ | _| jj| j|  ¡ d�\}}|jr>|  	|¡ d S ©N©r'   )
r   r   r   ÚFileÚ_fileÚopenr(   Ú_xrd_timeoutr   Ú
_xrd_error)r+   r   r   Údummyr   r   r   r*   `   s
    

zXRootDResource._openc                 C  s   t | jƒ}| d¡ |S )Nr0   ©ÚdictÚ__dict__Úpop©r+   Ústater   r   r   Ú__getstate__i   s    

zXRootDResource.__getstate__c                 C  s   || _ |  ¡  d S r!   ©r7   r*   r9   r   r   r   Ú__setstate__n   s    zXRootDResource.__setstate__r   ©Úreturnc                 C  s   | j d u rdS t| j ƒS d S )Nr   )r)   r   ©r+   r   r   r   r2   r   s    
zXRootDResource._xrd_timeoutc                 C  sp   | j j|  ¡ d� |jdv r0tj | j|j¡‚n<|jdv rTt	d|j› d| j› �ƒ‚nt
d|j› d| j› �ƒ‚d S )Nr.   )ée   i0  i�  r   zXRootD error: z	 in file )r0   Úcloser2   r   r   Z_utilZ_file_not_foundr(   r   r   r   )r+   r   r   r   r   r3   x   s    

ÿzXRootDResource._xrd_errorc                 C  s   | j S ©z1
        The timeout in seconds or None.
        ©r)   r@   r   r   r   r'   †   s    zXRootDResource.timeoutc                 C  s   | j S ©z4
        The ``XRootD.client.File`` object.
        )r0   r@   r   r   r   r   �   s    zXRootDResource.filec                 C  s*   | j  |  ¡ ¡\}}|jr$|  |¡ |jS r!   )r0   Ústatr2   r   r3   Úsize)r+   r   Úinfor   r   r   Ú	num_bytes”   s    
zXRootDResource.num_bytesÚboolc                 C  s   | j  ¡  S r!   )r0   Zis_openr@   r   r   r   Úclosed›   s    zXRootDResource.closedc                 C  s   | S r!   r   r@   r   r   r   Ú	__enter__Ÿ   s    zXRootDResource.__enter__c                 C  s   | j j|  ¡ d� d S r-   )r0   rB   r2   ©r+   Zexception_typeZexception_valueÚ	tracebackr   r   r   Ú__exit__¢   s    zXRootDResource.__exit__Úbytes©ÚstartÚstopr?   c                 C  s:   | j jt|ƒt|| ƒ|  ¡ d�\}}|jr6|  |¡ |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.   )r0   Úreadr   r2   r   r3   )r+   rR   rS   r   Údatar   r   r   Úget¥   s    	ÿ

zXRootDResource.getzuproot.source.chunk.Source)ÚsourcerR   rS   c                   s   ‡ ‡fdd„}t jj |¡S )aï  
        Args:
            source (:doc:`uproot.source.xrootd.MultithreadedXRootDSource`): 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.xrootd.XRootDResource.get` with ``start`` and
        ``stop``.
        c                   s   |   ˆ ˆ¡S r!   )rV   ©Úresource©rR   rS   r   r   ÚtaskÄ   s    z#XRootDResource.future.<locals>.task©r   rW   ÚfuturesZResourceFuture)rW   rR   rS   r[   r   rZ   r   Úfutureµ   s    zXRootDResource.futurerZ   c                   s   ‡ ‡‡fdd„}t jj |¡S )a�  
        Returns a :doc:`uproot.source.futures.ResourceFuture` to simply select
        the ``(start, stop)`` item from the ``results`` dict.

        In :ref:`uproot.source.xrootd.XRootDSource.chunks`, each chunk has a
        :ref:`uproot.source.xrootd.XRootDResource.partfuture` that are collectively
        filled by callbacks from :ref:`uproot.source.xrootd.XRootDResource.callbacker`.
        c                   s   ˆ ˆˆf S r!   r   rX   ©ÚresultsrR   rS   r   r   r[   Ô   s    z'XRootDResource.partfuture.<locals>.taskr\   )r`   rR   rS   r[   r   r_   r   Ú
partfutureÉ   s    zXRootDResource.partfuturec                   s   ‡ fdd„}t jj |¡S )zÓ
        Returns a :doc:`uproot.source.futures.ResourceFuture` that merges the
        chunks previously submitted via
        :ref:`uproot.source.xrootd.XRootDResource.partfuture` which had to be split
        c                   s>   t ˆ ƒdkrˆ d  ¡ S g }ˆ D ]}| | ¡ ¡ q d |¡S )Nr
   r   ó    )Úlenr   ÚappendÚjoin)rY   Zchunk_buffersr^   ©Úpartfuturesr   r   r[   á   s    z(XRootDResource.mergefuture.<locals>.taskr\   )rg   r[   r   rf   r   ÚmergefutureÙ   s    	zXRootDResource.mergefuturec                   s   ‡ ‡‡fdd„}|S )zf
        Returns an XRootD callback function to fill the ``futures`` and
        ``results``.
        c                   sŽ   | j rJzˆ | ¡ W qŠ tyF   t ¡ }ˆ  ¡ D ]}| |¡ q2Y qŠ0 n@|jD ]8}|j|j|j	  }}|j
ˆ||f< ˆ ||f  d ¡ qPd S r!   )r   r3   Ú	ExceptionÚsysÚexc_infoÚvaluesZ_set_excinfoÚchunksÚoffsetÚlengthÚbufferÚ_run)r   ÚresponseÚhostsÚexcinfor^   ÚchunkrR   rS   ©r]   r`   r+   r   r   Úcallbackò   s    
z+XRootDResource.callbacker.<locals>.callbackr   )r+   r]   r`   rw   r   rv   r   Ú
callbackerì   s    zXRootDResource.callbackerN)Ú__name__Ú
__module__Ú__qualname__Ú__doc__r,   r*   r;   r=   r2   r3   Úpropertyr'   r   rI   rK   rL   rO   rV   Ústaticmethodr^   ra   rh   rx   r   r   r   r   r#   R   s2   	

r#   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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 „ ƒZed!dœd"d#„ƒZd$d%„ Zd&d'„ Zeddœd(d)„ƒZd*S )+ÚXRootDSourcea   
    Args:
        file_path (str): A URL of the file to open.
        options: Must include ``"timeout"``, ``"max_num_elements"``, ``"use_threads"``, and ``"num_workers"``

    A :doc:`uproot.source.chunk.Source` that uses XRootD's vector-read
    to get many chunks in one request.
    r$   ©r&   c                 K  sh   t tjjjfi |¤Ž}|d | _|d | _|d | _|d | _d| _	d| _
d| _|| _d | _|  ¡  d S )Nr'   Zmax_num_elementsÚuse_threadsÚnum_workersr   )r6   r   Úreadingr1   Údefaultsr)   Ú_desired_max_num_elementsÚ_use_threadsÚ_num_workersÚ_num_requestsÚ_num_requested_chunksÚ_num_requested_bytesr(   Ú
_num_bytesr*   ©r+   r&   Úoptionsr   r   r   r,     s    



zXRootDSource.__init__c                 C  s|   t | j| jƒ| _| jr8tjj dd„ t	| j
ƒD ƒ¡| _ntjj tƒ ¡| _t| jjƒ\| _| _| jd urxt| j| jƒ| _d S )Nc                 S  s   g | ]
}t ƒ ‘qS r   )r"   ©Ú.0Ú_r   r   r   Ú
<listcomp>%  rb   z&XRootDSource._open.<locals>.<listcomp>)r#   r(   r)   Ú	_resourcer†   r   rW   r]   ÚResourceThreadPoolExecutorÚranger‡   Ú	_executorÚResourceTrivialExecutorr"   r    r   Ú_max_num_elementsÚ_max_element_sizer…   Úminr@   r   r   r   r*     s    ÿÿÿ
ÿzXRootDSource._openc                 C  s   t | jƒ}| d¡ |S ©Nr•   r5   r9   r   r   r   r;   4  s    

zXRootDSource.__getstate__c                 C  s   || _ |  ¡  d S r!   r<   r9   r   r   r   r=   9  s    zXRootDSource.__setstate__c                 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(   rc   Útypery   Úid)r+   Úpathr   r   r   Ú__repr__=  s    
zXRootDSource.__repr__r   zuproot.source.chunk.ChunkrQ   c                 C  s^   |  j d7  _ |  jd7  _|  j|| 7  _| j ||¡}tjj |¡}tjj	 
| |||¡S )Nr
   )rˆ   r‰   rŠ   r’   rV   r   rW   r]   ZTrivialFutureru   ÚChunk)r+   rR   rS   rU   r^   r   r   r   ru   C  s    zXRootDSource.chunkúlist[int, int]zqueue.Queuezlist[uproot.source.chunk.Chunk])ÚrangesÚnotificationsr?   c                   s6  ˆ j d7  _ ˆ jt|ƒ7  _ˆ jtdd„ |D ƒƒ7  _g g‰ i }ddddœ‡ ‡fdd„}|D ]¤\}}|| }g |||f< |ˆjkrò|ˆj }|ˆj }	t|ƒD ]$}
|||
ˆj  ˆj|||f ƒ q¤|	d	krð|||ˆj  |	|||f ƒ qb||||||f ƒ qbi }tˆ ƒD ]Ž\}}i }i }|D ]:\}}|| }ˆj 	|||¡}||||f< ||||f< �q(ˆj
 ||¡}ˆj
jj|ˆj
 ¡ |d
�}|j�rˆj
 |¡ �qg }|D ]„\}}g }|||f D ]\}}| |||f ¡ �qÄˆj |¡}tjj ˆ|||¡}| tjj ||¡¡ ˆj |¡ | |¡ �q¬|S )Nr
   c                 s  s   | ]\}}|| V  qd S r!   r   )r�   rR   rS   r   r   r   Ú	<genexpr>Q  rb   z&XRootDSource.chunks.<locals>.<genexpr>r   r¥   ©rR   ro   Zsub_ranges_listc                   sD   t ˆ d ƒˆjkrˆ  g ¡ ˆ d  | |f¡ | | | | f¡ d S )Néÿÿÿÿ)rc   r—   rd   r©   ©Zall_request_rangesr+   r   r   Úadd_request_range[  s    
z.XRootDSource.chunks.<locals>.add_request_ranger   )rm   r'   rw   )rˆ   r‰   rc   rŠ   Úsumr˜   r”   Ú	enumerateÚResourceClassra   r’   rx   r   Zvector_readr2   r   r3   rd   rh   r   rW   ru   r¤   Z_set_notifyZnotifierr•   Úsubmit)r+   r¦   r§   Z
sub_rangesr¬   rR   rS   ro   ZnsplitÚremÚiZglobal_futuresr�   Zrequest_rangesr]   r`   rG   ra   rw   r   rm   rg   Z	sub_startZsub_stopr^   ru   r   r«   r   rm   L  sj    	



ý
ýýzXRootDSource.chunksc                 C  s   | j S )zH
        The :doc:`uproot.source.xrootd.XRootDResource` object.
        )r’   r@   r   r   r   rY   ž  s    zXRootDSource.resourcer%   r>   c                 C  s   | j S rC   rD   r@   r   r   r   r'   ¥  s    zXRootDSource.timeoutc                 C  s   | j jS rE   )r’   r   r@   r   r   r   r   ¬  s    zXRootDSource.filerJ   c                 C  s   | j jS r!   )r’   rK   r@   r   r   r   rK   ³  s    zXRootDSource.closedc                 C  s   | S r!   r   r@   r   r   r   rL   ·  s    zXRootDSource.__enter__c                 C  s   | j  ¡  | j |||¡ d S r!   )r•   Úshutdownr’   rO   rM   r   r   r   rO   º  s    
zXRootDSource.__exit__c                 C  s   | j d u r| jj| _ | j S r!   )r‹   r’   rI   r@   r   r   r   rI   ¾  s    

zXRootDSource.num_bytesN)ry   rz   r{   r|   r#   r¯   r,   r*   r;   r=   r£   ru   rm   r}   rY   r'   r   rK   rL   rO   rI   r   r   r   r   r     s*   		R

r   c                   @  s^   e Zd ZdZeZddœdd„Zdd„ Zdd	„ Zd
d„ Z	e
ddœdd„ƒZe
ddœdd„ƒZdS )ÚMultithreadedXRootDSourcea  
    Args:
        file_path (str): A URL of the file to open.
        options: Must include ``"num_workers"``, ``"use_threads"``, and ``"timeout"``.

    A :doc:`uproot.source.chunk.MultithreadedSource` that manages many
    :doc:`uproot.source.xrootd.XRootDResource` objects.
    r$   r€   c                 K  s^   t tjjjfi |¤Ž}|d | _|d | _|d | _d| _d| _	d| _
|| _d | _|  ¡  d S )Nr‚   r'   r�   r   )r6   r   rƒ   r1   r„   r‡   r)   r†   rˆ   r‰   rŠ   r(   r‹   r*   rŒ   r   r   r   r,   Ñ  s    


z"MultithreadedXRootDSource.__init__c                   sJ   ˆ j r,tjj ‡ fdd„tˆ jƒD ƒ¡ˆ _ntjj t	ˆ j
ˆ jƒ¡ˆ _d S )Nc                   s   g | ]}t ˆ jˆ jƒ‘qS r   )r#   r(   r)   rŽ   r@   r   r   r‘   â  s   ÿz3MultithreadedXRootDSource._open.<locals>.<listcomp>)r†   r   rW   r]   r“   r”   r‡   r•   r–   r#   r(   r)   r@   r   r@   r   r*   ß  s    
þÿÿzMultithreadedXRootDSource._openc                 C  s   t | jƒ}| d¡ |S rš   r5   r9   r   r   r   r;   ì  s    

z&MultithreadedXRootDSource.__getstate__c                 C  s   || _ |  ¡  d S r!   r<   r9   r   r   r   r=   ñ  s    z&MultithreadedXRootDSource.__setstate__r%   r>   c                 C  s   | j S rC   rD   r@   r   r   r   r'   õ  s    z!MultithreadedXRootDSource.timeoutr   c                 C  s<   | j d u r6t| jdƒr*| jjd jj| _ n| jjj| _ | j S )NÚworkersr   )r‹   Úhasattrr•   rµ   rY   rI   r’   r@   r   r   r   rI   ü  s
    
z#MultithreadedXRootDSource.num_bytesN)ry   rz   r{   r|   r#   r¯   r,   r*   r;   r=   r}   r'   rI   r   r   r   r   r´   Å  s   	r´   )r|   Ú
__future__r   Ú
contextlibÚqueuerj   r   Zuproot.source.chunkZuproot.source.futuresr    Úcontextmanagerr"   rW   ru   ÚResourcer#   ZSourcer   ZMultithreadedSourcer´   r   r   r   r   Ú<module>   s   
5
 2 C