2014-11-27 14:11:47 +01:00
|
|
|
import os
|
2014-11-21 10:41:29 +01:00
|
|
|
|
2014-11-27 19:38:41 +01:00
|
|
|
class DisplayHTMLRaw(object):
|
2014-11-21 16:56:58 +01:00
|
|
|
|
2014-11-27 19:38:41 +01:00
|
|
|
def __init__(self, html=''):
|
|
|
|
self.html = html
|
|
|
|
|
|
|
|
def setRawHTML(self, html):
|
|
|
|
self.html = html
|
2014-11-21 16:56:58 +01:00
|
|
|
|
2014-11-27 19:38:41 +01:00
|
|
|
def _build(self, f, html):
|
|
|
|
if html: f.write(html)
|
|
|
|
|
2014-11-21 16:56:58 +01:00
|
|
|
def build(self, f):
|
2014-11-27 19:38:41 +01:00
|
|
|
self._build(self.html)
|
2014-11-21 16:56:58 +01:00
|
|
|
|
2014-11-27 19:38:41 +01:00
|
|
|
class DisplayHTMLBlock(DisplayHTMLRaw):
|
2014-11-25 16:59:29 +01:00
|
|
|
|
|
|
|
def __init__(self, title=''):
|
2014-11-27 19:38:41 +01:00
|
|
|
super(DisplayHTMLBlock, self).__init__(html='')
|
|
|
|
self.title = title
|
|
|
|
self.cssclass = 'iwla_block'
|
|
|
|
self.title_cssclass = 'iwla_block_title'
|
|
|
|
self.value_cssclass = 'iwla_block_value'
|
2014-11-25 16:59:29 +01:00
|
|
|
|
2014-11-27 19:38:41 +01:00
|
|
|
def getTitle(self):
|
|
|
|
return self.title
|
|
|
|
|
|
|
|
def setTitle(self, value):
|
|
|
|
self.title = value
|
|
|
|
|
|
|
|
def setCSSClass(self, cssclass):
|
|
|
|
self.cssclass = cssclass
|
|
|
|
|
|
|
|
def setTitleCSSClass(self, cssclass):
|
|
|
|
self.title_cssclass = cssclass
|
|
|
|
|
|
|
|
def setValueCSSClass(self, cssclass):
|
|
|
|
self.value_cssclass = cssclass
|
2014-11-25 16:59:29 +01:00
|
|
|
|
|
|
|
def build(self, f):
|
2014-11-27 19:38:41 +01:00
|
|
|
html = '<div class="%s">' % (self.cssclass)
|
|
|
|
if self.title:
|
|
|
|
html += '<div class="%s">%s</div>' % (self.title_cssclass, self.title)
|
|
|
|
html += '<div class="%s">%s</div>' % (self.value_cssclass, self.html)
|
|
|
|
html += '</div>'
|
|
|
|
|
|
|
|
self._build(f, html)
|
2014-11-25 16:59:29 +01:00
|
|
|
|
2014-11-21 16:56:58 +01:00
|
|
|
class DisplayHTMLBlockTable(DisplayHTMLBlock):
|
|
|
|
|
|
|
|
def __init__(self, title, cols):
|
2014-11-27 19:38:41 +01:00
|
|
|
super(DisplayHTMLBlockTable, self).__init__(title=title)
|
2014-11-21 16:56:58 +01:00
|
|
|
self.cols = cols
|
|
|
|
self.rows = []
|
2014-11-27 19:38:41 +01:00
|
|
|
self.cols_cssclasses = ['' for e in cols]
|
|
|
|
self.rows_cssclasses = []
|
2014-11-21 16:56:58 +01:00
|
|
|
|
|
|
|
def appendRow(self, row):
|
2014-11-24 13:44:04 +01:00
|
|
|
self.rows.append(listToStr(row))
|
2014-11-27 19:38:41 +01:00
|
|
|
self.rows_cssclasses.append(['' for e in row])
|
|
|
|
|
|
|
|
def getCellValue(row, col):
|
|
|
|
if row < 0 or col < 0 or\
|
|
|
|
row >= len(self.rows) or col >= len(self.cols):
|
|
|
|
raise ValueError('Invalid indices')
|
|
|
|
|
|
|
|
return self.rows[row][col]
|
|
|
|
|
|
|
|
def setCellValue(row, col, value):
|
|
|
|
if row < 0 or col < 0 or\
|
|
|
|
row >= len(self.rows) or col >= len(self.cols):
|
|
|
|
raise ValueError('Invalid indices')
|
|
|
|
|
|
|
|
return self.rows[row][col]
|
2014-11-21 16:56:58 +01:00
|
|
|
|
2014-11-27 19:38:41 +01:00
|
|
|
def setCellCSSClass(row, col, value):
|
|
|
|
if row < 0 or col < 0 or\
|
|
|
|
row >= len(self.rows) or col >= len(self.cols):
|
|
|
|
raise ValueError('Invalid indices')
|
|
|
|
|
|
|
|
self.rows_cssclasses[row][col] = value
|
|
|
|
|
|
|
|
def setRowCSSClass(row, value):
|
|
|
|
if row < 0 or row >= len(self.rows):
|
|
|
|
raise ValueError('Invalid indice')
|
|
|
|
|
|
|
|
for i in range(0, self.rows_cssclasses[row]):
|
|
|
|
self.rows_cssclasses[row][i] = value
|
|
|
|
|
2014-11-21 16:56:58 +01:00
|
|
|
def build(self, f):
|
2014-11-27 19:38:41 +01:00
|
|
|
if not self.html:
|
|
|
|
html = '<table>'
|
|
|
|
html += '<tr>'
|
|
|
|
for title in self.cols:
|
|
|
|
html += '<th>%s</th>' % (title)
|
|
|
|
html += '</tr>'
|
|
|
|
for row in self.rows:
|
|
|
|
html += '<tr>'
|
|
|
|
for v in row:
|
|
|
|
html += '<td>%s</td>' % (v)
|
|
|
|
html += '</tr>'
|
|
|
|
html += '</table>'
|
|
|
|
self.html = html
|
|
|
|
|
|
|
|
super(DisplayHTMLBlockTable, self).build(f)
|
2014-11-21 16:56:58 +01:00
|
|
|
|
|
|
|
class DisplayHTMLPage(object):
|
|
|
|
|
|
|
|
def __init__(self, title, filename):
|
|
|
|
self.title = title
|
|
|
|
self.filename = filename
|
|
|
|
self.blocks = []
|
|
|
|
|
|
|
|
def getFilename(self):
|
|
|
|
return self.filename;
|
|
|
|
|
2014-11-27 19:38:41 +01:00
|
|
|
def getBlock(self, title):
|
|
|
|
for b in self.blocks:
|
|
|
|
if title == b.getTitle():
|
|
|
|
return b
|
|
|
|
return None
|
|
|
|
|
2014-11-21 16:56:58 +01:00
|
|
|
def appendBlock(self, block):
|
|
|
|
self.blocks.append(block)
|
|
|
|
|
|
|
|
def build(self, root):
|
2014-11-27 14:11:47 +01:00
|
|
|
filename = root + self.filename
|
|
|
|
|
|
|
|
base = os.path.dirname(filename)
|
|
|
|
if not os.path.exists(base):
|
|
|
|
os.makedirs(base)
|
|
|
|
|
|
|
|
f = open(filename, 'w')
|
2014-11-27 19:38:41 +01:00
|
|
|
f.write('<!DOCTYPE html>')
|
|
|
|
f.write('<html>')
|
|
|
|
f.write('<head>')
|
|
|
|
f.write('<meta http-equiv="Content-type" content="text/html; charset=UTF-8" />')
|
|
|
|
if self.title:
|
|
|
|
f.write('<title>%s</title>' % (self.title))
|
|
|
|
f.write('</head>')
|
2014-11-21 16:56:58 +01:00
|
|
|
for block in self.blocks:
|
|
|
|
block.build(f)
|
|
|
|
f.write('</body></html>')
|
|
|
|
f.close()
|
|
|
|
|
|
|
|
class DisplayHTMLBuild(object):
|
|
|
|
|
|
|
|
def __init__(self):
|
|
|
|
self.pages = []
|
|
|
|
|
|
|
|
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):
|
|
|
|
for page in self.pages:
|
|
|
|
page.build(root)
|
2014-11-24 13:44:04 +01:00
|
|
|
|
|
|
|
def bytesToStr(bytes):
|
|
|
|
suffixes = ['', ' kB', ' MB', ' GB', ' TB']
|
|
|
|
|
|
|
|
for i in range(0, len(suffixes)):
|
|
|
|
if bytes < 1024: break
|
|
|
|
bytes /= 1024.0
|
|
|
|
|
|
|
|
if i:
|
|
|
|
return '%.02f%s' % (bytes, suffixes[i])
|
|
|
|
else:
|
|
|
|
return '%d%s' % (bytes, suffixes[i])
|
|
|
|
|
|
|
|
def _toStr(v):
|
|
|
|
if type(v) != str: return str(v)
|
|
|
|
else: return v
|
|
|
|
|
|
|
|
def listToStr(l): return map(lambda(v) : _toStr(v), l)
|