U
    .ep                     @  s   d Z ddlmZ ddlmZ ddlmZ dZddd	d
ZG dd dZ	G dd dZ
G dd de
ZG dd deZdd ZdS )zScheduling Utilities.    )annotations)count   )symbol_by_name)	FairCyclepriority_cycleround_robin_cyclesorted_cyclez%kombu.utils.scheduling:priority_cyclez(kombu.utils.scheduling:round_robin_cyclez#kombu.utils.scheduling:sorted_cycle)priorityZround_robinsortedc                   @  s<   e Zd ZdZefddZdd Zdd Zdd	 Zd
d Z	dS )r   a2  Cycle between resources.

    Consume from a set of resources, where each resource gets
    an equal chance to be consumed from.

    Arguments:
    ---------
        fun (Callable): Callback to call.
        resources (Sequence[Any]): List of resources.
        predicate (type): Exception predicate.
    c                 C  s   || _ || _|| _d| _d S )Nr   )fun	resources	predicatepos)selfr   r   r    r   :/tmp/pip-unpacked-wheel-48hrr5dg/kombu/utils/scheduling.py__init__!   s    zFairCycle.__init__c                 C  sP   z | j | j }|  jd7  _|W S  tk
rH   d| _| j sD|  Y q X q d S )Nr   r   )r   r   
IndexErrorr   )r   resourcer   r   r   _next'   s    zFairCycle._nextc              	   K  s\   t dD ]N}|  }z| j||f|W   S  | jk
rT   |t| jd krP Y qX qdS )zGet from next resource.r   r   N)r   r   r   r   lenr   )r   callbackkwargsZtriedr   r   r   r   get2   s    zFairCycle.getc                 C  s   dS )zClose cycle.Nr   r   r   r   r   close=   s    zFairCycle.closec                 C  s   dj | t| jdS )z``repr(cycle)``.z/<FairCycle: {self.pos}/{size} {self.resources}>)r   size)formatr   r   r   r   r   r   __repr__@   s     zFairCycle.__repr__N)
__name__
__module____qualname____doc__	Exceptionr   r   r   r   r   r   r   r   r   r      s   r   c                   @  s2   e Zd ZdZdddZdd Zdd Zd	d
 ZdS )r   z2Iterator that cycles between items in round-robin.Nc                 C  s   |d k	r|ng | _ d S )Nitemsr   itr   r   r   r   I   s    zround_robin_cycle.__init__c                 C  s   || j dd< dS )zUpdate items from iterable.Nr%   r'   r   r   r   updateL   s    zround_robin_cycle.updatec                 C  s   | j d| S zConsume n items.Nr%   r   nr   r   r   consumeP   s    zround_robin_cycle.consumec                 C  s:   | j }z|||| W n tk
r4   Y nX |S )z,Move most recently used item to end of list.)r&   appendpopindex
ValueError)r   	last_usedr&   r   r   r   rotateT   s    zround_robin_cycle.rotate)N)r    r!   r"   r#   r   r)   r-   r3   r   r   r   r   r   F   s
   
r   c                   @  s   e Zd ZdZdd ZdS )r   z"Cycle that repeats items in order.c                 C  s   dS )zUnused in this implementation.Nr   )r   r2   r   r   r   r3   a   s    zpriority_cycle.rotateN)r    r!   r"   r#   r3   r   r   r   r   r   ^   s   r   c                   @  s   e Zd ZdZdd ZdS )r	   zCycle in sorted order.c                 C  s   t | jd| S r*   )r   r&   r+   r   r   r   r-   h   s    zsorted_cycle.consumeN)r    r!   r"   r#   r-   r   r   r   r   r	   e   s   r	   c                 C  s
   t | tS )zGet cycle class by name.)r   CYCLE_ALIASES)namer   r   r   cycle_by_namem   s    r6   N)r#   
__future__r   	itertoolsr   Zimportsr   __all__r4   r   r   r   r	   r6   r   r   r   r   <module>   s   2