3 ›äL]€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 „ZdAd"d#„ZdBd$d%„ZdCd&d'„ZdDd(d)„Zd*d+„Zd,d-„Z d.d/„Z!d0d1„Z"e#d2d3„ƒZ$d4d5„Z%d6d7„Z&d8d9„Z'dFd;d<„Z(d=d>„Z)d?d@„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"ú3/tmp/pip-install-wfra5znf/paramiko/paramiko/file.pyÚ__init__2s zBufferedFile.__init__cCs |jƒdS)N)Úclose)r!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Ú ValueError)r!r"r"r#Ú__iter__DszBufferedFile.__iter__cCs|jƒd|_dS)zN Close the file. Future read and write operations will fail. TN)Úflushr)r!r"r"r#r%PszBufferedFile.closecCs|j|jjƒƒ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Úgetvaluer)r!r"r"r#r)WszBufferedFile.flushcCs|jƒ}|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|jƒ}|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,r-)r!r.r"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_READ)r!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_WRITE)r!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&|jt|ƒƒ}||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.readintoNc Csä|jrtdƒ‚|j|j@s"tdƒ‚|dks2|dkrÈ|j}tƒ|_|jt|ƒ7_xpy|j|j ƒ}Wnt k r|d}YnX|dks’t|ƒdkr”P||7}|j t|ƒ7_ |jt|ƒ7_qTW|S|t|jƒkr |jd|…}|j|d…|_|jt|ƒ7_|Sx¢t|jƒ|kr®|t|jƒ}|j|j @rFt |j|ƒ}y|j|ƒ}Wnt k rnd}YnX|dksˆt|ƒdkrŠP|j|7_|j t|ƒ7_ qW|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 zFile is closedzFile is not open for readingNr)rÚIOErrorrr1rrrr7Ú_readrÚEOFErrorrÚ FLAG_BUFFEREDÚmaxr)r!ÚsizeÚresultÚnew_dataÚ read_sizer"r"r#r6¬sP    zBufferedFile.readc CsÖ|jrtdƒ‚|j|j@s"tdƒ‚|j}d}xT|jr‚|j|j@r‚t|ƒdkr‚|dtkrr|dd…}|j t ƒn |j t ƒd|_|dk rÌ|dkrÌt|ƒ|kr¾||d…|_|d|…}d}P|t|ƒ}n|j }t |ksî|j|j@rðt |krðPy|j|ƒ}Wntk rd}YnX|dks2t|ƒdkrftƒ|_|jt|ƒ7_|j|j@r^|St|ƒS||7}|jt|ƒ7_q0W|jt ƒ}|j|j@rÈ|jt ƒ}|dkrÈ||ksÄ|dkrÈ|}|dkrþ|jt|ƒ7_|j|j@rö|St|ƒS|d}||tkr8|t|ƒkr8||tkr8|d7}|rT||d…|j|_n||d…|_|||…} |d|…t }t|jƒdkr | t kr d|_n |j | ƒ|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 zFile is closedzFile not open for readingFrrNTéÿÿÿÿ)rr:rr1rrÚFLAG_UNIVERSAL_NEWLINEr7rÚ_record_newlinerrrrr;r<rrÚ FLAG_BINARYr rÚfindr) r!r?r.Z truncatedÚnrAÚposÚrposZxposÚlfr"r"r#r,çsv               zBufferedFile.readlinecCsPg}d}xB|jƒ}t|ƒdkr P|j|ƒ|t|ƒ7}|dk r ||kr Pq W|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,r7Úappend)r!ÚsizehintÚlinesZ byte_countr.r"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). )r)r!r"r"r#ÚtellzszBufferedFile.tellcCsît|tƒr|jdƒ}|jr"tdƒ‚|j|j@s6tdƒ‚|j|j@sP|j|ƒdS|j j |ƒ|j|j @rÒ|j t ƒ}|dkrÎ|j jƒ}|t|ƒt|ƒ7}|j|d|d…ƒtƒ|_ |j j ||dd…ƒdS|j jƒ|jkrê|jƒ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-8zFile is closedzFile not open for writingNrr)Ú isinstancer Úencoderr:rr3r=r*rÚwriteÚFLAG_LINE_BUFFEREDÚrfindrr+r7rrSrr))r!r8Zlast_newline_posZwbufr"r"r#rV„s,         zBufferedFile.writecCsx|D]}|j|ƒqWdS)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)rV)r!Úsequencer.r"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|jS)N)r)r!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)r<)r!r?r"r"r#r;ÂszBufferedFile._readcCs tdƒ‚dS)zI (subclass override) Write data into the stream. zwrite not implementedN)r:)r!r8r"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ÚrcCs:|j|_|dkrd}|dkr4|j|j|jBO_nR|dkrf||_|j|jO_|j|jM_n |dkr†|j|j|jBM_d|ks–d|kr¦|j|jO_d|ks¶d|krÆ|j|jO_d|krü|j|j|jBO_|jƒ|_ |j |_ |_ d|kr|j|j O_d|kr6|j|j O_d |_d S) zM Subclasses call this method to initialize the BufferedFile. rrr_ú+ÚwÚaÚbÚUN)rrrr=rWr1r3Ú FLAG_APPENDr^r rrrFrDr)r!ÚmodeÚbufsizer"r"r#Ú _set_modeÞs0   zBufferedFile._set_modecCsrxlt|ƒdkrl|j|ƒ}||d…}|j|j@rN|j|7_|j|_|_q|j|7_|j|7_qWdS)Nr)r7r]rrer rr)r!r8Úcountr"r"r#r*s   zBufferedFile._write_allcCsd|j|j@sdS|jdkr"||_n>|j|krFt|jtƒrF|j|f|_n||jkr`|j|f7_dS)N)rrDrrTr )r!Únewliner"r"r#rEs     zBufferedFile._record_newline)N)N)N)rrC)r_rC)+Ú__name__Ú __module__Ú __qualname__Ú__doc__rÚSEEK_SETÚSEEK_CURÚSEEK_ENDr1r3rerFr=rWrDr$r&r(r%r)rr/r0r2r4r5r9r6r,rOrRrSrVrZr[Úpropertyr\r;r]r^rhr*rEr"r"r"r#r sN       ; i   $   (r N)Zparamiko.commonrrrrrZparamiko.py3compatrrr r r Z paramiko.utilr r r"r"r"r#Ús