
    7gwJ                     Z    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e	          ZdS )    )BytesIO)linefeed_byte_valuecrlfcr_bytelinefeed_bytecr_byte_value)ClosingContextManageruc                       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 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 Zd Zd Zd Z e!d             Z"d Z#d  Z$d! Z%d)d$Z&d% Z'd& Z(dS )*BufferedFilezc
    Reusable base class to implement Python-style file buffering around a
    simpler stream.
    i    r                   @      c                     d | _         d| _        | j        | _        t	                      | _        t                      | _        d| _        d| _	        dx| _
        | _        d| _        d S )Nr   F)newlines_flags_DEFAULT_BUFSIZE_bufsizer   _wbufferbytes_rbuffer_at_trailing_cr_closed_pos_realpos_sizeselfs    G/var/www/html/syslog/venv/lib/python3.11/site-packages/paramiko/file.py__init__zBufferedFile.__init__3   sZ    -		$ %&%	DM


    c                 .    |                                   d S N)closer!   s    r#   __del__zBufferedFile.__del__B   s    

r%   c                 2    | 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!   s    r#   __iter__zBufferedFile.__iter__E   s"     < 	=;<<<r%   c                 <    |                                   d| _        dS )zN
        Close the file.  Future read and write operations will fail.
        TN)flushr   r!   s    r#   r(   zBufferedFile.closeQ   s     	

r%   c                     |                      | 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!   s    r#   r.   zBufferedFile.flushX   s4    
 	..00111		r%   c                 @    |                                  }|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"   lines     r#   __next__zBufferedFile.__next__a   s$     }} 	 r%   c                 2    | j         | j        z  | 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!   s    r#   readablezBufferedFile.readabler   s     dn,??r%   c                 2    | j         | j        z  | j        k    S )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!   s    r#   writablezBufferedFile.writable|   s     do-$/AAr%   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.
        F r!   s    r#   seekablezBufferedFile.seekable   s	     ur%   c                     |                      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datas      r#   readintozBufferedFile.readinto   s;     yyT## [s4yy[4yyr%   Nc                 0   | j         rt          d          | j        | j        z  st          d          ||dk     rt	          | j                  }t                      | _        | xj        t          |          z  c_        	 	 | 	                    | j
                  }n# t          $ r d}Y nw xY w|t          |          dk    rnP|                    |           | xj        t          |          z  c_        | xj        t          |          z  c_        t          |          S |t          | j                  k    rB| j        d|         }| j        |d         | _        | xj        t          |          z  c_        |S t          | j                  |k     r|t          | j                  z
  }| j        | j        z  rt          | j        |          }	 | 	                    |          }n# t          $ r d}Y nw xY w|t          |          dk    rnE| xj        |z  c_        | xj        t          |          z  c_        t          | j                  |k     | j        d|         }| j        |d         | _        | xj        t          |          z  c_        |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   r8   	bytearrayr   r   r   rB   _readr   EOFErrorextendr   FLAG_BUFFEREDmaxr   )r"   sizeresultnew_data	read_sizes        r#   rA   zBufferedFile.read   s   " < 	,*+++dn, 	:8999LdQhht}--F!GGDMIIV$II	+$#zz$*?@@HH $ $ $#HHH$$#h--1*<*<h'''X.		S]]*			+ == 3t}%%%%]5D5)F M$%%0DMIIV$IIM$-  4''s4=111I{T// :y99	 ::i00        c(mmq&8&8MMX%MMMMS]]*MM $-  4'' uu%dee,		S[[ 		s$   B B-,B-G) )G87G8c                    | j         rt          d          | j        | j        z  st          d          | j        }d}	 | j        ry| j        | j        z  rjt          |          dk    rW|d         t          k    r%|dd         }| 	                    t                     n| 	                    t                     d| _        |H|dk    rBt          |          |k    r||d         | _        |d|         }d}n|t          |          z
  }n| j        }t          |v s| j        | j        z  r
t          |v rn	 |                     |          }n# t          $ r d}Y nw xY w|t          |          dk    rPt!                      | _        | xj        t          |          z  c_        | j        | j        z  r|nt'          |          S ||z  }| xj        t          |          z  c_        |                    t                    }| j        | j        z  r.|                    t                    }|dk    r||k     s|dk     r|}|dk    r=| xj        t          |          z  c_        | j        | j        z  r|nt'          |          S |dz   }||         t,          k    r)|t          |          k     r||         t          k    r|dz  }|r||d         | j        z   | _        n||d         | _        |||         }	|d|         t          z   }t          | j                  dk    r|	t          k    rd| _        n| 	                    |	           | xj        t          |          z  c_        | j        | j        z  r|n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
        rG   zFile not open for readingFTr   r   N)r   rH   r   r8   r   r   FLAG_UNIVERSAL_NEWLINErB   r   _record_newliner   r   r   r   rJ   rK   r   r   FLAG_BINARYr
   r   findr   )
r"   rO   r5   	truncatednrQ   posrposxposlfs
             r#   r3   zBufferedFile.readline   s   . < 	,*+++dn, 	75666}	'	+$-K$"==- IIMM 71118D((....((111',$  tqyyt99$$$(KDM;D $I3t99$M$$d99 %>Eoo ::a==        c(mmq&8&8 %		SYY&		#{T-==Jtt1T77JHDMMS]]*MMO'	+R ii&&;44 	99W%%D		s

cAgg"99IIT"II;)99F44qwwFQwI&&s4yy  T
111AID  	( K$-7DMM KDM#d(^DSDzM)!##"-- $(D    $$$		SYY		{T%55Btt1T77Bs   3E	 	EEc                     g }d}	 |                                  }t          |          dk    rn1|                    |           |t          |          z  }|||k    rnY|S )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   )r3   rB   append)r"   sizehintlines
byte_countr5   s        r#   	readlineszBufferedFile.readlines@  ss     
	==??D4yyA~~LL#d))#J$:+A+A	 r%   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.rH   )r"   offsetwhences      r#   seekzBufferedFile.seekV  s    $ 6777r%   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!   s    r#   tellzBufferedFile.tellj  s     yr%   c                 6   t          |t                    r|                    d          }| j        rt	          d          | j        | j        z  st	          d          | j        | j        z  s|                     |           dS | j	        
                    |           | j        | j        z  r|                    t                    }|dk    r| j	                                        }|t          |          t          |          z
  z  }|                     |d|dz                       t!                      | _	        | j	        
                    ||dz   d                    dS | j	                                        | j        k    r|                                  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-8rG   zFile not open for writingNr   r   )
isinstancestrencoder   rH   r   r;   rM   r0   r   writeFLAG_LINE_BUFFEREDrfindr   r1   rB   r   rk   r   r.   )r"   rD   last_newline_poswbufs       r#   rp   zBufferedFile.writet  s    dC   	(;;w''D< 	,*+++do- 	75666d00 	OOD!!!FD!!!;00 		#zz-881$$}--// CIID		$99 %;'7!';%; <=== '		##D)9A)=)?)?$@AAAF =4=00JJLLLr%   c                 :    |D ]}|                      |           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)rp   )r"   sequencer5   s      r#   
writelineszBufferedFile.writelines  s-      	 	DJJtr%   c                     | S )z
        Identical to ``iter(f)``.  This is a deprecated file interface that
        predates Python iterator support.
        r>   r!   s    r#   
xreadlineszBufferedFile.xreadlines  s	    
 r%   c                     | j         S r'   )r   r!   s    r#   closedzBufferedFile.closed  s
    |r%   c                     t                      )z
        (subclass override)
        Read data from the stream.  Return ``None`` or raise ``EOFError`` to
        indicate EOF.
        )rK   )r"   rO   s     r#   rJ   zBufferedFile._read  s     jjr%   c                      t          d          )zI
        (subclass override)
        Write data into the stream.
        zwrite not implementedrf   )r"   rD   s     r#   _writezBufferedFile._write  s    
 -...r%   c                     dS )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!   s    r#   	_get_sizezBufferedFile._get_size  s	     qr%   rrT   c                    | j         | _        |dk     rd}|dk    r| xj        | j        | j        z  z  c_        n]|dk    r3|| _        | xj        | j        z  c_        | xj        | j         z  c_        n$|dk    r| xj        | j        | j        z   z  c_        d|v sd|v r| xj        | j        z  c_        d|v sd|v r| xj        | j        z  c_        d|v rI| xj        | j        | j        z  z  c_        |                                 | _	        | j	        x| _
        | _        d|v r| xj        | j        z  c_        d|v r| xj        | j        z  c_        d	| _        d	S d	S )
zM
        Subclasses call this method to initialize the BufferedFile.
        r   r   r   +wabUN)r   r   r   rM   rq   r8   r;   FLAG_APPENDr   r    r   r   rW   rU   r   )r"   modebufsizes      r#   	_set_modezBufferedFile._set_mode  s~   
 -Q;; Ga<< KK4-0GGGKKKq[[#DMKK4--KKKKD333KKK\\KKT/$2IIJJKK4KKSD[[KK4>)KK4KKSD[[KK4?*KK$;;KK4?T-===KK))DJ(,
2DI$;;KK4++KK$;;KK466KK !DMMM ;r%   c                 T   t          |          }t          |          dk    r|                     |          }||d          }| j        | j        z  r$| xj        |z  c_        | j        x| _        | _        n | xj        |z  c_        | xj        |z  c_        t          |          dk    d S )Nr   )
memoryviewrB   r~   r   r   r    r   r   )r"   raw_datarD   counts       r#   r0   zBufferedFile._write_all  s     (##$ii!mmKK%%E<D{T-- '

e#

,0J6	DMM		U"		& $ii!mm tr%   c                     | j         | j        z  sd S | j        	|| _        d S | j        |k    r*t          | j        t                    r| j        |f| _        d S || j        vr| xj        |fz  c_        d S d S r'   )r   rU   r   rm   r   )r"   newlines     r#   rV   zBufferedFile._record_newline  s     d99 	F= #DMMM]g%%*T]E*J*J%!]G4DMMMDM))MMgZ'MMMM *)r%   r'   )r   )r   rT   ))__name__
__module____qualname____doc__r   SEEK_SETSEEK_CURSEEK_ENDr8   r;   r   rW   rM   rq   rU   r$   r)   r,   r(   r.   r6   r9   r<   r?   rE   rA   r3   rd   ri   rk   rp   rw   ry   propertyr{   rJ   r~   r   r   r0   rV   r>   r%   r#   r   r      s        
 HHHIJKKM!    
 
 
      "@ @ @B B B  
 
 
9 9 9 9vgC gC gC gCR   ,8 8 8 8(  " " "H       X
  / / /	 	 	&! &! &! &!P  ( ( ( ( (r%   r   N)ior   paramiko.commonr   r   r   r   r   paramiko.utilr	   r
   r   r>   r%   r#   <module>r      s   $                    3 2 2 2 2 2 2 2q( q( q( q( q(( q( q( q( q( q(r%   