a °…bÃLã@sXddlmZmZmZmZmZddlmZmZm Z m Z m Z ddl m Z Gdd„de ƒZdS)é)Úlinefeed_byte_valueÚcrlfÚcr_byteÚ linefeed_byteÚ cr_byte_value)ÚBytesIOÚPY2ÚuÚ bytes_typesÚ text_type)ÚClosingContextManagerc@s eZdZdZdZdZdZdZdZdZ dZ dZ dZ d Z d Zd d „Zd d„Zdd„Zdd„Zdd„Zerndd„Zndd„Zdd„Zdd„Zdd„Zdd „ZdBd"d#„ZdCd$d%„ZdDd&d'„ZdEd(d)„Zd*d+„Zd,d-„Z d.d/„Z!d0d1„Z"e#d2d3„ƒZ$d4d5„Z%d6d7„Z&d8d9„Z'dFdd?„Z)d@dA„Z*d!S)GÚ 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Ú_wbufferÚbytesÚ_rbufferÚ_at_trailing_crÚ_closedÚ_posÚ_realposÚ_size©Úself©r#ú4/tmp/pip-target-98j97qn4/lib/python/paramiko/file.pyÚ__init__2s zBufferedFile.__init__cCs | ¡dS©N)Úcloser!r#r#r$Ú__del__AszBufferedFile.__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Ú ValueErrorr!r#r#r$Ú__iter__DszBufferedFile.__iter__cCs| ¡d|_dS)zN Close the file. Future read and write operations will fail. TN)Úflushrr!r#r#r$r'PszBufferedFile.closecCs| |j ¡¡tƒ|_dS)z{ Write out any data in the write buffer. This may do nothing if write buffering is not turned on. N)Ú _write_allrÚgetvaluerr!r#r#r$r+WszBufferedFile.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`) read from the file. ©ÚreadlineÚ StopIteration©r"Úliner#r#r$Únextbs zBufferedFile.nextcCs| ¡}|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`) read from the file. r.r1r#r#r$Ú__next__ss 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_READr!r#r#r$Úreadable‚szBufferedFile.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_WRITEr!r#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#r!r#r#r$Úseekable–szBufferedFile.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)ÚreadÚlen)r"ZbuffÚdatar#r#r$Úreadinto szBufferedFile.readintoNcCsä|jrtdƒ‚|j|j@s"tdƒ‚|dus2|dkrÌt|jƒ}tƒ|_|jt|ƒ7_z|  |j ¡}Wnt y|d}Yn0|dusÄt|ƒdkr”qÄ|  |¡|j t|ƒ7_ |jt|ƒ7_qVt|ƒS|t|jƒkr|jd|…}|j|d…|_|jt|ƒ7_|St|jƒ|kr°|t|jƒ}|j|j@rHt|j|ƒ}z|  |¡}Wnt ynd}Yn0|dus°t|ƒdkrŒq°|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)rÚIOErrorrr5Ú bytearrayrrrr;Ú_readrÚEOFErrorÚextendrÚ FLAG_BUFFEREDÚmaxr)r"ÚsizeÚresultÚnew_dataÚ read_sizer#r#r$r:¬sN       zBufferedFile.readc CsÒ|jrtdƒ‚|j|j@s"tdƒ‚|j}d}|jr~|j|j@r~t|ƒdkr~|dtkrn|dd…}|  t ¡n |  t ¡d|_|durÊ|dkrÊt|ƒ|kr¼||d…|_|d|…}d}q€|t|ƒ}n|j }t |vsì|j|j@rðt |vrðq€z| |¡}Wntyd}Yn0|dus0t|ƒdkrdtƒ|_|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}||tkr4|t|ƒkr4||tkr4|d7}|rP||d…|j|_n||d…|_|||…} |d|…t }t|jƒdkrœ| t krœd|_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 r>zFile not open for readingFrrNTéÿÿÿÿ)rr?rr5rrÚFLAG_UNIVERSAL_NEWLINEr;rÚ_record_newlinerrrrrArBrrÚ FLAG_BINARYr rÚfindr) r"rFr2Ú truncatedÚnrHÚposÚrposZxposÚlfr#r#r$r/çs† ÿ þ ý       ÿÿ     ÿ þ ý  zBufferedFile.readlinecCsLg}d}| ¡}t|ƒdkrqH| |¡|t|ƒ7}|dur||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/r;Úappend)r"ÚsizehintÚlinesZ byte_countr2r#r#r$Ú readlinesPs    zBufferedFile.readlinescCs tdƒ‚dS)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.N©r?)r"ÚoffsetÚwhencer#r#r$ÚseekfszBufferedFile.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). )rr!r#r#r$ÚtellzszBufferedFile.tellcCsît|tƒr| d¡}|jr"tdƒ‚|j|j@s6tdƒ‚|j|j@sP| |¡dS|j   |¡|j|j @rÒ|  t ¡}|dkrÎ|j  ¡}|t|ƒt|ƒ7}| |d|d…¡tƒ|_ |j   ||dd…¡dS|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-8r>zFile not open for writingNrr)Ú isinstancer Úencoderr?rr7rDr,rÚwriteÚFLAG_LINE_BUFFEREDÚrfindrr-r;rr\rr+)r"r<Zlast_newline_posZwbufr#r#r$r_„s,         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)r_)r"Úsequencer2r#r#r$Ú writelines¨s  zBufferedFile.writelinescCs|S)z Identical to ``iter(f)``. This is a deprecated file interface that predates Python iterator support. r#r!r#r#r$Ú xreadlinesµszBufferedFile.xreadlinescCs|jSr&)rr!r#r#r$Úclosed¼szBufferedFile.closedcCs tƒ‚dS)zˆ (subclass override) Read data from the stream. Return ``None`` or raise ``EOFError`` to indicate EOF. N)rB)r"rFr#r#r$rAÂszBufferedFile._readcCs tdƒ‚dS)zI (subclass override) Write data into the stream. zwrite not implementedNrX)r"r<r#r#r$Ú_writeÊszBufferedFile._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#r!r#r#r$Ú _get_sizeÑs zBufferedFile._get_sizeÚrrJcCs:|j|_|dkrd}|dkr4|j|j|jBO_nR|dkrf||_|j|jO_|j|jM_n |dkr†|j|j|jBM_d|vs–d|vr¦|j|jO_d|vs¶d|vrÆ|j|jO_d|vrü|j|j|jBO_| ¡|_ |j |_ |_ d|vr|j|j O_d|vr6|j|j O_d |_d S) zM Subclasses call this method to initialize the BufferedFile. rrrhú+ÚwÚaÚbÚUN)rrrrDr`r5r7Ú FLAG_APPENDrgr rrrMrKr)r"ÚmodeÚbufsizer#r#r$Ú _set_modeÞs0   zBufferedFile._set_modecCsvt|ƒ}t|ƒdkrr| |¡}||d…}|j|j@rT|j|7_|j|_|_q|j|7_|j|7_qdS)Nr)Ú memoryviewr;rfrrnr rr)r"Úraw_datar<Úcountr#r#r$r,s    zBufferedFile._write_allcCsd|j|j@sdS|jdur"||_n>|j|krFt|jtƒrF|j|f|_n||jvr`|j|f7_dSr&)rrKrr]r )r"Únewliner#r#r$rLs   ÿ zBufferedFile._record_newline)N)N)N)r)rhrJ)+Ú__name__Ú __module__Ú __qualname__Ú__doc__rÚSEEK_SETÚSEEK_CURÚSEEK_ENDr5r7rnrMrDr`rKr%r(r*r'r+rr3r4r6r8r9r=r:r/rWr[r\r_rcrdÚpropertyrerArfrgrqr,rLr#r#r#r$r sP       ; i   $   (r N)Zparamiko.commonrrrrrZparamiko.py3compatrrr r r Z paramiko.utilr r r#r#r#r$Ús