-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathp01.py
197 lines (154 loc) · 5.62 KB
/
p01.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
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
import openpyxl
import datetime
import zoneinfo
import icalendar
import sys
import os
OK = 0
ENOENT = 2
class MeetingPattern:
def __init__(self, start, end, until, location, weekdays):
self.start_time = start
self.end_time = end
self.until_date = until
self.location = location
self.weekdays = weekdays
def print_patterns(self):
print("Class starts on:", self.start_time)
print("Class ends at:", self.end_time)
print("Last class is on:", self.until_date)
print("Weekdays:", self.weekdays)
class CourseSection:
def __init__(self, name, meeting_patterns):
self.name = name;
self.meeting_patterns = meeting_patterns;
def print_info(self):
print("Course section:", self.name)
for i in range (0, len(self.meeting_patterns)):
self.meeting_patterns[i].print_patterns()
print()
def parse_section_name(name):
slice_end = str(name).find(" - ")
if slice_end < 0:
print("Error: Section name not recognized")
return 0;
section_name = name[0:slice_end]
return section_name
def parse_section_meeting_patterns(mp_string):
patterns = []
lines = str(mp_string).split("\n")
for line in range(0, len(lines)):
substrings = lines[line].split("|")
if len(substrings) == 1:
# Empty line
continue
date_separator = substrings[0].index("- ") - 1
start_date = datetime.datetime.fromisoformat(
substrings[0][0:date_separator]
)
end_date = datetime.datetime.fromisoformat(
substrings[0][0:date_separator]
)
until_date = datetime.datetime.fromisoformat(
substrings[0][date_separator + 3:len(substrings[0]) - 1])
weekdays = []
days = substrings[1].lstrip().rstrip().split(" ")
for day in days:
weekdays.append(day[0:2].lower())
times = substrings[2].split(" - ")
start_hours = int(times[0][1:times[0].index(":")])
if times[0].find("p.m.") >= 0 and start_hours != 12:
start_hours += 12
start_minutes = int(
times[0][times[0].index(":") + 1:times[0].index(":") + 3]
)
end_hours = int(times[1][0:times[1].index(":")])
if times[1].find("p.m.") >= 0 and end_hours != 12:
end_hours += 12
end_minutes = int(
times[1][times[1].index(":") + 1:times[1].index(":") + 3]
)
start_date = start_date.replace(
hour = start_hours, minute = start_minutes,
tzinfo=zoneinfo.ZoneInfo("America/Vancouver")
)
end_date = end_date.replace(
hour = end_hours, minute = end_minutes,
tzinfo=zoneinfo.ZoneInfo("America/Vancouver")
)
until_date = until_date.replace(
hour = end_hours, minute = end_minutes,
tzinfo=zoneinfo.ZoneInfo("America/Vancouver")
)
location = substrings[3].lstrip().rstrip()
meeting_pattern = MeetingPattern(
start_date,
end_date,
until_date,
location,
weekdays
)
patterns.append(meeting_pattern)
return patterns
def parse_sections(sheet, sections):
# Data starts in Row 4
for row in range(4, sheet.max_row + 1):
# Sections names are in column 4 (D)
cell = sheet.cell(row = row, column = 4)
section_name = parse_section_name(cell.value)
if section_name == 0:
continue;
# Meeting patterns are in column 10 (J)
cell = sheet.cell(row = row, column = 10)
mp_string = cell.value
meeting_patterns = parse_section_meeting_patterns(mp_string)
section = CourseSection(section_name, meeting_patterns)
sections.append(section)
def gen_ics(sections):
cal = icalendar.Calendar()
cal.add('prodid', '-//henry.rov//P01_THREEHUNDRED//EN')
cal.add('version', '2.0')
for section in sections:
for pattern in section.meeting_patterns:
event = icalendar.Event()
event.add("summary", section.name);
event.add("dtstart", pattern.start_time)
event.add("dtstamp", datetime.datetime.utcnow())
event.add("dtend", pattern.end_time)
rrule = icalendar.vRecur(
freq = "WEEKLY",
until = pattern.until_date,
byday = pattern.weekdays
)
event.add("rrule", rrule)
event.add("location", pattern.location)
cal.add_component(event)
cal_file = open("out.ics", "wb")
cal_file.write(cal.to_ical())
cal_file.close()
def main():
if len(sys.argv) < 2:
print("No file to operate on")
return ENOENT
filename = sys.argv[1]
sheet = openpyxl.load_workbook(filename).active
sections = []
parse_sections(sheet, sections)
for i in range(0, len(sections)):
sections[i].print_info()
print("Verify the information above.")
while 1:
print("Create .ics? [Y/n]")
confirm_string = input()
if confirm_string == "N" or confirm_string == "n":
print("Cancelled.")
return OK
elif (confirm_string == "Y" or confirm_string == "y"
or confirm_string == "" or confirm_string.isspace()):
gen_ics(sections)
print("Done.")
print("Output written to out.ics.")
return OK
else:
print("Invalid input. Try again")
main()