iwla

iwla Git Source Tree

Root/display.py

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

Archive Download this file

Branches

Tags