#!/usr/bin/env python2
'''ballotnowauditlog.py - analyze a Hart InterCivic BallotNow audit log
Date: 2004-12-07

TODO: include record # in each output line
 make catalog of all codes and descriptions, and a sense for contents of line

 write a new program to just make audit log translucent, with "key" as arg
 add command line options for station (A-H), etc.

 also print contest for "Confirm Overvote", "Resolve Action", "Set Write-In",
 code 33 "Action cancelled"
 21 CVR Write

maybe Activate Contest, Save Contest,  Restore Options
 anything with race in it

  note truncated trailing lines, meaning some stuff is missed??

Sample input:

also:
 34 Resolve Action Select Steve Bosley for Contest REGENT O
     which also needs "Save Ballot"? and/or "Save Contest"?
 68 Save Ballot Batch 1, SeqNum 195, S/N 995
 65 Save Contest PRESIDENTIAL ELECTORS
 61 Set Write-in UNITED STATES SENATOR Write-in Uncertifi
 34 Resolve Action Select Write-In for Contest UNITED STATE
 63 Confirm Overvote REPRESENTATIVE TO THE 109th|UNITED STATE

 what is Restore Options?

96,758 pstahl 12/6/2004 10:24:35AM 30 Auto Resolve UnderVote Batch 126, SeqNum 309, S/N 656372
96,759 pstahl 12/6/2004 10:24:35AM 30 Contest Title STATE REPRESENTATIVE - DISTRICT 11
96,760 pstahl 12/6/2004 10:24:35AM 30 Contest Title |ST. VRAIN SCHOOL DISTRICT |BALLOT ISSU
96,761 pstahl 12/6/2004 10:24:35AM 30 Autoresolve damaged contest Batch 126, SeqNum 309, S/N 656372
96,762 pstahl 12/6/2004 10:24:35AM 30 Contest Title |ST. VRAIN SCHOOL DISTRICT |BALLOT ISSU
96,763 pstahl 12/6/2004 10:24:35AM 30 Auto Resolve UnderVote Batch 126, SeqNum 309, S/N 656372
96,764 pstahl 12/6/2004 10:24:35AM 30 Auto Resolve UnderVote Batch 126, SeqNum 309, S/N 656373

Sample invoice output:

656372	Auto Resolve UnderVote	ST. VRAIN SCHOOL DISTRICT |BALLOT ISSU
656372	Autoresolve damaged contest	ST. VRAIN SCHOOL DISTRICT |BALLOT ISSU

etc.

'''

# see also unit test suite and functional test suite examples in
#   /home/neal/py/pygoogle/google.py

__author__ = "Neal McBurnett <http://bcn.boulder.co.us/~neal/>"
__version__ = "0.1.0"
__cvsversion__ = "$Revision: 1.1 $"[11:-2]
# __date__ = "$Date: 2004/12/08 14:48:30 $"[7:-2]
__copyright__ = "Copyright (c) 2004 Neal McBurnett"
__license__ = "GPL"
__usage__ = "ballotnowauditlog auditlog.txt"
__credits__ = """ """

import sys
import string
import filterinput
from time import mktime, strptime
import getopt
import base64
import hmac

try:
    opts, args = getopt.getopt(sys.argv[1:], 'apnit', ["allProjects" "pki" "nmi" "invoice" "printTotals"])
except getopt.error, (msg):
    print "GetoptError: %s" % (msg)
    print "Error: " + __usage__
    sys.exit(2)

# in more recent python , try printTotals = "-t" in [x[0] for x in opts]
invoice = ("-i", "") in opts
printTotals = ("-t", "") in opts
allProjects = ("-a", "") in opts

set = []
pki = ("-p", "") in opts
nmi = ("-n", "") in opts

if nmi:
    set.append("nmi")
if pki:
    set.append("pki")

key = "xyzzy"
system="C"	# this should be a command-line argument, if not deduced from the file

# Reset argv for filterinput
filterinput.configure(args)

while filterinput.readline(stripeol = 1):
    #if filterinput.search(r' 53\s+Contest Title\s+(.*)'):
    if filterinput.search(r'Contest Title\s+(.*)'):
        (contest) = filterinput.group(1)
        print "Resolve	%s	%s	%s" % (serial, desc, contest)

    if filterinput.search(r' (\d\d)\s+(.*)Batch (\d+), SeqNum (\d+), S/N (\d+)'):
        (code, desc, batch, seq, serial) = filterinput.group(1,2,3,4,5)
        # convert serial# to translucent identifier via hmac (keyed-hash)
        serial = base64.encodestring(hmac.new(key, serial).digest())[0:15]
        print "Ballot	%s	%s-%s	%s	%s	%s" % (serial, system, batch, desc, code, seq)

        # get S/N
        #if date != ""  and  date[5:7] != (filterinput.group(1))[5:7]:

#Auto Resolve UnderVote

#collect Contest Titles
