Update code for Python3

This commit is contained in:
Grégory Soutadé 2020-12-09 13:24:29 +01:00
parent f457f4e390
commit c0e3e0a801
3 changed files with 36 additions and 24 deletions

View File

@ -76,16 +76,16 @@ class DisplayHTMLBlock(DisplayHTMLRaw):
return self.title return self.title
def setTitle(self, value): def setTitle(self, value):
self.title = unicode(value) self.title = value
def setCSSClass(self, cssclass): def setCSSClass(self, cssclass):
self.cssclass = unicode(cssclass) self.cssclass = cssclass
def setTitleCSSClass(self, cssclass): def setTitleCSSClass(self, cssclass):
self.title_cssclass = unicode(cssclass) self.title_cssclass = cssclass
def setValueCSSClass(self, cssclass): def setValueCSSClass(self, cssclass):
self.value_cssclass = unicode(cssclass) self.value_cssclass = cssclass
def _buildHTML(self): def _buildHTML(self):
html = u'<div class="%s">' % (self.cssclass) html = u'<div class="%s">' % (self.cssclass)
@ -143,14 +143,14 @@ class DisplayHTMLBlockTable(DisplayHTMLBlock):
row >= len(self.rows) or col >= len(self.cols): row >= len(self.rows) or col >= len(self.cols):
raise ValueError('Invalid indices %d,%d' % (row, col)) raise ValueError('Invalid indices %d,%d' % (row, col))
self.rows[row][col] = unicode(value) self.rows[row][col] = value
def setCellCSSClass(self, row, col, value): def setCellCSSClass(self, row, col, value):
if row < 0 or col < 0 or\ if row < 0 or col < 0 or\
row >= len(self.rows) or col >= len(self.cols): row >= len(self.rows) or col >= len(self.cols):
raise ValueError('Invalid indices %d,%d' % (row, col)) raise ValueError('Invalid indices %d,%d' % (row, col))
self.rows_cssclasses[row][col] = unicode(value) self.rows_cssclasses[row][col] = value
def getCellCSSClass(self, row, col): def getCellCSSClass(self, row, col):
if row < 0 or col < 0 or\ if row < 0 or col < 0 or\
@ -169,13 +169,13 @@ class DisplayHTMLBlockTable(DisplayHTMLBlock):
if row < 0 or row >= len(self.rows): if row < 0 or row >= len(self.rows):
raise ValueError('Invalid indice %d' % (row)) raise ValueError('Invalid indice %d' % (row))
self.rows_cssclasses[row] = [unicode(value)] * len(self.rows_cssclasses[row]) self.rows_cssclasses[row] = [value] * len(self.rows_cssclasses[row])
def setColCSSClass(self, col, value): def setColCSSClass(self, col, value):
if col < 0 or col >= len(self.cols): if col < 0 or col >= len(self.cols):
raise ValueError('Invalid indice %d' % (col)) raise ValueError('Invalid indice %d' % (col))
self.cols_cssclasses[col] = unicode(value) self.cols_cssclasses[col] = value
def setColsCSSClass(self, values): def setColsCSSClass(self, values):
if len(values) != len(self.cols): if len(values) != len(self.cols):
@ -255,7 +255,7 @@ class DisplayHTMLBlockTableWithGraph(DisplayHTMLBlockTable):
super(DisplayHTMLBlockTableWithGraph, self).appendRow(row) super(DisplayHTMLBlockTableWithGraph, self).appendRow(row)
def appendShortTitle(self, short_title): def appendShortTitle(self, short_title):
self.short_titles.append(unicode(short_title)) self.short_titles.append(short_title)
def setShortTitle(self, short_titles): def setShortTitle(self, short_titles):
self.short_titles = listToStr(short_titles) self.short_titles = listToStr(short_titles)
@ -267,8 +267,11 @@ class DisplayHTMLBlockTableWithGraph(DisplayHTMLBlockTable):
for i in range(0, self.nb_valid_rows): for i in range(0, self.nb_valid_rows):
row = self.raw_rows[i] row = self.raw_rows[i]
for j in range(1, len(row)): for j in range(1, len(row)):
if row[j] > self.maxes[j]: try:
self.maxes[j] = row[j] if row[j] > self.maxes[j]:
self.maxes[j] = row[j]
except:
continue
def _getIconFromStyle(self, style): def _getIconFromStyle(self, style):
if style.startswith(u'iwla_page'): icon = u'vp.png' if style.startswith(u'iwla_page'): icon = u'vp.png'
@ -321,7 +324,7 @@ class DisplayHTMLPage(object):
def __init__(self, iwla, title, filename, css_path): def __init__(self, iwla, title, filename, css_path):
self.iwla = iwla self.iwla = iwla
self.title = unicode(title) self.title = title
self.filename = filename self.filename = filename
self.blocks = [] self.blocks = []
self.css_path = listToStr(css_path) self.css_path = listToStr(css_path)
@ -421,27 +424,34 @@ class DisplayHTMLBuild(object):
# Global functions # Global functions
# #
def bytesToStr(bytes): def bytesToStr(_bytes):
suffixes = [u'', u' kB', u' MB', u' GB', u' TB'] 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)): for i in range(0, len(suffixes)):
if bytes < 1024: break if _bytes < 1024: break
bytes /= 1024.0 _bytes /= 1024.0
if i: if i:
return u'%.02f%s' % (bytes, suffixes[i]) return u'%.02f%s' % (_bytes, suffixes[i])
else: else:
return u'%d%s' % (bytes, suffixes[i]) return u'%d%s' % (_bytes, suffixes[i])
def _toStr(v): def _toStr(v):
return v
if type(v) != unicode: return unicode(v) if type(v) != unicode: return unicode(v)
else: return v else: return v
def listToStr(l): return map(lambda v : _toStr(v), l) def listToStr(l): return l #map(lambda v : _toStr(v), l)
def generateHTMLLink(url, name=None, max_length=100, prefix=u'http'): def generateHTMLLink(url, name=None, max_length=100, prefix=u'http'):
url = unicode(url) url = url
if not name: name = unicode(url) if not name: name = url
if not url.startswith(prefix): url = u'%s://%s' % (prefix, url) if not url.startswith(prefix): url = u'%s://%s' % (prefix, url)
return u'<a href="%s">%s</a>' % (url, name[:max_length]) return u'<a href="%s">%s</a>' % (url, name[:max_length])

View File

@ -482,9 +482,9 @@ class IWLA(object):
row = [0, nb_visits, stats['viewed_pages'], stats['viewed_hits'], stats['viewed_bandwidth'], stats['not_viewed_bandwidth']] row = [0, nb_visits, stats['viewed_pages'], stats['viewed_hits'], stats['viewed_bandwidth'], stats['not_viewed_bandwidth']]
if nb_days: if nb_days:
average_row = map(lambda v: int(v/nb_days), row) average_row = list(map(lambda v: int(v/nb_days), row))
else: else:
average_row = map(lambda v: 0, row) average_row = list(map(lambda v: 0, row))
average_row[0] = self._('Average') average_row[0] = self._('Average')
average_row[4] = bytesToStr(average_row[4]) average_row[4] = bytesToStr(average_row[4])
@ -642,6 +642,8 @@ class IWLA(object):
self._callPlugins(conf.POST_HOOK_DIRECTORY) self._callPlugins(conf.POST_HOOK_DIRECTORY)
if args.display_only: if args.display_only:
if not 'stats' in self.meta_infos.keys():
self.meta_infos['stats'] = {}
self._generateDisplay() self._generateDisplay()
return return

View File

@ -19,7 +19,7 @@
# #
import re import re
import urllib import urllib.parse
from iwla import IWLA from iwla import IWLA
from iplugin import IPlugin from iplugin import IPlugin
@ -113,7 +113,7 @@ class IWLAPostAnalysisReferers(IPlugin):
if groups: if groups:
key_phrase = groups.groupdict()['key_phrase'] key_phrase = groups.groupdict()['key_phrase']
try: try:
key_phrase = urllib.unquote_plus(key_phrase).decode('utf8') key_phrase = urllib.parse.unquote_plus(key_phrase)
except Exception as e: except Exception as e:
print(e) print(e)
continue continue