
    IiwJ                     R    S SK Jr  S SKJrJrJrJrJr  S SKJ	r	J
r
   " S S\	5      rg)    )BytesIO)linefeed_byte_valuecrlfcr_bytelinefeed_bytecr_byte_value)ClosingContextManageruc                       \ rS rSrSrSrSrSrSrSr	Sr
SrSrS	rS
rSrS rS rS rS rS rS rS rS rS rS rS'S jrS'S jrS'S jrS(S jrS rS rS r S r!\"S 5       r#S  r$S! r%S" r&S)S# jr'S$ r(S% r)S&r*g)*BufferedFile   zW
Reusable base class to implement Python-style file buffering around a
simpler stream.
i    r                   @      c                     S U l         SU l        U R                  U l        [	        5       U l        [        5       U l        SU l        SU l	        S=U l
        U l        SU l        g )Nr   F)newlines_flags_DEFAULT_BUFSIZE_bufsizer   _wbufferbytes_rbuffer_at_trailing_cr_closed_pos_realpos_sizeselfs    3/venv/lib/python3.13/site-packages/paramiko/file.py__init__BufferedFile.__init__3   sU    --	$ %&%	DM
    c                 $    U R                  5         g N)closer"   s    r$   __del__BufferedFile.__del__B   s    

r'   c                 >    U R                   (       a  [        S5      eU $ )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__BufferedFile.__iter__E   s     <<;<<r'   c                 2    U R                  5         SU l        g)z>
Close the file.  Future read and write operations will fail.
TN)flushr   r"   s    r$   r*   BufferedFile.closeQ   s     	

r'   c                 t    U R                  U R                  R                  5       5        [        5       U l        g)zc
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$   r2   BufferedFile.flushX   s)    
 	..01	r'   c                 @    U R                  5       nU(       d  [        eU$ )aP  
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__BufferedFile.__next__a   s     }}r'   c                 N    U R                   U R                  -  U R                  :H  $ )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$   readableBufferedFile.readabler   s      dnn,??r'   c                 N    U R                   U R                  -  U R                  :H  $ )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$   writableBufferedFile.writable|   s      doo-$//AAr'   c                     g)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$   seekableBufferedFile.seekable   s     r'   c                 f    U R                  [        U5      5      nX!S[        U5      & [        U5      $ )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$   readintoBufferedFile.readinto   s.     yyT# [s4y4yr'   Nc                 n   U R                   (       a  [        S5      eU R                  U R                  -  (       d  [        S5      eUb  US:  a  [	        U R
                  5      n[        5       U l        U =R                  [        U5      -  sl          U R                  U R                  5      nUb  [        U5      S:X  a  OOUR                  U5        U =R                  [        U5      -  sl        U =R                  [        U5      -  sl        M~  [        U5      $ U[        U R
                  5      ::  aC  U R
                  SU nU R
                  US U l        U =R                  [        U5      -  sl        U$ [        U R
                  5      U:  a  U[        U R
                  5      -
  nU R                  U R                  -  (       a  [        U R                   U5      n U R                  U5      nUb  [        U5      S:X  a  ONU =R
                  U-  sl        U =R                  [        U5      -  sl        [        U R
                  5      U:  a  M  U R
                  SU nU R
                  US U l        U =R                  [        U5      -  sl        U$ ! [         a    Sn GNf = f! [         a    Sn Nf = f)a1  
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   r?   	bytearrayr   r   r   rL   _readr   EOFErrorextendr    FLAG_BUFFEREDmaxr   )r#   sizeresultnew_data	read_sizes        r$   rK   BufferedFile.read   s,   " <<*++dnn,899LdQht}}-F!GDMIIV$I$#zz$*?*?@H $#h-1*<h'X.		S]*	  = 3t}}%%]]5D)F MM$%0DMIIV$IM$-- 4's4==11I{{T///y9	 ::i0  c(mq&8MMX%MMMS]*M $-- 4' u%de,		S[ 	9   $#H$&    s$   J J% J"!J"%J43J4c                 2   U R                   (       a  [        S5      eU R                  U R                  -  (       d  [        S5      eU R                  nSn U R
                  (       aq  U R                  U R                  -  (       aS  [        U5      S:  aD  US   [        :X  a  USS nU R                  [        5        OU R                  [        5        SU l        Ub5  US:  a/  [        U5      U:  a  X!S U l        USU nSnOU[        U5      -
  nOU R                  n[        U;   d(  U R                  U R                  -  (       a  [        U;   a  O U R                  U5      nUb  [        U5      S:X  aX  [!        5       U l        U =R"                  [        U5      -  sl        U R                  U R$                  -  (       a  U$ ['        U5      $ X%-  nU =R(                  [        U5      -  sl        GM  UR+                  [        5      nU R                  U R                  -  (       a(  UR+                  [        5      nUS:  a  Xv:  d  US:  a  UnUS:X  aI  U =R"                  [        U5      -  sl        U R                  U R$                  -  (       a  U$ ['        U5      $ US-   nX&   [,        :X  a   U[        U5      :  a  X(   [        :X  a  US-  nU(       a  X(S U R                  -   U l        O	X(S U l        X&U n	USU [        -   n[        U R                  5      S:X  a  U	[        :X  a  SU l        OU R                  U	5        U =R"                  [        U5      -  sl        U R                  U R$                  -  (       a  U$ ['        U5      $ ! [         a    Sn GN5f = f)	aE  
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
rR   zFile not open for readingFTr   r   N)r   rS   r   r?   r   r   FLAG_UNIVERSAL_NEWLINErL   r   _record_newliner   r   r   r   rU   rV   r   r   FLAG_BINARYr
   r    findr   )
r#   rZ   r;   	truncatednr\   posrposxposlfs
             r$   r9   BufferedFile.readline   s   . <<*++dnn,566}}	$$KK$"="==IM 7118D((.((1',$  tqyt9$$(KDM;D $I3t9$MM$d999go ::a=  c(mq&8 %		SY&	#{{T-=-==tJ1T7JDMMS]*MO R ii&;;44499W%D	
cAg"9IIT"I;;)9)994FqwFQwI&s4y 
11AID  K$--7DM KDMd^DSzM)!#"- $(D   $		SY	{{T%5%55tB1T7BU    s   N NNc                     / nSn U R                  5       n[        U5      S:X  a   U$ UR                  U5        U[        U5      -  nUb  X1:  a   U$ MN  )av  
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   )r9   rL   append)r#   sizehintlines
byte_countr;   s        r$   	readlinesBufferedFile.readlines@  se     
==?D4yA~
 	 LL#d)#J$:+A r'   c                     [        S5      e)aD  
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.rS   )r#   offsetwhences      r$   seekBufferedFile.seekV  s    $ 677r'   c                     U R                   $ )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$   tellBufferedFile.tellj  s     yyr'   c                 H   [        U[        5      (       a  UR                  S5      nU R                  (       a  [	        S5      eU R
                  U R                  -  (       d  [	        S5      eU R
                  U R                  -  (       d  U R                  U5        gU R                  R                  U5        U R
                  U R                  -  (       a  UR                  [        5      nUS:  az  U R                  R                  5       nU[        U5      [        U5      -
  -  nU R                  USUS-    5        [!        5       U l	        U R                  R                  X2S-   S 5        gU R                  R#                  5       U R$                  :  a  U R'                  5         g)a  
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-8rR   zFile not open for writingNr   r   )
isinstancestrencoder   rS   r   rC   rX   r5   r   writeFLAG_LINE_BUFFEREDrfindr   r6   rL   r   rz   r   r2   )r#   rN   last_newline_poswbufs       r$   r   BufferedFile.writet  s>    dC  ;;w'D<<*++doo-566d000OOD!D!;;000#zz-81$}}--/ CID	$99 %;'7!'; <= '	##DA)=)?$@A ==4==0JJLr'   c                 8    U H  nU R                  U5        M     g)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)r   )r#   sequencer;   s      r$   
writelinesBufferedFile.writelines  s     DJJt r'   c                     U $ )zg
Identical to ``iter(f)``.  This is a deprecated file interface that
predates Python iterator support.
rG   r"   s    r$   
xreadlinesBufferedFile.xreadlines  s	    
 r'   c                     U R                   $ r)   )r   r"   s    r$   closedBufferedFile.closed  s    ||r'   c                     [        5       e)zh
(subclass override)
Read data from the stream.  Return ``None`` or raise ``EOFError`` to
indicate EOF.
)rV   )r#   rZ   s     r$   rU   BufferedFile._read  s     jr'   c                     [        S5      e)z1
(subclass override)
Write data into the stream.
zwrite not implementedrt   )r#   rN   s     r$   _writeBufferedFile._write  s    
 -..r'   c                     g)a1  
(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   rG   r"   s    r$   	_get_sizeBufferedFile._get_size  s     r'   c                    U R                   U l        US:  a  SnUS:X  a-  U =R                  U R                  U R                  -  -  sl        OUS:  aF  X l        U =R                  U R                  -  sl        U =R                  U R                  ) -  sl        O3US:X  a-  U =R                  U R                  U R                  -  ) -  sl        SU;   d  SU;   a  U =R                  U R
                  -  sl        SU;   d  SU;   a  U =R                  U R                  -  sl        SU;   aY  U =R                  U R                  U R                  -  -  sl        U R                  5       U l	        U R                  =U l
        U l        SU;   a  U =R                  U R                  -  sl        SU;   a'  U =R                  U R                  -  sl        S	U l        g	g	)
z=
Subclasses call this method to initialize the BufferedFile.
r   r   r+wabUN)r   r   r   rX   r   r?   rC   FLAG_APPENDr   r!   r   r    rc   ra   r   )r#   modebufsizes      r$   	_set_modeBufferedFile._set_mode  sm   
 --Q; Ga< KK4--0G0GGGKq[#MKK4---KKKD3333K\KKT//$2I2IIJJK4KSD[KK4>>)K4KSD[KK4??*K$;KK4??T-=-===K)DJ(,

2DI$;KK4+++K$;KK4666K !DM r'   c                 p   [        U5      n[        U5      S:  a  U R                  U5      nX#S  nU R                  U R                  -  (       a.  U =R
                  U-  sl        U R
                  =U l        U l        O*U =R                  U-  sl        U =R                  U-  sl        [        U5      S:  a  M  g )Nr   )
memoryviewrL   r   r   r   r!   r   r    )r#   raw_datarN   counts       r$   r5   BufferedFile._write_all  s     (#$i!mKK%E<D{{T---

e#
,0JJ6	DM		U"	& $i!m r'   c                 <   U R                   U R                  -  (       d  g U R                  c  Xl        g U R                  U:w  a3  [        U R                  [        5      (       a  U R                  U4U l        g XR                  ;  a  U =R                  U4-  sl        g g r)   )r   ra   r   r}   r   )r#   newlines     r$   rb   BufferedFile._record_newline  ss     d999== #M]]g%*T]]E*J*J!]]G4DMMM)MMgZ'M *r'   )
r   r   r   r   r   r   r    r!   r   r   r)   )r   )r   r`   )+__name__
__module____qualname____firstlineno____doc__r   SEEK_SETSEEK_CURSEEK_ENDr?   rC   r   rc   rX   r   ra   r%   r+   r/   r*   r2   r<   r@   rD   rH   rO   rK   r9   rq   rw   rz   r   r   r   propertyr   rU   r   r   r   r5   rb   __static_attributes__rG   r'   r$   r   r      s    
 HHHIJKKM!
"@B
9vgCR,8("H  
/	&!P(r'   r   N)ior   paramiko.commonr   r   r   r   r   paramiko.utilr	   r
   r   rG   r'   r$   <module>r      s(   $   3q(( q(r'   