iwla

iwla Git Source Tree

Root/display.py

1# -*- coding: utf-8 -*-
2#
3# Copyright Grégory Soutadé 2015
4
5# This file is part of iwla
6
7# iwla is free software: you can redistribute it and/or modify
8# it under the terms of the GNU General Public License as published by
9# the Free Software Foundation, either version 3 of the License, or
10# (at your option) any later version.
11#
12# iwla is distributed in the hope that it will be useful,
13# but WITHOUT ANY WARRANTY; without even the implied warranty of
14# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15# GNU General Public License for more details.
16#
17# You should have received a copy of the GNU General Public License
18# along with iwla. If not, see <http://www.gnu.org/licenses/>.
19#
20
21import os
22import codecs
23import time
24import logging
25
26#
27# Create output HTML files
28#
29
30# Just for detection
31def _(name): pass
32_('January'), _('February'), _('March'), _('April'), _('May'), _('June'), _('July')
33_('August'), _('September'), _('October'), _('November'), _('December')
34del _
35
36class DisplayHTMLRaw(object):
37
38 def __init__(self, iwla, html=u''):
39 self.iwla = iwla
40 self.html = html
41
42 def setRawHTML(self, html):
43 self.html = html
44
45 def _buildHTML(self):
46 pass
47
48 def _build(self, f, html):
49 if html: f.write(html)
50
51 def build(self, f, filters=None):
52 if filters: self.filter(filters)
53 self._buildHTML()
54 self._build(f, self.html)
55
56 def _filter(self, function, **kwargs):
57 pass
58
59 def filter(self, filters):
60 for (args, function) in filters:
61 self._filter(function, **args)
62
63 def getTitle(self):
64 return ''
65
66class DisplayHTMLBlock(DisplayHTMLRaw):
67
68 def __init__(self, iwla, title=''):
69 super(DisplayHTMLBlock, self).__init__(iwla, html='')
70 self.title = title
71 self.cssclass = u'iwla_block'
72 self.title_cssclass = u'iwla_block_title'
73 self.value_cssclass = u'iwla_block_value'
74
75 def getTitle(self):
76 return self.title
77
78 def setTitle(self, value):
79 self.title = value
80
81 def setCSSClass(self, cssclass):
82 self.cssclass = cssclass
83
84 def setTitleCSSClass(self, cssclass):
85 self.title_cssclass = cssclass
86
87 def setValueCSSClass(self, cssclass):
88 self.value_cssclass = cssclass
89
90 def _buildHTML(self):
91 html = u'<div class="%s">' % (self.cssclass)
92 if self.title:
93 html += u'<div class="%s">%s</div>' % (self.title_cssclass, self.title)
94 html += u'<div class="%s">%s</div>' % (self.value_cssclass, self.html)
95 html += u'</div>'
96
97 self.html = html
98
99class DisplayHTMLBlockTable(DisplayHTMLBlock):
100
101 def __init__(self, iwla, title, cols, human_readable_cols=None):
102 super(DisplayHTMLBlockTable, self).__init__(iwla=iwla, title=title)
103 self.cols = listToStr(cols)
104 self.rows = []
105 self.cols_cssclasses = [u''] * len(cols)
106 self.rows_cssclasses = []
107 self.table_css = u'iwla_table'
108 self.human_readable_cols = human_readable_cols or []
109
110 def appendRow(self, row):
111 self.rows.append(listToStr(row))
112 self.rows_cssclasses.append([u''] * len(row))
113
114 def insertCol(self, col_number, col_title='', col_css_class=''):
115 self.cols.insert(col_number, col_title)
116 for r in self.rows:
117 r.insert(col_number, u'')
118 for r in self.rows_cssclasses:
119 v = r[0]
120 # If all cells have the same CSS class, set it
121 for cur_value in r:
122 if v != cur_value:
123 v = None
124 break
125 v = v or u''
126 r.insert(col_number, v)
127 self.cols_cssclasses.insert(col_number, col_css_class)
128
129 def getNbRows(self):
130 return len(self.rows)
131
132 def getNbCols(self):
133 return len(self.cols)
134
135 def getCellValue(self, row, col):
136 if row < 0 or col < 0 or\
137 row >= len(self.rows) or col >= len(self.cols):
138 raise ValueError('Invalid indices %d,%d' % (row, col))
139
140 return self.rows[row][col]
141
142 def setCellValue(self, row, col, value):
143 if row < 0 or col < 0 or\
144 row >= len(self.rows) or col >= len(self.cols):
145 raise ValueError('Invalid indices %d,%d' % (row, col))
146
147 self.rows[row][col] = value
148
149 def setCellCSSClass(self, row, col, value):
150 if row < 0 or col < 0 or\
151 row >= len(self.rows) or col >= len(self.cols):
152 raise ValueError('Invalid indices %d,%d' % (row, col))
153
154 self.rows_cssclasses[row][col] = value
155
156 def getCellCSSClass(self, row, col):
157 if row < 0 or col < 0 or\
158 row >= len(self.rows) or col >= len(self.cols):
159 raise ValueError('Invalid indices %d,%d' % (row, col))
160
161 return self.rows_cssclasses[row][col]
162
163 def getColCSSClass(self, col):
164 if col < 0 or col >= len(self.cols):
165 raise ValueError('Invalid indice %d' % (col))
166
167 return self.cols_cssclasses[col]
168
169 def setRowCSSClass(self, row, value):
170 if row < 0 or row >= len(self.rows):
171 raise ValueError('Invalid indice %d' % (row))
172
173 self.rows_cssclasses[row] = [value] * len(self.rows_cssclasses[row])
174
175 def setColCSSClass(self, col, value):
176 if col < 0 or col >= len(self.cols):
177 raise ValueError('Invalid indice %d' % (col))
178
179 self.cols_cssclasses[col] = value
180
181 def setColsCSSClass(self, values):
182 if len(values) != len(self.cols):
183 raise ValueError('Invalid values size')
184
185 self.cols_cssclasses = listToStr(values)
186
187 def computeRatio(self, column, column_insertion=None):
188 if column_insertion is None:
189 column_insertion = column+1
190
191 total = 0
192 for r in self.rows:
193 if r[column]:
194 total += int(r[column])
195
196 self.insertCol(column_insertion, self.iwla._('Ratio'), u'iwla_hit')
197 for (index, r) in enumerate(self.rows):
198 val = r[column] and int(r[column]) or 0
199 self.setCellValue(index, column_insertion, '%.1f%%' % (float(val*100)/float(total)))
200
201 def _filter(self, function, column, args):
202 target_col = None
203 for col in range(0, len(self.cols)):
204 if self.cols[col] == column:
205 target_col = col
206 break
207 if target_col is None: return
208 for row in self.rows:
209 res = function(row[target_col], **args)
210 if res:
211 row[target_col] = res
212
213 def _buildHTML(self):
214 style = u''
215 if self.table_css: style = u' class="%s"' % (self.table_css)
216 html = u'<table%s>' % (style)
217 if self.cols:
218 html += u'<tr>'
219 for i in range (0, len(self.cols)):
220 title = self.cols[i]
221 style = self.getColCSSClass(i)
222 if style: style = u' class="%s"' % (style)
223 html += u'<th%s>%s</th>' % (style, title)
224 html += u'</tr>'
225 for i in range(0, len(self.rows)):
226 row = self.rows[i]
227 html += u'<tr>'
228 for j in range(0, len(row)):
229 v = row[j]
230 if j in self.human_readable_cols:
231 v = bytesToStr(v)
232 style = self.getCellCSSClass(i, j)
233 if style: style = u' class="%s"' % (style)
234 html += u'<td%s>%s</td>' % (style, v)
235 html += u'</tr>'
236 html += u'</table>'
237
238 self.html += html
239
240 super(DisplayHTMLBlockTable, self)._buildHTML()
241
242class DisplayHTMLBlockTableWithGraph(DisplayHTMLBlockTable):
243
244 def __init__(self, iwla, title, cols, short_titles=None, nb_valid_rows=0, graph_cols=None,
245 human_readable_cols=None):
246 super(DisplayHTMLBlockTableWithGraph, self).__init__(iwla=iwla, title=title, cols=cols,
247 human_readable_cols=human_readable_cols)
248 self.short_titles = short_titles or []
249 self.short_titles = listToStr(self.short_titles)
250 self.nb_valid_rows = nb_valid_rows
251 self.icon_path = self.iwla.getConfValue('icon_path', '/')
252 self.maxes = [0] * len(cols)
253 self.table_graph_css = u'iwla_graph_table'
254 self.td_img_css = u'iwla_td_img'
255 self.graph_cols = graph_cols or []
256
257 def appendShortTitle(self, short_title):
258 self.short_titles.append(short_title)
259
260 def setShortTitle(self, short_titles):
261 self.short_titles = listToStr(short_titles)
262
263 def setNbValidRows(self, nb_valid_rows):
264 self.nb_valid_rows = nb_valid_rows
265
266 def _computeMax(self):
267 for i in range(0, self.nb_valid_rows):
268 row = self.row[i]
269 for j in range(1, len(row)):
270 if type(row[j]) != int:
271 continue
272 if row[j] > self.maxes[j]:
273 self.maxes[j] = row[j]
274
275 def _getIconFromStyle(self, style):
276 if style.startswith(u'iwla_page'): icon = u'vp.png'
277 elif style.startswith(u'iwla_hit'): icon = u'vh.png'
278 elif style.startswith(u'iwla_bandwidth'): icon = u'vk.png'
279 elif style.startswith(u'iwla_visitor'): icon = u'vu.png'
280 elif style.startswith(u'iwla_visit'): icon = u'vv.png'
281 else: return ''
282
283 return u'/%s/other/%s' % (self.icon_path, icon)
284
285 def _buildHTML(self):
286 self._computeMax()
287
288 style = u''
289 if self.table_graph_css: style = u' class="%s"' % (self.table_graph_css)
290 html = u'<table%s>' % (style)
291 html += u'<tr>'
292 for i in range(0, self.nb_valid_rows):
293 row = self.rows[i]
294 css = u''
295 if self.td_img_css: css=u' class="%s"' % (self.td_img_css)
296 html += u'<td%s>' % (css)
297 for j in self.graph_cols:
298 style = self.getColCSSClass(j)
299 icon = self._getIconFromStyle(style)
300 if not icon: continue
301 if style: style = u' class="%s"' % (style)
302 alt = u'%s: %s' % (row[j], self.cols[j])
303 if self.maxes[j]:
304 height = int((self.rows[i][j] * 100) / self.maxes[j]) or 1
305 else:
306 height = 1
307 html += u'<img%s src="%s" height="%d" width="6" alt="%s" title="%s" />' % (style, icon, height, alt, alt)
308 html += u'</td>'
309 html += u'</tr>'
310 html += u'<tr>'
311 for i in range(0, len(self.short_titles)):
312 style = self.getCellCSSClass(i, 0)
313 if style: style = u' class="%s"' % (style)
314 html += u'<td%s>%s</td>' % (style, self.short_titles[i])
315 html += u'</tr>'
316 html += u'</table>'
317
318 self.html += html
319
320 super(DisplayHTMLBlockTableWithGraph, self)._buildHTML()
321
322class DisplayHTMLPage(object):
323
324 def __init__(self, iwla, title, filename, css_path):
325 self.iwla = iwla
326 self.title = title
327 self.filename = filename
328 self.blocks = []
329 self.css_path = listToStr(css_path)
330 self.logger = logging.getLogger(self.__class__.__name__)
331
332 def getFilename(self):
333 return self.filename;
334
335 def getBlock(self, title):
336 for b in self.blocks:
337 if title == b.getTitle():
338 return b
339 return None
340
341 def getAllBlocks(self):
342 return self.blocks
343
344 def appendBlock(self, block):
345 self.blocks.append(block)
346
347 def build(self, root, displayVersion=True, filters=None):
348 filename = os.path.join(root, self.filename)
349
350 base = os.path.dirname(filename)
351 if not os.path.exists(base):
352 os.makedirs(base)
353
354 self.logger.debug('Write %s' % (filename))
355
356 if self.iwla.dry_run: return
357
358 f = codecs.open(filename, 'w', 'utf-8')
359 f.write(u'<!DOCTYPE html>')
360 f.write(u'<html>')
361 f.write(u'<head>')
362 f.write(u'<meta http-equiv="Content-type" content="text/html; charset=UTF-8" />')
363 for css in self.css_path:
364 f.write(u'<link rel="stylesheet" href="/%s"/>' % (css))
365 if self.title:
366 f.write(u'<title>%s</title>' % (self.title))
367 f.write(u'</head><body>')
368 for block in self.blocks:
369 block.build(f, filters=filters)
370 if displayVersion:
371 f.write(u'<div style="text-align:center;width:100%%">Generated by <a href="%s">IWLA %s</a></div>' %
372 ("http://indefero.soutade.fr/p/iwla", self.iwla.getVersion()))
373 f.write(u'</body></html>')
374 f.close()
375
376class DisplayHTMLBuild(object):
377
378 def __init__(self, iwla):
379 self.iwla = iwla
380 self.filters = []
381 self.clear()
382
383 def clear(self):
384 self.pages = []
385
386 def createPage(self, *args):
387 return DisplayHTMLPage(self.iwla, *args)
388
389 def createBlock(self, _class, *args):
390 return _class(self.iwla, *args)
391
392 def getPage(self, filename):
393 for page in self.pages:
394 if page.getFilename() == filename:
395 return page
396 return None
397
398 def getAllPages(self):
399 return self.pages
400
401 def addPage(self, page):
402 self.pages.append(page)
403
404 def build(self, root):
405 if not self.iwla.dry_run:
406 display_root = self.iwla.getConfValue('DISPLAY_ROOT', '')
407 if not os.path.exists(display_root):
408 os.makedirs(display_root)
409 for res_path in self.iwla.getResourcesPath():
410 target = os.path.abspath(res_path)
411 link_name = os.path.join(display_root, res_path)
412 if not os.path.exists(link_name):
413 os.symlink(target, link_name)
414
415 for page in self.pages:
416 page.build(root, filters=self.filters)
417
418 def addColumnFilter(self, column, function, args):
419 self.filters.append(({'column':column, 'args':args}, function))
420
421
422#
423# Global functions
424#
425
426def bytesToStr(_bytes):
427 suffixes = [u'', u' kB', u' MB', u' GB', u' TB']
428
429 for i in range(0, len(suffixes)):
430 if _bytes < 1024: break
431 _bytes /= 1024.0
432
433 if i:
434 return '%.02f%s' % (_bytes, suffixes[i])
435 else:
436 return '%d%s' % (_bytes, suffixes[i])
437
438def _toStr(v):
439 return v
440 if type(v) != unicode: return unicode(v)
441 else: return v
442
443def listToStr(l): return l #map(lambda v : _toStr(v), l)
444
445def generateHTMLLink(url, name=None, max_length=100, prefix=u'http'):
446 url = url
447 if not name: name = url
448 if not url.startswith(prefix): url = u'%s://%s' % (prefix, url)
449 return u'<a href="%s">%s</a>' % (url, name[:max_length])
450
451def createCurTitle(iwla, title):
452 title = iwla._(title)
453 month_name = time.strftime(u'%B', iwla.getCurTime())
454 year = time.strftime(u'%Y', iwla.getCurTime())
455 title += u' - %s %s' % (iwla._(month_name), year)
456 domain_name = iwla.getConfValue('domain_name', '')
457 if domain_name:
458 title += u' - %s' % (domain_name)
459 return title

Archive Download this file

Branches

Tags