From d377461602378a20f57dfea864d535579dc36a40 Mon Sep 17 00:00:00 2001 From: bobloy Date: Tue, 8 Sep 2020 13:56:28 -0400 Subject: [PATCH] WIP adding timezone to Cron triggers --- fifo/datetime_cron_converters.py | 15 +++++++++++++-- fifo/fifo.py | 11 ++++++++--- fifo/task.py | 5 ++++- 3 files changed, 25 insertions(+), 6 deletions(-) diff --git a/fifo/datetime_cron_converters.py b/fifo/datetime_cron_converters.py index d59ef37..b7f3dc4 100644 --- a/fifo/datetime_cron_converters.py +++ b/fifo/datetime_cron_converters.py @@ -1,8 +1,8 @@ -from datetime import datetime +from datetime import datetime, tzinfo from typing import TYPE_CHECKING from apscheduler.triggers.cron import CronTrigger -from dateutil import parser +from dateutil import parser, tz from discord.ext.commands import BadArgument, Converter from fifo.timezones import assemble_timezones @@ -11,6 +11,17 @@ if TYPE_CHECKING: DatetimeConverter = datetime CronConverter = str else: + class TimezoneConverter(Converter): + async def convert(self, ctx, argument) -> tzinfo: + tzinfos = assemble_timezones() + if argument.upper() in tzinfos: + return tzinfos[argument.upper()] + + timez = tz.gettz(argument) + + if timez is not None: + return timez + raise BadArgument() class DatetimeConverter(Converter): async def convert(self, ctx, argument) -> datetime: diff --git a/fifo/fifo.py b/fifo/fifo.py index e84e342..91991b0 100644 --- a/fifo/fifo.py +++ b/fifo/fifo.py @@ -11,7 +11,7 @@ from redbot.core import Config, checks, commands from redbot.core.bot import Red from redbot.core.commands import TimedeltaConverter -from .datetime_cron_converters import CronConverter, DatetimeConverter +from .datetime_cron_converters import CronConverter, DatetimeConverter, TimezoneConverter from .task import Task schedule_log = logging.getLogger("red.fox_v3.fifo.scheduler") @@ -444,7 +444,12 @@ class FIFO(commands.Cog): @fifo_trigger.command(name="cron") async def fifo_trigger_cron( - self, ctx: commands.Context, task_name: str, *, cron_str: CronConverter + self, + ctx: commands.Context, + task_name: str, + optional_tz: Optional[TimezoneConverter] = None, + *, + cron_str: CronConverter, ): """ Add a cron "time of day" trigger to the specified task @@ -460,7 +465,7 @@ class FIFO(commands.Cog): ) return - result = await task.add_trigger("cron", cron_str) + result = await task.add_trigger("cron", cron_str, optional_tz) if not result: await ctx.maybe_send_embed( "Failed to add a cron trigger to this task, see console for logs" diff --git a/fifo/task.py b/fifo/task.py index 83158d8..764ab8f 100644 --- a/fifo/task.py +++ b/fifo/task.py @@ -300,7 +300,10 @@ class Task: self.data["command_str"] = command_str return True - async def add_trigger(self, param, parsed_time: Union[timedelta, datetime, str]): + async def add_trigger( + self, param, parsed_time: Union[timedelta, datetime, str], timezone=None + ): + # TODO: Save timezone separately for cron and date triggers trigger_data = {"type": param, "time_data": parsed_time} if not get_trigger(trigger_data): return False