iwla/display.py
2014-11-30 19:05:17 +01:00

226 lines
6.6 KiB
Python

import os
class DisplayHTMLRaw(object):
def __init__(self, html=''):
self.html = html
def setRawHTML(self, html):
self.html = html
def _build(self, f, html):
if html: f.write(html)
def build(self, f):
self._build(self.html)
class DisplayHTMLBlock(DisplayHTMLRaw):
def __init__(self, title=''):
super(DisplayHTMLBlock, self).__init__(html='')
self.title = title
self.cssclass = 'iwla_block'
self.title_cssclass = 'iwla_block_title'
self.value_cssclass = 'iwla_block_value'
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
def build(self, f):
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)
class DisplayHTMLBlockTable(DisplayHTMLBlock):
def __init__(self, title, cols):
super(DisplayHTMLBlockTable, self).__init__(title=title)
self.cols = cols
self.rows = []
self.cols_cssclasses = ['' for e in cols]
self.rows_cssclasses = []
def appendRow(self, row):
self.rows.append(listToStr(row))
self.rows_cssclasses.append(['' for e in row])
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))
return self.rows[row][col]
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] = 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))
for i in range(0, self.rows_cssclasses[row]):
self.rows_cssclasses[row][i] = value
def setColCSSClass(self, col, value):
if col < 0 or col >= len(self.cols):
raise ValueError('Invalid indice %d' % (col))
self.cols_cssclasses[col] = value
def setColsCSSClass(self, values):
if len(values) != len(self.cols):
raise ValueError('Invalid values size')
self.cols_cssclasses = values
def build(self, f):
if not self.html:
html = '<table>'
if self.cols:
html += '<tr>'
for i in range (0, len(self.cols)):
title = self.cols[i]
style = self.getColCSSClass(i)
if style: style = ' class="%s"' % (style)
html += '<th%s>%s</th>' % (style, title)
html += '</tr>'
for i in range(0, len(self.rows)):
row = self.rows[i]
html += '<tr>'
for j in range(0, len(row)):
v = row[j]
style = self.getCellCSSClass(i, j)
if style: style = ' class="%s"' % (style)
html += '<td%s>%s</td>' % (style, v)
html += '</tr>'
html += '</table>'
self.html = html
super(DisplayHTMLBlockTable, self).build(f)
class DisplayHTMLPage(object):
def __init__(self, title, filename, css_path):
self.title = title
self.filename = filename
self.blocks = []
self.css_path = css_path
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 = root + self.filename
base = os.path.dirname(filename)
if not os.path.exists(base):
os.makedirs(base)
f = open(filename, 'w')
f.write('<!DOCTYPE html>')
f.write('<html>')
f.write('<head>')
f.write('<meta http-equiv="Content-type" content="text/html; charset=UTF-8" />')
for css in self.css_path:
f.write('<link rel="stylesheet" href="%s"/>' % (css))
if self.title:
f.write('<title>%s</title>' % (self.title))
f.write('</head>')
for block in self.blocks:
block.build(f)
f.write('</body></html>')
f.close()
class DisplayHTMLBuild(object):
def __init__(self, iwla):
self.pages = []
self.iwla = iwla
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', '')
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)
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)