# Weblog viewer # No 'from .. import' since var 'request' treads on NS's in Woven meths. import webloglib as wll import os, sys from urllib import unquote_plus as uqp from twisted.internet import reactor from twisted.web import microdom from twisted.web.woven import page, widgets this_dir = os.path.dirname(__file__) if not this_dir in sys.path: sys.path.append(this_dir) logfile = '../access-log' LOG = open(logfile) RECS = [] NUM_ROWS = 25 def update(): global RECS RECS.extend(LOG.readlines()) RECS = RECS[-NUM_ROWS*3:] reactor.callLater(5, update) update() class WeblogViewer(page.Page): """A Page used for viewing Apache access logs.""" templateDirectory = this_dir templateFile = "WeblogViewer.xhtml" # View factories and updates def wvupdate_alternateColor(self, request, node, data): """Makes our table rows alternate CSS classes""" # microdom.lmx is very handy; another example is located here: # http://twistedmatrix.com/documents/howto/picturepile#auto0 tr = microdom.lmx(node) tr['class'] = ('odd','even')[data['_number']%2] # Model factories def wmfactory_filename(self, request): """Returns the filename of the log being examined.""" return os.path.split(logfile)[1] def wmfactory_entries(self, request): """Return list of dict objects representing log entries""" entries = [] for rec in RECS: hit = [field.strip('"') for field in wll.log_fields(rec)] if hit[wll.status] == '200' and hit[wll.referrer] != '-': # We add _number so that our alternateColor view will work. d = {'_number': len(entries), 'ip': hit[wll.ip], 'timestamp': hit[wll.timestamp], 'request': hit[wll.request], 'request_resource': hit[wll.request].split()[1], 'status': hit[wll.status], 'bytes': hit[wll.bytes], 'referrer': uqp(hit[wll.referrer]).replace('&',' &'), 'agent': hit[wll.agent], } entries.append(d) return entries[-NUM_ROWS:] resource = WeblogViewer()