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-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
|
|
|
"""
|
|
|
|
|
2018-07-22 12:23:39 +00:00
|
|
|
from datetime import datetime
|
2018-08-15 13:51:31 +00:00
|
|
|
from asyncio import Handle
|
|
|
|
from typing import Callable, Union, Optional, Tuple, Any
|
2011-04-08 14:36:00 +00:00
|
|
|
|
2017-11-12 14:03:09 +00:00
|
|
|
|
2018-07-14 14:25:09 +00:00
|
|
|
class DelayedEvent:
|
2011-04-08 14:36:00 +00:00
|
|
|
"""
|
2014-07-31 23:22:59 +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-08 14:36:00 +00:00
|
|
|
"""
|
2017-11-12 14:03:09 +00:00
|
|
|
|
2018-07-23 18:58:30 +00:00
|
|
|
def __init__(self, delay: Union[int, float], callback: Callable,
|
|
|
|
*args) -> None:
|
2013-03-08 18:39:34 +00:00
|
|
|
"""
|
2014-07-31 23:22:59 +00:00
|
|
|
Create a new DelayedEvent.
|
2013-03-08 18:39:34 +00:00
|
|
|
|
2014-07-31 23:22:59 +00:00
|
|
|
:param int delay: The number of seconds.
|
2013-03-08 18:39:34 +00:00
|
|
|
:param function callback: The handler that will be executed.
|
2018-08-08 13:26:48 +00:00
|
|
|
:param args: Optional arguments passed to the handler.
|
2013-03-08 18:39:34 +00:00
|
|
|
"""
|
2020-12-12 17:44:37 +00:00
|
|
|
self.callback: Callable = callback
|
|
|
|
self.args: Tuple[Any, ...] = args
|
|
|
|
self.delay: Union[int, float] = delay
|
2014-07-31 23:22:59 +00:00
|
|
|
# An asyncio handler, as returned by call_later() or call_at()
|
2020-12-12 17:44:37 +00:00
|
|
|
self.handler: Optional[Handle] = None
|
2011-04-09 20:18:36 +00:00
|
|
|
|
2017-11-12 14:03:09 +00:00
|
|
|
|
2014-07-31 23:22:59 +00:00
|
|
|
class TimedEvent(DelayedEvent):
|
2011-04-09 20:18:36 +00:00
|
|
|
"""
|
2014-07-31 23:22:59 +00:00
|
|
|
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.
|
2011-04-09 20:18:36 +00:00
|
|
|
"""
|
2017-11-12 14:03:09 +00:00
|
|
|
|
2018-07-22 12:23:39 +00:00
|
|
|
def __init__(self, date: datetime, callback: Callable, *args) -> None:
|
2013-03-08 18:39:34 +00:00
|
|
|
"""
|
2014-07-31 23:22:59 +00:00
|
|
|
Create a new timed event.
|
2013-03-08 18:39:34 +00:00
|
|
|
|
2014-07-31 23:22:59 +00:00
|
|
|
:param datetime.datetime date: Time at which the callback must be run.
|
2013-03-08 18:39:34 +00:00
|
|
|
:param function callback: The handler that will be executed.
|
2018-08-08 13:26:48 +00:00
|
|
|
:param args: Optional arguments passed to the handler.
|
2013-03-08 18:39:34 +00:00
|
|
|
"""
|
2018-07-22 12:23:39 +00:00
|
|
|
delta = date - datetime.now()
|
2014-07-31 23:22:59 +00:00
|
|
|
delay = delta.total_seconds()
|
|
|
|
DelayedEvent.__init__(self, delay, callback, *args)
|