diff options
author | Emmanuel Gil Peyrot <linkmauve@linkmauve.fr> | 2016-03-31 18:54:41 +0100 |
---|---|---|
committer | Emmanuel Gil Peyrot <linkmauve@linkmauve.fr> | 2016-06-11 20:49:43 +0100 |
commit | 332a5c2553db41de777473a1e1be9cd1522c9496 (patch) | |
tree | 3ee06a59f147ccc4009b35cccfbe2461bcd18310 /poezio/timed_events.py | |
parent | cf44cf7cdec9fdb35caa372563d57e7045dc29dd (diff) | |
download | poezio-332a5c2553db41de777473a1e1be9cd1522c9496.tar.gz poezio-332a5c2553db41de777473a1e1be9cd1522c9496.tar.bz2 poezio-332a5c2553db41de777473a1e1be9cd1522c9496.tar.xz poezio-332a5c2553db41de777473a1e1be9cd1522c9496.zip |
Move the src directory to poezio, for better cython compatibility.
Diffstat (limited to 'poezio/timed_events.py')
-rw-r--r-- | poezio/timed_events.py | 58 |
1 files changed, 58 insertions, 0 deletions
diff --git a/poezio/timed_events.py b/poezio/timed_events.py new file mode 100644 index 00000000..7f43d05f --- /dev/null +++ b/poezio/timed_events.py @@ -0,0 +1,58 @@ +# Copyright 2010-2011 Florent Le Coz <louiz@louiz.org> +# +# This file is part of Poezio. +# +# Poezio is free software: you can redistribute it and/or modify +# it under the terms of the zlib license. See the COPYING file. + +""" +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). +""" + +import asyncio +import logging + +log = logging.getLogger(__name__) + +import datetime + +class DelayedEvent(object): + """ + 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. + """ + def __init__(self, delay, callback, *args): + """ + 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. + """ + self.callback = callback + self.args = args + self.delay = delay + # An asyncio handler, as returned by call_later() or call_at() + self.handler = None + +class TimedEvent(DelayedEvent): + """ + An event with a callback that is called when the specified time is passed. + + The callback and its arguments should be passed as the lasts arguments. + """ + def __init__(self, date, callback, *args): + """ + 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. + """ + delta = date - datetime.datetime.now() + delay = delta.total_seconds() + DelayedEvent.__init__(self, delay, callback, *args) |