U vfwJ@sLddlmZddlmZmZmZmZmZddlm Z m Z Gddde Z dS))BytesIO)linefeed_byte_valuecrlfcr_byte linefeed_byte cr_byte_value)ClosingContextManageruc@seZdZdZdZdZdZdZdZdZ dZ dZ dZ d Z d Zd d Zd dZddZddZddZddZddZddZddZddZd@d d!ZdAd"d#ZdBd$d%ZdCd&d'Zd(d)Zd*d+Zd,d-Zd.d/Z e!d0d1Z"d2d3Z#d4d5Z$d6d7Z%dDd:d;Z&dd?Z(dS)E BufferedFilezc Reusable base class to implement Python-style file buffering around a simpler stream. i r @cCsFd|_d|_|j|_t|_t|_d|_d|_ d|_ |_ d|_ dS)NrF) newlines_flags_DEFAULT_BUFSIZE_bufsizer_wbufferbytes_rbuffer_at_trailing_cr_closed_pos_realpos_sizeselfr >/opt/alt/python38/lib/python3.8/site-packages/paramiko/file.py__init__3s zBufferedFile.__init__cCs |dSN)closerr r r!__del__BszBufferedFile.__del__cCs|jrtd|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. zI/O operation on closed file)r ValueErrorrr r r!__iter__EszBufferedFile.__iter__cCs|d|_dS)zN Close the file. Future read and write operations will fail. TN)flushrrr r r!r$QszBufferedFile.closecCs||jt|_dS)z{ Write out any data in the write buffer. This may do nothing if write buffering is not turned on. N) _write_allrgetvaluerrr r r!r(XszBufferedFile.flushcCs|}|st|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)rliner r r!__next__as zBufferedFile.__next__cCs|j|j@|jkS)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_READrr r r!readablerszBufferedFile.readablecCs|j|j@|jkS)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_WRITErr r r!writable|szBufferedFile.writablecCsdS)z Check if the file supports random access. :returns: `True` if the file supports random access. If `False`, `seek` will raise an exception. Fr rr r r!seekableszBufferedFile.seekablecCs&|t|}||dt|<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)readlen)rZbuffdatar r r!readintoszBufferedFile.readintoNcCs|jrtd|j|j@s"td|dks2|dkrt|j}t|_|jt|7_z| |j }Wnt k r~d}YnX|dkst|dkrq| ||j t|7_ |jt|7_qVt|S|t|jkr|jd|}|j|d|_|jt|7_|St|j|kr|t|j}|j|j@rJt|j|}z| |}Wnt k rrd}YnX|dkst|dkrq|j|7_|j t|7_ q|jd|}|j|d|_|jt|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 closedzFile is not open for readingNr)rIOErrorrr/ bytearrayrrrr5_readrEOFErrorextendr FLAG_BUFFEREDmaxr)rsizeresultnew_data read_sizer r r!r4sN      zBufferedFile.readc Cs|jrtd|j|j@s"td|j}d}|jr~|j|j@r~t|dkr~|dtkrn|dd}| t n | t d|_|dk r|dkrt||kr||d|_|d|}d}q|t|}n|j }t |ks|j|j@rt |krqz||}Wntk rd}YnX|dks2t|dkrft|_|jt|7_|j|j@r^|St|S||7}|jt|7_q,|t }|j|j@r|t }|dkr||ks|dkr|}|dkr|jt|7_|j|j@r|St|S|d}||tkr6|t|kr6||tkr6|d7}|rR||d|j|_n||d|_|||} |d|t }t|jdkr| t krd|_n | | |jt|7_|j|j@r|St|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 r8zFile not open for readingFrr NT)rr9rr/rrFLAG_UNIVERSAL_NEWLINEr5r_record_newlinerrrrr;r<rr FLAG_BINARYr rfindr) rr@r- truncatednrBposrposZxposlfr r r!r+s                   zBufferedFile.readlinecCsLg}d}|}t|dkrqH|||t|7}|dk r||krqHq|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. rN)r+r5append)rsizehintlinesZ byte_countr-r r r! readlines@s    zBufferedFile.readlinescCs tddS)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. zFile does not support seeking.Nr9)roffsetwhencer r r!seekVszBufferedFile.seekcCs|jS)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 ` of bytes). )rrr r r!telljszBufferedFile.tellcCst|tr|d}|jr"td|j|j@s6td|j|j@sP||dS|j ||j|j @r| t }|dkr|j }|t|t|7}||d|dt|_ |j ||dddS|j |jkr|dS)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 zutf-8r8zFile not open for writingNrr ) isinstancestrencoderr9rr1r>r)rwriteFLAG_LINE_BUFFEREDrfindrr*r5rrVrr()rr6Zlast_newline_posZwbufr r r!rZts,         zBufferedFile.writecCs|D]}||qdS)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)rZ)rsequencer-r r r! writeliness  zBufferedFile.writelinescCs|S)z Identical to ``iter(f)``. This is a deprecated file interface that predates Python iterator support. r rr r r! xreadlinesszBufferedFile.xreadlinescCs|jSr#)rrr r r!closedszBufferedFile.closedcCs tdS)z (subclass override) Read data from the stream. Return ``None`` or raise ``EOFError`` to indicate EOF. N)r<)rr@r r r!r;szBufferedFile._readcCs tddS)zI (subclass override) Write data into the stream. zwrite not implementedNrR)rr6r r r!_writeszBufferedFile._writecCsdS)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, rr rr r r! _get_sizes zBufferedFile._get_sizerrDcCs:|j|_|dkrd}|dkr4|j|j|jBO_nR|dkrf||_|j|jO_|j|jM_n |dkr|j|j|jBM_d|ksd|kr|j|jO_d|ksd|kr|j|jO_d|kr|j|j|jBO_||_ |j |_ |_ d|kr|j|j O_d|kr6|j|j O_d |_d S) zM Subclasses call this method to initialize the BufferedFile. rr rc+wabUN)rrrr>r[r/r1 FLAG_APPENDrbrrrrGrEr)rmodebufsizer r r! _set_modes0   zBufferedFile._set_modecCsvt|}t|dkrr||}||d}|j|j@rT|j|7_|j|_|_q|j|7_|j|7_qdS)Nr) memoryviewr5rarrirrr)rraw_datar6countr r r!r)s    zBufferedFile._write_allcCsd|j|j@sdS|jdkr"||_n>|j|krFt|jtrF|j|f|_n||jkr`|j|f7_dSr#)rrErrWr)rnewliner r r!rFs   zBufferedFile._record_newline)N)N)N)r)rcrD))__name__ __module__ __qualname____doc__rSEEK_SETSEEK_CURSEEK_ENDr/r1rirGr>r[rEr"r%r'r$r(r.r0r2r3r7r4r+rQrUrVrZr^r_propertyr`r;rarbrlr)rFr r r r!r sL      ; i   $   (r N) iorZparamiko.commonrrrrrZ paramiko.utilrr r r r r r!s