# -*- coding: utf-8 -*- # # Copyright Grégory Soutadé 2015 # This file is part of iwla # iwla is free software: you can redistribute it and/or modify # it under the terms of the GNU General Public License as published by # the Free Software Foundation, either version 3 of the License, or # (at your option) any later version. # # iwla is distributed in the hope that it will be useful, # but WITHOUT ANY WARRANTY; without even the implied warranty of # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the # GNU General Public License for more details. # # You should have received a copy of the GNU General Public License # along with iwla. If not, see . # import os import codecs import time import logging # # Create output HTML files # class DisplayHTMLRaw(object): def __init__(self, iwla, html=u''): self.iwla = iwla self.html = html def setRawHTML(self, html): self.html = html def _buildHTML(self): pass def _build(self, f, html): if html: f.write(html) def build(self, f): self._buildHTML() self._build(f, self.html) class DisplayHTMLBlock(DisplayHTMLRaw): def __init__(self, iwla, title=''): super(DisplayHTMLBlock, self).__init__(iwla, html='') self.title = title self.cssclass = u'iwla_block' self.title_cssclass = u'iwla_block_title' self.value_cssclass = u'iwla_block_value' def getTitle(self): return self.title def setTitle(self, value): self.title = unicode(value) def setCSSClass(self, cssclass): self.cssclass = unicode(cssclass) def setTitleCSSClass(self, cssclass): self.title_cssclass = unicode(cssclass) def setValueCSSClass(self, cssclass): self.value_cssclass = unicode(cssclass) def _buildHTML(self): html = u'
' % (self.cssclass) if self.title: html += u'
%s
' % (self.title_cssclass, self.title) html += u'
%s
' % (self.value_cssclass, self.html) html += u'
' self.html = html class DisplayHTMLBlockTable(DisplayHTMLBlock): def __init__(self, iwla, title, cols): super(DisplayHTMLBlockTable, self).__init__(iwla=iwla, title=title) self.cols = listToStr(cols) self.rows = [] self.cols_cssclasses = [u''] * len(cols) self.rows_cssclasses = [] self.table_css = u'iwla_table' def appendRow(self, row): self.rows.append(listToStr(row)) self.rows_cssclasses.append([u''] * len(row)) def getNbRows(self): return len(self.rows) def getNbCols(self): return len(self.cols) def getCellValue(self, row, col): if row < 0 or col < 0 or\ row >= len(self.rows) or col >= len(self.cols): raise ValueError('Invalid indices %d,%d' % (row, col)) return self.rows[row][col] def setCellValue(self, row, col, value): if row < 0 or col < 0 or\ row >= len(self.rows) or col >= len(self.cols): raise ValueError('Invalid indices %d,%d' % (row, col)) self.rows[row][col] = unicode(value) def setCellCSSClass(self, row, col, value): if row < 0 or col < 0 or\ row >= len(self.rows) or col >= len(self.cols): raise ValueError('Invalid indices %d,%d' % (row, col)) self.rows_cssclasses[row][col] = unicode(value) def getCellCSSClass(self, row, col): if row < 0 or col < 0 or\ row >= len(self.rows) or col >= len(self.cols): raise ValueError('Invalid indices %d,%d' % (row, col)) return self.rows_cssclasses[row][col] def getColCSSClass(self, col): if col < 0 or col >= len(self.cols): raise ValueError('Invalid indice %d' % (col)) return self.cols_cssclasses[col] def setRowCSSClass(self, row, value): if row < 0 or row >= len(self.rows): raise ValueError('Invalid indice %d' % (row)) self.rows_cssclasses[row] = [unicode(value)] * len(self.rows_cssclasses[row]) def setColCSSClass(self, col, value): if col < 0 or col >= len(self.cols): raise ValueError('Invalid indice %d' % (col)) self.cols_cssclasses[col] = unicode(value) def setColsCSSClass(self, values): if len(values) != len(self.cols): raise ValueError('Invalid values size') self.cols_cssclasses = listToStr(values) def _buildHTML(self): style = u'' if self.table_css: style = u' class="%s"' % (self.table_css) html = u'' % (style) if self.cols: html += u'' for i in range (0, len(self.cols)): title = self.cols[i] style = self.getColCSSClass(i) if style: style = u' class="%s"' % (style) html += u'%s' % (style, title) html += u'' for i in range(0, len(self.rows)): row = self.rows[i] html += u'' for j in range(0, len(row)): v = row[j] style = self.getCellCSSClass(i, j) if style: style = u' class="%s"' % (style) html += u'%s' % (style, v) html += u'' html += u'' self.html += html super(DisplayHTMLBlockTable, self)._buildHTML() class DisplayHTMLBlockTableWithGraph(DisplayHTMLBlockTable): def __init__(self, iwla, title, cols, short_titles=None, nb_valid_rows=0, graph_cols=None): super(DisplayHTMLBlockTableWithGraph, self).__init__(iwla=iwla, title=title, cols=cols) self.short_titles = short_titles or [] self.short_titles = listToStr(self.short_titles) self.nb_valid_rows = nb_valid_rows self.icon_path = self.iwla.getConfValue('icon_path', '/') self.raw_rows = [] self.maxes = [0] * len(cols) self.table_graph_css = u'iwla_graph_table' self.td_img_css = u'iwla_td_img' self.graph_cols = graph_cols or [] def appendRow(self, row): self.raw_rows.append(row) super(DisplayHTMLBlockTableWithGraph, self).appendRow(row) def appendShortTitle(self, short_title): self.short_titles.append(unicode(short_title)) def setShortTitle(self, short_titles): self.short_titles = listToStr(short_titles) def setNbValidRows(self, nb_valid_rows): self.nb_valid_rows = nb_valid_rows def _computeMax(self): for i in range(0, self.nb_valid_rows): row = self.raw_rows[i] for j in range(1, len(row)): if row[j] > self.maxes[j]: self.maxes[j] = row[j] def _getIconFromStyle(self, style): if style.startswith(u'iwla_page'): icon = u'vp.png' elif style.startswith(u'iwla_hit'): icon = u'vh.png' elif style.startswith(u'iwla_bandwidth'): icon = u'vk.png' elif style.startswith(u'iwla_visitor'): icon = u'vu.png' elif style.startswith(u'iwla_visit'): icon = u'vv.png' else: return '' return u'/%s/%s' % (self.icon_path, icon) def _buildHTML(self): self._computeMax() style = u'' if self.table_graph_css: style = u' class="%s"' % (self.table_graph_css) html = u'' % (style) html += u'' for i in range(0, self.nb_valid_rows): row = self.rows[i] css = u'' if self.td_img_css: css=u' class="%s"' % (self.td_img_css) html += u'' % (css) for j in self.graph_cols: style = self.getColCSSClass(j) icon = self._getIconFromStyle(style) if not icon: continue if style: style = u' class="%s"' % (style) alt = u'%s: %s' % (row[j], self.cols[j]) if self.maxes[j]: height = int((self.raw_rows[i][j] * 100) / self.maxes[j]) or 1 else: height = 1 html += u'' % (style, icon, height, alt, alt) html += u'' html += u'' html += u'' for i in range(0, len(self.short_titles)): style = self.getCellCSSClass(i, 0) if style: style = u' class="%s"' % (style) html += u'%s' % (style, self.short_titles[i]) html += u'' html += u'' self.html += html super(DisplayHTMLBlockTableWithGraph, self)._buildHTML() class DisplayHTMLPage(object): def __init__(self, iwla, title, filename, css_path): self.iwla = iwla self.title = unicode(title) self.filename = filename self.blocks = [] self.css_path = listToStr(css_path) self.logger = logging.getLogger(self.__class__.__name__) def getFilename(self): return self.filename; def getBlock(self, title): for b in self.blocks: if title == b.getTitle(): return b return None def appendBlock(self, block): self.blocks.append(block) def build(self, root): filename = os.path.join(root, self.filename) base = os.path.dirname(filename) if not os.path.exists(base): os.makedirs(base) self.logger.debug('Write %s' % (filename)) f = codecs.open(filename, 'w', 'utf-8') f.write(u'') f.write(u'') f.write(u'') f.write(u'') for css in self.css_path: f.write(u'' % (css)) if self.title: f.write(u'%s' % (self.title)) f.write(u'') for block in self.blocks: block.build(f) f.write(u'
Generated by IWLA %s
' % ("http://indefero.soutade.fr/p/iwla", self.iwla.getVersion())) f.write(u'') f.close() class DisplayHTMLBuild(object): def __init__(self, iwla): self.pages = [] self.iwla = iwla def createPage(self, *args): return DisplayHTMLPage(self.iwla, *args) def createBlock(self, _class, *args): return _class(self.iwla, *args) def getPage(self, filename): for page in self.pages: if page.getFilename() == filename: return page return None def addPage(self, page): self.pages.append(page) def build(self, root): display_root = self.iwla.getConfValue('DISPLAY_ROOT', '') if not os.path.exists(display_root): os.makedirs(display_root) for res_path in self.iwla.getResourcesPath(): target = os.path.abspath(res_path) link_name = os.path.join(display_root, res_path) if not os.path.exists(link_name): os.symlink(target, link_name) for page in self.pages: page.build(root) # # Global functions # def bytesToStr(bytes): suffixes = [u'', u' kB', u' MB', u' GB', u' TB'] for i in range(0, len(suffixes)): if bytes < 1024: break bytes /= 1024.0 if i: return u'%.02f%s' % (bytes, suffixes[i]) else: return u'%d%s' % (bytes, suffixes[i]) def _toStr(v): if type(v) != unicode: return unicode(v) else: return v def listToStr(l): return map(lambda(v) : _toStr(v), l) def generateHTMLLink(url, name=None, max_length=100, prefix=u'http'): url = unicode(url) if not name: name = unicode(url) if not url.startswith(prefix): url = u'%s://%s' % (prefix, url) return u'%s' % (url, name[:max_length]) def createCurTitle(iwla, title): title = iwla._(title) + time.strftime(u' - %B %Y', iwla.getCurTime()) return title