changeset 2:343351be18e8

Se crea una funcion para leer el listado de votantes en su formato original
author "German Poo-Caaman~o <gpoo@gnome.org>"
date Thu, 07 Dec 2006 16:37:33 -0300
parents 910b2909e680
children b8f7fd73d44e
files vote-counter.py
diffstat 1 files changed, 44 insertions(+), 21 deletions(-) [+]
line wrap: on
line diff
--- a/vote-counter.py	Thu Dec 07 12:21:02 2006 -0300
+++ b/vote-counter.py	Thu Dec 07 16:37:33 2006 -0300
@@ -15,8 +15,8 @@
         self.token = 0
         self.votes = []
 
-    def add_vote (self, name, id):
-        self.votes.append ((name, id))
+    def add_vote(self, name, id):
+        self.votes.append((name, id))
 
 class Candidate:
     def __init__ (self, name, id):
@@ -48,12 +48,40 @@
 secret_cookie = sys.argv[2] # secret cookie
 voter_list = sys.argv[3]    # list of valid voter addresses
 
-# hash from valid addresses to whether they have sent in a ballot yet
-valid_addresses = {}
+def get_voters(filename):
+	# hash from valid addresses to whether they have sent in a ballot yet
+	addresses = {}
+
+	nospam_re = re.compile("no_spam\.?")
+	comment_re = re.compile("^#.*")
+	entry_re = re.compile(" *(.*?)(<.*?>) *\((.*?)\) *")
+
+	def unmunge_email (addr):
+		unmunged = nospam_re.sub ("", addr)
+		return unmunged
+
+	fp = open(filename)
 
-voter_handle = open (voter_list)
-for voter_addr in voter_handle.readlines ():
-    valid_addresses[string.strip (voter_addr)] = 0
+	for line in fp.readlines():
+		line = comment_re.sub("", line)
+		string.strip(line)
+		if line == "" or line == "\n":
+			continue
+
+		match = entry_re.search(line)
+		if match:
+			name = string.strip(match.group(1))
+			email = unmunge_email(string.strip(match.group(2)))
+			contribution = string.strip(match.group(3))
+			addresses[string.strip(email)] = 0
+		else:
+			print "No match: " + line
+
+	fp.close()
+
+	return addresses
+
+valid_addresses = get_voters(voter_list)
 
 handle = open (filename)
 lines = handle.readlines ()
@@ -119,7 +147,7 @@
         continue
     
 if current_ballot:
-    ballots.append (current_ballot)    
+    ballots.append(current_ballot)    
         
 handle.close ()
 
@@ -133,7 +161,7 @@
     return 0
 
 dup_tokens = {}
-def md5_is_bad (b):
+def md5_is_bad(b):
     #key = b.member + secret_cookie
     key = "%s%s" % (b.member, secret_cookie)
     m = md5.new (key)
@@ -150,8 +178,8 @@
         print "Bad auth token is %s hashed from '%s'" % (token, key)
         return 1
 
-def valid_voter (addr):
-    return valid_addresses.has_key (addr)
+def valid_voter(addr):
+    return valid_addresses.has_key(addr)
 
 valid_ballots = {}
 
@@ -189,8 +217,8 @@
 
 ## Print results only after all errors have been printed, so
 ## we don't lose any errors.
-valids = valid_ballots.values ()
-valids.sort (tupcmp)
+valids = valid_ballots.values()
+valids.sort(tupcmp)
 for (b, i) in valids:
     print "Ballot %d:" % i
 
@@ -218,11 +246,11 @@
     if not valid_addresses[addr]:
         print addr
 
-def cmpcand (a, b):
+def cmpcand(a, b):
     return cmp (a.count, b.count)
 
-cand_list = candidates.values ()
-cand_list.sort (cmpcand)
+cand_list = candidates.values()
+cand_list.sort(cmpcand)
 
 print ""
 print ""
@@ -233,8 +261,3 @@
 
 for c in cand_list:
     print "  %s (%d votes)" % (c.name, c.count)
-
-
-
-
-