a
    xdc                     @  s   U d Z ddlmZ ddlmZ ddlmZ ddlmZm	Z	m
Z
 ddlmZmZmZmZmZmZ ddlmZ d	d
gZedeedf dZeddddd	ZdZded< edZG dd dZefdddddd
ZdS )z&
Implementation for async generators.
    )annotations)get_running_loop)asynccontextmanager)EmptyFullQueue)AnyAsyncGeneratorCallableIterableTypeVarUnion   )run_in_executor_with_contextaclosinggenerator_to_async_generator_T_GeneratorN)boundz"AsyncGenerator[_T_Generator, None])thingreturnc                 C s.   z| V  W |   I dH  n|   I dH  0 dS )z1Similar to `contextlib.aclosing`, in Python 3.10.N)aclose)r    r   `/var/www/html/Ranjet/env/lib/python3.9/site-packages/prompt_toolkit/eventloop/async_generator.pyr      s    i  intDEFAULT_BUFFER_SIZE_Tc                   @  s   e Zd ZdS )_DoneN)__name__
__module____qualname__r   r   r   r   r   0   s   r   zCallable[[], Iterable[_T]]zAsyncGenerator[_T, None])get_iterablebuffer_sizer   c                  s   dt |dt }dd fdd}t|}z\z }W n& tyf   |djI dH }Y n0 t|trvq~q4|V  q4W d|I dH  nd|I dH  0 dS )	a]  
    Turn a generator or iterable into an async generator.

    This works by running the generator in a background thread.

    :param get_iterable: Function that returns a generator or iterable when
        called.
    :param buffer_size: Size of the queue between the async consumer and the
        synchronous generator that produces items.
    F)maxsizeNone)r   c                    s`  z
  D ]} rT W zj t dd W qP tyJ   rBY dS Y qY q0 qPqdS zj | dd W q ty   rY  W zj t dd W q ty   rY dS Y q~Y q~0 qq~dS Y qTY qT0 q
qTq
W zj t dd W n" ty   rY dS Y qY q0 q\qnNzj t dd W n. tyP   rFY    Y dS Y qY n0 qZq0 dS )z~
        Consume the generator in background thread.
        When items are received, they'll be pushed to the queue.
        r   )timeoutN)putr   r   )itemr    qZquittingr   r   runnerG   sR    




z,generator_to_async_generator.<locals>.runnerNT)	r   r   r   
get_nowaitr   Zrun_in_executorget
isinstancer   )r    r!   Zloopr)   Zrunner_fr&   r   r'   r   r   4   s"    
"

)__doc__
__future__r   Zasyncior   
contextlibr   queuer   r   r   typingr   r	   r
   r   r   r   utilsr   __all__r   r   r   __annotations__r   r   r   r   r   r   r   <module>   s"    