summaryrefslogtreecommitdiff
path: root/src/timed_events.py
diff options
context:
space:
mode:
authorFlorent Le Coz <louiz@louiz.org>2011-04-09 22:18:36 +0200
committerFlorent Le Coz <louiz@louiz.org>2011-04-09 22:18:36 +0200
commit35b6e146cb6a0b313d6297f0d91654aa21f58c1b (patch)
treefde1876ece761df454766bbf67e4412d7b8b4e36 /src/timed_events.py
parent27a20b349cf1975cd75b3d9bc61155ed58ce01d0 (diff)
downloadpoezio-35b6e146cb6a0b313d6297f0d91654aa21f58c1b.tar.gz
poezio-35b6e146cb6a0b313d6297f0d91654aa21f58c1b.tar.bz2
poezio-35b6e146cb6a0b313d6297f0d91654aa21f58c1b.tar.xz
poezio-35b6e146cb6a0b313d6297f0d91654aa21f58c1b.zip
Basic timed event implementation.
Diffstat (limited to 'src/timed_events.py')
-rw-r--r--src/timed_events.py44
1 files changed, 42 insertions, 2 deletions
diff --git a/src/timed_events.py b/src/timed_events.py
index 6e4da396..7cb92cde 100644
--- a/src/timed_events.py
+++ b/src/timed_events.py
@@ -14,15 +14,55 @@
# You should have received a copy of the GNU General Public License
# along with Poezio. If not, see <http://www.gnu.org/licenses/>.
+"""
+To use these, just use core.add_timed_event(event)
+where event is an instance of one of these classes
+"""
+
+import logging
+
+log = logging.getLogger(__name__)
+
+import datetime
class TimedEvent(object):
"""
An event with a callback that is called when the specified time is passed
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
- a maximum
+ a maximum.
"""
- def __init__(self, callback, *args, **kwargs):
+ def __init__(self, date, callback, *args):
self._callback = callback
self.args = args
+ 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):
+ """
+ returns True if the callback should be called
+ """
+ if self.next_call_date < current_date:
+ return True
+ else:
+ return False
+
+class DelayedEvent(TimedEvent):
+ """
+ The date is calculated from now + a delay in seconds
+ Use it if you want an event to happen in, e.g. 6 seconds
+ """
+ def __init__(self, delay, callback, *args):
+ date = datetime.datetime.now() + datetime.timedelta(0, delay)
+ TimedEvent.__init__(self, date, callback, args)