Author: gstein
Date: Sat Jun 8 08:12:42 2013
New Revision: 1490932
URL: http://svn.apache.org/r1490932
Log:
Continued draft work.
* lib/steve.py:
(filestuff, get_hash_of, hash_file, read_tally, found_in_group,
get_date, contains_duplicates, not_valid): new functions based
on those found in steve.pm
(_RE_TALLY): helper regex for read_tally()
(randomize, ballots): draft skeletons for these funcs from steve.pm
Modified:
steve/trunk/lib/steve.py
Modified: steve/trunk/lib/steve.py
URL:
http://svn.apache.org/viewvc/steve/trunk/lib/steve.py?rev=1490932&r1=1490931&r2=1490932&view=diff
==============================================================================
--- steve/trunk/lib/steve.py (original)
+++ steve/trunk/lib/steve.py Sat Jun 8 08:12:42 2013
@@ -20,6 +20,9 @@
import sys
import os
+import hashlib
+import re
+import time
SCRIPT = os.path.basename(sys.argv[0])
@@ -56,3 +59,75 @@ def get_group(fname):
group.add(line)
return group
+
+
+def filestuff(fname):
+ "Compute a unique key for FNAME based on its file info."
+ s = os.stat(fname)
+ return '%d:%d' % (s.st_ino, s.st_mtime)
+
+
+def get_hash_of(datum):
+ "Compute the (hex) MD5 hash of the string DATUM."
+ return hashlib.md5(datum).hexdigest()
+
+
+def hash_file(fname):
+ "Compute the (hex) MD5 hash of the file FNAME."
+ return get_hash_of(open(fname).read())
+
+
+_RE_TALLY = re.compile('] (\\S+) (\\S+)$')
+
+def read_tally(fname):
+ result = { }
+ for line in open(fname).readlines():
+ match = _RE_TALLY.search(line)
+ if match:
+ result[match.group(1)] = match.group(2)
+ else:
+ print 'WARNING: Invalid vote in tally:', line
+
+ return result
+
+
+def found_in_group(voter, fname):
+ "Returns True if the given VOTER is in the group specified in FNAME."
+ group = get_group(fname)
+ return voter in group
+
+
+def get_date():
+ "Format and return the current time."
+ return time.strftime('%Y/%m/%d %H:%M:%S', time.gmtime())
+
+
+def contains_duplicates(s):
+ "Returns True if any characters in S are duplicated."
+ return len(set(s)) != len(s)
+
+
+def not_valid(votes, valid):
+ "Returns True if any vote in VOTES is not in VALID."
+ for v in votes:
+ if v not in valid:
+ return True
+ return False
+
+
+def randomize(text):
+ ### todo
+
+ prolog = [ ]
+ choices = [ ]
+ epilog = [ ]
+
+ return prolog, choices, epilog
+
+
+def ballots(text):
+ ### todo
+
+ ballots = [ ]
+
+ return ballots