Skip to content

Commit

Permalink
Merge pull request #13 from sindrig/master
Browse files Browse the repository at this point in the history
Avoid indexerror in replace names
  • Loading branch information
kaka2507 authored Jun 19, 2023
2 parents a770e3f + f227611 commit d497fcf
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 14 deletions.
30 changes: 16 additions & 14 deletions cron_validator/util.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,10 @@
import dateutil.parser
import pytz

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)

def get_tz(tz_name):
"""
Expand Down Expand Up @@ -39,18 +43,16 @@ def replace_names(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]
)
if len(parts) > 3:
parts[3] = re.sub(
month_names_re,
lambda m: str(month_names.index(m.group().lower()) + 1),
parts[3]
)
if len(parts) > 4:
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)
2 changes: 2 additions & 0 deletions test/test_util.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,3 +42,5 @@ def test_replace_names():
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"

assert replace_names("invalid_cron") == "invalid_cron"

0 comments on commit d497fcf

Please sign in to comment.