Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Avoid indexerror in replace names #13

Merged
merged 1 commit into from
Jun 19, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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"