2018-09-28 15:51:36 +00:00
|
|
|
import time, uuid
|
|
|
|
|
|
|
|
class Timer(object):
|
2018-10-13 00:13:14 +00:00
|
|
|
def __init__(self, id, context, name, delay, next_due, kwargs):
|
2018-09-28 15:51:36 +00:00
|
|
|
self.id = id
|
2018-10-13 00:13:14 +00:00
|
|
|
self.context = context
|
2018-09-28 15:51:36 +00:00
|
|
|
self.name = name
|
|
|
|
self.delay = delay
|
|
|
|
if next_due:
|
|
|
|
self.next_due = next_due
|
|
|
|
else:
|
|
|
|
self.set_next_due()
|
|
|
|
self.kwargs = kwargs
|
|
|
|
self._done = False
|
|
|
|
|
|
|
|
def set_next_due(self):
|
|
|
|
self.next_due = time.time()+self.delay
|
|
|
|
def due(self):
|
|
|
|
return self.time_left() <= 0
|
|
|
|
def time_left(self):
|
|
|
|
return self.next_due-time.time()
|
|
|
|
|
|
|
|
def redo(self):
|
|
|
|
self._done = False
|
|
|
|
self.set_next_due()
|
2018-09-28 16:00:49 +00:00
|
|
|
def finish(self):
|
2018-09-28 15:51:36 +00:00
|
|
|
self._done = True
|
|
|
|
def done(self):
|
|
|
|
return self._done
|
|
|
|
|
2018-10-12 16:54:15 +00:00
|
|
|
class TimersContext(object):
|
|
|
|
def __init__(self, parent, context):
|
|
|
|
self._parent = parent
|
|
|
|
self.context = context
|
|
|
|
def add(self, name, delay, next_due=None, **kwargs):
|
|
|
|
self._parent._add(self.context, name, delay, next_due, None, False,
|
|
|
|
kwargs)
|
|
|
|
def add_persistent(self, name, delay, next_due=None, **kwargs):
|
|
|
|
self._parent._add(self.context, name, delay, next_due, None, True,
|
|
|
|
kwargs)
|
|
|
|
|
2018-09-28 15:51:36 +00:00
|
|
|
class Timers(object):
|
2018-09-28 16:01:20 +00:00
|
|
|
def __init__(self, database, events, log):
|
|
|
|
self.database = database
|
2018-09-28 15:51:36 +00:00
|
|
|
self.events = events
|
|
|
|
self.log = log
|
|
|
|
self.timers = []
|
2018-10-12 16:54:15 +00:00
|
|
|
self.context_timers = {}
|
|
|
|
|
|
|
|
def new_context(self, context):
|
|
|
|
return TimersContext(self, context)
|
2018-09-28 15:51:36 +00:00
|
|
|
|
|
|
|
def setup(self, timers):
|
|
|
|
for name, timer in timers:
|
|
|
|
id = name.split("timer-", 1)[1]
|
|
|
|
self._add(timer["name"], timer["delay"], timer[
|
|
|
|
"next-due"], id, False, timer["kwargs"])
|
|
|
|
|
|
|
|
def _persist(self, timer):
|
2018-09-28 16:01:20 +00:00
|
|
|
self.database.bot_settings.set("timer-%s" % timer.id, {
|
2018-09-28 15:51:36 +00:00
|
|
|
"name": timer.name, "delay": timer.delay,
|
|
|
|
"next-due": timer.next_due, "kwargs": timer.kwargs})
|
|
|
|
def _remove(self, timer):
|
2018-10-13 08:09:24 +00:00
|
|
|
if timer.context and timer.context in self.context_timers:
|
2018-10-13 00:13:14 +00:00
|
|
|
self.context_timers[timer.context].remove(timer)
|
|
|
|
if not self.context_timers[timer.context]:
|
|
|
|
del self.context_timers[timer.conteext]
|
|
|
|
else:
|
|
|
|
self.timers.remove(timer)
|
2018-09-28 16:01:20 +00:00
|
|
|
self.database.bot_settings.delete("timer-%s" % timer.id)
|
2018-09-28 15:51:36 +00:00
|
|
|
|
|
|
|
def add(self, name, delay, next_due=None, **kwargs):
|
2018-10-12 16:54:15 +00:00
|
|
|
self._add(None, name, delay, next_due, None, False, kwargs)
|
2018-09-28 15:51:36 +00:00
|
|
|
def add_persistent(self, name, delay, next_due=None, **kwargs):
|
2018-10-12 16:54:15 +00:00
|
|
|
self._add(None, name, delay, next_due, None, True, kwargs)
|
|
|
|
def _add(self, context, name, delay, next_due, id, persist, kwargs):
|
2018-09-28 15:51:36 +00:00
|
|
|
id = id or uuid.uuid4().hex
|
2018-10-13 00:13:14 +00:00
|
|
|
timer = Timer(id, context, name, delay, next_due, kwargs)
|
2018-09-28 15:51:36 +00:00
|
|
|
if persist:
|
|
|
|
self._persist(timer)
|
2018-10-12 16:54:15 +00:00
|
|
|
|
|
|
|
if context and not persist:
|
|
|
|
if not context in self.context_timers:
|
|
|
|
self.context_timers[context] = []
|
|
|
|
self.context_timers[context].append(timer)
|
|
|
|
else:
|
|
|
|
self.timers.append(timer)
|
2018-09-28 15:51:36 +00:00
|
|
|
|
|
|
|
def next(self):
|
2018-10-12 17:15:22 +00:00
|
|
|
times = filter(None, [timer.time_left() for timer in self.get_timers()])
|
2018-09-28 15:51:36 +00:00
|
|
|
if not times:
|
|
|
|
return None
|
|
|
|
return max(min(times), 0)
|
|
|
|
|
2018-10-12 16:54:15 +00:00
|
|
|
def get_timers(self):
|
|
|
|
return self.timers + sum(self.context_timers.values(), [])
|
|
|
|
|
2018-09-28 15:51:36 +00:00
|
|
|
def call(self):
|
2018-10-12 16:54:15 +00:00
|
|
|
for timer in self.get_timers():
|
2018-09-28 15:51:36 +00:00
|
|
|
if timer.due():
|
|
|
|
timer.finish()
|
2018-09-28 16:01:41 +00:00
|
|
|
self.events.on("timer.%s" % timer.name).call(timer=timer,
|
|
|
|
**timer.kwargs)
|
2018-09-28 15:51:36 +00:00
|
|
|
if timer.done():
|
|
|
|
self._remove(timer)
|
2018-10-12 16:54:15 +00:00
|
|
|
|
|
|
|
def purge_context(self, context):
|
|
|
|
if context in self.context_timers:
|
|
|
|
del self.context_timers[context]
|