NiXSapmSum.getTotal(): deleted
authorPascal Volk <user@localhost.localdomain.org>
Mon, 08 Jun 2009 16:10:07 +0000
changeset 2 a72ea07394cc
parent 1 7d5cee19c20a
child 3 6b0d09cdfbdb
NiXSapmSum.getTotal(): deleted buildTable(): implemented showResult(): generate output via cStringIO.StringIO moved table stuff to the new function buildTable() main(): check if output format is supported before any action
nixspamsum
--- a/nixspamsum	Sun Jun 07 16:54:22 2009 +0000
+++ b/nixspamsum	Mon Jun 08 16:10:07 2009 +0000
@@ -4,7 +4,7 @@
 
 __author__ = 'Pascal Volk'
 __version__ = '0.1.1'
-__date__ = '2009-06-07'
+__date__ = '2009-06-08'
 
 import os
 import re
@@ -50,9 +50,6 @@
     def getMXs(self):
         return self._mxs
 
-    def getTotal(self):
-        return sum(self._mxs.values())
-
 def getOptionParser():
     from optparse import OptionParser
     description = 'do something ...'
@@ -89,39 +86,48 @@
             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
+    clen = len(str(max(domains.values())))
+    total = sum(domains.values())
+    if percent:
+        format = '%%%ds  %%%dd  %%5.2f %%%%\n' % (dlen, clen)
+        for d, c in doms:
+            dfrac = 100./total*c
+            output.write(format % (d, c, dfrac))
+    else:
+        format = '%%%ds  %%%dd\n' % (dlen, clen)
+        for d in doms:
+            output.write(format % d)
+
 def showResult(nixspamsum, options):
+    from cStringIO import StringIO
+    output = StringIO()
     if options.countByDom:
         nixspamsum.countByDom()
         domains = nixspamsum.getDomains()
     else:
         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)
-    # print table
+    # build the 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
+        buildTable(output, domains, options.percent, options.order)
+    # generate comma separated values
     elif options.oFormat == 'csv':
-        for d in doms:
-            print "'%s',%d" % d
-    # print a hint ;-)
+        for d in domains.items():
+            output.write("'%s',%d\n" % d)
+    # should never be reached
     else:
-        print "Output format '%s' is not supported" % options.oFormat
+        print "Oops, error in function showResult() happend"
+    # show the result
+    print output.getvalue()
 
 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 logfiles specified')
     nixss = NiXSapmSum()