U
    ¯ªh"  ã                   @   s:   d dl Z d dlZd dlZddlmZ G dd„ de jƒZdS )é    Né   )Úhelpersc                   @   sf   e Zd ZdZdddœdd„Zdd„ Zd	d
„ Zdd„ Zdd„ Zdd„ Z	dd„ Z
ejdd„ ƒZdd„ ZdS )ÚRequestIterah  
    Helper class to deal with requests that need offsets to iterate.

    It has some facilities, such as automatically sleeping a desired
    amount of time between requests if needed (but not more).

    Can be used synchronously if the event loop is not running and
    as an asynchronous iterator otherwise.

    `limit` is the total amount of items that the iterator should return.
    This is handled on this base class, and will be always ``>= 0``.

    `left` will be reset every time the iterator is used and will indicate
    the amount of items that should be emitted left, so that subclasses can
    be more efficient and fetch only as many items as they need.

    Iterators may be used with ``reversed``, and their `reverse` flag will
    be set to `True` if that's the case. Note that if this flag is set,
    `buffer` should be filled in reverse too.
    FN)ÚreverseÚ	wait_timec                K   sX   || _ || _|| _|| _t|d kr*tdƒn|dƒ| _| j| _d | _d| _	d | _
d| _d S )NÚinfr   )Úclientr   r   ÚkwargsÚmaxÚfloatÚlimitÚleftÚbufferÚindexÚtotalÚ	last_load)Úselfr   r   r   r   r	   © r   ú8/tmp/pip-unpacked-wheel-c81u5j2r/telethon/requestiter.pyÚ__init__   s    zRequestIter.__init__c                 Ë   s   dS )aA  
        Called when asynchronous initialization is necessary. All keyword
        arguments passed to `__init__` will be forwarded here, and it's
        preferable to use named arguments in the subclasses without defaults
        to avoid forgetting or misspelling any of them.

        This method may ``raise StopAsyncIteration`` if it cannot continue.

        This method may actually fill the initial buffer if it needs to,
        and similarly to `_load_next_chunk`, ``return True`` to indicate
        that this is the last iteration (just the initial load).
        Nr   )r   r	   r   r   r   Ú_init)   s    zRequestIter._initc                 Ã   sÚ   | j d kr0g | _ | jf | jŽI d H r0t| j ƒ| _| jdkr>t‚| jt| j ƒkr¤| jr~t 	| jt
 
¡ | j  ¡I d H  t
 
¡ | _d| _g | _ |  ¡ I d H r¤t| j ƒ| _| j s®t‚| j | j }|  jd8  _|  jd7  _|S )Nr   r   )r   r   r	   Úlenr   ÚStopAsyncIterationr   r   ÚasyncioÚsleepÚtimer   Ú_load_next_chunk)r   Úresultr   r   r   Ú	__anext__7   s,    

ÿ

zRequestIter.__anext__c                 C   s2   z| j j |  ¡ ¡W S  tk
r,   t‚Y nX d S ©N)r   ÚloopZrun_until_completer   r   ÚStopIteration©r   r   r   r   Ú__next__U   s    zRequestIter.__next__c                 C   s   d | _ d| _d| _| j| _| S )Nr   )r   r   r   r   r   r"   r   r   r   Ú	__aiter__[   s
    zRequestIter.__aiter__c                 C   s   | j j ¡ rtdƒ‚|  ¡ S )NzZYou must use "async for" if the event loop is running (i.e. you are inside an "async def"))r   r    Z
is_runningÚRuntimeErrorr$   r"   r   r   r   Ú__iter__b   s
    ÿzRequestIter.__iter__c                 Ã   s2   t  ¡ }| 2 z3 dH W }| |¡ q6 | j|_|S )z
        Create a `self` iterator and collect it into a `TotalList`
        (a normal list with a `.total` attribute).
        N)r   Z	TotalListÚappendr   )r   r   Úmessager   r   r   Úcollectk   s
    zRequestIter.collectc                 Ã   s   t ‚dS )a	  
        Called when the next chunk is necessary.

        It should extend the `buffer` with new items.

        It should return `True` if it's the last chunk,
        after which moment the method won't be called again
        during the same iteration.
        N)ÚNotImplementedErrorr"   r   r   r   r   w   s    zRequestIter._load_next_chunkc                 C   s   | j  | _ | S r   )r   r"   r   r   r   Ú__reversed__„   s    
zRequestIter.__reversed__)Ú__name__Ú
__module__Ú__qualname__Ú__doc__r   r   r   r#   r$   r&   r)   ÚabcÚabstractmethodr   r+   r   r   r   r   r      s   	
r   )r0   r   r   Ú r   ÚABCr   r   r   r   r   Ú<module>   s   