-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcal_to_ical.py
127 lines (106 loc) · 5.7 KB
/
cal_to_ical.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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
from yaml import load
from icalendar import *
import pytz, datetime, re
link = re.compile(r'\[([^\]]*)\]\(([^\)]*)\)')
def deMd(s):
return link.sub(r'\1 (\2)', s.replace('`', ''))
try:
from yaml import CLoader as Loader
except ImportError:
from yaml import Loader
def fixworking():
"""change to this script's directory as the working directory"""
import os, os.path
os.chdir(os.path.realpath(os.path.dirname(__file__)))
eastern = pytz.timezone('America/New_York')
class RealCal:
def __init__(self, name):
self.name = name
self.cal = Calendar()
self.cal.add('prodid', '-//University of Virginia//'+name+'//EN')
self.cal.add('calscale', 'GREGORIAN')
self.cal.add('version', '2.0')
self.cal.add('name', name)
def event(self, name, start, duration=None, location=None, end=None, details=None):
e = Event()
e.add('dtstamp', datetime.datetime.now(tz=eastern))
e.add('uid', name+start.isoformat()[:20].rstrip('0:T-'))
e.add('dtstart', start)
if end is not None: e.add('dtend', end)
elif duration is not None: e.add('dtend', start+duration)
e.add('summary', name)
if location is not None: e.add('location', location)
if details is not None: e.add('description', details)
self.cal.add_component(e)
def __str__(self):
return self.cal.to_ical().decode('utf-8').replace('\r\n','\n').strip()
def bytes(self):
return self.cal.to_ical()
try:
from yaml import CLoader as Loader
except ImportError:
from yaml import Loader
def calendar(data):
ans = RealCal('cs1110.s2018')
m50 = datetime.timedelta(0, 50*60)
m75 = datetime.timedelta(0, 75*60)
m180 = datetime.timedelta(0, 180*60)
m5 = datetime.timedelta(0, 5*60)
breaks = []
exams = {}
for k,v in data['Special Dates'].items():
if 'recess' in k or 'break' in k or 'Reading' in k:
if type(v) is dict: breaks.append((v['start'], v['end']))
else: breaks.append((v, v))
elif 'xam' in k:
exams[v] = k
oneday = datetime.timedelta(1)
d = data['Special Dates']['Courses begin']
classnum = 0
while d < data['Special Dates']['Courses end']:
if not any(d >= b[0] and d <= b[1] for b in breaks):
if d.weekday() in (0,2,4):
sec001 = datetime.datetime(d.year, d.month, d.day, 14, 0, 0, tzinfo=eastern)
sec002 = datetime.datetime(d.year, d.month, d.day, 12, 0, 0, tzinfo=eastern)
sec003 = datetime.datetime(d.year, d.month, d.day, 10, 0, 0, tzinfo=eastern)
sec004 = datetime.datetime(d.year, d.month, d.day, 11, 0, 0, tzinfo=eastern)
topic = exams.get(d, data['classes'][classnum])
if topic in data['reading']: topic = deMd(topic) + '\r\nSee '+deMd(' and '.join(data['reading'][topic])) +' for more'
else: topic = deMd(topic)
if d not in exams: classnum += 1
ans.event('1110-001', sec001, m50, location='WIL 402', details=topic)
ans.event('1110-002', sec002, m50, location='RICE 130', details=topic)
ans.event('1110-003', sec003, m50, location='RICE 130', details=topic)
ans.event('1110-004', sec004, m50, location='RICE 130', details=topic)
if d.weekday() != 4:
ans.event('1111', sec001, m75, location='CLRK 108')
if d.weekday() == 3:
ans.event('Lab 101', datetime.datetime(d.year, d.month, d.day, 12, 30, 0, tzinfo=eastern), m75, location='OLS 001')
ans.event('Lab 102', datetime.datetime(d.year, d.month, d.day, 9, 30, 0, tzinfo=eastern), m75, location='OLS 009')
ans.event('Lab 103', datetime.datetime(d.year, d.month, d.day, 11, 0, 0, tzinfo=eastern), m75, location='OLS 001')
ans.event('Lab 104', datetime.datetime(d.year, d.month, d.day, 14, 0, 0, tzinfo=eastern), m75, location='OLS 009')
ans.event('Lab 105', datetime.datetime(d.year, d.month, d.day, 15, 30, 0, tzinfo=eastern), m75, location='OLS 009')
ans.event('Lab 106', datetime.datetime(d.year, d.month, d.day, 17, 0, 0, tzinfo=eastern), m75, location='OLS 009')
ans.event('Lab 107', datetime.datetime(d.year, d.month, d.day, 18, 30, 0, tzinfo=eastern), m75, location='OLS 009')
ans.event('Lab 108', datetime.datetime(d.year, d.month, d.day, 20, 0, 0, tzinfo=eastern), m75, location='OLS 001')
ans.event('Lab 109', datetime.datetime(d.year, d.month, d.day, 12, 30, 0, tzinfo=eastern), m75, location='MEC 213')
ans.event('Lab 110', datetime.datetime(d.year, d.month, d.day, 15, 30, 0, tzinfo=eastern), m75, location='MEC 213')
ans.event('Lab 113', datetime.datetime(d.year, d.month, d.day, 8, 0, 0, tzinfo=eastern), m75, location='OLS 001')
d += oneday
e3 = data['Special Dates']['Exam 3']
ans.event('Final Exam', datetime.datetime(e3.year, e3.month, e3.day, 19, 00, 0, tzinfo=eastern), m180, location='TBA')
for a,v in data['assignments'].items():
if a.lower().startswith('lab') or v.get('group','').lower() == 'lab': continue
if 'due' not in v: continue
d = v['due']
if not isinstance(d, datetime.datetime):
d = datetime.datetime(d.year, d.month, d.day, 23 if v.get('group') == 'project' else 9, 55, 0, tzinfo=eastern)
ans.event(a+' due', d, m5)
return ans
if __name__ == '__main__':
fixworking()
with open('markdown/cal.yaml') as stream:
data = load(stream, Loader=Loader)
cal = calendar(data)
with open('markdown/cal.ics', 'wb') as f:
f.write(cal.bytes())