o
    ¯bE  ã                   @   sx   U d Z ddlmZ ddlmZ ddlmZ ddlmZ ddl	m
Z
 ddlmZ g Zee ed< eeƒG d	d
„ d
ƒƒZdS )z%
Helpers for working with producers.
é    )ÚList)Úimplementer)ÚIPushProducer)Ú	cooperate)Úlog)Úsafe_strÚ__all__c                   @   sL   e Zd ZdZdZdd„ Zdd„ Zdd„ Zd	d
„ Zdd„ Z	dd„ Z
dd„ ZdS )Ú_PullToPushaÙ  
    An adapter that converts a non-streaming to a streaming producer.

    Because of limitations of the producer API, this adapter requires the
    cooperation of the consumer. When the consumer's C{registerProducer} is
    called with a non-streaming producer, it must wrap it with L{_PullToPush}
    and then call C{startStreaming} on the resulting object. When the
    consumer's C{unregisterProducer} is called, it must call
    C{stopStreaming} on the L{_PullToPush} instance.

    If the underlying producer throws an exception from C{resumeProducing},
    the producer will be unregistered from the consumer.

    @ivar _producer: the underling non-streaming producer.

    @ivar _consumer: the consumer with which the underlying producer was
                     registered.

    @ivar _finished: C{bool} indicating whether the producer has finished.

    @ivar _coopTask: the result of calling L{cooperate}, the task driving the
                     streaming producer.
    Fc                 C   s   || _ || _d S )N)Ú	_producerÚ	_consumer)ÚselfÚpullProducerÚconsumer© r   úD/usr/lib/python3/dist-packages/twisted/internet/_producer_helpers.pyÚ__init__2   s   
z_PullToPush.__init__c                 c   sŠ   � 	 z| j  ¡  W n7 ty@   t ddt| j ƒf ¡ z| j ¡  W n ty=   t ddt| jƒf ¡ d| _Y Y dS w Y nw dV  q)zì
        A generator that calls C{resumeProducing} on the underlying producer
        forever.

        If C{resumeProducing} throws an exception, the producer is
        unregistered, which should result in streaming stopping.
        TNz%%s failed, producing will be stopped:z!%s failed to unregister producer:)	r
   ÚresumeProducingÚBaseExceptionr   Úerrr   r   ÚunregisterProducerÚ	_finished©r   r   r   r   Ú_pull6   s6   €
ÿþ
ÿþ÷ýùéz_PullToPush._pullc                 C   s   t |  ¡ ƒ| _dS )z‡
        This should be called by the consumer when the producer is registered.

        Start streaming data to the consumer.
        N)r   r   Ú	_coopTaskr   r   r   r   ÚstartStreamingW   s   z_PullToPush.startStreamingc                 C   s   | j rdS d| _ | j ¡  dS )z�
        This should be called by the consumer when the producer is
        unregistered.

        Stop streaming data to the consumer.
        NT)r   r   Ústopr   r   r   r   ÚstopStreaming_   s   z_PullToPush.stopStreamingc                 C   ó   | j  ¡  dS )z7
        @see: C{IPushProducer.pauseProducing}
        N)r   Úpauser   r   r   r   ÚpauseProducingk   ó   z_PullToPush.pauseProducingc                 C   r   )z8
        @see: C{IPushProducer.resumeProducing}
        N)r   Úresumer   r   r   r   r   q   r    z_PullToPush.resumeProducingc                 C   s   |   ¡  | j ¡  dS )z6
        @see: C{IPushProducer.stopProducing}
        N)r   r
   ÚstopProducingr   r   r   r   r"   w   s   z_PullToPush.stopProducingN)Ú__name__Ú
__module__Ú__qualname__Ú__doc__r   r   r   r   r   r   r   r"   r   r   r   r   r	      s    !r	   N)r&   Útypingr   Úzope.interfacer   Útwisted.internet.interfacesr   Útwisted.internet.taskr   Útwisted.pythonr   Útwisted.python.reflectr   r   ÚstrÚ__annotations__r	   r   r   r   r   Ú<module>   s   