Author: jim
Date: Thu Jun 13 13:18:44 2013
New Revision: 1492665

URL: http://svn.apache.org/r1492665
Log:
Make number of seats cli driven : idea and code from Jake Farrell

Modified:
    steve/trunk/monitoring/stv_tool.py

Modified: steve/trunk/monitoring/stv_tool.py
URL: 
http://svn.apache.org/viewvc/steve/trunk/monitoring/stv_tool.py?rev=1492665&r1=1492664&r2=1492665&view=diff
==============================================================================
--- steve/trunk/monitoring/stv_tool.py (original)
+++ steve/trunk/monitoring/stv_tool.py Thu Jun 13 13:18:44 2013
@@ -30,6 +30,7 @@
 import sys
 import os.path
 import random
+import argparse
 import ConfigParser
 import re
 
@@ -88,6 +89,11 @@ def read_nominees(votefile):
   ini_fname = os.path.join(os.path.dirname(votefile),
                            'board_nominations.ini')
 
+  # Use the below try instead to catch this??
+  if not os.path.exists(ini_fname):
+    print >> sys.stderr, "Error: board_nominations.ini could not be found at " 
+ ini_fname
+    sys.exit(2)
+
   config = ConfigParser.ConfigParser()
   config.read(ini_fname)
   try:
@@ -377,24 +383,26 @@ def dbg(fmt, *args):
     print fmt % args
 
 
-def usage():
-  print 'USAGE: %s [-v] RAW_VOTES_FILE' % (os.path.basename(sys.argv[0]),)
-  sys.exit(1)
-
-
 if __name__ == '__main__':
-  if len(sys.argv) < 2 or len(sys.argv) > 3:
-    usage()
-
-  if sys.argv[1] == '-v':
-    VERBOSE = True
+  parser = argparse.ArgumentParser(description="Calculate a winner for a vote")
+  parser.add_argument('raw_file')
+  parser.add_argument("-s", "--seats", dest="seats", type=int,
+                      help="Number of seats available, default 9",
+                      default=9)
+  parser.add_argument("-v", "--verbose", dest="verbose", action="store_true",
+                      help="Enable verbose logging", default=False)
+
+  args = parser.parse_args()
+
+  VERBOSE = args.verbose
+  votefile = args.raw_file
+  num_seats = args.seats
 
-  votefile = sys.argv[-1]
   if not os.path.exists(votefile):
-    usage()
+    parser.print_help()
+    sys.exit(1)
 
   names, votes = load_votes(votefile)
 
-  ### take the count from options? (for whatif.cgi)
-  run_vote(names, votes, 9)
+  run_vote(names, votes, num_seats)
   print 'Done!'


Reply via email to