From 2cda635683a3d38ccae1f31524432ba55aa02819 Mon Sep 17 00:00:00 2001 From: mhrmsn <49688144+mhrmsn@users.noreply.github.com> Date: Mon, 29 May 2023 16:07:57 +0200 Subject: [PATCH 1/4] Added function to replace names --- cron_validator/util.py | 25 +++++++++++++++++++++++++ 1 file changed, 25 insertions(+) diff --git a/cron_validator/util.py b/cron_validator/util.py index 559a88b..55ff15c 100644 --- a/cron_validator/util.py +++ b/cron_validator/util.py @@ -1,4 +1,5 @@ import datetime +import re import dateutil.parser import pytz @@ -29,3 +30,27 @@ def str_to_datetime(datetime_str, tz_name="UTC"): :return: """ return dateutil.parser.parse(datetime_str).replace(tzinfo=get_tz(tz_name)) + + +def replace_names(expression): + """ + + :param expression: + :return: + """ + parts = expression.split(" ") + month_names = ["jan", "feb", "mar", "apr", "may", "jun", "jul", "aug", "sep", "oct", "nov", "dec"] + day_of_week_names = ["sun", "mon", "tue", "wed", "thu", "fri", "sat"] + month_names_re = re.compile(rf"(? Date: Mon, 29 May 2023 16:08:25 +0200 Subject: [PATCH 2/4] Replace month and day of week names in expression --- cron_validator/validator.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/cron_validator/validator.py b/cron_validator/validator.py index 6eeaa13..bfd5cd1 100644 --- a/cron_validator/validator.py +++ b/cron_validator/validator.py @@ -1,7 +1,7 @@ from dateutil import rrule from .regexes import ElementPart, Version, element_kind_map, regex_dict -from .util import ts_to_datetime +from .util import replace_names, ts_to_datetime class CronValidator: @@ -12,6 +12,7 @@ def parse(cls, expression, version=Version.UNIX): :param str expression: :return: """ + expression = replace_names(expression) parts = expression.split(" ") if len(parts) != 5: raise ValueError("Invalid expression") From 33686d02912479b1c6230367a8b12af895239d82 Mon Sep 17 00:00:00 2001 From: mhrmsn <49688144+mhrmsn@users.noreply.github.com> Date: Mon, 29 May 2023 16:08:38 +0200 Subject: [PATCH 3/4] Added unit test for replace_names --- test/test_util.py | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) diff --git a/test/test_util.py b/test/test_util.py index 2bb1dad..7eb5cdf 100644 --- a/test/test_util.py +++ b/test/test_util.py @@ -1,6 +1,6 @@ from dateutil import rrule -from cron_validator.util import str_to_datetime +from cron_validator.util import replace_names, str_to_datetime tz_name = "Asia/Ho_Chi_Minh" utc_tz = "UTC" @@ -33,3 +33,12 @@ def test_iterator_day(): assert delta.total_seconds() < 0 for dt in rrule.rrule(rrule.MINUTELY, dtstart=dt1, until=dt2): print(dt) + + +def test_replace_names(): + assert replace_names("* * * */2,3 6") == "* * * */2,3 6" + assert replace_names("* * * may fri") == "* * * 5 5" + assert replace_names("* * * jan-sep,nov mon/2") == "* * * 1-9,11 1/2" + assert replace_names("* * * feb,aug,oct tue,WED,sAT") == "* * * 2,8,10 2,3,6" + assert replace_names("* * * MAR-apr thu-fri") == "* * * 3-4 4-5" + assert replace_names("* * * mAy,jun,JUL-DEC SUN/3") == "* * * 5,6,7-12 0/3" From 6128dcaf0be3054a87e8187b1ba051ce8eb651bc Mon Sep 17 00:00:00 2001 From: mhrmsn <49688144+mhrmsn@users.noreply.github.com> Date: Mon, 29 May 2023 16:09:05 +0200 Subject: [PATCH 4/4] Added validation tests for month and day of week names replacement --- test/test_valid_regex.py | 29 +++++++++++++++++++++++++++++ 1 file changed, 29 insertions(+) diff --git a/test/test_valid_regex.py b/test/test_valid_regex.py index a49e46b..af1f869 100644 --- a/test/test_valid_regex.py +++ b/test/test_valid_regex.py @@ -107,3 +107,32 @@ def test_validator_day_of_week_part(): assert_validate_successfully("* * * * */6") assert_validate_fail("* * * * */0") assert_validate_fail("* * * * */7") + + +def test_validator_month_names(): + assert_validate_successfully("* * * jan *") + assert_validate_successfully("* * * FEB,mar,Apr *") + assert_validate_successfully("* * * MAY-JUL *") + assert_validate_successfully("* * * jun-Aug *") + assert_validate_successfully("* * * Sep,Oct *") + assert_validate_successfully("* * * dec/3 *") + assert_validate_fail("* * * January *") + assert_validate_fail("* * * jan1 *") + assert_validate_fail("* * * 1jan *") + assert_validate_fail("* * * 2/feb *") + assert_validate_fail("* * * */feb *") + assert_validate_fail("* * * NOV/0 *") + + +def test_validator_day_of_week_names(): + assert_validate_successfully("* * * * sun") + assert_validate_successfully("* * * * mon,TUE,Wed") + assert_validate_successfully("* * * * FRI-SAT") + assert_validate_successfully("* * * * wed/2") + assert_validate_fail("* * * * Sunday") + assert_validate_fail("* * * * sun,mon-fri") + assert_validate_fail("* * * * tue/0") + assert_validate_fail("* * * * */mon") + assert_validate_fail("* * * * 1/wed") + assert_validate_fail("* * * * mon/mon") +