a
    zn„f8_  ã                   @  sv   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Ze 	d¡Z
e 	d¡Ze 	d¡Ze 	d¡ZG d	d
„ d
ƒZdS )zâ
This module defines the :doc:`uproot.source.cursor.Cursor`, which maintains
a thread-local pointer into a :doc:`uproot.source.chunk.Chunk` and performs
the lowest level of interpretation (numbers, strings, raw arrays, etc.).
é    )ÚannotationsNz_0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ!"#$%&'()*+,-./:;<=>?@[\]^_`{|}~ z>fz>BHz<Iz<Qc                   @  s"  e Zd ZdZdVd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	dWd ddœdd„Z
dXdd dœdd„Zddœdd„Zddœdd „Zd!d"„ Zd#dd$œd%d&„ZdYd#d'ddd(œd)d*„ZdZd#d'ddd(œd+d,„Zd[d#ddd-d.œd/d0„Zd\d#dddd-d1œd2d3„Zd]d#ddd4œd5d6„Zd^d7d8„Zd_d#dd9ddd:d;œd<d=„Ze d>¡Ze d?¡Zd`d#ddd@d.œdAdB„Zdad#dddCd.œdDdE„Zdbd#dddd@dFœdGdH„Zdcd#ddddCdFœdIdJ„Zddd#dddCd.œdKdL„Zded#dddCd.œdMdN„Z dfd#dddOd.œdPdQ„Z!i ddde"j#fd#ddRd9ddSœdTdU„Z$dS )gÚCursora‹  
    Args:
        index (int): Global seek position in the ROOT file or local position
            in an uncompressed :doc:`uproot.source.chunk.Chunk`.
        origin (int): Zero-point for numerical keys in ``refs``.
        refs (None or dict): References to data already read in
            :doc:`uproot.deserialization.read_object_any`.

    Represents a seek point in a ROOT file, which may be held for later
    reference or advanced while interpreting data from a
    :doc:`uproot.source.chunk.Chunk`.

    A cursor also holds references to previously read data that might be
    requested by :doc:`uproot.deserialization.read_object_any`.
    r   NÚintzdict | None)ÚindexÚoriginÚrefsc                 C  s   || _ || _|| _d S ©N)Ú_indexÚ_originÚ_refs)Úselfr   r   r   © r   úW/home/yuhw/wc/wire-cell-python/wcpy/lib/python3.9/site-packages/uproot/source/cursor.pyÚ__init__0   s    zCursor.__init__c              	   C  s¼   | j dkrdn
d| j › �}| jd u s2t| jƒdkr8d}nn| jd u sPt| jƒdk rvd t| jƒd dd„ | jD ƒ¡¡}n0d	 t| jƒd d
d„ t| jƒd d… D ƒ¡¡}d| j› |› |› d�S )Nr   Ú z	, origin=é   z, {} refs: {}z, c                 s  s   | ]}t |ƒV  qd S r   ©Ústr©Ú.0Úxr   r   r   Ú	<genexpr><   ó    z"Cursor.__repr__.<locals>.<genexpr>z, {} refs: {}...c                 s  s   | ]}t |ƒV  qd S r   r   r   r   r   r   r   @   r   zCursor(ú))r
   r   ÚlenÚformatÚjoinÚlistr	   )r   ÚoÚrr   r   r   Ú__repr__5   s    ÿ(ÿzCursor.__repr__)Úreturnc                 C  s   | j S )z†
        Global seek position in the ROOT file or local position in an
        uncompressed :doc:`uproot.source.chunk.Chunk`.
        ©r	   ©r   r   r   r   r   E   s    zCursor.indexc                 C  s   | j S )zc
        Zero-point for numerical keys in
        :ref:`uproot.source.cursor.Cursor.refs`.
        )r
   r#   r   r   r   r   M   s    zCursor.originÚdictc                 C  s   | j du ri | _ | j S )zk
        References to data already read in
        :doc:`uproot.deserialization.read_object_any`.
        N)r   r#   r   r   r   r   U   s    
zCursor.refs)Úotherr!   c                 C  s$   |du r| j | j S | j |j  S dS )ar  
        The number of bytes between this :doc:`uproot.source.cursor.Cursor`
        and its :ref:`uproot.source.cursor.Cursor.origin` (if None)
        or the ``other`` :doc:`uproot.source.cursor.Cursor` (if provided).

        If the displacement is positive, ``self`` is later in the file than the
        ``origin`` or ``other``; if negative, it is earlier.
        N)r	   r
   )r   r%   r   r   r   Údisplacement_   s    	zCursor.displacementTÚbool)Ú	link_refsr!   c                 C  s>   |s| j du r"t| j| j| j d�S t| j| jt| j ƒd�S dS )zÎ
        Returns a copy of this :doc:`uproot.source.cursor.Cursor`. If
        ``link_refs`` is True, any :ref:`uproot.source.cursor.Cursor.refs`
        will be *referenced*, rather than *copied*.
        N)r   r   )r   r   r	   r
   r$   )r   r(   r   r   r   Úcopym   s    zCursor.copy)r   c                 C  s
   || _ dS )zi
        Move the :ref:`uproot.source.cursor.Cursor.index` to a specified seek
        position.
        Nr"   )r   r   r   r   r   Úmove_tox   s    zCursor.move_to)Ú	num_bytesc                 C  s   |  j |7  _ dS )zb
        Move the :ref:`uproot.source.cursor.Cursor.index` forward
        ``num_bytes``.
        Nr"   )r   r+   r   r   r   Úskip   s    zCursor.skipc                 C  sT   t |ddƒ}t |ddƒ}|du s2t|tƒr2|du rDtdt|ƒ› �ƒ‚|j| | _dS )z¦
        Move the :ref:`uproot.source.cursor.Cursor.index` just after an object
        that has a starting ``obj.cursor`` and an expected ``obj.num_bytes``.
        ÚcursorNr+   zUCursor.skip_after can only be used on an object with a `cursor` and `num_bytes`, not )ÚgetattrÚ
isinstancer   Ú	TypeErrorÚtyper   r	   )r   ÚobjZstart_cursorr+   r   r   r   Ú
skip_after†   s    ÿþýÿÿzCursor.skip_afterzuproot.source.chunk.Chunk)ÚchunkÚcontextc                 C  s<   t jj|| |dd�\}}}|du r&dS |  j|7  _dS dS )a(  
        Args:
            chunk (:doc:`uproot.source.chunk.Chunk`): Buffer of contiguous data
                from the file :doc:`uproot.source.chunk.Source`.
            context (dict): Auxiliary data used in deserialization.

        Move the :ref:`uproot.source.cursor.Cursor.index` to a seek position
        beyond the serialized data for an object that can be interpreted with
        :doc:`uproot.deserialization.numbytes_version`.

        Returns True if successful (cursor has moved), False otherwise (cursor
        has NOT moved).
        F©ÚmoveNT)ÚuprootZdeserializationZnumbytes_versionr	   )r   r4   r5   r+   ÚversionZis_memberwiser   r   r   Ú	skip_over˜   s    ÿzCursor.skip_overzstruct.Struct)r4   r   r5   r7   c                 C  s0   | j }||j }|r|| _ | | ||| |¡¡S )a»  
        Args:
            chunk (:doc:`uproot.source.chunk.Chunk`): Buffer of contiguous data
                from the file :doc:`uproot.source.chunk.Source`.
            format (``struct.Struct``): Specification to interpret the bytes of
                data.
            context (dict): Auxiliary data used in deserialization.
            move (bool): If True, move the
                :ref:`uproot.source.cursor.Cursor.index` past the fields;
                otherwise, leave it where it is.

        Interpret data at this :ref:`uproot.source.cursor.Cursor.index` with a
        specified format. Returns a tuple of data whose types and length are
        determined by the ``format``.
        ©r	   ÚsizeÚunpackÚget©r   r4   r   r5   r7   ÚstartÚstopr   r   r   Úfields¯   s
    
zCursor.fieldsc                 C  s4   | j }||j }|r|| _ | | ||| |¡¡d S )aª  
        Args:
            chunk (:doc:`uproot.source.chunk.Chunk`): Buffer of contiguous data
                from the file :doc:`uproot.source.chunk.Source`.
            format (``struct.Struct``): Specification to interpret the bytes of
                data.
            context (dict): Auxiliary data used in deserialization.
            move (bool): If True, move the
                :ref:`uproot.source.cursor.Cursor.index` past the fields;
                otherwise, leave it where it is.

        Interpret data at this :ref:`uproot.source.cursor.Cursor.index` with a
        format that only specifies one field, returning a single item instead of
        a tuple.
        r   r;   r?   r   r   r   ÚfieldË   s
    
zCursor.fieldÚfloat)r4   r5   r7   r!   c                 C  s4   | j }|tj }|r|| _ t | ||| |¡¡d S )a"  
        Args:
            chunk (:doc:`uproot.source.chunk.Chunk`): Buffer of contiguous data
                from the file :doc:`uproot.source.chunk.Source`.
            context (dict): Auxiliary data used in deserialization.
            move (bool): If True, move the
                :ref:`uproot.source.cursor.Cursor.index` past the fields;
                otherwise, leave it where it is.

        Interpret data at this :ref:`uproot.source.cursor.Cursor.index` as
        ROOT's ``Double32_t`` type, returning the Python ``float``.
        r   )r	   Ú_raw_double32r<   r=   r>   )r   r4   r5   r7   r@   rA   r   r   r   Údouble32ç   s
    
zCursor.double32)r4   Únum_bitsr5   r7   r!   c           
      C  sš   | j }|tj }|r|| _ t | ||| |¡¡\}}t |gtj¡}	|	dK }	|	|d|d > d @ d| > O }	|	 tj	¡}	d|d > |@ dkr’|	 }	|	 
¡ S )a]  
        Args:
            chunk (:doc:`uproot.source.chunk.Chunk`): Buffer of contiguous data
                from the file :doc:`uproot.source.chunk.Source`.
            num_bits (int): Number of bits in the mantissa.
            context (dict): Auxiliary data used in deserialization.
            move (bool): If True, move the
                :ref:`uproot.source.cursor.Cursor.index` past the fields;
                otherwise, leave it where it is.

        Interpret data at this :ref:`uproot.source.cursor.Cursor.index` as
        ROOT's ``Float16_t`` type, returning the Python ``float``.
        é   é   r   )r	   Ú_raw_float16r<   r=   r>   ÚnumpyÚarrayZint32ÚviewZfloat32Úitem)
r   r4   rG   r5   r7   r@   rA   ÚexponentZmantissaÚoutr   r   r   Úfloat16ý   s    
 zCursor.float16)r4   r5   r7   c                 C  s.   |  | j| jd | |¡}|r*|  jd7  _|S )aò  
        Args:
            chunk (:doc:`uproot.source.chunk.Chunk`): Buffer of contiguous data
                from the file :doc:`uproot.source.chunk.Source`.
            context (dict): Auxiliary data used in deserialization.
            move (bool): If True, move the
                :ref:`uproot.source.cursor.Cursor.index` past the fields;
                otherwise, leave it where it is.

        Interpret data at this :ref:`uproot.source.cursor.Cursor.index` as a raw
        byte.
        rI   )r>   r	   )r   r4   r5   r7   rP   r   r   r   Úbyte$  s    zCursor.bytec                 C  s(   | j }|| }|r|| _ | ||| |¡S )a@  
        Args:
            chunk (:doc:`uproot.source.chunk.Chunk`): Buffer of contiguous data
                from the file :doc:`uproot.source.chunk.Source`.
            length (int): Number of bytes to retrieve.
            context (dict): Auxiliary data used in deserialization.
            move (bool): If True, move the
                :ref:`uproot.source.cursor.Cursor.index` past the fields;
                otherwise, leave it where it is.

        Interpret data at this :ref:`uproot.source.cursor.Cursor.index` as raw
        bytes with a given ``length``.
        )r	   r>   )r   r4   Úlengthr5   r7   r@   rA   r   r   r   Úbytes6  s
    zCursor.bytesznumpy.dtypeznumpy.ndarray)r4   rS   Údtyper5   r7   r!   c                 C  s8   | j }|||j  }|r|| _ tj| ||| |¡|d�S )aš  
        Args:
            chunk (:doc:`uproot.source.chunk.Chunk`): Buffer of contiguous data
                from the file :doc:`uproot.source.chunk.Source`.
            length (int): Number of bytes to retrieve.
            dtype (``numpy.dtype``): Data type for the array.
            context (dict): Auxiliary data used in deserialization.
            move (bool): If True, move the
                :ref:`uproot.source.cursor.Cursor.index` past the fields;
                otherwise, leave it where it is.

        Interpret data at this :ref:`uproot.source.cursor.Cursor.index` as a
        one-dimensional array with a given ``length`` and ``dtype``.
        ©rU   )r	   ÚitemsizerK   Ú
frombufferr>   )r   r4   rS   rU   r5   r7   r@   rA   r   r   r   rL   J  s
    zCursor.arrayÚu1z>i4rT   c                 C  s”   | j }|d }| ||| |¡d }|dkrb|}|d }| ||| |¡}tj|| jd� | j¡d }|}|t|ƒ }|r||| _ tj	 
| ||| |¡¡S )aµ  
        Args:
            chunk (:doc:`uproot.source.chunk.Chunk`): Buffer of contiguous data
                from the file :doc:`uproot.source.chunk.Source`.
            context (dict): Auxiliary data used in deserialization.
            move (bool): If True, move the
                :ref:`uproot.source.cursor.Cursor.index` past the fields;
                otherwise, leave it where it is.

        Interpret data at this :ref:`uproot.source.cursor.Cursor.index` as a
        bytestring.

        The first byte is taken to be the length of the subsequent string unless
        it is equal to 255, in which case, the next 4 bytes are taken to be an
        ``numpy.int32`` length.
        rI   r   éÿ   é   rV   )r	   r>   rK   rX   Ú_u1rM   Ú_i4r   r8   Ú_utilÚtobytes)r   r4   r5   r7   r@   rA   rS   Zlength_datar   r   r   Ú
bytestringi  s    zCursor.bytestringr   c                 C  s   | j |||d�jdd�S )a¿  
        Args:
            chunk (:doc:`uproot.source.chunk.Chunk`): Buffer of contiguous data
                from the file :doc:`uproot.source.chunk.Source`.
            context (dict): Auxiliary data used in deserialization.
            move (bool): If True, move the
                :ref:`uproot.source.cursor.Cursor.index` past the fields;
                otherwise, leave it where it is.

        Interpret data at this :ref:`uproot.source.cursor.Cursor.index` as a
        UTF-8 encoded string.

        The first byte is taken to be the length of the subsequent string unless
        it is equal to 255, in which case, the next 4 bytes are taken to be an
        ``numpy.int32`` length.
        r6   Úsurrogateescape©Úerrors)r`   Údecode)r   r4   r5   r7   r   r   r   ÚstringŠ  s    ÿzCursor.string)r4   r5   rS   r7   r!   c                 C  s4   | j }|| }|r|| _ | ||| |¡}tj |¡S )a1  
        Args:
            chunk (:doc:`uproot.source.chunk.Chunk`): Buffer of contiguous data
                from the file :doc:`uproot.source.chunk.Source`.
            context (dict): Auxiliary data used in deserialization.
            length (int): Number of bytes in the bytestring.
            move (bool): If True, move the
                :ref:`uproot.source.cursor.Cursor.index` past the fields;
                otherwise, leave it where it is.

        Interpret data at this :ref:`uproot.source.cursor.Cursor.index` as a
        bytestring.
        )r	   r>   r8   r^   r_   )r   r4   r5   rS   r7   r@   rA   Údatar   r   r   Úbytestring_with_length¡  s    zCursor.bytestring_with_lengthc                 C  s   | j ||||d�jdd�S )a7  
        Args:
            chunk (:doc:`uproot.source.chunk.Chunk`): Buffer of contiguous data
                from the file :doc:`uproot.source.chunk.Source`.
            context (dict): Auxiliary data used in deserialization.
            length (int): Number of bytes in the string.
            move (bool): If True, move the
                :ref:`uproot.source.cursor.Cursor.index` past the fields;
                otherwise, leave it where it is.

        Interpret data at this :ref:`uproot.source.cursor.Cursor.index` as a
        UTF-8 encoded string.
        r6   ra   rb   )rg   rd   )r   r4   r5   rS   r7   r   r   r   Ústring_with_length¼  s    ÿzCursor.string_with_lengthc                 C  s’   |  | j| |¡}d}d}|dkr`|t|ƒkrNtd| j› d| j› d| jj› �ƒ‚|| }|d7 }q|rr|  j|7  _tj	 
|d|d … ¡jdd�S )	a  
        Args:
            chunk (:doc:`uproot.source.chunk.Chunk`): Buffer of contiguous data
                from the file :doc:`uproot.source.chunk.Source`.
            context (dict): Auxiliary data used in deserialization.
            move (bool): If True, move the
                :ref:`uproot.source.cursor.Cursor.index` past the fields;
                otherwise, leave it where it is.

        Interpret data at this :ref:`uproot.source.cursor.Cursor.index` as a
        null-terminated, UTF-8 encoded string.
        r   Nz6C-style string has no terminator (null byte) in Chunk ú:z
of file path rI   ra   rb   )Ú	remainderr	   r   ÚOSErrorÚ_startÚ_stopÚ_sourceÚ	file_pathr8   r^   r_   rd   )r   r4   r5   r7   rj   Z
local_stopÚcharr   r   r   Ú	classnameÔ  s"    ÿÿ
ÿzCursor.classnamec                 C  sD   |r |   |t|¡}|  |||¡S | j}| j||dd�}|| _|S d S )NTr6   )rC   Ú_rntuple_string_lengthrh   r	   Úrntuple_string)r   r4   r5   r7   rS   r   rP   r   r   r   rs   ö  s    zCursor.rntuple_stringzdatetime.datetimec                 C  s   | j |t||d�}tj |¡S )Nr6   )rC   Ú_rntuple_datetimeÚdatetimeÚfromtimestamp)r   r4   r5   r7   Úrawr   r   r   Úrntuple_datetime  s    zCursor.rntuple_datetimez
int | None)r4   r5   Úlimit_bytesrU   Úoffsetc              	     s  |  | j| |¡}|dur$|d|… }|durÄt|tjƒsBt |¡}dgt|ƒ }|}	t|ƒ| |j |j }
||||
 …  |¡D ]}|	|j7 }	|||	d < q‚d|jd d › d|jd d › d�‰ tdt	t 
t|ƒd ¡ƒd	 d	ƒD �]$}|||d	 … }d
}|du�rˆd}||d… D ] }|du �r2|d7 }n �q<�qtd|jd | ƒ}dg| |||d	 …  }d| }d|| d |j  }| |d d ¡ | |d dd„ |D ƒ¡ d ¡ | |d dd„ |D ƒ¡ d ¡ |duræ| |d ‡ fdd„|D ƒ¡ d ¡ qædS )a5
  
        Args:
            chunk (:doc:`uproot.source.chunk.Chunk`): Data to examine.
            context (dict): Auxiliary data used in deserialization.
            limit_bytes (None or int): Number of bytes to limit the output to.
                A line of debugging output (without any ``offset``) is 20 bytes,
                so multiples of 20 show full lines. If None, everything is
                shown to the end of the :doc:`uproot.source.chunk.Chunk`,
                which might be large.
            dtype (None, ``numpy.dtype``, or its constructor argument): If None,
                present only the bytes as decimal values (0-255). Otherwise,
                also interpret them as an array of a given NumPy type.
            offset (int): Number of bytes to skip before interpreting a ``dtype``;
                can be helpful if the numerical values are out of phase with
                the first byte shown. Not to be confused with ``skip_bytes``,
                which determines which bytes are shown at all. Any ``offset``
                values that are equivalent modulo ``dtype.itemsize`` show
                equivalent interpretations.
            stream (object with a ``write(str)`` method): Stream to write the
                debugging output to.

        Peek at data by printing it to the ``stream`` (usually stdout). The data
        are always presented as decimal bytes and ASCII characters, but may
        also be interpreted as numbers.

        Example output with ``dtype=">f4"`` and ``offset=3``.

        .. code-block::

            --+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+-
            123 123 123  63 140 204 205  64  12 204 205  64  83  51  51  64 140 204 205  64
              {   {   {   ? --- --- ---   @ --- --- ---   @   S   3   3   @ --- --- ---   @
                                    1.1             2.2             3.3             4.4
                --+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+-
                176   0   0  64 211  51  51  64 246 102 102  65  12 204 205  65  30 102 102  66
                --- --- ---   @ ---   3   3   @ ---   f   f   A --- --- ---   A ---   f   f   B
                        5.5             6.6             7.7             8.8             9.9
                --+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+-
                202   0   0  67  74   0   0  67 151 128   0 123 123
                --- --- ---   C   J --- ---   C --- --- ---   {   {
                      101.0           202.0           303.0
        NrI   z{0:>r[   Ú.zs}r   g      4@é   r   z    zP--+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+-Ú
ú c                 s  s   | ]}|d ›V  qdS )Z3dNr   r   r   r   r   r   a  r   zCursor.debug.<locals>.<genexpr>c                 s  s&   | ]}t |ƒtv r|d ›ndV  qdS )z>3cz---N)ÚchrÚ_printable_charactersr   r   r   r   r   d  s   ÿc                 3  s$   | ]}|d urˆ   t|ƒ¡V  qd S r   )r   r   r   ©Ú	formatterr   r   r   n  s   þ)rj   r	   r/   rK   rU   r   rW   rM   Úranger   ÚceilÚmaxÚwriter   )r   r4   r5   ry   rU   rz   Ústreamrf   ZinterpretedÚiZinterpreted_lengthr   Ú
line_startZ	line_dataÚprefixZnonesÚfillZline_interpretedZinterpreted_prefixr   r�   r   Údebug  sf    3
þ
&(





ÿ"
þÿûÿ	þÿúÿzCursor.debug)r   N)N)T)T)T)T)T)T)T)T)T)T)T)T)T)T)T)%Ú__name__Ú
__module__Ú__qualname__Ú__doc__r   r    Úpropertyr   r   r   r&   r)   r*   r,   r3   r:   rB   rC   rF   rQ   rR   rT   rL   rK   rU   r\   r]   r`   re   rg   rh   rq   rs   rx   ÚsysÚstdoutrŒ   r   r   r   r   r      sb   	 û! û ÿ û'
 ú

 ÿ" ÿ û  û ÿ# ÿ ÿ	ùr   )r�   Ú
__future__r   ru   Ústructr’   rK   r8   r€   ÚStructrE   rJ   rr   rt   r   r   r   r   r   Ú<module>   s   ÿ



