o
    Df]                     @  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 ddlmZ d	d
gZedeedf dZeddd	ZdZded< edZG dd dZefddd
ZdS )z&
Implementation for async generators.
    )annotations)get_running_loop)asynccontextmanager)EmptyFullQueue)AnyAsyncGeneratorCallableIterableTypeVar   )run_in_executor_with_contextaclosinggenerator_to_async_generator_T_GeneratorN)boundthingreturn"AsyncGenerator[_T_Generator, None]c                 C s.   z| V  W |   I dH  dS |   I dH  w )z1Similar to `contextlib.aclosing`, in Python 3.10.N)aclose)r    r   a/home/ubuntu/webapp/venv/lib/python3.10/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   1   s    r   get_iterableCallable[[], Iterable[_T]]buffer_sizeAsyncGenerator[_T, None]c                  s   dt |dt }d	 fdd}t|}z/	 z }W n ty3   |djI dH }Y nw t|tr:n|V  qW d|I dH  dS d|I dH  w )
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)maxsizer   Nonec                    s,  zv  D ]V} r% W 	 zj t dd W dS  ty$   r"Y dS Y q
w 	 z	j | dd W n) tyX   rVY  W 	 zj t dd W dS  tyU   rSY dS Y q;w Y q%w 	 qW 	 zj t dd W dS  tyv   rtY dS Y q\w 	 zj t dd W 	 w  ty   rY    Y dS Y qww )z~
        Consume the generator in background thread.
        When items are received, they'll be pushed to the queue.
        Tr   )timeoutN)putr   r   )itemr    qquittingr   r   runnerH   sf   
z,generator_to_async_generator.<locals>.runnerTN)r   r%   )	r   r   r   
get_nowaitr   run_in_executorget
isinstancer   )r    r"   loopr,   runner_fr(   r   r)   r   r   5   s,   
"
)r   r   r   r   )r    r!   r"   r   r   r#   )__doc__
__future__r   asyncior   
contextlibr   queuer   r   r   typingr   r	   r
   r   r   utilsr   __all__r   r   r   __annotations__r   r   r   r   r   r   r   <module>   s$    