-
Notifications
You must be signed in to change notification settings - Fork 8
/
tt_prettyprinter.py
163 lines (142 loc) · 6.08 KB
/
tt_prettyprinter.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
# -*- coding: cp1252 -*-
import string
from tt_generator import *
class HTMLPrettyPrinter:
def __init__(self):
self.course_colors = {}
self.color_gen = color_generator()
def color(self, course_name):
if not course_name in self.course_colors.keys():
self.course_colors[course_name] = self.color_gen.next()
return self.course_colors[course_name]
def print_timetables(self, timetables, total_combinations):
table_id = 0
print intro(len(timetables), total_combinations)
for timetable in timetables:
table_id += 1
print self.format_timetable(timetable, table_id)
print outro()
def format_timetable(self, timetable, table_id):
# initialize content for each cell
content = [["class='period-empty-slot' > " \
for x in xrange(32)] \
for y in xrange(6)]
# fill out the content according to the lesson slots
for slot in timetable.lessons:
day_i = slot.day
start_i = time_index(slot.start)
end_i = time_index(slot.end)
content[day_i][start_i] = "class='period-first-slot' "
content[day_i][end_i-1] = "class='period-last-slot' "
for time_i in xrange(start_i+1, end_i-1):
content[day_i][time_i] = "class='period-middle-slot' "
for time_i in xrange(start_i, end_i):
content[day_i][time_i] += "style='background-color: %s' " \
% (self.color(slot.course_name()))
content[day_i][time_i] += "headers='weekday%i hour%i' " \
% (day_i, time_i)
content[day_i][time_i] += "title='%s-%s'> " \
% (slot.start, slot.end)
content[day_i][start_i] += "%s (%s) %s" \
% (slot.course_name(), slot.lesson_category(), slot.room)
# format the HTML for the timetable
html_result = [table_intro(table_id)]
for row in xrange(0,32):
html_result.append(" <tr>\n")
html_result.append(" <th class='period-hours' id='hour%i'>%s-%s</th>\n" % (row, time_from_index(row), time_from_index(row+1)))
for column in xrange(0,6):
html_result.append(" <td %s</td>\n" % (content[column][row]))
html_result.append(" </tr>\n")
html_result.append(table_outro())
return string.join(html_result, '')
def time_index(time):
return (time.minutes - 8*60) / 30
def time_from_index(index):
return Time(index/2 + 8, (index%2) * 30)
def color_generator():
pallete = [ "#A8FFFF", "#AFEEEE", "#00FFFF", "#87CEFA", "#A8D4FF", \
"#B0C4DE", "#BAEDD3", "#7FFFD4", "#51FFA9", "#40E0D0", \
"#D8BAED", "#ADFF2F", "#32CD32", "#00FF7F", "#F9A8FF", \
"#FFBA51", "#FF9A00", "#FFA07A", "#F7AFB3", "#FFC0A8", \
"#FFC0CB", "#FFD700", "#FFDEAD", "#FFECA8", "#F0E68C", \
"#FFFFFF", "#F0FFF0", "#F0FFFF", "#F8F8FF", "#F5F5F5", \
"#FFF5EE", "#F5F5DC", "#FFFFF0", "#FDF5E6", "#FAEBD7", \
"#FFE4E1", "#FAF0E6"]
for color in pallete:
yield color
def intro(total_selected, total_combinations):
return """\
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=cp1252">
<title>Horários gerados</title>
<link rel='stylesheet' type='text/css' media='screen' href='https://fenix.tecnico.ulisboa.pt/cursos/meic-a/static/css/publicPages.css' />
<link rel='stylesheet' type='text/css' media='screen,print' href='http://fenix.ist.utl.pt/CSS/dotist_timetables.css' />
<script type='text/javascript'>
var current = 1;
var total = %i;
function showTable(i) {
document.getElementById('tt'+i).style.display = 'block';
}
function hideTable(i) {
document.getElementById('tt'+i).style.display = 'none';
}
function updateLabel() {
document.getElementById('label').innerHTML = current+'/'+total;
}
function goRight() {
hideTable(current);
current = (current+total)%%total+1;
showTable(current);
updateLabel();
}
function goLeft() {
hideTable(current);
current = (current+total-2)%%total+1;
showTable(current);
updateLabel();
}
</script>
</head>
<body>
<p style='text-align: center'>
<span id='label' style='font: bold 20px verdana'></span><br>
<span style='%sfont: 10px verdana'>(A mostrar os 100 horários mais compactos dum total de %i combinações)</span>
</p>
<table border='0'>
<tr>
<td style='width: 50px'> <img src='left.png' onclick='goLeft()'> </td>
<td style='width: 100%%'>
""" % (total_selected, "" if (total_selected < total_combinations) else "display:none;", total_combinations)
def table_intro(id_number):
return """\
<div style='display: none' id='tt%i' class='mtop15'>
<table class='timetable' style='margin-left: auto; margin-right: auto' cellspacing='0' cellpadding='0' width='98%%'>
<tr>
<th>Horas/Dias</th>
<th colspan='1' width='14%%' id='weekday0'>Segunda</th>
<th colspan='1' width='14%%' id='weekday1'>Terça</th>
<th colspan='1' width='14%%' id='weekday2'>Quarta</th>
<th colspan='1' width='14%%' id='weekday3'>Quinta</th>
<th colspan='1' width='14%%' id='weekday4'>Sexta</th>
<th colspan='1' width='14%%' id='weekday5'>Sábado</th>
</tr>
""" % (id_number)
def table_outro():
return """\
</table>
</div>
"""
def outro():
return """\
</td>
<td style='width: 50px'> <img src='right.png' onclick='goRight()'> </td>
</tr>
</table>
<script type='text/javascript'>
showTable(current);
updateLabel();
</script>
</body>
</html>"""