
    hL                       d dl mZ d dlZd dlZd dlZd dlmZ d dlmZm	Z	 d dl
mZ d dlmZmZmZmZmZ ddlmZ dd	lmZ dd
lmZ erd dlmZmZmZmZ  G d dee         Z G d dee         Z G d dee         Z G d dee         Z e	 	 	 	 d	 	 	 	 	 	 	 	 	 dd       Z!e	 	 	 	 d	 	 	 	 	 	 	 	 	 dd       Z!	 	 	 	 d	 	 	 	 	 	 	 	 	 ddZ!e	 	 	 d 	 	 	 	 	 	 	 d!d       Z"e	 	 	 d 	 	 	 	 	 	 	 d"d       Z"	 	 	 d 	 	 	 	 	 	 	 d#dZ"d$dZ#d%dZ$y)&    )annotationsN)Iterable)BytesIOTextIOWrapper)TracebackType)TYPE_CHECKINGAnyAnyStrGenericoverload   )	to_thread)	AsyncFile)checkpoint_if_cancelled)OpenBinaryModeOpenTextModeReadableBufferWriteableBufferc                     e Zd ZU dZded<   e	 	 	 	 	 	 	 ddd	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 dd       Ze	 	 	 	 	 	 ddd	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 dd       Z	 	 	 	 	 	 	 ddd	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 dd	Zdd
Z	 	 	 	 	 	 	 	 ddZy)TemporaryFilea  
    An asynchronous temporary file that is automatically created and cleaned up.

    This class provides an asynchronous context manager interface to a temporary file.
    The file is created using Python's standard `tempfile.TemporaryFile` function in a
    background thread, and is wrapped as an asynchronous file using `AsyncFile`.

    :param mode: The mode in which the file is opened. Defaults to "w+b".
    :param buffering: The buffering policy (-1 means the default buffering).
    :param encoding: The encoding used to decode or encode the file. Only applicable in
        text mode.
    :param newline: Controls how universal newlines mode works (only applicable in text
        mode).
    :param suffix: The suffix for the temporary file name.
    :param prefix: The prefix for the temporary file name.
    :param dir: The directory in which the temporary file is created.
    :param errors: The error handling scheme used for encoding/decoding errors.
    AsyncFile[AnyStr]_async_file.errorsc                    y N 	selfmode	bufferingencodingnewlinesuffixprefixdirr   s	            :D:\jyotish\venv\Lib\site-packages\anyio/_core/_tempfile.py__init__zTemporaryFile.__init__/            c                    y r   r   r   s	            r'   r(   zTemporaryFile.__init__<   r)   r*   Nc               t    || _         || _        || _        || _        || _        || _        || _        || _        y r   r    r!   r"   r#   r$   r%   r&   r   r   s	            r'   r(   zTemporaryFile.__init__J   s<     	" "("("r*   c                    K   t        j                   fd       d {   }t        |       _         j                  S 7  w)Nc            
         t        j                   j                   j                   j                   j
                   j                   j                   j                   j                        S )Nr   )
tempfiler   r    r!   r"   r#   r$   r%   r&   r   r   s   r'   <lambda>z*TemporaryFile.__aenter__.<locals>.<lambda>a   sH    H**		{{	r*   r   run_syncr   r   r   fps   ` r'   
__aenter__zTemporaryFile.__aenter___   s@     %%	
 
 %R=
   AA !Ac                T   K   | j                   j                          d {    y 7 wr   r   acloser   exc_type	exc_value	tracebacks       r'   	__aexit__zTemporaryFile.__aexit__o          %%'''   (&(.......)r   zTemporaryFile[bytes]r    r   r!   intr"   
str | Noner#   rE   r$   rE   r%   rE   r&   rE   r   rE   )......)r   zTemporaryFile[str]r    r   r!   rD   r"   rE   r#   rE   r$   rE   r%   rE   r&   rE   r   rE   )w+bNNNNN)r    zOpenTextMode | OpenBinaryModer!   rD   r"   rE   r#   rE   r$   rE   r%   rE   r&   rE   r   rE   returnNonerH   r   r=   ztype[BaseException] | Noner>   zBaseException | Noner?   zTracebackType | NonerH   rI   	__name__
__module____qualname____doc____annotations__r   r(   r7   r@   r   r*   r'   r   r      s   & #"  #"!   !"  	
        "!   !   	
       /4#"!! "+  	
      
*  (,( (( (	(
 
(r*   r   c                  2   e Zd ZU dZded<   e	 	 	 	 	 	 	 	 dddd	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 dd       Ze	 	 	 	 	 	 	 dddd	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 dd       Z	 	 	 	 	 	 	 	 ddd	d	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 dd
ZddZ	 	 	 	 	 	 	 	 ddZy)NamedTemporaryFilea|  
    An asynchronous named temporary file that is automatically created and cleaned up.

    This class provides an asynchronous context manager for a temporary file with a
    visible name in the file system. It uses Python's standard
    :func:`~tempfile.NamedTemporaryFile` function and wraps the file object with
    :class:`AsyncFile` for asynchronous operations.

    :param mode: The mode in which the file is opened. Defaults to "w+b".
    :param buffering: The buffering policy (-1 means the default buffering).
    :param encoding: The encoding used to decode or encode the file. Only applicable in
        text mode.
    :param newline: Controls how universal newlines mode works (only applicable in text
        mode).
    :param suffix: The suffix for the temporary file name.
    :param prefix: The prefix for the temporary file name.
    :param dir: The directory in which the temporary file is created.
    :param delete: Whether to delete the file when it is closed.
    :param errors: The error handling scheme used for encoding/decoding errors.
    :param delete_on_close: (Python 3.12+) Whether to delete the file on close.
    r   r   .)r   delete_on_closec	                    y r   r   r   r    r!   r"   r#   r$   r%   r&   deleter   rT   s              r'   r(   zNamedTemporaryFile.__init__        r*   c	                    y r   r   rV   s              r'   r(   zNamedTemporaryFile.__init__   rX   r*   NTc	          
     l    |||||||||	d	| _         t        j                  dk\  r|
| j                   d<   y y )N)	r    r!   r"   r#   r$   r%   r&   rW   r         rT   )_paramssysversion_inforV   s              r'   r(   zNamedTemporaryFile.__init__   sL     " 
(
 w&.=DLL*+ 'r*   c                    K   t        j                   fd       d {   }t        |       _         j                  S 7  w)Nc                 B    t        j                  di  j                  S Nr   )r0   rS   r^   r1   s   r'   r2   z/NamedTemporaryFile.__aenter__.<locals>.<lambda>   s    H//?$,,?r*   r3   r5   s   ` r'   r7   zNamedTemporaryFile.__aenter__   s>     %%?
 
 %R=	
r8   c                T   K   | j                   j                          d {    y 7 wr   r:   r<   s       r'   r@   zNamedTemporaryFile.__aexit__   rA   rB   ........)r   zNamedTemporaryFile[bytes]r    r   r!   rD   r"   rE   r#   rE   r$   rE   r%   rE   r&   rE   rW   boolr   rE   rT   rf   rC   )r   zNamedTemporaryFile[str]r    r   r!   rD   r"   rE   r#   rE   r$   rE   r%   rE   r&   rE   rW   rf   r   rE   rT   rf   )rF   rG   NNNNNT)r    OpenBinaryMode | OpenTextModer!   rD   r"   rE   r#   rE   r$   rE   r%   rE   r&   rE   rW   rf   r   rE   rT   rf   rH   rI   rJ   rK   rL   r   r*   r'   rS   rS   x   s   , #"  #"!   ! #'  	
          "!   ! #%  	
        " /4#"!!> " $>+> > 	>
 > > > > > > > 
>8 (,( (( (	(
 
(r*   rS   c                  B    e Zd ZU dZdZded<   e	 	 	 	 	 	 	 	 ddd	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 dd       Ze	 	 	 	 	 	 	 	 ddd	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 d d       Z	 	 	 	 	 	 	 	 d!d	d	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 d" fd
Zd# fdZd#dZ	d#dZ
ed$d       Zd%d& fdZd%d' fdZd( fdZd) fdZd* fdZd* fdZej(                  fd+ fdZd, fdZd-d. fdZed/d       Zed0d       Zd1 fdZe	 	 	 	 	 	 d2d       Ze	 	 	 	 	 	 d3d       Zd4 fdZ xZS )5SpooledTemporaryFilea  
    An asynchronous spooled temporary file that starts in memory and is spooled to disk.

    This class provides an asynchronous interface to a spooled temporary file, much like
    Python's standard :class:`~tempfile.SpooledTemporaryFile`. It supports asynchronous
    write operations and provides a method to force a rollover to disk.

    :param max_size: Maximum size in bytes before the file is rolled over to disk.
    :param mode: The mode in which the file is opened. Defaults to "w+b".
    :param buffering: The buffering policy (-1 means the default buffering).
    :param encoding: The encoding used to decode or encode the file (text mode only).
    :param newline: Controls how universal newlines mode works (text mode only).
    :param suffix: The suffix for the temporary file name.
    :param prefix: The prefix for the temporary file name.
    :param dir: The directory in which the temporary file is created.
    :param errors: The error handling scheme used for encoding/decoding errors.
    Frf   _rolled.r   c	                    y r   r   
r   max_sizer    r!   r"   r#   r$   r%   r&   r   s
             r'   r(   zSpooledTemporaryFile.__init__        r*   c	                    y r   r   rl   s
             r'   r(   zSpooledTemporaryFile.__init__   rn   r*   Nc	          	         ||||||||	d| _         || _        d|v rt        
|   t	                      y t        
|   t        t	               ||	|d             y )Nr-   bT)r"   r   r#   write_through)_tempfile_params	_max_sizesuperr(   r   r   )r   rm   r    r!   r"   r#   r$   r%   r&   r   	__class__s             r'   r(   zSpooledTemporaryFile.__init__  sl     " 	1
 "$;GWY'GI%!#"&r*   c                   K   | j                   s| j                  j                          y t        |           d {    y 7 wr   )rj   _fpcloseru   r;   r   rv   s    r'   r;   zSpooledTemporaryFile.aclose3  s/     ||HHNNgns   9AAAc                   K   | j                   s'| j                  j                         | j                  k  ry | j	                          d {    y 7 wr   )rj   rx   tellrt   rolloverr1   s    r'   _checkzSpooledTemporaryFile._check:  s4     <<488==?dnn<mmos   AA
AAc                0   K    j                   ry d _          j                  }|j                  d       t        j                   fd       d {    _         j                  |j                                d {    |j                          y 7 A7 w)NTr   c                 B    t        j                  di  j                  S rc   )r0   r   rs   r1   s   r'   r2   z/SpooledTemporaryFile.rollover.<locals>.<lambda>H  s    H**CT-B-BCr*   )rj   rx   seekr   r4   writereadry   )r   buffers   ` r'   r}   zSpooledTemporaryFile.rollover@  st     <<A"++C
 
 jj'''	
 	(s$   ABB+B<B=BBc                .    | j                   j                  S r   )rx   closedr1   s    r'   r   zSpooledTemporaryFile.closedM  s    xxr*   c                   K   | j                   s-t                d {    | j                  j                  |      S t        |   |       d {   S 7 67 wr   )rj   r   rx   r   ru   r   sizerv   s     r'   r   zSpooledTemporaryFile.readQ  sJ     ||)+++88==&&W\$''' , (!   AA1AAAAc                   K   | j                   s-t                d {    | j                  j                  |      S t        |   |       d {   S 7 67 wr   )rj   r   rx   read1ru   r   s     r'   r   zSpooledTemporaryFile.read1X  sJ     ||)+++88>>$''W]4((( , )r   c                   K   | j                   s,t                d {    | j                  j                         S t        |           d {   S 7 47 wr   )rj   r   rx   readlineru   rz   s    r'   r   zSpooledTemporaryFile.readline_  sI     ||)+++88$$&&W%''' , (!   AA/AAAAc                   K   | j                   s,t                d {    | j                  j                         S t        |           d {   S 7 47 wr   )rj   r   rx   	readlinesru   rz   s    r'   r   zSpooledTemporaryFile.readlinesf  sI     ||)+++88%%''W&((( , )r   c                   K   | j                   s-t                d {    | j                  j                  |       t        |   |       d {   S 7 67 wr   )rj   r   rx   readintoru   r   rq   rv   s     r'   r   zSpooledTemporaryFile.readintom  sJ     ||)+++HHa W%a((( , )r   c                   K   | j                   s-t                d {    | j                  j                  |       t        |   |       d {   S 7 67 wr   )rj   r   rx   r   ru   	readinto1r   s     r'   r   zSpooledTemporaryFile.readinto1t  sJ     ||)+++HHa W&q))) , *r   c                   K   | j                   s.t                d {    | j                  j                  ||      S t        |   ||       d {   S 7 87 wr   )rj   r   rx   r   ru   )r   offsetwhencerv   s      r'   r   zSpooledTemporaryFile.seek{  sN     ||)+++88==00W\&&111 , 2s!   AA3AAAAc                   K   | j                   s,t                d {    | j                  j                         S t        |           d {   S 7 47 wr   )rj   r   rx   r|   ru   rz   s    r'   r|   zSpooledTemporaryFile.tell  sD     ||)+++88==?"W\^## , $r   c                   K   | j                   s-t                d {    | j                  j                  |      S t        |   |       d {   S 7 67 wr   )rj   r   rx   truncateru   r   s     r'   r   zSpooledTemporaryFile.truncate  sM     ||)+++88$$T**W%d+++ , ,r   c                   K   y wr   r   r   rq   s     r'   r   zSpooledTemporaryFile.write  s	     RU   c                   K   y wr   r   r   s     r'   r   zSpooledTemporaryFile.write  s	     EHr   c                   K   | j                   sGt                d{    | j                  j                  |      }| j	                          d{    |S t
        |   |       d{   S 7 P7 7 w)a  
        Asynchronously write data to the spooled temporary file.

        If the file has not yet been rolled over, the data is written synchronously,
        and a rollover is triggered if the size exceeds the maximum size.

        :param s: The data to write.
        :return: The number of bytes written.
        :raises RuntimeError: If the underlying file is not initialized.

        N)rj   r   rx   r   r~   ru   )r   rq   resultrv   s      r'   r   zSpooledTemporaryFile.write  sc      ||)+++XX^^A&F++-MW]1%%% , &3   A4A.2A4A0A4)A2*A40A42A4c                   K   y wr   r   r   liness     r'   
writelineszSpooledTemporaryFile.writelines        r   c                   K   y wr   r   r   s     r'   r   zSpooledTemporaryFile.writelines  r   r   c                   K   | j                   sGt                d{    | j                  j                  |      }| j	                          d{    |S t
        |   |       d{   S 7 P7 7 w)aw  
        Asynchronously write a list of lines to the spooled temporary file.

        If the file has not yet been rolled over, the lines are written synchronously,
        and a rollover is triggered if the size exceeds the maximum size.

        :param lines: An iterable of lines to write.
        :raises RuntimeError: If the underlying file is not initialized.

        N)rj   r   rx   r   r~   ru   )r   r   r   rv   s      r'   r   zSpooledTemporaryFile.writelines  sf      ||)+++XX((/F++-MW'... , /r   re   )r   SpooledTemporaryFile[bytes]rm   rD   r    r   r!   rD   r"   rE   r#   rE   r$   rE   r%   rE   r&   rE   r   rE   )r   SpooledTemporaryFile[str]rm   rD   r    r   r!   rD   r"   rE   r#   rE   r$   rE   r%   rE   r&   rE   r   rE   )r   rF   rG   NNNNN)rm   rD   r    rg   r!   rD   r"   rE   r#   rE   r$   rE   r%   rE   r&   rE   r   rE   rH   rI   rH   rI   )rH   rf   )rG   )r   rD   rH   r
   )r   r   r   rD   rH   bytes)rH   r
   )rH   zlist[AnyStr])r   r   rq   r   rH   rD   )r   rD   r   
int | NonerH   rD   )rH   rD   r   )r   r   rH   rD   )r   r   rq   r   rH   rD   )r   r   rq   strrH   rD   )rq   zReadableBuffer | strrH   rD   )r   r   r   zIterable[ReadableBuffer]rH   rI   )r   r   r   zIterable[str]rH   rI   )r   z(Iterable[str] | Iterable[ReadableBuffer]rH   rI   )rM   rN   rO   rP   rj   rQ   r   r(   r;   r~   r}   propertyr   r   r   r   r   r   r   osSEEK_SETr   r|   r   r   r   __classcell__)rv   s   @r'   ri   ri      s   $ GT ""!   !)  	
          "!   !'  	
         .3#"!!# "## ,# 	#
 # # # # # # 
#J  ()())* <>;; 2$, U UH H&( )2J	  '0=	 / /r*   ri   c                  f    e Zd ZdZ	 	 	 d
ddd	 	 	 	 	 	 	 	 	 	 	 ddZddZ	 	 	 	 	 	 	 	 ddZdd	Zy)TemporaryDirectorya  
    An asynchronous temporary directory that is created and cleaned up automatically.

    This class provides an asynchronous context manager for creating a temporary
    directory. It wraps Python's standard :class:`~tempfile.TemporaryDirectory` to
    perform directory creation and cleanup operations in a background thread.

    :param suffix: Suffix to be added to the temporary directory name.
    :param prefix: Prefix to be added to the temporary directory name.
    :param dir: The parent directory where the temporary directory is created.
    :param ignore_cleanup_errors: Whether to ignore errors during cleanup
        (Python 3.10+).
    :param delete: Whether to delete the directory upon closing (Python 3.12+).
    NFT)ignore_cleanup_errorsrW   c               X    || _         || _        || _        || _        || _        d | _        y r   )r$   r%   r&   r   rW   _tempdir)r   r$   r%   r&   r   rW   s         r'   r(   zTemporaryDirectory.__init__  s/     &,%+"%%:"<@r*   c                  K   | j                   | j                  | j                  dt        j                  dk\  r| j
                  d<   t        j                  dk\  r| j                  d<   t        j                  fd       d {   | _	        t        j                  | j                  j                         d {   S 7 :7 w)Nr$   r%   r&   )r\   
   r   r[   rW   c                 .    t        j                  di  S rc   )r0   r   )paramss   r'   r2   z/TemporaryDirectory.__aenter__.<locals>.<lambda>  s    H//9&9r*   )r$   r%   r&   r_   r`   r   rW   r   r4   r   	__enter__)r   r   s    @r'   r7   zTemporaryDirectory.__aenter__  s     kkkk88"

 w&.2.H.HF*+w&#{{F8'009
 
 ''(?(?@@@
 As$   BCC5C=C>CCc                   K   | j                   5t        j                  | j                   j                  |||       d {    y y 7 wr   )r   r   r4   __exit__r<   s       r'   r@   zTemporaryDirectory.__aexit__  sG      ==$$$&&)Y   %s   <AAAc                   K   | j                   2t        j                  | j                   j                         d {    y y 7 wr   )r   r   r4   cleanupr1   s    r'   r   zTemporaryDirectory.cleanup  s6     ==$$$T]]%:%:;;; %;s   9AAANNN)r$   AnyStr | Noner%   r   r&   r   r   rf   rW   rf   rH   rI   rH   r   rK   r   )rM   rN   rO   rP   r(   r7   r@   r   r   r*   r'   r   r     s    " !% $!	A ',AA A 	A  $A A 
A"A"	,	 (	 (		
 
	<r*   r   c                   K   y wr   r   r$   r%   r&   texts       r'   mkstempr     s      r   c                   K   y wr   r   r   s       r'   r   r     s      r   c                l   K   t        j                  t        j                  | |||       d{   S 7 w)a  
    Asynchronously create a temporary file and return an OS-level handle and the file
    name.

    This function wraps `tempfile.mkstemp` and executes it in a background thread.

    :param suffix: Suffix to be added to the file name.
    :param prefix: Prefix to be added to the file name.
    :param dir: Directory in which the temporary file is created.
    :param text: Whether the file is opened in text mode.
    :return: A tuple containing the file descriptor and the file name.

    N)r   r4   r0   r   r   s       r'   r   r     s-     & ##H$4$4ffc4PPPPs   +424c                   K   y wr   r   r   s      r'   mkdtempr   0  s     
 r   c                   K   y wr   r   r   s      r'   r   r   8  s     
 r   c                j   K   t        j                  t        j                  | ||       d{   S 7 w)a  
    Asynchronously create a temporary directory and return its path.

    This function wraps `tempfile.mkdtemp` and executes it in a background thread.

    :param suffix: Suffix to be added to the directory name.
    :param prefix: Prefix to be added to the directory name.
    :param dir: Parent directory where the temporary directory is created.
    :return: The path of the created temporary directory.

    N)r   r4   r0   r   r   s      r'   r   r   @  s+       ##H$4$4ffcJJJJs   *313c                 d   K   t        j                  t        j                         d{   S 7 w)z
    Asynchronously return the name of the directory used for temporary files.

    This function wraps `tempfile.gettempdir` and executes it in a background thread.

    :return: The path of the temporary directory as a string.

    N)r   r4   r0   
gettempdirr   r*   r'   r   r   S  s%      ##H$7$78888   '0.0c                 d   K   t        j                  t        j                         d{   S 7 w)z
    Asynchronously return the name of the directory used for temporary files in bytes.

    This function wraps `tempfile.gettempdirb` and executes it in a background thread.

    :return: The path of the temporary directory as bytes.

    N)r   r4   r0   gettempdirbr   r*   r'   r   r   _  s%      ##H$8$89999r   )NNNF)
r$   rE   r%   rE   r&   rE   r   rf   rH   ztuple[int, str])
r$   bytes | Noner%   r   r&   r   r   rf   rH   ztuple[int, bytes])
r$   r   r%   r   r&   r   r   rf   rH   ztuple[int, str | bytes]r   )r$   rE   r%   rE   r&   rE   rH   r   )r$   r   r%   r   r&   r   rH   r   )r$   r   r%   r   r&   r   rH   zstr | bytesr   )rH   r   )%
__future__r   r   r_   r0   collections.abcr   ior   r   typesr   typingr   r	   r
   r   r    r   _core._fileior   lowlevelr   	_typeshedr   r   r   r   r   rS   ri   r   r   r   r   r   r   r*   r'   <module>r      s+   " 	 
  $ %    % .WW\(GFO \(~a( a(Hg/9V, g/T?< ?<D 
	 
 	
  
 
	 
 	
  
 ! 	QQQ 
Q 	Q
 Q, 
 
 		 
 
 
 	 
 ! KKK 
K 	K&	9	:r*   