Last active
March 8, 2020 19:11
-
-
Save zdxerr/51860696b49b49834f0e5a26f5bcd160 to your computer and use it in GitHub Desktop.
Cron Job Scheduler Syntax Parser
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
""" | |
""" | |
import calendar | |
import locale | |
import yaml | |
import nltk | |
import re | |
import datetime | |
locale.setlocale(locale.LC_ALL, 'en_US.UTF-8') | |
# schedule = { | |
# "schedule": "every monday, tuesday, wednesday, thursday, friday 09:00" | |
# } | |
# print(yaml.dump(schedule)) | |
""" | |
tasks: | |
- clone: ... | |
- install: ... | |
- | |
check folde existence? | |
""" | |
configuration_str = """ | |
schedule: every Monday,Tuesday, Thursday, Friday 20:00 | |
--- | |
schedule: next Friday 20:00 | |
--- | |
schedule: 2020-05-02 10:00 | |
""" | |
weekdays = list(calendar.day_name) | |
# print(list(calendar.day_abbr)) | |
PREFIX = "every" | |
def schedule_parser(schedule_str): | |
parts = { | |
"type": r"^every|^next", | |
# "date": r"([12]\d{3}-(0[1-9]|1[0-2])-(0[1-9]|[12]\d|3[01]))", | |
"comma": r",", | |
"day": "|".join(weekdays), | |
"time": r"([0-1][0-9]|[2][0-3]):([0-5][0-9])", | |
"whitespace": r"\s+", | |
"missmatch": r"." | |
} | |
expr = re.compile('|'.join(f"(?P<{name}>{expr})" for name, expr in parts.items())) | |
tokens = expr.finditer(schedule_str) | |
# Schedule type A every day at time | |
schedule_type = None | |
days = [] | |
time = None | |
for token in tokens: | |
kind = token.lastgroup | |
value = token.group() | |
column = token.start() | |
# print(kind, value, column) | |
# if token.isspace(): | |
# continue | |
# if schedule_type is None: | |
if kind in ("whitespace", "comma", "date"): | |
continue | |
elif kind == "type": | |
schedule_type = value | |
elif kind == "day": | |
days.append(value) | |
elif kind == "time": | |
time = value | |
# print(kind, value, column) | |
else: | |
raise Exception(f"Invalid token \"{value}\" ({kind}) found at column {column}") | |
# print(kind, value, column) | |
return schedule_type, days, time | |
def next_date(day, time): | |
today = datetime.datetime.today() | |
date = today + datetime.timedelta(days=7 - today.weekday() + weekdays.index(day)) | |
hour, minute = map(int, time.split(":")) | |
date = date.replace(hour=hour, minute=minute, second=0, microsecond=0) | |
return date | |
if __name__ == "__main__": | |
for configuration in yaml.safe_load_all(configuration_str): | |
schedule = configuration["schedule"] | |
schedule_type, days, time = schedule_parser(schedule) | |
break | |
today = datetime.datetime.today() | |
last_monday = today - datetime.timedelta(days=today.weekday()) | |
coming_monday = today + datetime.timedelta(days=-today.weekday(), weeks=1) | |
# print("Today:", today) | |
# print("Weekday:", today.weekday()) | |
# print("Last Monday:", last_monday) | |
# print("Coming Monday:", coming_monday) | |
# print(calendar.day_name._days) | |
for day in days: | |
date = next_date(day, time) | |
print(day, date) | |
print(today < date) | |
# print(date) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment