From 71c0ee572c19c438081fafa478b887a0c9514ab9 Mon Sep 17 00:00:00 2001 From: Hinrich Mahler <22366557+Bibo-Joshi@users.noreply.github.com> Date: Sun, 29 Dec 2024 13:52:23 +0100 Subject: [PATCH 1/3] Add Parameter `pattern` to `JobQueue.jobs()` --- telegram/ext/_jobqueue.py | 34 +++++++++++++++++++++++++++++----- tests/ext/test_jobqueue.py | 30 +++++++++++++++++++++++------- 2 files changed, 52 insertions(+), 12 deletions(-) diff --git a/telegram/ext/_jobqueue.py b/telegram/ext/_jobqueue.py index acc752b5f51..78d6be55c23 100644 --- a/telegram/ext/_jobqueue.py +++ b/telegram/ext/_jobqueue.py @@ -19,6 +19,7 @@ """This module contains the classes JobQueue and Job.""" import asyncio import datetime as dtm +import re import weakref from typing import TYPE_CHECKING, Any, Generic, Optional, Union, cast, overload @@ -38,6 +39,8 @@ from telegram.ext._utils.types import CCT, JobCallback if TYPE_CHECKING: + from collections.abc import Iterable + if APS_AVAILABLE: from apscheduler.job import Job as APSJob @@ -693,22 +696,43 @@ async def stop(self, wait: bool = True) -> None: # so give it a tiny bit of time to actually shut down. await asyncio.sleep(0.01) - def jobs(self) -> tuple["Job[CCT]", ...]: + def jobs(self, pattern: Union[str, re.Pattern, None] = None) -> tuple["Job[CCT]", ...]: """Returns a tuple of all *scheduled* jobs that are currently in the :class:`JobQueue`. + Args: + pattern (:obj:`str` | :obj:`re.Pattern`, optional): A regular expression pattern. If + passend, only jobs whose name matches the pattern will be returned. + Defaults to :obj:`None`. + + Hint: + This uses :func:`re.search` and not :func:`re.match`. + + .. versionadded:: NEXT.VERSION + Returns: tuple[:class:`Job`]: Tuple of all *scheduled* jobs. """ - return tuple(Job.from_aps_job(job) for job in self.scheduler.get_jobs()) + jobs_generator: Iterable[Job] = ( + Job.from_aps_job(job) for job in self.scheduler.get_jobs() + ) + if pattern is None: + return tuple(jobs_generator) + return tuple( + job for job in jobs_generator if (job.name and re.compile(pattern).search(job.name)) + ) def get_jobs_by_name(self, name: str) -> tuple["Job[CCT]", ...]: - """Returns a tuple of all *pending/scheduled* jobs with the given name that are currently + """Returns a tuple of all *scheduled* jobs with the given name that are currently in the :class:`JobQueue`. + Hint: + This method is a convenience wrapper for :meth:`jobs` with a pattern that matches the + given name. + Returns: - tuple[:class:`Job`]: Tuple of all *pending* or *scheduled* jobs matching the name. + tuple[:class:`Job`]: Tuple of all *scheduled* jobs matching the name. """ - return tuple(job for job in self.jobs() if job.name == name) + return self.jobs(f"^{re.escape(name)}$") class Job(Generic[CCT]): diff --git a/tests/ext/test_jobqueue.py b/tests/ext/test_jobqueue.py index 5ca908d4a02..5c006e1051c 100644 --- a/tests/ext/test_jobqueue.py +++ b/tests/ext/test_jobqueue.py @@ -21,6 +21,7 @@ import datetime as dtm import logging import platform +import re import time import pytest @@ -443,19 +444,34 @@ async def test_default_tzinfo(self, tz_bot): await jq.stop() - async def test_get_jobs(self, job_queue): + @pytest.mark.parametrize( + "pattern", [None, "not", re.compile("not")], ids=["None", "str", "re.Pattern"] + ) + async def test_get_jobs(self, job_queue, pattern): callback = self.job_run_once - job1 = job_queue.run_once(callback, 10, name="name1") + job1 = job_queue.run_once(callback, 10, name="is|a|match") await asyncio.sleep(0.03) # To stablize tests on windows - job2 = job_queue.run_once(callback, 10, name="name1") + job2 = job_queue.run_once(callback, 10, name="is|a|match") await asyncio.sleep(0.03) - job3 = job_queue.run_once(callback, 10, name="name2") + job3 = job_queue.run_once(callback, 10, name="not|is|a|match") await asyncio.sleep(0.03) + job4 = job_queue.run_once(callback, 10, name="is|a|match|not") + await asyncio.sleep(0.03) + job5 = job_queue.run_once(callback, 10, name="something_else") + await asyncio.sleep(0.03) + # name-less job + job6 = job_queue.run_once(callback, 10) + await asyncio.sleep(0.03) + + if pattern is None: + assert job_queue.jobs(pattern) == (job1, job2, job3, job4, job5, job6) + else: + assert job_queue.jobs(pattern) == (job3, job4) - assert job_queue.jobs() == (job1, job2, job3) - assert job_queue.get_jobs_by_name("name1") == (job1, job2) - assert job_queue.get_jobs_by_name("name2") == (job3,) + assert job_queue.jobs() == (job1, job2, job3, job4, job5, job6) + assert job_queue.get_jobs_by_name("is|a|match") == (job1, job2) + assert job_queue.get_jobs_by_name("something_else") == (job5,) async def test_job_run(self, app): job = app.job_queue.run_repeating(self.job_run_once, 0.02) From 1f8e44cc7d3f00508d04561453b0159c02612185 Mon Sep 17 00:00:00 2001 From: Hinrich Mahler <22366557+Bibo-Joshi@users.noreply.github.com> Date: Sun, 29 Dec 2024 14:11:13 +0100 Subject: [PATCH 2/3] Improve type completeness --- telegram/ext/_jobqueue.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/telegram/ext/_jobqueue.py b/telegram/ext/_jobqueue.py index 78d6be55c23..5c08b0dc8ba 100644 --- a/telegram/ext/_jobqueue.py +++ b/telegram/ext/_jobqueue.py @@ -696,7 +696,7 @@ async def stop(self, wait: bool = True) -> None: # so give it a tiny bit of time to actually shut down. await asyncio.sleep(0.01) - def jobs(self, pattern: Union[str, re.Pattern, None] = None) -> tuple["Job[CCT]", ...]: + def jobs(self, pattern: Union[str, re.Pattern[str], None] = None) -> tuple["Job[CCT]", ...]: """Returns a tuple of all *scheduled* jobs that are currently in the :class:`JobQueue`. Args: From 14110bbc3ce0c631af86d7792e9e8b5f54d003c4 Mon Sep 17 00:00:00 2001 From: Bibo-Joshi <22366557+Bibo-Joshi@users.noreply.github.com> Date: Wed, 1 Jan 2025 19:44:42 +0100 Subject: [PATCH 3/3] Update telegram/ext/_jobqueue.py Co-authored-by: Harshil <37377066+harshil21@users.noreply.github.com> --- telegram/ext/_jobqueue.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/telegram/ext/_jobqueue.py b/telegram/ext/_jobqueue.py index a320004dbf4..9f20a3a3c78 100644 --- a/telegram/ext/_jobqueue.py +++ b/telegram/ext/_jobqueue.py @@ -701,7 +701,7 @@ def jobs(self, pattern: Union[str, re.Pattern[str], None] = None) -> tuple["Job[ Args: pattern (:obj:`str` | :obj:`re.Pattern`, optional): A regular expression pattern. If - passend, only jobs whose name matches the pattern will be returned. + passed, only jobs whose name matches the pattern will be returned. Defaults to :obj:`None`. Hint: