iwla

iwla Git Source Tree

Root/display.py

1import os
2
3class DisplayHTMLRaw(object):
4
5 def __init__(self, html=''):
6 self.html = html
7
8 def setRawHTML(self, html):
9 self.html = html
10
11 def _buildHTML(self):
12 pass
13
14 def _build(self, f, html):
15 if html: f.write(html)
16
17 def build(self, f):
18 self._buildHTML()
19 self._build(f, self.html)
20
21class DisplayHTMLBlock(DisplayHTMLRaw):
22
23 def __init__(self, title=''):
24 super(DisplayHTMLBlock, self).__init__(html='')
25 self.title = title
26 self.cssclass = 'iwla_block'
27 self.title_cssclass = 'iwla_block_title'
28 self.value_cssclass = 'iwla_block_value'
29
30 def getTitle(self):
31 return self.title
32
33 def setTitle(self, value):
34 self.title = value
35
36 def setCSSClass(self, cssclass):
37 self.cssclass = cssclass
38
39 def setTitleCSSClass(self, cssclass):
40 self.title_cssclass = cssclass
41
42 def setValueCSSClass(self, cssclass):
43 self.value_cssclass = cssclass
44
45 def _buildHTML(self):
46 html = '<div class="%s">' % (self.cssclass)
47 if self.title:
48 html += '<div class="%s">%s</div>' % (self.title_cssclass, self.title)
49 html += '<div class="%s">%s</div>' % (self.value_cssclass, self.html)
50 html += '</div>'
51
52 self.html = html
53
54class DisplayHTMLBlockTable(DisplayHTMLBlock):
55
56 def __init__(self, title, cols):
57 super(DisplayHTMLBlockTable, self).__init__(title=title)
58 self.cols = cols
59 self.rows = []
60 self.cols_cssclasses = [''] * len(cols)
61 self.rows_cssclasses = []
62 self.table_css = 'iwla_table'
63
64 def appendRow(self, row):
65 self.rows.append(listToStr(row))
66 self.rows_cssclasses.append([''] * len(row))
67
68 def getCellValue(self, row, col):
69 if row < 0 or col < 0 or\
70 row >= len(self.rows) or col >= len(self.cols):
71 raise ValueError('Invalid indices %d,%d' % (row, col))
72
73 return self.rows[row][col]
74
75 def setCellValue(self, row, col, value):
76 if row < 0 or col < 0 or\
77 row >= len(self.rows) or col >= len(self.cols):
78 raise ValueError('Invalid indices %d,%d' % (row, col))
79
80 self.rows[row][col] = value
81
82 def setCellCSSClass(self, row, col, value):
83 if row < 0 or col < 0 or\
84 row >= len(self.rows) or col >= len(self.cols):
85 raise ValueError('Invalid indices %d,%d' % (row, col))
86
87 self.rows_cssclasses[row][col] = value
88
89 def getCellCSSClass(self, row, col):
90 if row < 0 or col < 0 or\
91 row >= len(self.rows) or col >= len(self.cols):
92 raise ValueError('Invalid indices %d,%d' % (row, col))
93
94 return self.rows_cssclasses[row][col]
95
96 def getColCSSClass(self, col):
97 if col < 0 or col >= len(self.cols):
98 raise ValueError('Invalid indice %d' % (col))
99
100 return self.cols_cssclasses[col]
101
102 def setRowCSSClass(self, row, value):
103 if row < 0 or row >= len(self.rows):
104 raise ValueError('Invalid indice %d' % (row))
105
106 self.rows_cssclasses[row] = [value] * len(self.rows_cssclasses[row])
107
108 def setColCSSClass(self, col, value):
109 if col < 0 or col >= len(self.cols):
110 raise ValueError('Invalid indice %d' % (col))
111
112 self.cols_cssclasses[col] = value
113
114 def setColsCSSClass(self, values):
115 if len(values) != len(self.cols):
116 raise ValueError('Invalid values size')
117
118 self.cols_cssclasses = values
119
120 def _buildHTML(self):
121 style = ''
122 if self.table_css: style = ' class="%s"' % (self.table_css)
123 html = '<table%s>' % (style)
124 if self.cols:
125 html += '<tr>'
126 for i in range (0, len(self.cols)):
127 title = self.cols[i]
128 style = self.getColCSSClass(i)
129 if style: style = ' class="%s"' % (style)
130 html += '<th%s>%s</th>' % (style, title)
131 html += '</tr>'
132 for i in range(0, len(self.rows)):
133 row = self.rows[i]
134 html += '<tr>'
135 for j in range(0, len(row)):
136 v = row[j]
137 style = self.getCellCSSClass(i, j)
138 if style: style = ' class="%s"' % (style)
139 html += '<td%s>%s</td>' % (style, v)
140 html += '</tr>'
141 html += '</table>'
142
143 self.html += html
144
145 super(DisplayHTMLBlockTable, self)._buildHTML()
146
147class DisplayHTMLBlockTableWithGraph(DisplayHTMLBlockTable):
148
149 def __init__(self, title, cols, short_titles=[], nb_valid_rows=0):
150 super(DisplayHTMLBlockTableWithGraph, self).__init__(title=title, cols=cols)
151 self.short_titles = short_titles
152 self.nb_valid_rows = nb_valid_rows
153 # TOFIX
154 self.icon_path = 'resources/icon'
155 # self.icon_path = self.iwla.getConfValue('icon_path', '/')
156 self.raw_rows = []
157 self.maxes = [0] * len(cols)
158 self.table_graph_css = 'iwla_graph_table'
159
160 def appendRow(self, row):
161 self.raw_rows.append(row)
162 super(DisplayHTMLBlockTableWithGraph, self).appendRow(row)
163
164 def appendShortTitle(self, short_title):
165 self.short_titles.append(short_title)
166
167 def setShortTitle(self, short_titles):
168 self.short_titles = short_titles
169
170 def setNbValidRows(self, nb_valid_rows):
171 self.nb_valid_rows = nb_valid_rows
172
173 def _computeMax(self):
174 for i in range(0, self.nb_valid_rows):
175 row = self.raw_rows[i]
176 for j in range(1, len(row)):
177 if row[j] > self.maxes[j]:
178 self.maxes[j] = row[j]
179
180 def _getIconFromStyle(self, style):
181 if style.startswith('iwla_page'): icon = 'vp.png'
182 elif style.startswith('iwla_hit'): icon = 'vh.png'
183 elif style.startswith('iwla_bandwidth'): icon = 'vk.png'
184 elif style.startswith('iwla_visit'): icon = 'vv.png'
185 elif style.startswith('iwla_search'): icon = 'vu.png'
186 else: return ''
187
188 return '%s/%s' % (self.icon_path, icon)
189
190 def _buildHTML(self):
191 self._computeMax()
192
193 style = ''
194 if self.table_graph_css: style = ' class="%s"' % (self.table_graph_css)
195 html = '<table%s>' % (style)
196 html += '<tr>'
197 for i in range(0, self.nb_valid_rows):
198 row = self.rows[i]
199 html += '<td>'
200 for j in range(1, len(row)):
201 style = self.getColCSSClass(j)
202 icon = self._getIconFromStyle(style)
203 if not icon: continue
204 if style: style = ' class="%s"' % (style)
205 alt = '%s: %s' % (row[j], self.cols[j])
206 if self.maxes[j]:
207 height = int((self.raw_rows[i][j] * 100) / self.maxes[j])
208 else:
209 height = 0
210 html += '<img%s align="bottom" src="%s" height="%d" width="6" alt="%s" title="%s" />' % (style, icon, height, alt, alt)
211 html += '</td>'
212 html += '</tr>'
213 html += '<tr>'
214 for i in range(0, len(self.short_titles)):
215 style = self.getCellCSSClass(i, 0)
216 if style: style = ' class="%s"' % (style)
217 html += '<td%s>%s</td>' % (style, self.short_titles[i])
218 html += '</tr>'
219 html += '</table>'
220
221 self.html += html
222
223 super(DisplayHTMLBlockTableWithGraph, self)._buildHTML()
224
225class DisplayHTMLPage(object):
226
227 def __init__(self, title, filename, css_path):
228 self.title = title
229 self.filename = filename
230 self.blocks = []
231 self.css_path = css_path
232
233 def getFilename(self):
234 return self.filename;
235
236 def getBlock(self, title):
237 for b in self.blocks:
238 if title == b.getTitle():
239 return b
240 return None
241
242 def appendBlock(self, block):
243 self.blocks.append(block)
244
245 def build(self, root):
246 filename = root + self.filename
247
248 base = os.path.dirname(filename)
249 if not os.path.exists(base):
250 os.makedirs(base)
251
252 f = open(filename, 'w')
253 f.write('<!DOCTYPE html>')
254 f.write('<html>')
255 f.write('<head>')
256 f.write('<meta http-equiv="Content-type" content="text/html; charset=UTF-8" />')
257 for css in self.css_path:
258 f.write('<link rel="stylesheet" href="%s"/>' % (css))
259 if self.title:
260 f.write('<title>%s</title>' % (self.title))
261 f.write('</head>')
262 for block in self.blocks:
263 block.build(f)
264 f.write('</body></html>')
265 f.close()
266
267class DisplayHTMLBuild(object):
268
269 def __init__(self, iwla):
270 self.pages = []
271 self.iwla = iwla
272
273 def getPage(self, filename):
274 for page in self.pages:
275 if page.getFilename() == filename:
276 return page
277 return None
278
279 def addPage(self, page):
280 self.pages.append(page)
281
282 def build(self, root):
283 display_root = self.iwla.getConfValue('DISPLAY_ROOT', '')
284 for res_path in self.iwla.getResourcesPath():
285 target = os.path.abspath(res_path)
286 link_name = os.path.join(display_root, res_path)
287 if not os.path.exists(link_name):
288 os.symlink(target, link_name)
289
290 for page in self.pages:
291 page.build(root)
292
293def bytesToStr(bytes):
294 suffixes = ['', ' kB', ' MB', ' GB', ' TB']
295
296 for i in range(0, len(suffixes)):
297 if bytes < 1024: break
298 bytes /= 1024.0
299
300 if i:
301 return '%.02f%s' % (bytes, suffixes[i])
302 else:
303 return '%d%s' % (bytes, suffixes[i])
304
305def _toStr(v):
306 if type(v) != str: return str(v)
307 else: return v
308
309def listToStr(l): return map(lambda(v) : _toStr(v), l)

Archive Download this file

Branches

Tags