1# 2# format.py 3# 4# This file contains global functions for formatting and printing data. This 5# file should be imported into individual log-parsing scripts located in 6# logs/*. Data is all formatted in HTML. Writing to disk and/or emailng data 7# is left to __main__.py. 8# 9 10import os 11import re 12import locale 13 14from.config import prefs 15#import util 16#import interface 17import logparse 18from.import interface, util 19 20import logging 21logger = logging.getLogger(__name__) 22 23locale.setlocale(locale.LC_ALL,'')# inherit system locale 24#DEG = "°".encode('unicode_escape') 25DEG = u'\N{DEGREE SIGN}' 26CEL ="C" 27TIMEFMT ="%X" 28DATEFMT ="%x" 29 30definit_varfilter(): 31global varfilter 32global varpattern 33 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']))} 34 varfilter =dict((re.escape(k), v)for k, v in varfilter.items()) 35 varpattern = re.compile("|".join(varfilter.keys())) 36 37defwritetitle(title):# write title for a section 38if(title ==''or'\n'in title): 39 logger.error("Invalid title") 40raiseValueError 41 logger.debug("Writing title for "+ title) 42returntag('h2',0, title) 43 44defopentag(tag, block =0,id=None, cl =None):# write html opening tag 45 output ="" 46if(block): 47 output +='\n' 48 output +='<'+ tag 49if(id!=None): 50 output +=" id='"+id+"'" 51if(cl !=None): 52 output +=" class='"+ cl +"'" 53 output +='>' 54if(block): 55 output +='\n' 56return output 57 58defclosetag(tag, block =0):# write html closing tag 59if(block ==0): 60return"</"+ tag +">" 61else: 62return"\n</"+ tag +">\n" 63 64deftag(tag, block =0, content =""):# write html opening tag, content, and html closing tag 65 o =opentag(tag, block) 66 c =closetag(tag, block) 67return o + content + c 68 69defheader(template):# return a parsed html header from file 70# try: 71# copyfile(config['css'], config['dest'] + '/' + os.path.basename(config['css'])) 72# logger.debug("copied main.css") 73# except Exception as e: 74# logger.warning("could not copy main.css - " + str(e)) 75init_varfilter() 76 headercontent =open(template,'r').read() 77 headercontent = varpattern.sub(lambda m: varfilter[re.escape(m.group(0))], headercontent) 78return headercontent 79 80deforderbyfreq(l):# order a list by the frequency of its elements and remove duplicates 81 temp_l = l[:] 82 l =list(set(l)) 83 l = [[i, temp_l.count(i)]for i in l]# add count of each element 84 l.sort(key=lambda x:temp_l.count(x[0]))# sort by count 85 l = [i[0] +' ('+str(i[1]) +')'for i in l]# put element and count into string 86 l = l[::-1]# reverse 87return l 88 89defaddtag(l, tag):# add prefix and suffix tags to each item in a list 90 l2 = ['<'+ tag +'>'+ i +'</'+ tag +'>'for i in l] 91return l2 92 93deftruncl(input, limit):# truncate list 94if(len(input) > limit): 95 more =str(len(input) - limit) 96 output =input[:limit] 97 output.append("+ "+ more +" more") 98return(output) 99else: 100return(input) 101 102defplural(noun, quantity):# return "1 noun" or "n nouns" 103if(quantity ==1): 104return(str(quantity) +" "+ noun) 105else: 106return(str(quantity) +" "+ noun +"s") 107 108defparsesize(num, suffix='B'):# return human-readable size from number of bytes 109for unit in['','Ki','Mi','Gi','Ti','Pi','Ei','Zi']: 110ifabs(num) <1024.0: 111return"%3.1f%s%s"% (num, unit, suffix) 112 num /=1024.0 113return"%.1f%s%s"% (num,'Yi', suffix) 114 115deffsubject(template):# Replace variables in the title template provided in config 116 r = varpattern.sub(lambda m: varfilter[re.escape(m.group(0))], template) 117 logger.debug("Returning subject line "+ r) 118return r 119 120defwritedata(subtitle, data =None):# write title and data 121if(subtitle ==""): 122 logger.warning("No subtitle provided.. skipping section") 123return 124 125if(data ==None orlen(data) ==0): 126 logger.debug("No data provided.. just printing subtitle") 127returntag('p',0, subtitle) 128else: 129 logger.debug("Received data "+str(data)) 130 subtitle +=':' 131if(len(data) ==1): 132returntag('p',0, subtitle +' '+ data[0]) 133else: 134 output ="" 135 output +=tag('p',0, subtitle) 136 output +=opentag('ul',1) 137for datum in data: 138 output +=tag('li',0, datum) 139 output +=closetag('ul',1) 140return output 141