Skip to content

Commit

Permalink
Merge pull request #12 from mhrmsn/names
Browse files Browse the repository at this point in the history
Allow month and day of week names
  • Loading branch information
kaka2507 authored Jun 12, 2023
2 parents 7b76984 + 6128dca commit 295d8f0
Show file tree
Hide file tree
Showing 4 changed files with 66 additions and 2 deletions.
25 changes: 25 additions & 0 deletions cron_validator/util.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import datetime
import re

import dateutil.parser
import pytz
Expand Down Expand Up @@ -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"(?<![\d\/])({'|'.join(month_names)})(?!\d)", re.IGNORECASE)
day_of_week_names_re = re.compile(rf"(?<![\d\/])({'|'.join(day_of_week_names)})(?!\d)", re.IGNORECASE)
parts[3] = re.sub(
month_names_re,
lambda m: str(month_names.index(m.group().lower()) + 1),
parts[3]
)
parts[4] = re.sub(
day_of_week_names_re,
lambda m: str(day_of_week_names.index(m.group().lower())),
parts[4]
)
return " ".join(parts)
3 changes: 2 additions & 1 deletion cron_validator/validator.py
Original file line number Diff line number Diff line change
@@ -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:
Expand All @@ -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")
Expand Down
11 changes: 10 additions & 1 deletion test/test_util.py
Original file line number Diff line number Diff line change
@@ -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"
Expand Down Expand Up @@ -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"
29 changes: 29 additions & 0 deletions test/test_valid_regex.py
Original file line number Diff line number Diff line change
Expand Up @@ -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")

0 comments on commit 295d8f0

Please sign in to comment.