2015-12-31 14:55:15 +01:00
|
|
|
#!/usr/bin/env python
|
|
|
|
#
|
|
|
|
# A library that provides a Python interface to the Telegram Bot API
|
2018-01-04 16:16:06 +01:00
|
|
|
# Copyright (C) 2015-2018
|
2016-01-05 14:12:03 +01:00
|
|
|
# Leandro Toledo de Souza <devs@python-telegram-bot.org>
|
2015-12-31 14:55:15 +01:00
|
|
|
#
|
|
|
|
# This program is free software: you can redistribute it and/or modify
|
|
|
|
# it under the terms of the GNU Lesser Public License as published by
|
|
|
|
# the Free Software Foundation, either version 3 of the License, or
|
|
|
|
# (at your option) any later version.
|
|
|
|
#
|
|
|
|
# This program is distributed in the hope that it will be useful,
|
|
|
|
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
# GNU Lesser Public License for more details.
|
|
|
|
#
|
|
|
|
# You should have received a copy of the GNU Lesser Public License
|
|
|
|
# along with this program. If not, see [http://www.gnu.org/licenses/].
|
2016-05-26 14:01:59 +02:00
|
|
|
"""This module contains the classes JobQueue and Job."""
|
2015-12-31 14:55:15 +01:00
|
|
|
|
|
|
|
import logging
|
|
|
|
import time
|
2018-05-21 15:00:47 +02:00
|
|
|
import datetime
|
2016-12-14 23:08:03 +01:00
|
|
|
import weakref
|
2016-11-08 23:39:25 +01:00
|
|
|
from numbers import Number
|
2018-05-21 15:00:47 +02:00
|
|
|
from threading import Thread, Lock, Event
|
2018-05-21 15:00:47 +02:00
|
|
|
from queue import PriorityQueue, Empty
|
2015-12-31 14:55:15 +01:00
|
|
|
|
|
|
|
|
2016-11-08 23:39:25 +01:00
|
|
|
class Days(object):
|
|
|
|
MON, TUE, WED, THU, FRI, SAT, SUN = range(7)
|
|
|
|
EVERY_DAY = tuple(range(7))
|
|
|
|
|
|
|
|
|
2015-12-31 14:55:15 +01:00
|
|
|
class JobQueue(object):
|
2017-09-01 08:43:08 +02:00
|
|
|
"""This class allows you to periodically perform tasks with the bot.
|
2015-12-31 14:55:15 +01:00
|
|
|
|
|
|
|
Attributes:
|
2018-01-20 14:27:01 +01:00
|
|
|
_queue (:obj:`PriorityQueue`): The queue that holds the Jobs.
|
2018-05-21 15:00:47 +02:00
|
|
|
bot (:class:`telegram.Bot`): Bot that's send to the handlers.
|
|
|
|
|
|
|
|
Args:
|
2017-07-23 22:33:08 +02:00
|
|
|
bot (:class:`telegram.Bot`): The bot instance that should be passed to the jobs.
|
2018-05-21 15:00:47 +02:00
|
|
|
|
2015-12-31 14:55:15 +01:00
|
|
|
"""
|
|
|
|
|
2018-05-21 15:00:47 +02:00
|
|
|
def __init__(self, bot):
|
2018-01-20 14:27:01 +01:00
|
|
|
self._queue = PriorityQueue()
|
2018-05-21 15:00:47 +02:00
|
|
|
self.bot = bot
|
2016-06-21 21:25:15 +02:00
|
|
|
self.logger = logging.getLogger(self.__class__.__name__)
|
2016-06-22 00:24:59 +02:00
|
|
|
self.__start_lock = Lock()
|
|
|
|
self.__next_peek_lock = Lock() # to protect self._next_peek & self.__tick
|
2016-05-25 22:51:13 +02:00
|
|
|
self.__tick = Event()
|
2016-06-22 00:24:59 +02:00
|
|
|
self.__thread = None
|
2016-05-28 13:46:57 +02:00
|
|
|
self._next_peek = None
|
|
|
|
self._running = False
|
2015-12-31 14:55:15 +01:00
|
|
|
|
2016-12-14 16:27:45 +01:00
|
|
|
def _put(self, job, next_t=None, last_t=None):
|
2015-12-31 14:55:15 +01:00
|
|
|
if next_t is None:
|
2016-12-14 06:30:18 +01:00
|
|
|
next_t = job.interval
|
2016-12-14 23:08:03 +01:00
|
|
|
if next_t is None:
|
|
|
|
raise ValueError('next_t is None')
|
2016-11-08 23:39:25 +01:00
|
|
|
|
2016-12-14 06:30:18 +01:00
|
|
|
if isinstance(next_t, datetime.datetime):
|
2016-12-14 23:08:03 +01:00
|
|
|
next_t = (next_t - datetime.datetime.now()).total_seconds()
|
2016-12-13 23:38:13 +01:00
|
|
|
|
|
|
|
elif isinstance(next_t, datetime.time):
|
|
|
|
next_datetime = datetime.datetime.combine(datetime.date.today(), next_t)
|
|
|
|
|
|
|
|
if datetime.datetime.now().time() > next_t:
|
|
|
|
next_datetime += datetime.timedelta(days=1)
|
|
|
|
|
2016-12-14 23:08:03 +01:00
|
|
|
next_t = (next_datetime - datetime.datetime.now()).total_seconds()
|
2016-12-13 23:38:13 +01:00
|
|
|
|
2016-12-14 23:08:03 +01:00
|
|
|
elif isinstance(next_t, datetime.timedelta):
|
2016-12-13 23:38:13 +01:00
|
|
|
next_t = next_t.total_seconds()
|
2015-12-31 14:55:15 +01:00
|
|
|
|
2016-12-14 16:27:45 +01:00
|
|
|
next_t += last_t or time.time()
|
2016-01-04 00:01:00 +01:00
|
|
|
|
2016-06-21 21:20:57 +02:00
|
|
|
self.logger.debug('Putting job %s with t=%f', job.name, next_t)
|
2016-12-14 16:27:45 +01:00
|
|
|
|
2018-01-20 14:27:01 +01:00
|
|
|
self._queue.put((next_t, job))
|
2015-12-31 14:55:15 +01:00
|
|
|
|
2016-05-26 14:01:59 +02:00
|
|
|
# Wake up the loop if this job should be executed next
|
2016-06-22 00:24:59 +02:00
|
|
|
self._set_next_peek(next_t)
|
2016-05-25 22:51:13 +02:00
|
|
|
|
2016-12-14 18:01:44 +01:00
|
|
|
def run_once(self, callback, when, context=None, name=None):
|
2017-09-01 08:43:08 +02:00
|
|
|
"""Creates a new ``Job`` that runs once and adds it to the queue.
|
2016-12-14 16:27:45 +01:00
|
|
|
|
|
|
|
Args:
|
2017-07-23 22:33:08 +02:00
|
|
|
callback (:obj:`callable`): The callback function that should be executed by the new
|
|
|
|
job. It should take ``bot, job`` as parameters, where ``job`` is the
|
|
|
|
:class:`telegram.ext.Job` instance. It can be used to access it's
|
|
|
|
``job.context`` or change it to a repeating job.
|
2018-02-19 09:36:40 +01:00
|
|
|
when (:obj:`int` | :obj:`float` | :obj:`datetime.timedelta` | \
|
|
|
|
:obj:`datetime.datetime` | :obj:`datetime.time`):
|
2016-12-14 16:27:45 +01:00
|
|
|
Time in or at which the job should run. This parameter will be interpreted
|
|
|
|
depending on its type.
|
2016-12-14 23:08:03 +01:00
|
|
|
|
2017-07-23 22:33:08 +02:00
|
|
|
* :obj:`int` or :obj:`float` will be interpreted as "seconds from now" in which the
|
|
|
|
job should run.
|
|
|
|
* :obj:`datetime.timedelta` will be interpreted as "time from now" in which the
|
|
|
|
job should run.
|
|
|
|
* :obj:`datetime.datetime` will be interpreted as a specific date and time at
|
|
|
|
which the job should run.
|
|
|
|
* :obj:`datetime.time` will be interpreted as a specific time of day at which the
|
|
|
|
job should run. This could be either today or, if the time has already passed,
|
2016-12-14 23:08:03 +01:00
|
|
|
tomorrow.
|
|
|
|
|
2017-07-23 22:33:08 +02:00
|
|
|
context (:obj:`object`, optional): Additional data needed for the callback function.
|
|
|
|
Can be accessed through ``job.context`` in the callback. Defaults to ``None``.
|
|
|
|
name (:obj:`str`, optional): The name of the new job. Defaults to
|
|
|
|
``callback.__name__``.
|
2016-12-14 16:27:45 +01:00
|
|
|
|
|
|
|
Returns:
|
2017-07-23 22:33:08 +02:00
|
|
|
:class:`telegram.ext.Job`: The new ``Job`` instance that has been added to the job
|
|
|
|
queue.
|
|
|
|
|
2017-09-01 08:43:08 +02:00
|
|
|
"""
|
2016-12-14 23:08:03 +01:00
|
|
|
job = Job(callback, repeat=False, context=context, name=name, job_queue=self)
|
2016-12-14 16:27:45 +01:00
|
|
|
self._put(job, next_t=when)
|
|
|
|
return job
|
|
|
|
|
2016-12-14 18:01:44 +01:00
|
|
|
def run_repeating(self, callback, interval, first=None, context=None, name=None):
|
2017-09-01 08:43:08 +02:00
|
|
|
"""Creates a new ``Job`` that runs once and adds it to the queue.
|
2016-12-14 16:27:45 +01:00
|
|
|
|
|
|
|
Args:
|
2017-07-23 22:33:08 +02:00
|
|
|
callback (:obj:`callable`): The callback function that should be executed by the new
|
|
|
|
job. It should take ``bot, job`` as parameters, where ``job`` is the
|
|
|
|
:class:`telegram.ext.Job` instance. It can be used to access it's
|
|
|
|
``Job.context`` or change it to a repeating job.
|
|
|
|
interval (:obj:`int` | :obj:`float` | :obj:`datetime.timedelta`): The interval in which
|
|
|
|
the job will run. If it is an :obj:`int` or a :obj:`float`, it will be interpreted
|
|
|
|
as seconds.
|
2018-02-19 09:36:40 +01:00
|
|
|
first (:obj:`int` | :obj:`float` | :obj:`datetime.timedelta` | \
|
|
|
|
:obj:`datetime.datetime` | :obj:`datetime.time`, optional):
|
2017-07-23 22:33:08 +02:00
|
|
|
Time in or at which the job should run. This parameter will be interpreted
|
|
|
|
depending on its type.
|
|
|
|
|
|
|
|
* :obj:`int` or :obj:`float` will be interpreted as "seconds from now" in which the
|
|
|
|
job should run.
|
|
|
|
* :obj:`datetime.timedelta` will be interpreted as "time from now" in which the
|
|
|
|
job should run.
|
|
|
|
* :obj:`datetime.datetime` will be interpreted as a specific date and time at
|
|
|
|
which the job should run.
|
|
|
|
* :obj:`datetime.time` will be interpreted as a specific time of day at which the
|
|
|
|
job should run. This could be either today or, if the time has already passed,
|
2016-12-14 23:08:03 +01:00
|
|
|
tomorrow.
|
|
|
|
|
2016-12-14 16:27:45 +01:00
|
|
|
Defaults to ``interval``
|
2017-07-23 22:33:08 +02:00
|
|
|
context (:obj:`object`, optional): Additional data needed for the callback function.
|
|
|
|
Can be accessed through ``job.context`` in the callback. Defaults to ``None``.
|
|
|
|
name (:obj:`str`, optional): The name of the new job. Defaults to
|
|
|
|
``callback.__name__``.
|
2016-12-14 16:27:45 +01:00
|
|
|
|
|
|
|
Returns:
|
2017-07-23 22:33:08 +02:00
|
|
|
:class:`telegram.ext.Job`: The new ``Job`` instance that has been added to the job
|
|
|
|
queue.
|
2017-09-01 08:43:08 +02:00
|
|
|
|
2016-12-14 16:27:45 +01:00
|
|
|
"""
|
2016-12-14 23:08:03 +01:00
|
|
|
job = Job(callback,
|
|
|
|
interval=interval,
|
|
|
|
repeat=True,
|
|
|
|
context=context,
|
|
|
|
name=name,
|
|
|
|
job_queue=self)
|
2016-12-14 16:27:45 +01:00
|
|
|
self._put(job, next_t=first)
|
|
|
|
return job
|
|
|
|
|
2016-12-14 18:01:44 +01:00
|
|
|
def run_daily(self, callback, time, days=Days.EVERY_DAY, context=None, name=None):
|
2017-09-01 08:43:08 +02:00
|
|
|
"""Creates a new ``Job`` that runs once and adds it to the queue.
|
2016-12-14 16:27:45 +01:00
|
|
|
|
|
|
|
Args:
|
2017-07-23 22:33:08 +02:00
|
|
|
callback (:obj:`callable`): The callback function that should be executed by the new
|
|
|
|
job. It should take ``bot, job`` as parameters, where ``job`` is the
|
|
|
|
:class:`telegram.ext.Job` instance. It can be used to access it's ``Job.context``
|
|
|
|
or change it to a repeating job.
|
|
|
|
time (:obj:`datetime.time`): Time of day at which the job should run.
|
|
|
|
days (Tuple[:obj:`int`], optional): Defines on which days of the week the job should
|
|
|
|
run. Defaults to ``EVERY_DAY``
|
|
|
|
context (:obj:`object`, optional): Additional data needed for the callback function.
|
|
|
|
Can be accessed through ``job.context`` in the callback. Defaults to ``None``.
|
|
|
|
name (:obj:`str`, optional): The name of the new job. Defaults to
|
|
|
|
``callback.__name__``.
|
2016-12-14 16:27:45 +01:00
|
|
|
|
|
|
|
Returns:
|
2017-07-23 22:33:08 +02:00
|
|
|
:class:`telegram.ext.Job`: The new ``Job`` instance that has been added to the job
|
|
|
|
queue.
|
2017-09-01 08:43:08 +02:00
|
|
|
|
2016-12-14 16:27:45 +01:00
|
|
|
"""
|
|
|
|
job = Job(callback,
|
|
|
|
interval=datetime.timedelta(days=1),
|
|
|
|
repeat=True,
|
|
|
|
days=days,
|
|
|
|
context=context,
|
2016-12-14 23:08:03 +01:00
|
|
|
name=name,
|
|
|
|
job_queue=self)
|
2016-12-14 16:27:45 +01:00
|
|
|
self._put(job, next_t=time)
|
|
|
|
return job
|
|
|
|
|
2016-06-22 00:24:59 +02:00
|
|
|
def _set_next_peek(self, t):
|
2017-07-23 22:33:08 +02:00
|
|
|
# """
|
|
|
|
# Set next peek if not defined or `t` is before next peek.
|
|
|
|
# In case the next peek was set, also trigger the `self.__tick` event.
|
|
|
|
# """
|
2016-06-22 00:24:59 +02:00
|
|
|
with self.__next_peek_lock:
|
|
|
|
if not self._next_peek or self._next_peek > t:
|
|
|
|
self._next_peek = t
|
|
|
|
self.__tick.set()
|
2016-01-05 13:32:19 +01:00
|
|
|
|
2015-12-31 14:55:15 +01:00
|
|
|
def tick(self):
|
2017-09-01 08:43:08 +02:00
|
|
|
"""Run all jobs that are due and re-enqueue them with their interval."""
|
2016-12-14 17:15:52 +01:00
|
|
|
now = time.time()
|
2016-06-22 00:24:59 +02:00
|
|
|
|
2016-12-14 17:15:52 +01:00
|
|
|
self.logger.debug('Ticking jobs with t=%f', now)
|
2016-05-25 22:51:13 +02:00
|
|
|
|
2016-12-14 17:15:52 +01:00
|
|
|
while True:
|
|
|
|
try:
|
2018-01-20 14:27:01 +01:00
|
|
|
t, job = self._queue.get(False)
|
2016-12-14 17:15:52 +01:00
|
|
|
except Empty:
|
|
|
|
break
|
|
|
|
|
|
|
|
self.logger.debug('Peeked at %s with t=%f', job.name, t)
|
|
|
|
|
|
|
|
if t > now:
|
|
|
|
# We can get here in two conditions:
|
|
|
|
# 1. At the second or later pass of the while loop, after we've already
|
|
|
|
# processed the job(s) we were supposed to at this time.
|
|
|
|
# 2. At the first iteration of the loop only if `self.put()` had triggered
|
|
|
|
# `self.__tick` because `self._next_peek` wasn't set
|
|
|
|
self.logger.debug("Next task isn't due yet. Finished!")
|
2018-01-20 14:27:01 +01:00
|
|
|
self._queue.put((t, job))
|
2016-12-14 17:15:52 +01:00
|
|
|
self._set_next_peek(t)
|
|
|
|
break
|
|
|
|
|
2016-12-20 22:37:36 +01:00
|
|
|
if job.removed:
|
2016-12-14 17:15:52 +01:00
|
|
|
self.logger.debug('Removing job %s', job.name)
|
|
|
|
continue
|
|
|
|
|
|
|
|
if job.enabled:
|
2016-06-22 00:24:59 +02:00
|
|
|
try:
|
2016-12-14 17:15:52 +01:00
|
|
|
current_week_day = datetime.datetime.now().weekday()
|
|
|
|
if any(day == current_week_day for day in job.days):
|
|
|
|
self.logger.debug('Running job %s', job.name)
|
2018-05-21 15:00:47 +02:00
|
|
|
job.run(self.bot)
|
2016-12-14 17:15:52 +01:00
|
|
|
|
2018-02-19 11:41:38 +01:00
|
|
|
except Exception:
|
2016-12-14 17:15:52 +01:00
|
|
|
self.logger.exception('An uncaught error was raised while executing job %s',
|
|
|
|
job.name)
|
|
|
|
else:
|
|
|
|
self.logger.debug('Skipping disabled job %s', job.name)
|
|
|
|
|
2016-12-20 22:37:36 +01:00
|
|
|
if job.repeat and not job.removed:
|
2016-12-14 17:15:52 +01:00
|
|
|
self._put(job, last_t=t)
|
|
|
|
else:
|
2016-12-14 23:08:03 +01:00
|
|
|
self.logger.debug('Dropping non-repeating or removed job %s', job.name)
|
2016-05-25 22:51:13 +02:00
|
|
|
|
2015-12-31 14:55:15 +01:00
|
|
|
def start(self):
|
2017-09-01 08:43:08 +02:00
|
|
|
"""Starts the job_queue thread."""
|
2016-06-22 00:24:59 +02:00
|
|
|
self.__start_lock.acquire()
|
2016-05-26 14:01:59 +02:00
|
|
|
|
2016-05-28 13:46:57 +02:00
|
|
|
if not self._running:
|
|
|
|
self._running = True
|
2016-06-22 00:24:59 +02:00
|
|
|
self.__start_lock.release()
|
|
|
|
self.__thread = Thread(target=self._main_loop, name="job_queue")
|
|
|
|
self.__thread.start()
|
2016-06-21 21:20:57 +02:00
|
|
|
self.logger.debug('%s thread started', self.__class__.__name__)
|
2015-12-31 14:55:15 +01:00
|
|
|
else:
|
2016-06-22 00:24:59 +02:00
|
|
|
self.__start_lock.release()
|
2015-12-31 14:55:15 +01:00
|
|
|
|
2016-06-22 00:24:59 +02:00
|
|
|
def _main_loop(self):
|
2016-01-04 00:01:00 +01:00
|
|
|
"""
|
2016-05-26 14:01:59 +02:00
|
|
|
Thread target of thread ``job_queue``. Runs in background and performs ticks on the job
|
|
|
|
queue.
|
2017-07-23 22:33:08 +02:00
|
|
|
|
2017-09-01 08:43:08 +02:00
|
|
|
"""
|
2016-05-28 13:46:57 +02:00
|
|
|
while self._running:
|
2016-06-22 00:24:59 +02:00
|
|
|
# self._next_peek may be (re)scheduled during self.tick() or self.put()
|
|
|
|
with self.__next_peek_lock:
|
2016-12-14 23:08:03 +01:00
|
|
|
tmout = self._next_peek - time.time() if self._next_peek else None
|
2016-06-22 00:24:59 +02:00
|
|
|
self._next_peek = None
|
2016-05-25 22:51:13 +02:00
|
|
|
self.__tick.clear()
|
2016-06-22 00:24:59 +02:00
|
|
|
|
|
|
|
self.__tick.wait(tmout)
|
|
|
|
|
|
|
|
# If we were woken up by self.stop(), just bail out
|
|
|
|
if not self._running:
|
|
|
|
break
|
2016-05-25 22:51:13 +02:00
|
|
|
|
2016-01-04 00:01:00 +01:00
|
|
|
self.tick()
|
|
|
|
|
2016-06-21 21:20:57 +02:00
|
|
|
self.logger.debug('%s thread stopped', self.__class__.__name__)
|
2016-01-04 01:56:22 +01:00
|
|
|
|
2015-12-31 14:55:15 +01:00
|
|
|
def stop(self):
|
2017-09-01 08:43:08 +02:00
|
|
|
"""Stops the thread."""
|
2016-06-22 00:24:59 +02:00
|
|
|
with self.__start_lock:
|
2016-05-28 13:46:57 +02:00
|
|
|
self._running = False
|
2015-12-31 14:55:15 +01:00
|
|
|
|
2016-05-25 22:51:13 +02:00
|
|
|
self.__tick.set()
|
2016-06-22 00:24:59 +02:00
|
|
|
if self.__thread is not None:
|
|
|
|
self.__thread.join()
|
2016-05-25 22:51:13 +02:00
|
|
|
|
|
|
|
def jobs(self):
|
2017-09-01 08:43:08 +02:00
|
|
|
"""Returns a tuple of all jobs that are currently in the ``JobQueue``."""
|
2018-01-20 14:27:01 +01:00
|
|
|
with self._queue.mutex:
|
|
|
|
return tuple(job[1] for job in self._queue.queue if job)
|
2016-05-25 22:51:13 +02:00
|
|
|
|
2018-02-19 09:36:40 +01:00
|
|
|
def get_jobs_by_name(self, name):
|
|
|
|
"""Returns a tuple of jobs with the given name that are currently in the ``JobQueue``"""
|
|
|
|
with self._queue.mutex:
|
|
|
|
return tuple(job[1] for job in self._queue.queue if job and job[1].name == name)
|
|
|
|
|
2016-05-25 22:51:13 +02:00
|
|
|
|
|
|
|
class Job(object):
|
2017-09-01 08:43:08 +02:00
|
|
|
"""This class encapsulates a Job.
|
2016-05-25 22:51:13 +02:00
|
|
|
|
|
|
|
Attributes:
|
2017-07-23 22:33:08 +02:00
|
|
|
callback (:obj:`callable`): The callback function that should be executed by the new job.
|
|
|
|
context (:obj:`object`): Optional. Additional data needed for the callback function.
|
|
|
|
name (:obj:`str`): Optional. The name of the new job.
|
2016-05-25 22:51:13 +02:00
|
|
|
|
|
|
|
Args:
|
2017-07-23 22:33:08 +02:00
|
|
|
callback (:obj:`callable`): The callback function that should be executed by the new job.
|
|
|
|
It should take ``bot, job`` as parameters, where ``job`` is the
|
|
|
|
:class:`telegram.ext.Job` instance. It can be used to access it's :attr:`context`
|
|
|
|
or change it to a repeating job.
|
|
|
|
interval (:obj:`int` | :obj:`float` | :obj:`datetime.timedelta`, optional): The interval in
|
|
|
|
which the job will run. If it is an :obj:`int` or a :obj:`float`, it will be
|
|
|
|
interpreted as seconds. If you don't set this value, you must set :attr:`repeat` to
|
|
|
|
``False`` and specify :attr:`next_t` when you put the job into the job queue.
|
|
|
|
repeat (:obj:`bool`, optional): If this job should be periodically execute its callback
|
|
|
|
function (``True``) or only once (``False``). Defaults to ``True``.
|
|
|
|
context (:obj:`object`, optional): Additional data needed for the callback function. Can be
|
|
|
|
accessed through ``job.context`` in the callback. Defaults to ``None``.
|
|
|
|
name (:obj:`str`, optional): The name of the new job. Defaults to ``callback.__name__``.
|
|
|
|
days (Tuple[:obj:`int`], optional): Defines on which days of the week the job should run.
|
2016-12-14 16:27:45 +01:00
|
|
|
Defaults to ``Days.EVERY_DAY``
|
2017-09-25 20:57:53 +02:00
|
|
|
job_queue (:class:`telegram.ext.JobQueue`, optional): The ``JobQueue`` this job belongs to.
|
2016-12-14 23:08:03 +01:00
|
|
|
Only optional for backward compatibility with ``JobQueue.put()``.
|
2017-09-01 08:43:08 +02:00
|
|
|
|
2016-05-25 22:51:13 +02:00
|
|
|
"""
|
|
|
|
|
2016-12-14 16:27:45 +01:00
|
|
|
def __init__(self,
|
|
|
|
callback,
|
|
|
|
interval=None,
|
|
|
|
repeat=True,
|
|
|
|
context=None,
|
|
|
|
days=Days.EVERY_DAY,
|
2016-12-14 23:08:03 +01:00
|
|
|
name=None,
|
|
|
|
job_queue=None):
|
2016-12-14 16:27:45 +01:00
|
|
|
|
2016-05-25 22:51:13 +02:00
|
|
|
self.callback = callback
|
2016-05-26 13:55:30 +02:00
|
|
|
self.context = context
|
2016-12-14 16:27:45 +01:00
|
|
|
self.name = name or callback.__name__
|
2016-05-25 22:51:13 +02:00
|
|
|
|
2016-12-14 16:27:45 +01:00
|
|
|
self._repeat = repeat
|
|
|
|
self._interval = None
|
|
|
|
self.interval = interval
|
|
|
|
self.repeat = repeat
|
2016-11-08 23:39:25 +01:00
|
|
|
|
2016-12-14 16:27:45 +01:00
|
|
|
self._days = None
|
|
|
|
self.days = days
|
2016-12-13 23:38:13 +01:00
|
|
|
|
2016-12-14 23:08:03 +01:00
|
|
|
self._job_queue = weakref.proxy(job_queue) if job_queue is not None else None
|
2016-12-14 06:30:18 +01:00
|
|
|
|
2016-05-25 22:51:13 +02:00
|
|
|
self._remove = Event()
|
|
|
|
self._enabled = Event()
|
|
|
|
self._enabled.set()
|
|
|
|
|
2018-05-21 15:00:47 +02:00
|
|
|
def run(self, bot):
|
2017-09-01 08:43:08 +02:00
|
|
|
"""Executes the callback function."""
|
2018-05-21 15:00:47 +02:00
|
|
|
self.callback(bot, self)
|
2016-05-25 22:51:13 +02:00
|
|
|
|
|
|
|
def schedule_removal(self):
|
|
|
|
"""
|
|
|
|
Schedules this job for removal from the ``JobQueue``. It will be removed without executing
|
|
|
|
its callback function again.
|
2017-09-01 08:43:08 +02:00
|
|
|
|
2016-05-25 22:51:13 +02:00
|
|
|
"""
|
|
|
|
self._remove.set()
|
|
|
|
|
2016-12-19 23:14:03 +01:00
|
|
|
@property
|
2016-12-20 22:37:36 +01:00
|
|
|
def removed(self):
|
2017-09-01 08:43:08 +02:00
|
|
|
""":obj:`bool`: Whether this job is due to be removed."""
|
2016-12-14 23:08:03 +01:00
|
|
|
return self._remove.is_set()
|
|
|
|
|
2016-12-14 16:27:45 +01:00
|
|
|
@property
|
|
|
|
def enabled(self):
|
2017-09-01 08:43:08 +02:00
|
|
|
""":obj:`bool`: Whether this job is enabled."""
|
2016-05-25 22:51:13 +02:00
|
|
|
return self._enabled.is_set()
|
|
|
|
|
2016-12-14 16:27:45 +01:00
|
|
|
@enabled.setter
|
|
|
|
def enabled(self, status):
|
2016-05-25 22:51:13 +02:00
|
|
|
if status:
|
|
|
|
self._enabled.set()
|
|
|
|
else:
|
|
|
|
self._enabled.clear()
|
2015-12-31 14:55:15 +01:00
|
|
|
|
2016-12-14 16:27:45 +01:00
|
|
|
@property
|
|
|
|
def interval(self):
|
2017-07-23 22:33:08 +02:00
|
|
|
"""
|
|
|
|
:obj:`int` | :obj:`float` | :obj:`datetime.timedelta`: Optional. The interval in which the
|
|
|
|
job will run.
|
|
|
|
|
2017-09-01 08:43:08 +02:00
|
|
|
"""
|
2016-12-14 16:27:45 +01:00
|
|
|
return self._interval
|
|
|
|
|
|
|
|
@interval.setter
|
|
|
|
def interval(self, interval):
|
|
|
|
if interval is None and self.repeat:
|
|
|
|
raise ValueError("The 'interval' can not be 'None' when 'repeat' is set to 'True'")
|
|
|
|
|
2016-12-14 23:08:03 +01:00
|
|
|
if not (interval is None or isinstance(interval, (Number, datetime.timedelta))):
|
2016-12-14 16:27:45 +01:00
|
|
|
raise ValueError("The 'interval' must be of type 'datetime.timedelta',"
|
|
|
|
" 'int' or 'float'")
|
|
|
|
|
|
|
|
self._interval = interval
|
|
|
|
|
|
|
|
@property
|
|
|
|
def interval_seconds(self):
|
2017-09-01 08:43:08 +02:00
|
|
|
""":obj:`int`: The interval for this job in seconds."""
|
2018-01-20 14:27:01 +01:00
|
|
|
interval = self.interval
|
|
|
|
if isinstance(interval, datetime.timedelta):
|
|
|
|
return interval.total_seconds()
|
2016-12-14 16:27:45 +01:00
|
|
|
else:
|
2018-01-20 14:27:01 +01:00
|
|
|
return interval
|
2016-12-14 16:27:45 +01:00
|
|
|
|
|
|
|
@property
|
|
|
|
def repeat(self):
|
2017-09-01 08:43:08 +02:00
|
|
|
""":obj:`bool`: Optional. If this job should periodically execute its callback function."""
|
2016-12-14 16:27:45 +01:00
|
|
|
return self._repeat
|
|
|
|
|
|
|
|
@repeat.setter
|
|
|
|
def repeat(self, repeat):
|
|
|
|
if self.interval is None and repeat:
|
|
|
|
raise ValueError("'repeat' can not be set to 'True' when no 'interval' is set")
|
|
|
|
self._repeat = repeat
|
|
|
|
|
|
|
|
@property
|
|
|
|
def days(self):
|
2017-09-01 08:43:08 +02:00
|
|
|
"""Tuple[:obj:`int`]: Optional. Defines on which days of the week the job should run."""
|
2016-12-14 16:27:45 +01:00
|
|
|
return self._days
|
|
|
|
|
|
|
|
@days.setter
|
|
|
|
def days(self, days):
|
|
|
|
if not isinstance(days, tuple):
|
|
|
|
raise ValueError("The 'days' argument should be of type 'tuple'")
|
|
|
|
|
|
|
|
if not all(isinstance(day, int) for day in days):
|
|
|
|
raise ValueError("The elements of the 'days' argument should be of type 'int'")
|
|
|
|
|
|
|
|
if not all(0 <= day <= 6 for day in days):
|
|
|
|
raise ValueError("The elements of the 'days' argument should be from 0 up to and "
|
|
|
|
"including 6")
|
|
|
|
|
|
|
|
self._days = days
|
|
|
|
|
|
|
|
@property
|
|
|
|
def job_queue(self):
|
2017-09-01 08:43:08 +02:00
|
|
|
""":class:`telegram.ext.JobQueue`: Optional. The ``JobQueue`` this job belongs to."""
|
2016-12-14 16:27:45 +01:00
|
|
|
return self._job_queue
|
|
|
|
|
|
|
|
@job_queue.setter
|
|
|
|
def job_queue(self, job_queue):
|
2016-12-14 23:08:03 +01:00
|
|
|
# Property setter for backward compatibility with JobQueue.put()
|
|
|
|
if not self._job_queue:
|
|
|
|
self._job_queue = weakref.proxy(job_queue)
|
2016-12-14 16:27:45 +01:00
|
|
|
else:
|
2016-12-14 23:08:03 +01:00
|
|
|
raise RuntimeError("The 'job_queue' attribute can only be set once.")
|
2015-12-31 14:55:15 +01:00
|
|
|
|
2016-05-25 22:51:13 +02:00
|
|
|
def __lt__(self, other):
|
|
|
|
return False
|