'
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 insertCol(self, col_number, col_title='', col_css_class=''):
self.cols.insert(col_number, col_title)
for r in self.rows:
r.insert(col_number, u'')
for r in self.rows_cssclasses:
v = r[0]
# If all cells have the same CSS class, set it
for cur_value in r:
if v != cur_value:
v = None
break
v = v or u''
r.insert(col_number, v)
self.cols_cssclasses.insert(col_number, col_css_class)
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 computeRatio(self, column, column_insertion=None):
if column_insertion is None:
column_insertion = column+1
total = 0
for r in self.rows:
if r[column]:
total += int(r[column])
self.insertCol(column_insertion, self.iwla._('Ratio'), u'iwla_hit')
for (index, r) in enumerate(self.rows):
val = r[column] and int(r[column]) or 0
self.setCellValue(index, column_insertion, '%.1f%%' % (float(val*100)/float(total)))
def _filter(self, function, column, args):
target_col = None
for col in range(0, len(self.cols)):
if self.cols[col] == column:
target_col = col
break
if target_col is None: return
for row in self.rows:
res = function(row[target_col], **args)
if res:
row[target_col] = res
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'