generated from just-the-docs/just-the-docs-template
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgen_schedule.py
executable file
·79 lines (66 loc) · 2.5 KB
/
gen_schedule.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
#!/usr/bin/env python3
import datetime
import argparse
import re
def parse_date(s):
try:
return datetime.datetime.strptime(s, '%m-%d-%Y')
except ValueError:
raise argparse.ArgumentTypeError('Invalid date: {}'.format(s))
def format_date(fmt_str, date):
out = fmt_str
out = re.sub(r'<MONTH>', str(date.month), out)
out = re.sub(r'<DAY>', str(date.day), out)
out = re.sub(r'<YEAR>', str(date.year), out)
out = re.sub(r'<DAY_NAME>', date.strftime('%A'), out)
out = re.sub(r'<MONTH_NAME>', date.strftime('%B'), out)
out = re.sub(r'<BR>', '\n', out)
return out
def generate(args):
start_date = args.start
end_date = args.end
cur_d = args.start
week = 0
start_day = cur_d.weekday()
offsets = []
days_sorted = 'MTWRFsS'
for i in range(7):
d = (start_day + i) % 7
if days_sorted[d] in args.days:
offsets.append(i)
offset = 0
week = 0
cur_d = args.start
while True:
cur_d = start_date + \
datetime.timedelta(weeks=week, days=offsets[offset])
offset = (offset + 1) % len(offsets)
if offset == 0:
week += 1
if cur_d > end_date:
break
print(format_date(args.template, cur_d))
def main():
parser = argparse.ArgumentParser(
prog='gen_schedule.py',
description='generate list of class days')
parser.add_argument('-s', '--start', type=parse_date, required=True,
help='start date of semester MM-DD-YYYY')
parser.add_argument('-e', '--end', type=parse_date, required=True,
help='end date of semester MM-DD-YYYY')
parser.add_argument('-d', '--days', type=str, required=True,
help='string representing days of week (SMTWRFs) for class (e.g., MWF, TR, etc.).')
parser.add_argument('-t', '--template', type=str,
default=("- month_name: <MONTH_NAME><BR>"
" month: <MONTH><BR>"
" day_name: <DAY_NAME><BR>"
" day: <DAY><BR>"
" topic: <BR>"
" reading: <BR>"
" assignment: <BR>"),
help=("template for printing days placeholders: <MONTH>, "
"<DAY>, <YEAR>, <MONTH_NAME>, <DAY_NAME>, <BR>"))
args = parser.parse_args()
generate(args)
if __name__ == '__main__':
main()