# Path: bl/debug.py
# def debug(message):
#     if DEBUG:
#         log.msg(message)
# 
# DEBUG = _Debug()
# 
# Path: bl/utils.py
# def getClock(clock=None):
#     if clock is None:
#         from bl.scheduler import BeatClock
#         return BeatClock.defaultClock
#     return clock
# 
# def exhaustCall(v):
#     """
#     Get an uncallable value at the end of a call chain or `v` itself
#     if `v` is not callable.
#     """
#     while callable(v):
#         v = v()
#     return v


import random
from itertools import cycle
from pprint import pformat
from zope.interface import Interface, Attribute, implements
from twisted.python import log
from bl.debug import debug, DEBUG
from bl.utils import getClock, exhaustCall
class IndexedArp(BaseArp):
    index = 0
    count = 0
    direction = 1

    def sort(self, values):
        raise NotImplementedError

    def reset(self, values):
        values = self.sort(values)
        self.count = len(values)
        if self.values:
            factor = len(values) / float(len(self.values))
            if factor != 1:
                self.index = int(self.index * factor)
                self.index = self.index % self.count
                if self.index == self.count:
                    self.index -= 1
            elif self.index >= self.count:
                self.index = self.index % self.count
        self.values = values

    def __call__(self):
        if not self.values:
            return
        if self.index >= len(self.values):
            self.reset(self.values)
        v = self.values[self.index]
        self.index += self.direction
        self.index = self.index % self.count

# Path: bl/debug.py
# def debug(message):
#     if DEBUG:
#         log.msg(message)
# 
# DEBUG = _Debug()
# 
# Path: bl/utils.py
# def getClock(clock=None):
#     if clock is None:
#         from bl.scheduler import BeatClock
#         return BeatClock.defaultClock
#     return clock
# 
# def exhaustCall(v):
#     """
#     Get an uncallable value at the end of a call chain or `v` itself
#     if `v` is not callable.
#     """
#     while callable(v):
#         v = v()
#     return v


# Path: bl/arp.py
import random
from itertools import cycle
from pprint import pformat
from zope.interface import Interface, Attribute, implements
from twisted.python import log
from bl.debug import debug, DEBUG
from bl.utils import getClock, exhaustCall

class IndexedArp(BaseArp):
    index = 0
    count = 0
    direction = 1

    def sort(self, values):
        raise NotImplementedError

    def reset(self, values):
        values = self.sort(values)
        self.count = len(values)
        if self.values:
            factor = len(values) / float(len(self.values))
            if factor != 1:
                self.index = int(self.index * factor)
                self.index = self.index % self.count
                if self.index == self.count:
                    self.index -= 1
            elif self.index >= self.count:
                self.index = self.index % self.count
        self.values = values

    def __call__(self):
        if not self.values:
            return
        if self.index >= len(self.values):
            self.reset(self.values)
        v = self.values[self.index]
        self.index += self.direction
        self.index = self.index % self.count

        return exhaustCall(v)