Small code cleanups. Added current year to the copyright notices.
authorPascal Volk <user@localhost.localdomain.org>
Wed, 17 Feb 2010 23:34:16 +0000
changeset 7 83c4798e291d
parent 6 723b4c4e15cb
child 8 7f131cf431bc
Small code cleanups. Added current year to the copyright notices. - Let the OptionParser check the arguments for the options 'format' and 'oFormat'. - Replaced function getDomLen() by a one-liner.
COPYING
nixspamsum
--- a/COPYING	Fri Nov 27 13:52:11 2009 +0000
+++ b/COPYING	Wed Feb 17 23:34:16 2010 +0000
@@ -1,4 +1,4 @@
-Copyright (c) 2009, Pascal Volk
+Copyright (c) 2009 - 2010, Pascal Volk
 All rights reserved.
 
 Redistribution and use in source and binary forms, with or without modification,
--- a/nixspamsum	Fri Nov 27 13:52:11 2009 +0000
+++ b/nixspamsum	Wed Feb 17 23:34:16 2010 +0000
@@ -1,6 +1,6 @@
 #!/usr/bin/env python
 # -*- coding: utf-8 -*-
-# Copyright 2009 Pascal Volk
+# Copyright 2009 - 2010 Pascal Volk
 # See COPYING for distribution information.
 
 __author__ = 'Pascal Volk'
@@ -66,15 +66,18 @@
     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',
+            choices=('csv', 'table'),
             help='the output format: table or csv [default: %default]')
     parser.add_option('-p', action='store_true', dest='percent', default=False,
             help='show also percentages in table output [default: %default]')
     parser.add_option('-s', dest='order', default='name', metavar='SORTBY',
             help='arrange output by: name or count [default: %default]')
     parser.add_option('-t', dest='format', default='postfix',metavar='MTA',
+            choices=('postfix',),
             help='MTA that generated the maillog [default: %default]')
     return parser
 
+
 def openLogFile(fname):
     try:
         fh = open(fname)
@@ -83,18 +86,11 @@
         os.sys.stderr.write('Warning: %s\nskipped file: %s\n' % (e.strerror,
             fname))
 
-def getDomLen(domainnames):
-    dlen = 0
-    for d in domainnames:
-        l = len(d)
-        if l > dlen:
-            dlen = l
-    return dlen
 
 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)
-    dlen = getDomLen(domains.keys())+1
+    dlen = len(max(domains.iterkeys(), key=len)) + 1
     clen = len(str(max(domains.values())))
     total = sum(domains.values())
     if percent:
@@ -117,7 +113,7 @@
         domains = nixspamsum.getDomains()
     else:
         domains = nixspamsum.getMXs()
-    if not len(domains):
+    if not domains:
         print "No NiX Spam DNSBL rejects found"
         return
 
@@ -141,8 +137,6 @@
 def main():
     parser = getOptionParser()
     opts, args = parser.parse_args()
-    if opts.oFormat not in ('csv', 'table'):
-        parser.error("Output format '%s' is not supported" % opts.oFormat)
     if len(args) < 1:
         parser.error('No log file specified')
     nixss = NiXSapmSum()