PEP-8-ified the code. Version 0.1.3 0.1.3
authorPascal Volk <user@localhost.localdomain.org>
Sun, 11 Apr 2010 19:39:48 +0000
changeset 11 a5f5a8f288da
parent 10 07b9fe5c6fcf
child 12 bcd26b807771
PEP-8-ified the code. Version 0.1.3
nixspamsum
--- a/nixspamsum	Sun Apr 11 19:16:28 2010 +0000
+++ b/nixspamsum	Sun Apr 11 19:39:48 2010 +0000
@@ -4,13 +4,14 @@
 # See COPYING for distribution information.
 
 __author__ = 'Pascal Volk'
-__version__ = '0.1.2'
-__date__ = '2009-07-03'
+__version__ = '0.1.3'
+__date__ = '2010-04-11'
 
 import os
 import re
 import fileinput
 
+
 class NiXSapmSum(object):
     """
     Small log parser class to parse and summarize NiX Spam DNSBL lookup
@@ -26,10 +27,9 @@
                 \sSpam\ssent\sto\sthe\smailhost\s(%s|%s)
                 \swas\sdetected\sby\sNiX\sSpam.*$''' % (RE_FQDN, RE_IPv4)
 
-
     def __init__(self):
         self._doms = {}
-        self._mxs  = {}
+        self._mxs = {}
         self._repo = None
 
     def setLogFormat(self, format='postfix'):
@@ -46,7 +46,7 @@
                 try:
                     self._mxs[mx] += 1
                 except KeyError:
-                    self._mxs[mx]  = 1
+                    self._mxs[mx] = 1
 
     def countByDom(self):
         ipv4po = re.compile(NiXSapmSum.RE_IPv4)
@@ -59,7 +59,7 @@
             try:
                 self._doms[dom] += self._mxs[mx]
             except KeyError:
-                self._doms[dom]  = self._mxs[mx]
+                self._doms[dom] = self._mxs[mx]
 
     def getDomains(self):
         return self._doms
@@ -71,14 +71,15 @@
 def getOptionParser():
     from optparse import OptionParser
     description = 'NiX Spam DNSBL lookup based rejects summarizer'
-    usage  = 'usage: %prog [options] maillog [maillog [...]]'
-    version = '%prog '+__version__
-    parser = OptionParser(description=description,usage=usage,version=version)
+    usage = 'usage: %prog [options] maillog [maillog [...]]'
+    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',
             help='count per MX host [default]')
-    parser.add_option('-o', dest='oFormat', default='table',metavar='FORMAT',
+    parser.add_option('-o', dest='oFormat', default='table', metavar='FORMAT',
             choices=('csv', 'table'),
             help='the output format: table or csv [default: %default]')
     parser.add_option('-p', action='store_true', dest='percent', default=False,
@@ -86,7 +87,7 @@
     parser.add_option('-s', dest='order', default='name', metavar='SORTBY',
             choices=('count', 'name'),
             help='arrange output by: name or count [default: %default]')
-    parser.add_option('-t', dest='format', default='postfix',metavar='MTA',
+    parser.add_option('-t', dest='format', default='postfix', metavar='MTA',
             choices=('postfix',),
             help='MTA that generated the maillog [default: %default]')
     return parser
@@ -123,24 +124,25 @@
 
 def buildTable(output, domains, percent, orderBy):
     k = 0 if orderBy == 'name' else 1
-    doms = sorted(domains.items(), lambda d,c: cmp(d[k],c[k]), reverse=k)
+    doms = sorted(domains.items(), lambda d, c: cmp(d[k], c[k]), reverse=k)
     dlen = len(max(domains.iterkeys(), key=len)) + 1
     clen = len(str(max(domains.values())))
     total = sum(domains.values())
     if percent:
         format = ' %%%ds  %%%dd  %%6.2f %%%%\n' % (-dlen, clen)
         for d, c in doms:
-            dfrac = 100./total*c
+            dfrac = 100. / total * c
             output.write(format % (d, c, dfrac))
-        output.write('%s\n' % ((clen+dlen+14)*'-'))
+        output.write('%s\n' % ((clen + dlen + 14) * '-'))
         output.write(format % ('total', total, 100))
     else:
         format = ' %%%ds  %%%dd\n' % (-dlen, clen)
         for d in doms:
             output.write(format % d)
-        output.write('%s\n' % ((clen+dlen+4)*'-'))
+        output.write('%s\n' % ((clen + dlen + 4) * '-'))
         output.write(format % ('total', total))
 
+
 def showResult(nixspamsum, options):
     if options.countByDom:
         nixspamsum.countByDom()
@@ -168,6 +170,7 @@
     # show the result
     print output.getvalue()
 
+
 def main():
     parser = getOptionParser()
     opts, args = parser.parse_args()
@@ -189,5 +192,6 @@
     showResult(nixss, opts)
     return 0
 
+
 if __name__ == '__main__':
     os.sys.exit(main())