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 = ['' for e in cols]
61 self.rows_cssclasses = []
62
63 def appendRow(self, row):
64 self.rows.append(listToStr(row))
65 self.rows_cssclasses.append(['' for e in row])
66
67 def getCellValue(self, row, col):
68 if row < 0 or col < 0 or\
69 row >= len(self.rows) or col >= len(self.cols):
70 raise ValueError('Invalid indices %d,%d' % (row, col))
71
72 return self.rows[row][col]
73
74 def setCellValue(self, row, col, value):
75 if row < 0 or col < 0 or\
76 row >= len(self.rows) or col >= len(self.cols):
77 raise ValueError('Invalid indices %d,%d' % (row, col))
78
79 return self.rows[row][col]
80
81 def setCellCSSClass(self, row, col, value):
82 if row < 0 or col < 0 or\
83 row >= len(self.rows) or col >= len(self.cols):
84 raise ValueError('Invalid indices %d,%d' % (row, col))
85
86 self.rows_cssclasses[row][col] = value
87
88 def getCellCSSClass(self, row, col):
89 if row < 0 or col < 0 or\
90 row >= len(self.rows) or col >= len(self.cols):
91 raise ValueError('Invalid indices %d,%d' % (row, col))
92
93 return self.rows_cssclasses[row][col]
94
95 def getColCSSClass(self, col):
96 if col < 0 or col >= len(self.cols):
97 raise ValueError('Invalid indice %d' % (col))
98
99 return self.cols_cssclasses[col]
100
101 def setRowCSSClass(self, row, value):
102 if row < 0 or row >= len(self.rows):
103 raise ValueError('Invalid indice %d' % (row))
104
105 for i in range(0, self.rows_cssclasses[row]):
106 self.rows_cssclasses[row][i] = value
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 html = '<table>'
122 if self.cols:
123 html += '<tr>'
124 for i in range (0, len(self.cols)):
125 title = self.cols[i]
126 style = self.getColCSSClass(i)
127 if style: style = ' class="%s"' % (style)
128 html += '<th%s>%s</th>' % (style, title)
129 html += '</tr>'
130 for i in range(0, len(self.rows)):
131 row = self.rows[i]
132 html += '<tr>'
133 for j in range(0, len(row)):
134 v = row[j]
135 style = self.getCellCSSClass(i, j)
136 if style: style = ' class="%s"' % (style)
137 html += '<td%s>%s</td>' % (style, v)
138 html += '</tr>'
139 html += '</table>'
140
141 self.html += html
142
143 super(DisplayHTMLBlockTable, self)._buildHTML()
144
145class DisplayHTMLBlockTableWithGraph(DisplayHTMLBlockTable):
146
147 def __init__(self, title, cols, short_titles=[], nb_valid_rows=0):
148 super(DisplayHTMLBlockTableWithGraph, self).__init__(title=title, cols=cols)
149 self.short_titles = short_titles
150 self.nb_valid_rows = nb_valid_rows
151 # TOFIX
152 self.icon_path = '/resources/icon'
153 # self.icon_path = self.iwla.getConfValue('icon_path', '/')
154 self.raw_rows = []
155 self.maxes = [0 for c in cols]
156
157 def appendRow(self, row):
158 self.raw_rows.append(row)
159 super(DisplayHTMLBlockTableWithGraph, self).appendRow(row)
160
161 def appendShortTitle(self, short_title):
162 self.short_titles.append(short_title)
163
164 def setShortTitle(self, short_titles):
165 self.short_titles = short_titles
166
167 def setNbValidRows(self, nb_valid_rows):
168 self.nb_valid_rows = nb_valid_rows
169
170 def _computeMax(self):
171 for i in range(0, self.nb_valid_rows):
172 row = self.raw_rows[i]
173 for j in range(1, len(row)):
174 if row[j] > self.maxes[j]:
175 self.maxes[j] = row[j]
176
177 def _getIconFromStyle(self, style):
178 if style.startswith('iwla_page'): icon = 'vp.png'
179 elif style.startswith('iwla_hit'): icon = 'vh.png'
180 elif style.startswith('iwla_bandwidth'): icon = 'vk.png'
181 elif style.startswith('iwla_visit'): icon = 'vv.png'
182 elif style.startswith('iwla_search'): icon = 'vu.png'
183 else: return ''
184
185 return '%s/%s' % (self.icon_path, icon)
186
187 def _buildHTML(self):
188 self._computeMax()
189
190 html = '<table>'
191 html += '<tr>'
192 for i in range(0, self.nb_valid_rows):
193 row = self.rows[i]
194 html += '<td>'
195 for j in range(1, len(row)):
196 style = self.getColCSSClass(j)
197 icon = self._getIconFromStyle(style)
198 if not icon: continue
199 if style: style = ' class="%s"' % (style)
200 alt = '%s: %s' % (row[j], self.cols[j])
201 if self.maxes[j]:
202 height = int((self.raw_rows[i][j] * 100) / self.maxes[j])
203 else:
204 height = 0
205 html += '<img%s align="bottom" src="%s" height="%d" width="6" alt="%s" title="%s" />' % (style, icon, height, alt, alt)
206 html += '</td>'
207 html += '</tr>'
208 html += '<tr>'
209 for i in range(0, len(self.short_titles)):
210 style = self.getCellCSSClass(0, j)
211 if style: style = ' class="%s"' % (style)
212 html += '<td%s>%s</td>' % (style, self.short_titles[i])
213 html += '</tr>'
214 html += '</table>'
215
216 self.html += html
217
218 super(DisplayHTMLBlockTableWithGraph, self)._buildHTML()
219
220class DisplayHTMLPage(object):
221
222 def __init__(self, title, filename, css_path):
223 self.title = title
224 self.filename = filename
225 self.blocks = []
226 self.css_path = css_path
227
228 def getFilename(self):
229 return self.filename;
230
231 def getBlock(self, title):
232 for b in self.blocks:
233 if title == b.getTitle():
234 return b
235 return None
236
237 def appendBlock(self, block):
238 self.blocks.append(block)
239
240 def build(self, root):
241 filename = root + self.filename
242
243 base = os.path.dirname(filename)
244 if not os.path.exists(base):
245 os.makedirs(base)
246
247 f = open(filename, 'w')
248 f.write('<!DOCTYPE html>')
249 f.write('<html>')
250 f.write('<head>')
251 f.write('<meta http-equiv="Content-type" content="text/html; charset=UTF-8" />')
252 for css in self.css_path:
253 f.write('<link rel="stylesheet" href="%s"/>' % (css))
254 if self.title:
255 f.write('<title>%s</title>' % (self.title))
256 f.write('</head>')
257 for block in self.blocks:
258 block.build(f)
259 f.write('</body></html>')
260 f.close()
261
262class DisplayHTMLBuild(object):
263
264 def __init__(self, iwla):
265 self.pages = []
266 self.iwla = iwla
267
268 def getPage(self, filename):
269 for page in self.pages:
270 if page.getFilename() == filename:
271 return page
272 return None
273
274 def addPage(self, page):
275 self.pages.append(page)
276
277 def build(self, root):
278 display_root = self.iwla.getConfValue('DISPLAY_ROOT', '')
279 for res_path in self.iwla.getResourcesPath():
280 target = os.path.abspath(res_path)
281 link_name = os.path.join(display_root, res_path)
282 if not os.path.exists(link_name):
283 os.symlink(target, link_name)
284
285 for page in self.pages:
286 page.build(root)
287
288def bytesToStr(bytes):
289 suffixes = ['', ' kB', ' MB', ' GB', ' TB']
290
291 for i in range(0, len(suffixes)):
292 if bytes < 1024: break
293 bytes /= 1024.0
294
295 if i:
296 return '%.02f%s' % (bytes, suffixes[i])
297 else:
298 return '%d%s' % (bytes, suffixes[i])
299
300def _toStr(v):
301 if type(v) != str: return str(v)
302 else: return v
303
304def listToStr(l): return map(lambda(v) : _toStr(v), l)

Archive Download this file

Branches

Tags