-
Notifications
You must be signed in to change notification settings - Fork 1
/
RoutineReader.py
153 lines (134 loc) · 4.63 KB
/
RoutineReader.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
import xml.etree.ElementTree as ET
import CursesWindow
import sys
import datetime
GetNumber = CursesWindow.GetNumber
DaysList = [ "Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday" ]
def TwoDigits(x):
return GetNumber(x).rjust(2, GetNumber(0))
def GetTime(hr, mn):
mnstr = ":" + TwoDigits(mn)
if CursesWindow.Hour24:
return TwoDigits(hr) + mnstr
if hr > 12:
return TwoDigits(hr-12) + mnstr + " PM"
if hr == 12:
return TwoDigits(hr) + mnstr + " PM"
return TwoDigits(hr) + mnstr + " AM"
def GetTimeString(group, day, timeId):
if not day in group:
return "not found"
times = group[day]
if timeId >= len(times):
return ""
time = times[timeId]
string = GetTime(time["start"]["hr"], time["start"]["min"]) \
+ " - " + \
GetTime(time["end"]["hr"], time["end"]["min"])
return string
def ParseTimeStr(string):
time = {}
strings = string.split(':')
if (strings[0]!=''):
time["hr"] = int(strings[0])
else:
time["hr"] = 0
if len(strings) == 2:
time["min"] = int(strings[1])
else:
time["min"] = 0
return time
class RoutineReader:
def __init__(self):
self.tables = []
def ReadFile(self, filename):
tree = ET.parse(filename)
routineX = tree.getroot()
if routineX.tag != 'Routine':
return
self.tables = []
for tableX in routineX:
if tableX.tag != 'Table':
continue
table = {}
for groupX in tableX:
if groupX.tag != 'Group' or not 'id' in groupX.attrib:
continue
group = {}
for dayX in groupX:
day = []
for timeX in dayX:
if timeX.tag != 'Time' or not 'start' in timeX.attrib or not 'end' in timeX.attrib:
continue
time = {}
time["start"] = ParseTimeStr(timeX.attrib['start'])
time["end"] = ParseTimeStr(timeX.attrib['end'])
day.append(time)
group[dayX.tag] = day
table[int(groupX.attrib['id'])] = group
self.tables.append(table)
def GetDay(self, day, group):
days = self.tables[0][group]
return days[DaysList[day]]
def GetRoutine(self):
tableId=1
padding = 17
if not CursesWindow.Hour24:
padding = 22
strings = [["LoadShedding Schedule"]]
for table in self.tables:
strings.append([""])
strings.append(["Table #"+GetNumber(tableId)+":"])
headings = ["Group".center(6)]
for day in DaysList:
headings.append(day.center(padding))
strings.append(headings)
#strings.append([""])
for groupId, group in table.items():
temp = GetNumber(groupId)
timeId = 0
while True:
substr = [temp.center(6)]
for day in DaysList:
substr.append(GetTimeString(group, day, timeId).center(padding))
if not any(':' in string for string in substr):
strings.append([""])
break
strings.append(substr)
timeId += 1
temp = " "
tableId += 1
return strings
def main(filename, group = -1, nepali = False, twelveHr = False):
if group > -1:
CursesWindow.Group = group
if nepali:
CursesWindow.Language = CursesWindow.Nepali
if twelveHr:
CursesWindow.Hour24 = False
reader = RoutineReader()
reader.ReadFile(filename)
strings = reader.GetRoutine()
width = 126
if not CursesWindow.Hour24:
width = 161
height = len(strings) + 1
day = (datetime.datetime.today().weekday() + 1) % 7 + 1
heads = []
highlightCells = []
groups = []
for i in range(0, len(strings)):
if any(':' in string for string in strings[i]) and len(strings[i]) > 1:
highlightCells.append([i, day])
if not strings[i][0].isspace():
groups.append(i)
elif any('Group' in string for string in strings[i]):
heads.append(i);
window = CursesWindow.CursesWindow(strings, width, height, highlightCells, heads, groups)
if window.refresh:
main(filename)
if __name__ == "__main__":
filename = "routine.xml"
if len(sys.argv) > 1:
filename = sys.argv[1]
main(filename)