a
    zn„f©  ã                   @  sN   d Z ddlmZ ddlZddlZddlmZ ddlZddlZ	G dd„ dƒZ
dS )au  
This module defines a physical layer for writing local files.

Defines a :doc:`uproot.sink.file.FileSink`, which can wrap a Python file-like object
that has ``read``, ``write``, ``seek``, ``tell``, and ``flush`` methods and has a
context manager (Python's ``with`` statement) to ensure that files are properly closed
(although files are flushed after every object-write).
é    )ÚannotationsN)ÚIOc                   @  s  e Zd ZdZddœdd„Zedddœd	d
„ƒZedddœdd„ƒZeddœdd„ƒZ	eddœdd„ƒZ
dd„ Zdd„ Zdd„ Zddœdd„Zddœdd„Zeddœdd „ƒZd!d"„ Zd#d$„ Zd%d&„ Zeddœd'd(„ƒZddd)œd*d+„Zdd,œd-d.„Zd5dddd0d1œd2d3„Zd4S )6ÚFileSinkaX  
    Args:
        urlpath_or_file_like (str, Path, or file-like object): If a string or Path, a
            filesystem URL that specifies the file to open by fsspec. If a file-like object, it
            must have ``read``, ``write``, ``seek``, ``tell``, and ``flush`` methods.

    An object that can write (and read) files on a local or remote filesystem.
    It can be initialized from a file-like object (already opened) or a filesystem URL.
    If initialized from a filesystem URL, fsspec is used to open the file.
    In this case the file is opened in the first read or write operation.
    zstr | IO)Úurlpath_or_file_likec                 K  s†   d | _ d | _tjj|dddd�rF|| _tjj| jdddd�s‚tdƒ‚n<| j|fi |¤Žsj| j|fi |¤Ž tj	|fddi|¤Ž| _ d S )NF)ÚreadableÚwritableÚseekableTzawritable file can only be created from a file path or an object that supports reading and writingÚmodezr+b)
Ú
_open_fileÚ_fileÚuprootZ_utilZis_file_likeÚ	TypeErrorÚ_file_existsÚ_truncate_fileÚfsspecÚopen)Úselfr   Ústorage_options© r   úS/home/yuhw/wc/wire-cell-python/wcpy/lib/python3.9/site-packages/uproot/sink/file.pyÚ__init__$   s*    ÿ
ÿÿÿÿÿzFileSink.__init__ÚstrÚbool)ÚurlpathÚreturnc                 K  s"   t jj|fi |¤Ž\}}| |¡S )zª
        Args:
            urlpath (str): A filesystem URL that specifies the file to check by fsspec.

        Returns True if the file exists; False otherwise.
        )r   ÚcoreÚ	url_to_fsÚexists)Úclsr   r   ÚfsÚ
local_pathr   r   r   r   ;   s    zFileSink._file_existsÚNonec                 K  sT   t jj|fi |¤Ž\}}|j | |j¡dd… ¡}|j|dd� |j|dd� dS )zÆ
        Args:
            urlpath (str): A filesystem URL that specifies the file to truncate by fsspec.

        Truncates the file to zero bytes. Creates parent directories if necessary.
        NéÿÿÿÿT)Úexist_ok)Útruncate)r   r   r   ÚsepÚjoinÚsplitZmkdirsÚtouch)r   r   r   r   r    Zparent_directoryr   r   r   r   F   s    zFileSink._truncate_file)r   c                 C  s
   | j du S )zO
        True if constructed with a file-like object; False otherwise.
        N)r
   ©r   r   r   r   Úfrom_objectS   s    zFileSink.from_objectz
str | Nonec                 C  s   | j r| j jS dS )z[
        A path to the file, which is None if constructed with a file-like object.
        N)r
   Úpathr)   r   r   r   Ú	file_pathZ   s    zFileSink.file_pathc                 C  s"   | j s| j ¡ | _ | j  d¡ dS )zn
        Opens the file if it is not already open.
        Sets the file's position to the beginning.
        r   N)r   r
   r   Úseekr)   r   r   r   Ú_ensurea   s    zFileSink._ensurec                 C  s   t | jƒ}| d¡ |S )Nr   )ÚdictÚ__dict__Úpop©r   Ústater   r   r   Ú__getstate__k   s    

zFileSink.__getstate__c                 C  s   || _ d | _d S ©N)r0   r   r2   r   r   r   Ú__setstate__p   s    zFileSink.__setstate__Úintc                 C  s   |   ¡  | j ¡ S )zG
        Calls the file or file-like object's ``tell`` method.
        )r.   r   Útellr)   r   r   r   r8   t   s    zFileSink.tellc                 C  s   |   ¡  | j ¡ S )zH
        Calls the file or file-like object's ``flush`` method.
        )r.   r   Úflushr)   r   r   r   r9   {   s    zFileSink.flushc                 C  s
   | j du S )z>
        True if the file is closed; False otherwise.
        N)r   r)   r   r   r   Úclosed‚   s    zFileSink.closedc                 C  s*   | j dur&t| j dƒr | j  ¡  d| _ dS )z…
        Closes the file (calls ``close`` if it has such a method) and sets it to
        None so that closure is permanent.
        NÚclose)r   Úhasattrr;   r)   r   r   r   r;   ‰   s    

zFileSink.closec                 C  s   | S r5   r   r)   r   r   r   Ú	__enter__“   s    zFileSink.__enter__c                 C  s   |   ¡  d S r5   )r;   )r   Zexception_typeZexception_valueÚ	tracebackr   r   r   Ú__exit__–   s    zFileSink.__exit__c                 C  s   | j d urd| j › �S dS )Nz

in path: Ú )r,   r)   r   r   r   Úin_path™   s    zFileSink.in_path)Úlocationr   c                 C  s    |   ¡  | j |¡ | j |¡S )a  
        Args:
            location (int): Position in the file to write.
            serialization (bytes or NumPy array): Data to write to the file.

        Writes data to the file at a specific location, calling the file-like
        object's ``seek`` and ``write`` methods.
        )r.   r   r-   Úwrite)r   rB   Zserializationr   r   r   rC   �   s    	zFileSink.write)Úlengthc                 C  sB   |   ¡  | j dtj¡ || j ¡  }|dkr>| j d| ¡ dS )z–
        Sets the file's length to ``length``, truncating with zeros if necessary.

        Calls ``seek``, ``tell``, and possibly ``write``.
        r   ó    N)r.   r   r-   ÚosÚSEEK_ENDr8   rC   )r   rD   Úmissingr   r   r   Úset_file_lengthª   s
    zFileSink.set_file_lengthTÚbytes)rB   Ú	num_bytesÚinsistr   c                 C  s†   |   ¡  | j |¡ | j |¡}|du rPt|ƒ|kr‚td|› d|› | j› �ƒ‚n2t|tj	ƒr‚t|ƒ|k r‚td|› d|› | j› �ƒ‚|S )aA  
        Args:
            location (int): Position in the file to read.
            num_bytes (int): Number of bytes to read from the file.
            insist (bool): If True, raise an OSError if ``num_bytes`` cannot be read
                from the file. Otherwise, this function may return data with fewer
                than ``num_bytes``.

        Returns a bytes object of data from the file by calling the file-like
        object's ``seek`` and ``read`` methods. The ``insist`` parameter can be
        used to ensure that the output has the requested length.
        Tzcould not read z! bytes from the file at position )
r.   r   r-   ÚreadÚlenÚOSErrorrA   Ú
isinstanceÚnumbersÚIntegral)r   rB   rK   rL   Úoutr   r   r   rM   ¶   s    ÿÿzFileSink.readN)T)Ú__name__Ú
__module__Ú__qualname__Ú__doc__r   Úclassmethodr   r   Úpropertyr*   r,   r.   r4   r6   r8   r9   r:   r;   r=   r?   rA   rC   rI   rM   r   r   r   r   r      s2   


r   )rW   Ú
__future__r   rQ   rF   Útypingr   r   Zuproot._utilr   r   r   r   r   r   Ú<module>   s   	