Renamed class NiXSpamPlot to NiXSapmSum. Added getDomLen(). Rewrote showResult()
--- a/nixspamsum Sun Jun 07 11:40:02 2009 +0000
+++ b/nixspamsum Sun Jun 07 16:54:22 2009 +0000
@@ -3,13 +3,13 @@
# Copyright 2009 Pascal Volk
__author__ = 'Pascal Volk'
-__version__ = '0.1'
+__version__ = '0.1.1'
__date__ = '2009-06-07'
import os
import re
-class NiXSpamPlot:
+class NiXSapmSum:
"""Do sth ..."""
"""Regular expression pattern for mail logs from Postfix"""
@@ -22,7 +22,7 @@
def setLogFormat(self, format='postfix'):
if format == 'postfix':
- self._repo = re.compile(NiXSpamPlot.RE_PF)
+ self._repo = re.compile(NiXSapmSum.RE_PF)
else:
raise Exception('MTA/Logformat not supported yet.')
@@ -55,8 +55,10 @@
def getOptionParser():
from optparse import OptionParser
+ description = 'do something ...'
usage = 'usage: %prog [options] maillog [maillog [...]]'
- parser = OptionParser(usage=usage, description='do something ...')
+ version = '%prog '+__version__
+ parser = OptionParser(description=description,usage=usage,version=version)
parser.add_option('-d', action='store_true', dest='countByDom',
default=False, help='summarize all MX by domain')
parser.add_option('-m', action='store_false', dest='countByDom',
@@ -79,32 +81,57 @@
os.sys.stderr.write('Warning: %s\nskipped file: %s\n' % (e.strerror,
fname))
-def showResult(nixspamplot, options):
+def getDomLen(domainnames):
+ dlen = 0
+ for d in domainnames:
+ l = len(d)
+ if l > dlen:
+ dlen = l
+ return dlen
+
+def showResult(nixspamsum, options):
if options.countByDom:
- nixspamplot.countByDom()
- domains = nixspamplot.getDomains()
+ nixspamsum.countByDom()
+ domains = nixspamsum.getDomains()
else:
- domains = nixspamplot.getMXs()
- if options.percent:
- total = nixspamplot.getTotal()
+ domains = nixspamsum.getMXs()
k = 0 if options.order == 'name' else 1
doms = sorted(domains.items(), lambda d,c: cmp(d[k],c[k]), reverse=k)
- for d in doms:
- print "%s -> %d" % d
+ # print table
+ if options.oFormat == 'table':
+ dlen = getDomLen(domains.keys())+1
+ clen = len(str(max(domains.values())))
+ if options.percent:
+ total = nixspamsum.getTotal()
+ format = '%%%ds %%%dd %%5.2f %%%%' % (dlen, clen)
+ for d, c in doms:
+ dfrac = 100./total*c
+ print format % (d, c, dfrac)
+ else:
+ format = '%%%ds: %%%dd' % (dlen, clen)
+ for d in doms:
+ print format % d
+ # print comma separated values
+ elif options.oFormat == 'csv':
+ for d in doms:
+ print "'%s',%d" % d
+ # print a hint ;-)
+ else:
+ print "Output format '%s' is not supported" % options.oFormat
def main():
parser = getOptionParser()
opts, args = parser.parse_args()
if len(args) < 1:
parser.error('No logfiles specified')
- nsp = NiXSpamPlot()
- nsp.setLogFormat(opts.format)
+ nixss = NiXSapmSum()
+ nixss.setLogFormat(opts.format)
for fn in args:
fh = openLogFile(fn)
if fh is not None:
- nsp.parseLog(fh)
+ nixss.parseLog(fh)
fh.close()
- showResult(nsp, opts)
+ showResult(nixss, opts)
if __name__ == '__main__':
main()