2011-09-06 00:45:53 +00:00
|
|
|
|
# Copyright 2010-2011 Florent Le Coz <louiz@louiz.org>
|
2011-04-08 14:36:00 +00:00
|
|
|
|
#
|
|
|
|
|
# This file is part of Poezio.
|
|
|
|
|
#
|
|
|
|
|
# Poezio is free software: you can redistribute it and/or modify
|
2011-09-11 15:10:05 +00:00
|
|
|
|
# it under the terms of the zlib license. See the COPYING file.
|
2011-04-08 14:36:00 +00:00
|
|
|
|
|
2011-04-09 20:18:36 +00:00
|
|
|
|
"""
|
2013-03-08 18:39:34 +00:00
|
|
|
|
Timed events are the standard way to schedule events for later in poezio.
|
|
|
|
|
|
|
|
|
|
Once created, they must be added to the list of checked events with
|
|
|
|
|
:py:func:`Core.add_timed_event` (within poezio) or with
|
|
|
|
|
:py:func:`.PluginAPI.add_timed_event` (within a plugin).
|
2011-04-09 20:18:36 +00:00
|
|
|
|
"""
|
|
|
|
|
|
|
|
|
|
import logging
|
|
|
|
|
|
|
|
|
|
log = logging.getLogger(__name__)
|
|
|
|
|
|
|
|
|
|
import datetime
|
2011-04-08 14:36:00 +00:00
|
|
|
|
|
|
|
|
|
class TimedEvent(object):
|
|
|
|
|
"""
|
2013-03-08 18:39:34 +00:00
|
|
|
|
An event with a callback that is called when the specified time is passed.
|
|
|
|
|
|
2011-04-08 14:36:00 +00:00
|
|
|
|
Note that these events can NOT be used for very small delay or a very
|
|
|
|
|
precise date, since the check for events is done once per second, as
|
2011-04-09 20:18:36 +00:00
|
|
|
|
a maximum.
|
2013-03-08 18:39:34 +00:00
|
|
|
|
|
2011-04-10 01:52:46 +00:00
|
|
|
|
The callback and its arguments should be passed as the lasts arguments.
|
2011-04-08 14:36:00 +00:00
|
|
|
|
"""
|
2011-04-09 20:18:36 +00:00
|
|
|
|
def __init__(self, date, callback, *args):
|
2013-03-08 18:39:34 +00:00
|
|
|
|
"""
|
|
|
|
|
Create a new timed event.
|
|
|
|
|
|
|
|
|
|
:param datetime.datetime date: Time at which the callback must be run.
|
|
|
|
|
:param function callback: The handler that will be executed.
|
|
|
|
|
:param \*args: Optional arguments passed to the handler.
|
|
|
|
|
"""
|
2011-04-08 14:36:00 +00:00
|
|
|
|
self._callback = callback
|
|
|
|
|
self.args = args
|
2011-04-09 20:18:36 +00:00
|
|
|
|
self.repetive = False
|
|
|
|
|
self.next_call_date = date
|
|
|
|
|
|
|
|
|
|
def __call__(self):
|
|
|
|
|
"""
|
|
|
|
|
the call should return False if this event should be remove from
|
|
|
|
|
the events list.
|
|
|
|
|
If it’s true, the date should be updated beforehand to a later date,
|
|
|
|
|
or else it will be called every second
|
|
|
|
|
"""
|
|
|
|
|
self._callback(*self.args)
|
|
|
|
|
return self.repetive
|
|
|
|
|
|
|
|
|
|
def has_timed_out(self, current_date):
|
|
|
|
|
"""
|
2013-03-08 18:39:34 +00:00
|
|
|
|
Check if the event has timed out.
|
|
|
|
|
|
|
|
|
|
:param datetime.datetime current_date: The current date.
|
|
|
|
|
:returns: True if the callback should be called
|
|
|
|
|
:rtype: bool
|
2011-04-09 20:18:36 +00:00
|
|
|
|
"""
|
|
|
|
|
if self.next_call_date < current_date:
|
|
|
|
|
return True
|
|
|
|
|
else:
|
|
|
|
|
return False
|
|
|
|
|
|
2011-04-10 01:52:46 +00:00
|
|
|
|
def change_date(self, date):
|
|
|
|
|
"""
|
2013-03-08 18:39:34 +00:00
|
|
|
|
Simply change the date of the event.
|
|
|
|
|
|
|
|
|
|
:param datetime.datetime date: Next date.
|
2011-04-10 01:52:46 +00:00
|
|
|
|
"""
|
|
|
|
|
self.next_call_date = date
|
|
|
|
|
|
|
|
|
|
def add_delay(self, delay):
|
|
|
|
|
"""
|
2013-03-08 18:39:34 +00:00
|
|
|
|
Add a delay (in seconds) to the date.
|
|
|
|
|
|
|
|
|
|
:param int delay: The delay to add.
|
2011-04-10 01:52:46 +00:00
|
|
|
|
"""
|
|
|
|
|
self.next_call_date += datetime.timedelta(seconds=delay)
|
|
|
|
|
|
2011-04-09 20:18:36 +00:00
|
|
|
|
class DelayedEvent(TimedEvent):
|
|
|
|
|
"""
|
2013-03-08 18:39:34 +00:00
|
|
|
|
A TimedEvent, but with the date calculated from now + a delay in seconds.
|
|
|
|
|
Use it if you want an event to happen in, e.g. 6 seconds.
|
2011-04-09 20:18:36 +00:00
|
|
|
|
"""
|
|
|
|
|
def __init__(self, delay, callback, *args):
|
2013-03-08 18:39:34 +00:00
|
|
|
|
"""
|
|
|
|
|
Create a new DelayedEvent.
|
|
|
|
|
|
|
|
|
|
:param int delay: The number of seconds.
|
|
|
|
|
:param function callback: The handler that will be executed.
|
|
|
|
|
:param \*args: Optional arguments passed to the handler.
|
|
|
|
|
"""
|
2011-04-10 01:52:46 +00:00
|
|
|
|
date = datetime.datetime.now() + datetime.timedelta(seconds=delay)
|
2011-04-10 02:08:33 +00:00
|
|
|
|
TimedEvent.__init__(self, date, callback, *args)
|
2011-04-08 14:36:00 +00:00
|
|
|
|
|