-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcreate_weeks.py
executable file
·55 lines (45 loc) · 1.69 KB
/
create_weeks.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
#!/usr/bin/env python
# WEEKLY SCHEDULE CREATOR FOR TIFMASTER
#
# Accepts a template schedule for the first week of term, and creates
# weekly schedules up to a given end date, advancing the days appropriately
# but leaving the hours assignments unchanged.
# FIXME This is terribly non-idiomatic. Separate dates and hours
# lists are silly; we can just pass a value of type [(date,hours)]
# around, extend it to enddate, then write that.
from tif import from_iso
from datetime import date,timedelta
import sys
def read_proto(filename):
dates=[]
hours=[]
with open(filename,'r') as f:
for line in f:
line=line.strip()
if line.startswith('#'): continue
if line=='': continue
date,all_hours = line.split(None, 1)
dates.append(from_iso(date))
hours.append(all_hours)
return (dates,hours)
def write_files(dates,hours,enddate):
while dates[0] <= enddate:
filename = 'week_of_%s.txt' % dates[0].isoformat()
with open(filename,'w') as f:
print filename
for i in range(0,7):
f.write("%s %s\n" % (dates[i], hours[i]))
dates = next_week(dates)
def next_week(dates):
return [d + timedelta(days=7) for d in dates]
if __name__=='__main__':
if len(sys.argv) != 3:
print "%s [first week schedule.txt] [end date]" % sys.argv[0]
print " Outputs files with the names 'week_of_YYYY-MM-DD.txt'."
else:
our_name, proto_fn, enddate = sys.argv
dates,hours = read_proto(proto_fn)
if len(dates) != 7:
print "This is not a weekly file, aborting"
exit(-1)
write_files(dates,hours,from_iso(enddate))