# # format.py # # This file contains global functions for formatting and printing data. This # file should be imported into individual log-parsing scripts located in # logs/*. Data is all formatted in HTML. Writing to disk and/or emailng data # is left to __main__.py. # import os import re import locale from .config import prefs #import util #import interface import logparse from . import interface, util import logging logger = logging.getLogger(__name__) locale.setlocale(locale.LC_ALL, '') # inherit system locale #DEG = "°".encode('unicode_escape') DEG = u'\N{DEGREE SIGN}' CEL = "C" TIMEFMT = "%X" DATEFMT = "%x" def init_varfilter(): global varfilter global varpattern varfilter = {"$title$": prefs['title'], "$date$": interface.start.strftime(DATEFMT),"$time$": interface.start.strftime(TIMEFMT), "$hostname$": util.hostname(prefs['hostname-path']), "$version$": logparse.__version__, "$css$": os.path.relpath(prefs['css'], os.path.dirname(prefs['output']))} varfilter = dict((re.escape(k), v) for k, v in varfilter.items()) varpattern = re.compile("|".join(varfilter.keys())) def writetitle(title): # write title for a section if (title == '' or '\n' in title): logger.error("Invalid title") raise ValueError logger.debug("Writing title for " + title) return tag('h2', 0, title) def opentag(tag, block = 0, id = None, cl = None): # write html opening tag output = "" if (block): output += '\n' output += '<' + tag if (id != None): output += " id='" + id + "'" if (cl != None): output += " class='" + cl + "'" output += '>' if (block): output += '\n' return output def closetag(tag, block = 0): # write html closing tag if (block == 0): return "" else: return "\n\n" def tag(tag, block = 0, content = ""): # write html opening tag, content, and html closing tag o = opentag(tag, block) c = closetag(tag, block) return o + content + c def header(template): # return a parsed html header from file # try: # copyfile(config['css'], config['dest'] + '/' + os.path.basename(config['css'])) # logger.debug("copied main.css") # except Exception as e: # logger.warning("could not copy main.css - " + str(e)) init_varfilter() headercontent = open(template, 'r').read() headercontent = varpattern.sub(lambda m: varfilter[re.escape(m.group(0))], headercontent) return headercontent def orderbyfreq(l): # order a list by the frequency of its elements and remove duplicates temp_l = l[:] l = list(set(l)) l = [[i, temp_l.count(i)] for i in l] # add count of each element l.sort(key=lambda x:temp_l.count(x[0])) # sort by count l = [i[0] + ' (' + str(i[1]) + ')' for i in l] # put element and count into string l = l[::-1] # reverse return l def addtag(l, tag): # add prefix and suffix tags to each item in a list l2 = ['<' + tag + '>' + i + '' for i in l] return l2 def truncl(input, limit): # truncate list if (len(input) > limit): more = str(len(input) - limit) output = input[:limit] output.append("+ " + more + " more") return(output) else: return(input) def plural(noun, quantity): # return "1 noun" or "n nouns" if (quantity == 1): return(str(quantity) + " " + noun) else: return(str(quantity) + " " + noun + "s") def parsesize(num, suffix='B'): # return human-readable size from number of bytes for unit in ['','Ki','Mi','Gi','Ti','Pi','Ei','Zi']: if abs(num) < 1024.0: return "%3.1f %s%s" % (num, unit, suffix) num /= 1024.0 return "%.1f%s%s" % (num, 'Yi', suffix) def fsubject(template): # Replace variables in the title template provided in config r = varpattern.sub(lambda m: varfilter[re.escape(m.group(0))], template) logger.debug("Returning subject line " + r) return r def writedata(subtitle, data = None): # write title and data if (subtitle == ""): logger.warning("No subtitle provided.. skipping section") return if (data == None or len(data) == 0): logger.debug("No data provided.. just printing subtitle") return tag('p', 0, subtitle) else: logger.debug("Received data " + str(data)) subtitle += ':' if (len(data) == 1): return tag('p', 0, subtitle + ' ' + data[0]) else: output = "" output += tag('p', 0, subtitle) output += opentag('ul', 1) for datum in data: output += tag('li', 0, datum) output += closetag('ul', 1) return output