Author: humbedooh
Date: Fri Mar 27 09:08:08 2015
New Revision: 1669530

URL: http://svn.apache.org/r1669530
Log:
Add a unified tallying program, adjust all plugins to work with it.
This essentially obsoletes stv_tool.py as the calculations are done in the 
plugins, and the data loading is done here, either through election.py (if run 
locally on the vote machine) or through tally.py if run remotely.

Added:
    steve/trunk/pysteve/cli/tally.py
Modified:
    steve/trunk/pysteve/lib/plugins/ap.py
    steve/trunk/pysteve/lib/plugins/cop.py
    steve/trunk/pysteve/lib/plugins/dh.py
    steve/trunk/pysteve/lib/plugins/fic.py
    steve/trunk/pysteve/lib/plugins/fpp.py
    steve/trunk/pysteve/lib/plugins/mntv.py
    steve/trunk/pysteve/lib/plugins/stv.py
    steve/trunk/pysteve/lib/plugins/yna.py

Added: steve/trunk/pysteve/cli/tally.py
URL: 
http://svn.apache.org/viewvc/steve/trunk/pysteve/cli/tally.py?rev=1669530&view=auto
==============================================================================
--- steve/trunk/pysteve/cli/tally.py (added)
+++ steve/trunk/pysteve/cli/tally.py Fri Mar 27 09:08:08 2015
@@ -0,0 +1,92 @@
+#!/usr/bin/env python
+#
+# Licensed to the Apache Software Foundation (ASF) under one
+# or more contributor license agreements.  See the NOTICE file
+# distributed with this work for additional information
+# regarding copyright ownership.  The ASF licenses this file
+# to you under the Apache License, Version 2.0 (the
+# "License"); you may not use this file except in compliance
+# with the License.  You may obtain a copy of the License at
+#
+#   http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing,
+# software distributed under the License is distributed on an
+# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+# KIND, either express or implied.  See the License for the
+# specific language governing permissions and limitations
+# under the License.
+""" Tallying program for pySTeVe """
+import sys, re, json, os
+
+version = 2
+if sys.hexversion < 0x03000000:
+    import ConfigParser as configparser
+else:
+    import configparser
+    version = 3
+from os import listdir
+from os.path import isdir, isfile
+path = os.path.abspath(os.getcwd() + "/../") # Get parent dir, so we can snag 
'lib' from there
+
+sys.path.append(path)
+sys.path.append(os.path.basename(sys.argv[0]))
+
+# Fetch config (hack, hack, hack)
+config = configparser.RawConfigParser()
+config.read(path + '/steve.cfg')
+
+# Some quick paths
+homedir = config.get("general", "homedir")
+
+# Import the goodness
+from lib import response, voter, election, form, constants
+
+
+
+import argparse
+
+parser = argparse.ArgumentParser(description='Command line options.')
+parser.add_argument('-e', '--election', dest='election', type=str, 
help='Election to load')
+parser.add_argument('-i', '--issue', dest='issue', type=str, help='Issue to 
load')
+parser.add_argument('-f', '--file', dest='vfile', type=str, help='Monitor file 
to load. Used by monitors instead of specifying election and issue')
+parser.add_argument("-v", "--verbose", dest="verbose", action="store_true", 
help="Enable verbose logging", default=False)
+args = parser.parse_args()
+
+baseData = None
+issueData = None
+votes = None
+
+if args.election and args.issue:
+    issueData = election.getIssue(args.election, args.issue)
+    baseData = election.getBasedata(args.election)
+    votes = election.getVotes(args.election, args.issue)
+elif args.vfile:
+    with open(args.vfile, "r") as f:
+        js = json.loads(f.read())
+        f.close()
+    issueData = js['issue']
+    baseData = js['base']
+    votes = js['votes']
+else:
+    parser.print_help()
+            
+
+if baseData and issueData:
+    if not votes or len(votes) == 0:
+        print("No votes have been cast yet, cannot tally")
+        sys.exit(-1)
+    else:
+        tally, prettyprint = election.tally(votes, issueData)
+        if args.verbose and tally.get('debug'):
+            print("------\nDebug:\n------")
+            for line in tally['debug']:
+                print(line)
+        print("----------")
+        print("Base data:")
+        print("----------")
+        print("Election:   %s" % baseData['title'])
+        print("Issue:      %s" % issueData['title'])
+        print("Votes cast: %u" % len(votes))
+        print("\n-----------------\nElection results:\n-----------------")
+        print(prettyprint)
\ No newline at end of file

Modified: steve/trunk/pysteve/lib/plugins/ap.py
URL: 
http://svn.apache.org/viewvc/steve/trunk/pysteve/lib/plugins/ap.py?rev=1669530&r1=1669529&r2=1669530&view=diff
==============================================================================
--- steve/trunk/pysteve/lib/plugins/ap.py (original)
+++ steve/trunk/pysteve/lib/plugins/ap.py Fri Mar 27 09:08:08 2015
@@ -38,7 +38,7 @@ def tallyAP(votes, issue):
         else:
             raise Exception("Invalid vote found in votes db!")
 
-    return {
+    js = {
         'votes': len(votes),
         'yes': y,
         'no': n,
@@ -46,6 +46,14 @@ def tallyAP(votes, issue):
         'binding_yes': by,
         'binding_no': bn
     }
+    
+    return js, """
+Yes:            %4u
+No:             %4u
+Abstain:        %4u
+Binding Yes:    %4u
+Binding No:     %4u
+""" % (y,n,a,by,bn)
 
 
 def validateAP(vote, issue):

Modified: steve/trunk/pysteve/lib/plugins/cop.py
URL: 
http://svn.apache.org/viewvc/steve/trunk/pysteve/lib/plugins/cop.py?rev=1669530&r1=1669529&r2=1669530&view=diff
==============================================================================
--- steve/trunk/pysteve/lib/plugins/cop.py (original)
+++ steve/trunk/pysteve/lib/plugins/cop.py Fri Mar 27 09:08:08 2015
@@ -155,7 +155,10 @@ def tallyCOP(votes, issue):
         'winners': winners,
         'winnernames': winners,
         'debug': debug
-    }
+    }, """
+Winners:
+ - %s
+""" % "\n - ".join(winners)
 
 
 constants.appendVote (

Modified: steve/trunk/pysteve/lib/plugins/dh.py
URL: 
http://svn.apache.org/viewvc/steve/trunk/pysteve/lib/plugins/dh.py?rev=1669530&r1=1669529&r2=1669530&view=diff
==============================================================================
--- steve/trunk/pysteve/lib/plugins/dh.py (original)
+++ steve/trunk/pysteve/lib/plugins/dh.py Fri Mar 27 09:08:08 2015
@@ -82,7 +82,10 @@ def tallyDH(votes, issue):
         'votes': len(votes),
         'winners': winners,
         'winnernames': winnernames,
-    }
+    }, """
+Winners:
+ - %s
+""" % "\n - ".join(winnernames)
 
 
 constants.appendVote (

Modified: steve/trunk/pysteve/lib/plugins/fic.py
URL: 
http://svn.apache.org/viewvc/steve/trunk/pysteve/lib/plugins/fic.py?rev=1669530&r1=1669529&r2=1669530&view=diff
==============================================================================
--- steve/trunk/pysteve/lib/plugins/fic.py (original)
+++ steve/trunk/pysteve/lib/plugins/fic.py Fri Mar 27 09:08:08 2015
@@ -84,7 +84,10 @@ def tallyFIC(votes, issue):
         'votes': len(votes),
         'winners': winners,
         'winnernames': winnernames,
-    }
+    }, """
+Winners:
+ - %s
+""" % "\n - ".join(winnernames)
 
 
 constants.appendVote(

Modified: steve/trunk/pysteve/lib/plugins/fpp.py
URL: 
http://svn.apache.org/viewvc/steve/trunk/pysteve/lib/plugins/fpp.py?rev=1669530&r1=1669529&r2=1669530&view=diff
==============================================================================
--- steve/trunk/pysteve/lib/plugins/fpp.py (original)
+++ steve/trunk/pysteve/lib/plugins/fpp.py Fri Mar 27 09:08:08 2015
@@ -62,13 +62,18 @@ def tallyFPP(votes, issue):
 
 
     # Return the data
-    return {
+    js = {
         'votes': len(votes),
         'winners': winners,
         'winnernames': winnernames,
         'winnerpct': ((1.00*max(l)/len(votes))*100) if len(votes) > 0 else 
0.00,
         'tie': True if len(winners) > 1 else False
     }
+    
+    return js, """
+Winners:
+ - %s
+""" % "\n - ".join(["%s (%f%%)" % (n,js['winnerpct']) for n in winnernames])
 
 
 constants.appendVote (

Modified: steve/trunk/pysteve/lib/plugins/mntv.py
URL: 
http://svn.apache.org/viewvc/steve/trunk/pysteve/lib/plugins/mntv.py?rev=1669530&r1=1669529&r2=1669530&view=diff
==============================================================================
--- steve/trunk/pysteve/lib/plugins/mntv.py (original)
+++ steve/trunk/pysteve/lib/plugins/mntv.py Fri Mar 27 09:08:08 2015
@@ -77,7 +77,10 @@ def tallyMNTV(votes, issue):
         'votes': len(votes),
         'winners': winners,
         'winnernames': winnernames,
-    }
+    }, """
+Winners:
+ - %s
+""" % "\n - ".join(winnernames)
 
 
 constants.appendVote (

Modified: steve/trunk/pysteve/lib/plugins/stv.py
URL: 
http://svn.apache.org/viewvc/steve/trunk/pysteve/lib/plugins/stv.py?rev=1669530&r1=1669529&r2=1669530&view=diff
==============================================================================
--- steve/trunk/pysteve/lib/plugins/stv.py (original)
+++ steve/trunk/pysteve/lib/plugins/stv.py Fri Mar 27 09:08:08 2015
@@ -347,7 +347,10 @@ def tallySTV(votes, issue):
         'winners': winners,
         'winnernames': winnernames,
         'debug': debug
-    }
+    }, """
+Winners:
+ - %s
+""" % "\n - ".join(winnernames)
 
 
 constants.appendVote (

Modified: steve/trunk/pysteve/lib/plugins/yna.py
URL: 
http://svn.apache.org/viewvc/steve/trunk/pysteve/lib/plugins/yna.py?rev=1669530&r1=1669529&r2=1669530&view=diff
==============================================================================
--- steve/trunk/pysteve/lib/plugins/yna.py (original)
+++ steve/trunk/pysteve/lib/plugins/yna.py Fri Mar 27 09:08:08 2015
@@ -38,7 +38,11 @@ def tallyYNA(votes, issue):
         'yes': y,
         'no': n,
         'abstain': a
-    }
+    }, """
+Yes:            %4u
+No:             %4u
+Abstain:        %4u
+""" % (y,n,a)
 
 def validateYNA(vote, issue):
     "Tries to validate a vote, returns why if not valid, None otherwise"


Reply via email to