Add filter mechanism for plugins

This commit is contained in:
Gregory Soutade 2016-02-04 20:46:12 +01:00
parent 79da471398
commit bb189425f1
3 changed files with 42 additions and 9 deletions

View File

@ -48,10 +48,18 @@ class DisplayHTMLRaw(object):
def _build(self, f, html):
if html: f.write(html)
def build(self, f):
def build(self, f, filters=None):
if filters: self.filter(filters)
self._buildHTML()
self._build(f, self.html)
def _filter(self, function, **kwargs):
pass
def filter(self, filters):
for (args, function) in filters:
self._filter(function, **args)
def getTitle(self):
return ''
@ -189,6 +197,18 @@ class DisplayHTMLBlockTable(DisplayHTMLBlock):
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)
@ -315,11 +335,14 @@ class DisplayHTMLPage(object):
if title == b.getTitle():
return b
return None
def getAllBlocks(self):
return self.blocks
def appendBlock(self, block):
self.blocks.append(block)
def build(self, root, displayVersion=True):
def build(self, root, displayVersion=True, filters=None):
filename = os.path.join(root, self.filename)
base = os.path.dirname(filename)
@ -339,7 +362,7 @@ class DisplayHTMLPage(object):
f.write(u'<title>%s</title>' % (self.title))
f.write(u'</head><body>')
for block in self.blocks:
block.build(f)
block.build(f, filters=filters)
if displayVersion:
f.write(u'<center>Generated by <a href="%s">IWLA %s</a></center>' %
("http://indefero.soutade.fr/p/iwla", self.iwla.getVersion()))
@ -349,8 +372,12 @@ class DisplayHTMLPage(object):
class DisplayHTMLBuild(object):
def __init__(self, iwla):
self.pages = []
self.iwla = iwla
self.filters = []
self.clear()
def clear(self):
self.pages = []
def createPage(self, *args):
return DisplayHTMLPage(self.iwla, *args)
@ -364,6 +391,9 @@ class DisplayHTMLBuild(object):
return page
return None
def getAllPages(self):
return self.pages
def addPage(self, page):
self.pages.append(page)
@ -378,7 +408,11 @@ class DisplayHTMLBuild(object):
os.symlink(target, link_name)
for page in self.pages:
page.build(root)
page.build(root, filters=self.filters)
def addColumnFilter(self, column, function, args):
self.filters.append(({'column':column, 'args':args}, function))
#
# Global functions
@ -417,4 +451,3 @@ def createCurTitle(iwla, title):
if domain_name:
title += u' - %s' % (domain_name)
return title

View File

@ -230,7 +230,7 @@ class IWLA(object):
return self.meta_infos
def _clearDisplay(self):
self.display = DisplayHTMLBuild(self)
self.display.clear()
return self.display
def getDBFilename(self, time):

View File

@ -110,7 +110,7 @@ class IWLADisplayTrackUsers(IPlugin):
index = self.iwla.getDisplayIndex()
table = display.createBlock(DisplayHTMLBlockTable, title, [self.iwla._(u'IP'), self.iwla._(u'Last Access'), self.iwla._(u'Pages'), self.iwla._(u'Hits')])
table = display.createBlock(DisplayHTMLBlockTable, title, [self.iwla._(u'Host'), self.iwla._(u'Last Access'), self.iwla._(u'Pages'), self.iwla._(u'Hits')])
table.setColsCSSClass(['', '', 'iwla_page', 'iwla_hit'])
for ip in self.tracked_ip:
if not ip in hits.keys(): continue