From 31378ac756695e6a5f7acb9426487f61a8561289 Mon Sep 17 00:00:00 2001 From: simon Date: Sun, 10 Apr 2022 15:58:11 +0700 Subject: [PATCH] better generic schedule validator for impossible input, #209 --- tubearchivist/home/src/ta/config.py | 33 +++++++++++++++++++++-------- 1 file changed, 24 insertions(+), 9 deletions(-) diff --git a/tubearchivist/home/src/ta/config.py b/tubearchivist/home/src/ta/config.py index c89d62c..4b98c4a 100644 --- a/tubearchivist/home/src/ta/config.py +++ b/tubearchivist/home/src/ta/config.py @@ -220,18 +220,33 @@ class ScheduleBuilder: raise ValueError("invalid input") to_write = dict(zip(keys, values)) - all_hours = [int(i) for i in re.split(r"\D+", to_write["hour"])] - if max(all_hours) > 23: - print("hour can't be greater than 23") - raise ValueError("invalid input") - try: - int(to_write["minute"]) - except ValueError as error: - print("too frequent: only number in minutes are supported") - raise ValueError("invalid input") from error + self._validate_cron(to_write) return to_write + @staticmethod + def _validate_cron(to_write): + """validate all fields, raise value error for impossible schedule""" + all_hours = list(re.split(r"\D+", to_write["hour"])) + for hour in all_hours: + if hour.isdigit() and int(hour) > 23: + print("hour can not be greater than 23") + raise ValueError("invalid input") + + all_days = list(re.split(r"\D+", to_write["day_of_week"])) + for day in all_days: + if day.isdigit() and int(day) > 6: + print("day can not be greater than 6") + raise ValueError("invalid input") + + if not to_write["minute"].isdigit(): + print("too frequent: only number in minutes are supported") + raise ValueError("invalid input") + + if int(to_write["minute"]) > 59: + print("minutes can not be greater than 59") + raise ValueError("invalid input") + def build_schedule(self): """build schedule dict as expected by app.conf.beat_schedule""" schedule_dict = {}