Add parameter to do strToBytes in BlockTable and fix some errors with string and int

This commit is contained in:
2020-12-11 09:18:50 +01:00
parent 3ed8ba40be
commit a6f8c71bf2
7 changed files with 42 additions and 52 deletions

View File

@@ -98,13 +98,14 @@ class DisplayHTMLBlock(DisplayHTMLRaw):
class DisplayHTMLBlockTable(DisplayHTMLBlock):
def __init__(self, iwla, title, cols):
def __init__(self, iwla, title, cols, human_readable_cols=None):
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'
self.human_readable_cols = human_readable_cols or []
def appendRow(self, row):
self.rows.append(listToStr(row))
@@ -226,6 +227,8 @@ class DisplayHTMLBlockTable(DisplayHTMLBlock):
html += u'<tr>'
for j in range(0, len(row)):
v = row[j]
if j in self.human_readable_cols:
v = bytesToStr(v)
style = self.getCellCSSClass(i, j)
if style: style = u' class="%s"' % (style)
html += u'<td%s>%s</td>' % (style, v)
@@ -238,22 +241,19 @@ class DisplayHTMLBlockTable(DisplayHTMLBlock):
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)
def __init__(self, iwla, title, cols, short_titles=None, nb_valid_rows=0, graph_cols=None,
human_readable_cols=None):
super(DisplayHTMLBlockTableWithGraph, self).__init__(iwla=iwla, title=title, cols=cols,
human_readable_cols=human_readable_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(short_title)
@@ -265,13 +265,12 @@ class DisplayHTMLBlockTableWithGraph(DisplayHTMLBlockTable):
def _computeMax(self):
for i in range(0, self.nb_valid_rows):
row = self.raw_rows[i]
row = self.row[i]
for j in range(1, len(row)):
try:
if row[j] > self.maxes[j]:
self.maxes[j] = row[j]
except:
if type(row[j]) != int:
continue
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'
@@ -302,7 +301,7 @@ class DisplayHTMLBlockTableWithGraph(DisplayHTMLBlockTable):
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
height = int((self.rows[i][j] * 100) / self.maxes[j]) or 1
else:
height = 1
html += u'<img%s src="%s" height="%d" width="6" alt="%s" title="%s" />' % (style, icon, height, alt, alt)
@@ -427,20 +426,14 @@ class DisplayHTMLBuild(object):
def bytesToStr(_bytes):
suffixes = [u'', u' kB', u' MB', u' GB', u' TB']
try:
if type(_bytes) != int:
_bytes = int(_bytes, 10)
except:
return _bytes
for i in range(0, len(suffixes)):
if _bytes < 1024: break
_bytes /= 1024.0
if i:
return u'%.02f%s' % (_bytes, suffixes[i])
return '%.02f%s' % (_bytes, suffixes[i])
else:
return u'%d%s' % (_bytes, suffixes[i])
return '%d%s' % (_bytes, suffixes[i])
def _toStr(v):
return v