
    .PhGF                         d Z ddlZddlmZmZ ddlmZmZmZ ddl	m
Z
 ddlmZ ddlmZ ddlmZ d	Z	  G d
 de          Z G d de          Z G d de          Zd Z G d d          ZdS )z
Module ``ioutils`` implements a number of helper classes and functions which
are useful when dealing with input, output, and bytestreams in a variety of
ways.
    N)BytesIOIOBase)ABCMetaabstractmethodabstractproperty)EINVAL)EncodedFile)TemporaryFile)zip_longestiUS  c                      e Zd ZdZeZd+dZd,dZed-d            Z	ed             Z
ed.d
            Zed,d            Zed.d            Zd Zed             Zed             Zed             Zed             Zed             Zd Zd Z eee          Zed             Zd Zd Zd Zed             Zed             Zed             Zd Z d,dZ!d Z"d Z#d  Z$d! Z%d" Z&e&Z'd# Z(d$ Z)d% Z*d& Z+d' Z,d( Z-d) Z.d* Z/dS )/SpooledIOBaseai  
    A base class shared by the SpooledBytesIO and SpooledStringIO classes.

    The SpooledTemporaryFile class is missing several attributes and methods
    present in the StringIO implementation. This brings the api as close to
    parity as possible so that classes derived from SpooledIOBase can be used
    as near drop-in replacements to save memory.
    @KL Nc                 "    || _         || _        d S N)	_max_size_dir)selfmax_sizedirs      O/var/www/html/test/jupyter/venv/lib/python3.11/site-packages/boltons/ioutils.py__init__zSpooledIOBase.__init__H   s    !			    c                 :    | j         rt          |dn|          dS )z$Raise a ValueError if file is closedNzI/O operation on closed file.)closed
ValueError)r   msgs     r   _checkClosedzSpooledIOBase._checkClosedL   s8    ; 	6 # =<146 6 6	6 	6r   c                     dS )z!Read n characters from the bufferN r   ns     r   readzSpooledIOBase.readQ         r   c                     dS )zWrite into the bufferNr    r   ss     r   writezSpooledIOBase.writeU   r$   r   r   c                     dS )z"Seek to a specific point in a fileNr    r   posmodes      r   seekzSpooledIOBase.seekY   r$   r   c                     dS )zReturns the next available lineNr    r   lengths     r   readlinezSpooledIOBase.readline]   r$   r   c                     dS )z=Returns a list of all lines from the current position forwardNr    r   sizehints     r   	readlineszSpooledIOBase.readlinesa   r$   r   c                 b    |                                   |D ]}|                     |           dS )zv
        Write lines to the file from an interable.

        NOTE: writelines() does NOT add line separators.
        N)r   r(   )r   lineslines      r   
writelineszSpooledIOBase.writelinese   sD     	 	 	DJJt	 	r   c                     dS )z5Roll file-like-object over into a real temporary fileNr    r   s    r   rolloverzSpooledIOBase.rollovero   r$   r   c                     dS )zReturn the current positionNr    r;   s    r   tellzSpooledIOBase.tells   r$   r   c                     dS )zShould return a flo instanceNr    r;   s    r   bufferzSpooledIOBase.bufferw   r$   r   c                     dS )z>Returns whether the file has been rolled to a real file or notNr    r;   s    r   _rolledzSpooledIOBase._rolled{   r$   r   c                     dS )zReturns the length of the dataNr    r;   s    r   lenzSpooledIOBase.len   r$   r   c                     | j         j        S r   r@   	softspacer;   s    r   _get_softspacezSpooledIOBase._get_softspace   s    {$$r   c                     || j         _        d S r   rF   )r   vals     r   _set_softspacezSpooledIOBase._set_softspace   s     #r   c                     | j         S r   )r@   r;   s    r   _filezSpooledIOBase._file   s
    {r   c                 4    | j                                         S r   )r@   closer;   s    r   rO   zSpooledIOBase.close   s    {  """r   c                 \    |                                   | j                                        S r   )r   r@   flushr;   s    r   rQ   zSpooledIOBase.flush   s(    {  """r   c                 \    |                                   | j                                        S r   )r   r@   isattyr;   s    r   rS   zSpooledIOBase.isatty   s(    {!!###r   c                     | j         j        S r   )r@   r   r;   s    r   r   zSpooledIOBase.closed   s    {!!r   c                 *    |                                  S r   )r>   r;   s    r   r+   zSpooledIOBase.pos   s    yy{{r   c                 *    |                                  S r   )getvaluer;   s    r   bufzSpooledIOBase.buf   s    }}r   c                 \    |                                   | j                                        S r   )r<   r@   filenor;   s    r   rZ   zSpooledIOBase.fileno   s#    {!!###r   c                 X   |                                   || j                                        S |dk     rt          t          d          |                                 }|                     |           | j                                         ||k     r|                     |           dS dS )z
        Truncate the contents of the buffer.

        Custom version of truncate that takes either no arguments (like the
        real SpooledTemporaryFile) or a single argument that truncates the
        value to a certain index location.
        Nr   zNegative size not allowed)r   r@   truncateOSErrorr   r>   r-   )r   sizer+   s      r   r\   zSpooledIOBase.truncate   s     	<;'')))!88&"=>>> iikk		$::IIcNNNNN :r   c                     |                                   |                                 }|                     d           |                                 }|                     |           |S )z!Return the entire files contents.r   )r   r>   r-   r#   r   r+   rJ   s      r   rW   zSpooledIOBase.getvalue   sQ    iikk		!iikk		#
r   c                     dS NTr    r;   s    r   seekablezSpooledIOBase.seekable       tr   c                     dS rb   r    r;   s    r   readablezSpooledIOBase.readable   rd   r   c                     dS rb   r    r;   s    r   writablezSpooledIOBase.writable   rd   r   c                 R   |                                   |                                 }|s|| j                                        }| j                            dt
          j                   || j                                        k    rt          | j                            |           |S Nr   )r   r1   r@   r>   r-   osSEEK_ENDStopIteration)r   r8   r+   s      r   __next__zSpooledIOBase.__next__   s    }} 	&+""$$CKQ,,,dk&&((((##  %%%r   c                     | j         S r   rD   r;   s    r   __len__zSpooledIOBase.__len__   s	    xr   c                 .    |                                   | S r   r   r;   s    r   __iter__zSpooledIOBase.__iter__       r   c                 .    |                                   | S r   rs   r;   s    r   	__enter__zSpooledIOBase.__enter__   ru   r   c                 8    | j                                          d S r   )rM   rO   )r   argss     r   __exit__zSpooledIOBase.__exit__   s    
r   c                 ,   t          || j                  r|                                 }|                                }	 |                     d           |                    d           d}t	          | |          D ]\  }}||k    rd} n|                     |           |                    |           |S # t
          $ rP 	 |                     |           n# t
          $ r Y nw xY w	 |                    |           n# t
          $ r Y nw xY w w xY wdS )Nr   TF)
isinstance	__class__r>   r-   r   	Exception)r   otherself_pos	other_poseq	self_line
other_lines          r   __eq__zSpooledIOBase.__eq__   sX   eT^,, 	yy{{H

I		!

1-8u-E-E  )Iz J.." / 		(###

9%%% 	  
 
 
IIh''''    DJJy))))    D
 usT   A6B7 7
DCD
C%"D$C%%D)C?>D?
D	DDDc                 .    |                      |           S r   )r   )r   r   s     r   __ne__zSpooledIOBase.__ne__  s    ;;u%%%%r   c                     dS rb   r    r;   s    r   __bool__zSpooledIOBase.__bool__  rd   r   c                 R    	 |                                   dS # t          $ r Y dS w xY w)z;Can fail when called at program exit so suppress traceback.N)rO   r~   r;   s    r   __del__zSpooledIOBase.__del__  s:    	JJLLLLL 	 	 	DD	s    
&&)r   Nr   r   r   )0__name__
__module____qualname____doc__r   __metaclass__r   r   r   r#   r(   r-   r1   r5   r9   r<   r>   r   r@   rB   rD   rH   rK   propertyrG   rM   rO   rQ   rS   r   r+   rX   rZ   r\   rW   rc   rf   rh   rn   nextrq   rt   rw   rz   r   r   r   r   r    r   r   r   r   =   s@         M   6 6 6 6
 0 0 0 ^0 $ $ ^$ 1 1 1 ^1 . . . ^. L L L ^L   D D ^D * * ^* + + + M M M - - -% % %$ $ $ 88I  X# # ## # #$ $ $ " " X"   X   X$ $ $   ,        
 
 
 D          :& & &      r   r   c                       e Zd ZdZddZd ZddZddZdd	Zd
 Z	e
d             Ze
d             Ze
d             Zd ZdS )SpooledBytesIOa  
    SpooledBytesIO is a spooled file-like-object that only accepts bytes. On
    Python 2.x this means the 'str' type; on Python 3.x this means the 'bytes'
    type. Bytes are written in and retrieved exactly as given, but it will
    raise TypeErrors if something other than bytes are written.

    Example::

        >>> from boltons import ioutils
        >>> with ioutils.SpooledBytesIO() as f:
        ...     f.write(b"Happy IO")
        ...     _ = f.seek(0)
        ...     isinstance(f.getvalue(), bytes)
        True
    r   c                 ^    |                                   | j                            |          S r   )r   r@   r#   r!   s     r   r#   zSpooledBytesIO.read*  s*    {"""r   c                 v   |                                   t          |t                    s4t          d                    t          |          j                            |                                 t          |          z   | j	        k    r| 
                                 | j                            |           d S )Nzbytes expected, got {})r   r|   bytes	TypeErrorformattyper   r>   rD   r   r<   r@   r(   r&   s     r   r(   zSpooledBytesIO.write.  s    !U## 	4;;Q      99;;Q4>11MMOOO!r   r   c                 `    |                                   | j                            ||          S r   )r   r@   r-   r*   s      r   r-   zSpooledBytesIO.seek9  s,    {T***r   Nc                     |                                   |r| j                            |          S | j                                        S r   )r   r@   r1   r/   s     r   r1   zSpooledBytesIO.readline=  sF     	*;''///;'')))r   c                 6    | j                             |          S r   )r@   r5   r3   s     r   r5   zSpooledBytesIO.readlinesD  s    {$$X...r   c                 6   | j         st          | j                  }| j                                        }|                    | j                                                   |                    |           | j                                         || _	        dS dS )z$Roll the StringIO over to a TempFiler   N)
rB   r
   r   r@   r>   r(   rW   r-   rO   _bufferr   tmpr+   s      r   r<   zSpooledBytesIO.rolloverG  s    | 	DI...C+""$$CIIdk**,,---HHSMMMKDLLL	 	r   c                 8    t          | j        t                     S r   )r|   r@   r   r;   s    r   rB   zSpooledBytesIO._rolledQ  s    dk73333r   c                 f    	 | j         S # t          $ r t                      | _         Y nw xY w| j         S r   )r   AttributeErrorr   r;   s    r   r@   zSpooledBytesIO.bufferU  sA    	%< 	% 	% 	%"99DLLL	%|s   	 ))c                 P   |                                  }| j        rA|                     d           t          j        |                                           j        }n4|                     dt          j                   |                                  }|                     |           |S )z Determine the length of the filer   )r>   rB   r-   rk   fstatrZ   st_sizerl   r`   s      r   rD   zSpooledBytesIO.len]  s}     iikk< 	IIaLLL(4;;==))1CCIIa%%%))++C		#
r   c                 \    |                                   | j                                        S r   )r   r@   r>   r;   s    r   r>   zSpooledBytesIO.tellj  s(    {!!!r   r   r   r   )r   r   r   r   r#   r(   r-   r1   r5   r<   r   rB   r@   rD   r>   r    r   r   r   r     s          # # # #	 	 	+ + + +* * * */ / / /   4 4 X4   X 
 
 X
" " " " "r   r   c                        e Zd ZdZ fdZddZd Zd ZddZdd
Z	ddZ
ed             Zed             Zd Zd Zed             Z xZS )SpooledStringIOu9  
    SpooledStringIO is a spooled file-like-object that only accepts unicode
    values. On Python 2.x this means the 'unicode' type and on Python 3.x this
    means the 'str' type. Values are accepted as unicode and then coerced into
    utf-8 encoded bytes for storage. On retrieval, the values are returned as
    unicode.

    Example::

        >>> from boltons import ioutils
        >>> with ioutils.SpooledStringIO() as f:
        ...     f.write(u"— Hey, an emdash!")
        ...     _ = f.seek(0)
        ...     isinstance(f.read(), str)
        True

    c                 H    d| _          t                      j        |i | d S rj   )_tellsuperr   )r   ry   kwargsr}   s      r   r   zSpooledStringIO.__init__  s,    
$)&)))))r   r   c                     |                                   | j        j                            ||          }|                                 t          |          z   | _        |S r   )r   r@   readerr#   r>   rD   r   )r   r"   rets      r   r#   zSpooledStringIO.read  sM    k %%a++YY[[3s88+

r   c                 "   |                                   t          |t                    s4t          d                    t          |          j                            |                                 }| j                                        t          |
                    d                    z   | j        k    r|                                  | j                            |
                    d                     |t          |          z   | _        d S )Nzstr expected, got {}utf-8)r   r|   strr   r   r   r   r>   r@   rD   encoder   r<   r(   r   )r   r'   current_poss      r   r(   zSpooledStringIO.write  s    !S!! 	299Q      iikk;AHHW$5$5 6 66$.HHMMOOO!((7++,,, 3q66)


r   c                     ||z   }	 ||k    rnO|t           z   |k    r|                     ||z
             n(|                     t                     }|t           z  }|snV|S )z8Traverse from current position to the right n codepoints)READ_CHUNK_SIZEr#   )r   current_positionr"   destr   s        r   _traverse_codepointsz$SpooledStringIO._traverse_codepoints  s    !#	4''  /1D88		$!11222ii00 /  '	* r   r   c                 Z   |                                   |t          j        k    r8| j                            d           |                     d|           || _        n|t          j        k    rG|                                 }|                     |                                 |           ||z   | _        ne|t          j	        k    rB| j                            d           | j
        |z
  }|                     d|           || _        nt          d| d          |                                 S )z/Traverse from offset to the specified codepointr   zInvalid whence (z, should be 0, 1, or 2))r   rk   SEEK_SETr@   r-   r   r   SEEK_CURr>   rl   rD   r   )r   r+   r,   	start_posdest_positions        r   r-   zSpooledStringIO.seek  s   2;KQ%%a---DJJR[  		I%%diikk3777"SDJJR[  KQ HsNM%%a777&DJJ@4@@@   yy{{r   Nc                     |                                   | j                            |                              d          }|                                 t          |          z   | _        |S )Nr   )r   r@   r1   decoder>   rD   r   )r   r0   r   s      r   r1   zSpooledStringIO.readline  sV    k""6**11'::YY[[3s88+

r   c                     d | j                             |          D             }|                                 t          d |D                       z   | _        |S )Nc                 8    g | ]}|                     d           S )r   )r   .0xs     r   
<listcomp>z-SpooledStringIO.readlines.<locals>.<listcomp>  s$    JJJQqxx  JJJr   c              3   4   K   | ]}t          |          V  d S r   rp   r   s     r   	<genexpr>z,SpooledStringIO.readlines.<locals>.<genexpr>  s(      &;&;!s1vv&;&;&;&;&;&;r   )r@   r5   r>   sumr   )r   r4   r   s      r   r5   zSpooledStringIO.readlines  sU    JJ$+*?*?*I*IJJJYY[[3&;&;s&;&;&;#;#;;

r   c                     	 | j         S # t          $ r% t          t                      d          | _         Y nw xY w| j         S )Nr   data_encoding)r   r   r	   r   r;   s    r   r@   zSpooledStringIO.buffer  sS    	I< 	I 	I 	I&wyyHHHDLLL	I|s   	 ,88c                 B    t          | j        j        t                     S r   )r|   r@   streamr   r;   s    r   rB   zSpooledStringIO._rolled  s    dk0'::::r   c                 T   | j         st          t          | j                  d          }| j                                        }|                    | j                                                   |                    |           | j        	                                 || _
        dS dS )z"Roll the buffer over to a TempFiler   r   r   N)rB   r	   r
   r   r@   r>   r(   rW   r-   rO   r   r   s      r   r<   zSpooledStringIO.rollover  s    | 	m	:::,35 5 5C+""$$CIIdk**,,---HHSMMMKDLLL	 	r   c                 8    |                                   | j        S )zReturn the codepoint position)r   r   r;   s    r   r>   zSpooledStringIO.tell  s    zr   c                    | j                                         }| j                             d           d}	 |                     t                    }|sn|t          |          z  }0| j                             |           |S )z.Determine the number of codepoints in the filer   )r@   r>   r-   r#   r   rD   )r   r+   totalr   s       r   rD   zSpooledStringIO.len  s     k  	))O,,C SXXE		
 	r   r   r   r   )r   r   r   r   r   r#   r(   r   r-   r1   r5   r   r@   rB   r<   r>   rD   __classcell__)r}   s   @r   r   r   o  s        "* * * * *   
* 
* 
*  4   0      
   X ; ; X;	 	 	  
   X    r   r   c                     t          | dd          rdS t          | dd          r;	 t          |                                 t                    rdS n# t          $ r Y nw xY wdS )NencodingFTrW   )getattrr|   rW   r   r~   )fileobjs    r   is_text_fileobjr     s    w
E** tw
E** 	'**,,c22 t 	 	 	D	5s   'A 
AAc                   4    e Zd ZdZd ZddZej        fdZdS )MultiFileReaderaj  Takes a list of open files or file-like objects and provides an
    interface to read from them all contiguously. Like
    :func:`itertools.chain()`, but for reading files.

       >>> mfr = MultiFileReader(BytesIO(b'ab'), BytesIO(b'cd'), BytesIO(b'e'))
       >>> mfr.read(3).decode('ascii')
       u'abc'
       >>> mfr.read(3).decode('ascii')
       u'de'

    The constructor takes as many fileobjs as you hand it, and will
    raise a TypeError on non-file-like objects. A ValueError is raised
    when file-like objects are a mix of bytes- and text-handling
    objects (for instance, BytesIO and StringIO).
    c                    t          d |D                       st          d          t          d |D                       rd| _        n/t          d |D                       rt	          d          d| _        || _        d| _        d S )	Nc           	          g | ]>}t          t          |d d                    ot          t          |dd                    ?S )r#   Nr-   )callabler   r   fs     r   r   z,MultiFileReader.__init__.<locals>.<listcomp>  s^     J J J;< WQ5566 7WQ5566J J Jr   zCMultiFileReader expected file-like objects with .read() and .seek()c                 ,    g | ]}t          |          S r    r   r   s     r   r   z,MultiFileReader.__init__.<locals>.<listcomp>#  s     555q""555r    c                 ,    g | ]}t          |          S r    r   r   s     r   r   z,MultiFileReader.__init__.<locals>.<listcomp>&  s     777/!$$777r   zEAll arguments to MultiFileReader must handle bytes OR text, not a mixr   r   )allr   _joineranyr   	_fileobjs_index)r   fileobjss     r   r   zMultiFileReader.__init__  s     J J@HJ J J K K 	9 8 9 9 955H55566 	DLL77h77788 	 9 : : : DL!r   Nc                    |s)| j                             d | j        D                       S g }|dk    r| j        t	          | j                  k     r|                    | j        | j                                     |                     t	          |d                   }||k     r| xj        dz  c_        ||z  }|dk    r| j        t	          | j                  k     | j                             |          S )zRead up to the specified *amt*, seamlessly bridging across
        files. Returns the appropriate type of string (bytes or text)
        for the input, and returns an empty string when the files are
        exhausted.
        c              3   >   K   | ]}|                                 V  d S r   )r#   r   s     r   r   z'MultiFileReader.read.<locals>.<genexpr>6  s*      $F$F!QVVXX$F$F$F$F$F$Fr   r   r      )r   joinr   r   rD   appendr#   )r   amtpartsgots       r   r#   zMultiFileReader.read/  s      	G<$$$F$Ft~$F$F$FFFFAgg$+DN(;(;;;LL499#>>???eBi..CSyyq 3JC Agg$+DN(;(;;; |  '''r   c                     |t           j        k    rt          d          |dk    rt          d          | j        D ]}|                    d           dS )zwEnables setting position of the file cursor to a given
        *offset*. Currently only supports ``offset=0``.
        z0MultiFileReader.seek() only supports os.SEEK_SETr   z;MultiFileReader only supports seeking to start at this timeN)rk   r   NotImplementedErrorr   r-   )r   offsetwhencer   s       r   r-   zMultiFileReader.seek@  sv     R[  %BD D DQ;;%MO O O 	 	AFF1IIII	 	r   r   )	r   r   r   r   r   r#   rk   r   r-   r    r   r   r   r     s\            "( ( ( (" #%+      r   r   )r   rk   ior   r   abcr   r   r   errnor   codecsr	   tempfiler
   	itertoolsr   r   r   r   r   r   r   r    r   r   <module>r     s  F 
 
			                 
             " " " " " " ! ! ! ! ! !Y Y Y Y YF Y Y YxS" S" S" S" S"] S" S" S"lM M M M Mm M M M`  > > > > > > > > > >r   