o
    gwJ                     @   sL   d dl mZ d dlmZmZmZmZmZ d dlm	Z	m
Z
 G dd de	ZdS )    )BytesIO)linefeed_byte_valuecrlfcr_bytelinefeed_bytecr_byte_value)ClosingContextManageruc                   @   s  e Zd ZdZdZdZdZdZdZdZ	dZ
dZdZd	Zd
Zdd Zdd Zdd Zdd Zdd Zdd Zdd Zdd Zdd Zdd Zd@d d!Zd@d"d#Zd@d$d%ZdAd&d'Zd(d) Zd*d+ Zd,d- Zd.d/ Z e!d0d1 Z"d2d3 Z#d4d5 Z$d6d7 Z%dBd:d;Z&d<d= Z'd>d? Z(dS )CBufferedFilezc
    Reusable base class to implement Python-style file buffering around a
    simpler stream.
    i    r                   @      c                 C   sF   d | _ d| _| j| _t | _t | _d| _d| _	d | _
| _d| _d S )Nr   F)newlines_flags_DEFAULT_BUFSIZE_bufsizer   _wbufferbytes_rbuffer_at_trailing_cr_closed_pos_realpos_sizeself r    G/var/www/html/api-tag/env/lib/python3.10/site-packages/paramiko/file.py__init__3   s   
zBufferedFile.__init__c                 C   s   |    d S N)closer   r    r    r!   __del__B   s   zBufferedFile.__del__c                 C   s   | j rtd| S )z
        Returns an iterator that can be used to iterate over the lines in this
        file.  This iterator happens to return the file itself, since a file is
        its own iterator.

        :raises: ``ValueError`` -- if the file is closed.
        zI/O operation on closed file)r   
ValueErrorr   r    r    r!   __iter__E   s   zBufferedFile.__iter__c                 C   s   |    d| _dS )zN
        Close the file.  Future read and write operations will fail.
        TN)flushr   r   r    r    r!   r$   Q   s   
zBufferedFile.closec                 C   s   |  | j  t | _dS )z{
        Write out any data in the write buffer.  This may do nothing if write
        buffering is not turned on.
        N)
_write_allr   getvaluer   r   r    r    r!   r(   X   s   zBufferedFile.flushc                 C   s   |   }|st|S )a  
        Returns the next line from the input, or raises ``StopIteration``
        when EOF is hit.  Unlike python file objects, it's okay to mix
        calls to `.next` and `.readline`.

        :raises: ``StopIteration`` -- when the end of the file is reached.

        :returns:
            a line (`str`, or `bytes` if the file was opened in binary mode)
            read from the file.
        )readlineStopIteration)r   liner    r    r!   __next__a   s   zBufferedFile.__next__c                 C      | j | j@ | jkS )z
        Check if the file can be read from.

        :returns:
            `True` if the file can be read from. If `False`, `read` will raise
            an exception.
        )r   	FLAG_READr   r    r    r!   readabler      zBufferedFile.readablec                 C   r/   )z
        Check if the file can be written to.

        :returns:
            `True` if the file can be written to. If `False`, `write` will
            raise an exception.
        )r   
FLAG_WRITEr   r    r    r!   writable|   r2   zBufferedFile.writablec                 C      dS )z
        Check if the file supports random access.

        :returns:
            `True` if the file supports random access. If `False`, `seek` will
            raise an exception.
        Fr    r   r    r    r!   seekable   s   zBufferedFile.seekablec                 C   s&   |  t|}||dt|< t|S )z
        Read up to ``len(buff)`` bytes into ``bytearray`` *buff* and return the
        number of bytes read.

        :returns:
            The number of bytes read.
        N)readlen)r   buffdatar    r    r!   readinto   s   zBufferedFile.readintoNc                 C   s  | j rtd| j| j@ std|du s|dk rgt| j}t | _|  jt|7  _	 z| 	| j
}W n ty?   d}Y nw |du sJt|dkrO	 t|S || |  jt|7  _|  jt|7  _q,|t| jkr| jd| }| j|d | _|  jt|7  _|S t| j|k r|t| j }| j| j@ rt| j|}z| 	|}W n ty   d}Y nw |du st|dkrn|  j|7  _|  jt|7  _t| j|k s| jd| }| j|d | _|  jt|7  _|S )a  
        Read at most ``size`` bytes from the file (less if we hit the end of
        the file first).  If the ``size`` argument is negative or omitted,
        read all the remaining data in the file.

        .. note::
            ``'b'`` mode flag is ignored (``self.FLAG_BINARY`` in
            ``self._flags``), because SSH treats all files as binary, since we
            have no idea what encoding the file is in, or even if the file is
            text data.

        :param int size: maximum number of bytes to read
        :returns:
            data read from the file (as bytes), or an empty string if EOF was
            encountered immediately
        File is closedzFile is not open for readingNr   )r   IOErrorr   r0   	bytearrayr   r   r   r8   _readr   EOFErrorextendr   FLAG_BUFFEREDmaxr   )r   sizeresultnew_data	read_sizer    r    r!   r7      sX   

zBufferedFile.readc           
      C   s  | j rtd| j| j@ std| j}d}	 | jr@| j| j@ r@t|dkr@|d tkr8|dd }| 	t
 n| 	t d| _|dure|dkret||kr^||d | _|d| }d}n]|t| }n| j}t|v sv| j| j@ rwt|v rwnDz| |}W n ty   d}Y nw |du st|dkrt | _|  jt|7  _| j| j@ r|S t|S ||7 }|  jt|7  _q|t}| j| j@ r|t}|dkr||k s|dk r|}|dkr|  jt|7  _| j| j@ r|S t|S |d }|| tkr|t|k r|| tkr|d7 }|r||d | j | _n||d | _||| }	|d| t }t| jdkrC|	tkrCd| _n| 	|	 |  jt|7  _| j| j@ rZ|S t|S )	a  
        Read one entire line from the file.  A trailing newline character is
        kept in the string (but may be absent when a file ends with an
        incomplete line).  If the size argument is present and non-negative, it
        is a maximum byte count (including the trailing newline) and an
        incomplete line may be returned.  An empty string is returned only when
        EOF is encountered immediately.

        .. note::
            Unlike stdio's ``fgets``, the returned string contains null
            characters (``'\0'``) if they occurred in the input.

        :param int size: maximum length of returned string.
        :returns:
            next line of the file, or an empty string if the end of the
            file has been reached.

            If the file was opened in binary (``'b'``) mode: bytes are returned
            Else: the encoding of the file is assumed to be UTF-8 and character
            strings (`str`) are returned
        r<   zFile not open for readingFTr   r   N)r   r=   r   r0   r   r   FLAG_UNIVERSAL_NEWLINEr8   r   _record_newliner   r   r   r   r?   r@   r   r   FLAG_BINARYr	   r   findr   )
r   rD   r-   	truncatednrF   posrposxposlfr    r    r!   r+      s   



)

zBufferedFile.readlinec                 C   sR   g }d}	 |   }t|dkr	 |S || |t|7 }|dur(||kr(	 |S q)a  
        Read all remaining lines using `readline` and return them as a list.
        If the optional ``sizehint`` argument is present, instead of reading up
        to EOF, whole lines totalling approximately sizehint bytes (possibly
        after rounding up to an internal buffer size) are read.

        :param int sizehint: desired maximum number of bytes to read.
        :returns: list of lines read from the file.
        r   TN)r+   r8   append)r   sizehintlines
byte_countr-   r    r    r!   	readlines@  s   

zBufferedFile.readlinesc                 C      t d)a  
        Set the file's current position, like stdio's ``fseek``.  Not all file
        objects support seeking.

        .. note::
            If a file is opened in append mode (``'a'`` or ``'a+'``), any seek
            operations will be undone at the next write (as the file position
            will move back to the end of the file).

        :param int offset:
            position to move to within the file, relative to ``whence``.
        :param int whence:
            type of movement: 0 = absolute; 1 = relative to the current
            position; 2 = relative to the end of the file.

        :raises: ``IOError`` -- if the file doesn't support random access.
        zFile does not support seeking.r=   )r   offsetwhencer    r    r!   seekV  s   zBufferedFile.seekc                 C      | j S )z
        Return the file's current position.  This may not be accurate or
        useful if the underlying file doesn't support random access, or was
        opened in append mode.

        :returns: file position (`number <int>` of bytes).
        )r   r   r    r    r!   tellj  s   zBufferedFile.tellc                 C   s   t |tr
|d}| jrtd| j| j@ std| j| j@ s(| | dS | j	
| | j| j@ ri|t}|dkrg| j	 }|t|t| 7 }| |d|d   t | _	| j	
||d d  dS | j	 | jkru|   dS )a8  
        Write data to the file.  If write buffering is on (``bufsize`` was
        specified and non-zero), some or all of the data may not actually be
        written yet.  (Use `flush` or `close` to force buffered data to be
        written out.)

        :param data: ``str``/``bytes`` data to write
        zutf-8r<   zFile not open for writingNr   r   )
isinstancestrencoder   r=   r   r3   rB   r)   r   writeFLAG_LINE_BUFFEREDrfindr   r*   r8   r   r^   r   r(   )r   r:   last_newline_poswbufr    r    r!   rb   t  s,   
	



zBufferedFile.writec                 C   s   |D ]}|  | qdS )a?  
        Write a sequence of strings to the file.  The sequence can be any
        iterable object producing strings, typically a list of strings.  (The
        name is intended to match `readlines`; `writelines` does not add line
        separators.)

        :param sequence: an iterable sequence of strings.
        N)rb   )r   sequencer-   r    r    r!   
writelines  s   	zBufferedFile.writelinesc                 C   s   | S )z
        Identical to ``iter(f)``.  This is a deprecated file interface that
        predates Python iterator support.
        r    r   r    r    r!   
xreadlines  s   zBufferedFile.xreadlinesc                 C   r]   r#   )r   r   r    r    r!   closed  s   zBufferedFile.closedc                 C   s   t  )z
        (subclass override)
        Read data from the stream.  Return ``None`` or raise ``EOFError`` to
        indicate EOF.
        )r@   )r   rD   r    r    r!   r?     s   zBufferedFile._readc                 C   rX   )zI
        (subclass override)
        Write data into the stream.
        zwrite not implementedrY   )r   r:   r    r    r!   _write  s   zBufferedFile._writec                 C   r5   )ai  
        (subclass override)
        Return the size of the file.  This is called from within `_set_mode`
        if the file is opened in append mode, so the file position can be
        tracked and `seek` and `tell` will work correctly.  If the file is
        a stream that can't be randomly accessed, you don't need to override
        this method,
        r   r    r   r    r    r!   	_get_size  s   	zBufferedFile._get_sizerrH   c                 C   s:  | j | _|dk r
d}|dkr|  j| j| jB O  _n)|dkr3|| _|  j| jO  _|  j| j M  _n|dkrC|  j| j| jB  M  _d|v sKd|v rS|  j| jO  _d|v s[d|v rc|  j| jO  _d|v r~|  j| j| jB O  _|  | _	| j	 | _
| _d|v r|  j| jO  _d|v r|  j| jO  _d	| _d	S d	S )
zM
        Subclasses call this method to initialize the BufferedFile.
        r   r   rm   +wabUN)r   r   r   rB   rc   r0   r3   FLAG_APPENDrl   r   r   r   rK   rI   r   )r   modebufsizer    r    r!   	_set_mode  s2   

zBufferedFile._set_modec                 C   s   t |}t|dkr>| |}||d  }| j| j@ r*|  j|7  _| j | _| _n|  j|7  _|  j|7  _t|dks
d S )Nr   )
memoryviewr8   rk   r   rs   r   r   r   )r   raw_datar:   countr    r    r!   r)     s   
	zBufferedFile._write_allc                 C   sl   | j | j@ sd S | jd u r|| _d S | j|kr%t| jtr%| j|f| _d S || jvr4|  j|f7  _d S d S r#   )r   rI   r   r_   r   )r   newliner    r    r!   rJ     s   


zBufferedFile._record_newliner#   )r   )rm   rH   ))__name__
__module____qualname____doc__r   SEEK_SETSEEK_CURSEEK_ENDr0   r3   rs   rK   rB   rc   rI   r"   r%   r'   r$   r(   r.   r1   r4   r6   r;   r7   r+   rW   r\   r^   rb   rh   ri   propertyrj   r?   rk   rl   rv   r)   rJ   r    r    r    r!   r
      sN    	




;
i

$

(r
   N)ior   paramiko.commonr   r   r   r   r   paramiko.utilr   r	   r
   r    r    r    r!   <module>   s   