-
Notifications
You must be signed in to change notification settings - Fork 4
/
parse_excel.py
159 lines (135 loc) · 5.12 KB
/
parse_excel.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
from pathlib import Path
from openpyxl import load_workbook
from utils import config, read_json, to_title, write_json
def iter_rows(wb, column_map):
"""Generator for all the rows in the workbook"""
for sheet in wb:
rows = sheet.rows
next(rows)
next(rows)
for row in rows:
if not row:
continue
yield {name: row[col].value for name, col in column_map.items()}
def parse_main_tt(file_path: Path):
COLUMNS = { # 0 indexed column indices
"c_num": 1,
"c_title": 2,
"sec_num": 6,
"instr_name": 7,
"days": 8,
"hours": 9,
"midsem": 10,
"compre": 11,
}
workbook = load_workbook(file_path, read_only=True)
course_db = {}
for data in iter_rows(workbook, COLUMNS):
if not data["instr_name"]:
continue # blank row
# new Course
if data["c_num"]:
course = {
"name": to_title(data["c_title"]),
"sections": {},
}
if data["compre"]:
date, sess = data["compre"].split()
course["compre"] = {"date": date, "session": sess}
if data["midsem"]:
date, time = data["midsem"].split("\n")
course["midsem"] = {"date": date.strip(), "time": time.strip()}
course_db[data["c_num"]] = course # add to course
sec_type = "L"
sec_num_counter = 1
# new Tutorial or Practical section
if not data["c_num"] and data["c_title"]:
sec_type = data["c_title"][0]
sec_num_counter = 1
# new Section
if (
data["instr_name"]
and data.get("room", True)
and not sec_type == "L"
or data["sec_num"]
) or data["c_title"]:
sec_num = int(data["sec_num"] or sec_num_counter)
section = {"instructors": [], "sched": []}
course["sections"][sec_type + str(sec_num)] = section
sec_num_counter += 1
instructors = set() # keep track of unique instructors
if isinstance(data.get("hours"), (float, int)):
data["hours"] = str(int(data["hours"]))
if data.get("days"):
hours = []
for hour in map(int, data["hours"].split()):
if hour > 15:
hours.extend(map(int, str(hour)))
else:
hours.append(hour)
days = data["days"].split()
sched = {"room": data.get("room", ""), "days": days}
if len(hours) == hours[-1] - hours[0] + 1: # continuous hours
section["sched"].append(dict(**sched, hours=hours))
else:
for hour in hours: # separate sched for each hour
section["sched"].append(dict(**sched, hours=(hour,)))
if data["instr_name"].lower() not in instructors:
section["instructors"].append(data["instr_name"])
instructors.add(data["instr_name"].lower())
return course_db
def parse_midsem(file_path: Path):
COLUMNS = {"c_num": 1, "c_title": 2, "date": 3, "time": 4}
workbook = load_workbook(file_path, read_only=True)
midsem = {}
for row in iter_rows(workbook, COLUMNS):
if "*" in row["time"]:
continue
c_dept, c_num = row["c_num"].strip().split()
midsem[c_dept.strip() + " " + c_num.strip()] = {
"date": row["date"].strip(),
"time": row["time"].strip(),
}
return midsem
def parse_files(tt_file: Path, midsem_file: Path):
timetable = parse_main_tt(tt_file)
if midsem_file.is_file():
midsem = parse_midsem(midsem_file)
for k, v in midsem.items():
timetable[k]["midsem"] = v
else:
print(f"File '{midsem_file}' not found. Midsem details will not be added.")
return timetable
class CourseDB:
tt_file = Path(config["COURSES"]["tt_file"])
midsem_file = Path(config["COURSES"].get("midsem_file", ""))
_course_db = None
_timetable = None
def __new__(cls, *args, **kwargs):
if not cls._course_db:
cls._course_db = super().__new__(cls, *args, **kwargs)
return cls._course_db
def get_timetable(self, force_parse=False):
if not self.tt_file.exists():
raise FileNotFoundError(self.tt_file)
json_file = self.tt_file.with_suffix(".json")
if not json_file.exists() or (force_parse and self.tt_file.suffix != '.json'):
self._timetable = parse_files(self.tt_file, self.midsem_file)
write_json(json_file, self._timetable)
else:
self._timetable = read_json(json_file)
return self._timetable
def __getitem__(self, course_code):
if not self._timetable:
self._timetable = self.get_timetable()
return self._timetable.get(course_code)
@property
def timetable(self):
if not self._timetable:
self._timetable = self.get_timetable()
return self._timetable
course_db = CourseDB()
def main():
CourseDB().get_timetable(True)
if __name__ == "__main__":
main()