def merge(self, merged_output): # Create an audit header a = audit_header.AuditHeader() a.set_fields('tabulator_aggregation', 'Pito Salas', 'TTV Tabulator TAB02', 'TTV Tabulator 0.1 JUL-1-2008', self.new_prov) # Dump merge into a file in yaml format stream = open(merged_output + '.yaml', 'w') stream.write(a.serialize_yaml()) yaml.dump_all(self.b1, stream) stream.write('---\n') yaml.dump_all(self.b2, stream) # Dump merge into a file in xml format stream = open(merged_output + '.xml', 'w') stream.write(a.serialize_xml()) for file in (self.b1, self.b2): for record in file: stream.writelines(xmlSerialize(record)[173:]. \ replace('\t', ' ').replace('\n</plist>', '')) stream.close()
def __init__(self, type, args): # These variables are used to ensure that if the same random # data is generated in some circumstances, it will not appear # twice in the template. self.already_used_streps = [] self.already_used_dreps = [] self.already_used_names = [] self.already_used_cand_idents = [] self.already_used_supreme = False self.fname_list = [] self.lname_list = [] if type == 'election': # Generate a sample election and output it to the location # specified by the user in the command line, in yaml and xml # formats. b = {} # Load a list of first and last names from file stream = open('first_names', 'r') self.fnames = stream.readlines() stream = open('last_names', 'r') self.lnames = stream.readlines() # Give each file its own audit header, generate data a = audit_header.AuditHeader() a.set_fields('precinct_contestlist', 'Pito Salas', 'TTV Tabulator TAB02', 'TTV Tabulator 1.2 JUL-1-2008', []) b = self.random_elec() b['type'] = 'precinct_contestlist' # Dump output into a file in yaml format stream = open(args[0] + '.yaml', 'w') stream.write(a.serialize()) yaml.dump(b, stream) # Dump output into a file in XML file stream = open(args[0] + '.xml', 'w') stream.write(a.serialize()) stream.writelines(xmlSerialize(b)[173:]. \ replace('\t', ' ').replace('\n</plist>', '')) elif type == 'counts': # Load election specs from given file in yaml format stream = open(args[1] + '.yaml', 'r') for i in range(0,8): # Ignore the audit header stream.readline() e = yaml.load(stream) e['type'] = 'ballot_counter_total' # Make the number of random ballot_info records specified by # the user. Use the loaded election specs as a template, # assign the vote counts of each candidate a value between # 0 and 99. b_list = [] for i in range(0, int(args[0])): b = copy.deepcopy(e) for j in range(0, len(b['contests'])): for k in range(0, len(b['contests'][j]['candidates'])): r = random.randint(0,99) b['contests'][j]['candidates'][k]['count'] = r b_list.append(b) # Give each file its own audit header a = audit_header.AuditHeader() a.set_fields('precinct_contestlist', 'Pito Salas', 'TTV Tabulator TAB02', 'TTV Tabulator 1.2 JUL-1-2008', []) # Dump output into a file in yaml format stream = open(args[2] + '.yaml', 'w') stream.write(a.serialize()) yaml.dump_all(b_list, stream) # Dump output into a file in XML file stream = open(args[2] + '.xml', 'w') stream.write(a.serialize()) for record in b_list: stream.writelines(xmlSerialize(record)[173:]. \ replace('\t', ' ').replace('\n</plist>', '')) else: exit()
def __init__(self, args): # These variables are used to ensure that if the same random # data is generated in some circumstances, it will not appear # twice in the template. self.already_used_streps = [] self.already_used_dreps = [] self.already_used_names = [] self.already_used_cand_idents = [] self.already_used_supreme = False self.fname_list = [] self.lname_list = [] if args[0] == "jurisdiction": # Generate a sample jurisdiction_slate and output it to the # location specified by the user in the command line, in yaml # and xml formats. b = {} # Give each file its own audit header, generate data a = audit_header.AuditHeader() a.set_fields("jurisdiction_slate", "Pito Salas", "TTV Tabulator TAB02", "TTV Tabulator 0.1 JUL-1-2008", []) b = self.make_juris() b["type"] = "jurisdiction_slate" # Dump output into a file in yaml format stream = open(args[1] + ".yaml", "w") stream.write(a.serialize_yaml()) yaml.dump(b, stream) # Dump output into a file in XML file stream = open(args[1] + ".xml", "w") stream.write(a.serialize_xml()) stream.writelines(xmlSerialize(b)[173:].replace("\t", " ").replace("\n</plist>", "")) elif args[0] == "contestlist": # Generate a sample election and output it to the location # specified by the user in the command line, in yaml and xml # formats. b = {} # Give each file its own audit header, generate data a = audit_header.AuditHeader() a.set_fields( "precinct_contestlist", "Pito Salas", "TTV Tabulator TAB02", "TTV Tabulator 0.1 JUL-1-2008", [] ) b = self.random_elec() b["type"] = "precinct_contestlist" # Dump output into a file in yaml format stream = open(args[1] + ".yaml", "w") stream.write(a.serialize_yaml()) yaml.dump(b, stream) # Dump output into a file in XML file stream = open(args[1] + ".xml", "w") stream.write(a.serialize_xml()) stream.writelines(xmlSerialize(b)[173:].replace("\t", " ").replace("\n</plist>", "")) elif args[0] == "counts": # Load election specs from given file in yaml format stream = open(args[2] + ".yaml", "r") for i in range(0, 8): # Ignore the audit header stream.readline() e = yaml.load(stream) e["type"] = "ballot_counter_total" e["ident"] = "PREC-" + str(random.randint(1, 8)) if e.has_key("precinct_list"): del e["precinct_list"] del e["display_name"] del e["number_of_precincts"] # Make the number of random ballot_info records specified by # the user. Use the loaded election specs as a template, # assign the vote counts of each candidate a value between # 0 and 99. b_list = [] for i in range(0, int(args[1])): b = copy.deepcopy(e) for j in range(0, len(b["contests"])): for k in range(0, len(b["contests"][j]["candidates"])): r = random.randint(0, 99) b["contests"][j]["candidates"][k]["count"] = r b_list.append(b) # Give each file its own audit header a = audit_header.AuditHeader() a.set_fields( "ballot_counter_total", "Pito Salas", "TTV Tabulator TAB02", "TTV Tabulator 0.1 JUL-1-2008", [] ) # Dump output into a file in yaml format stream = open(args[3] + ".yaml", "w") stream.write(a.serialize_yaml()) yaml.dump_all(b_list, stream) # Dump output into a file in XML file stream = open(args[3] + ".xml", "w") stream.write(a.serialize_xml()) for record in b_list: stream.writelines(xmlSerialize(record)[173:].replace("\t", " ").replace("\n</plist>", "")) else: exit()
def __init__(self, args): # These variables are used to ensure that if the same random # data is generated in some circumstances, it will not appear # twice in the template. self.already_used_streps = [] self.already_used_dreps = [] self.already_used_names = [] self.already_used_cand_idents = [] self.already_used_supreme = False self.fname_list = [] self.lname_list = [] self.type = args[0] if self.type == "jurisdiction": # Generate a sample jurisdiction_slate and output it to the # location specified by the user in the command line, in yaml # and xml formats. b = {} # Give each file its own audit header, generate data a = audit_header.AuditHeader() a.set_fields("jurisdiction_slate", "Pito Salas", "TTV Tabulator TAB02", "TTV Tabulator 0.1 JUL-1-2008", []) b = self.make_juris() b["type"] = "jurisdiction_slate" # Dump output into a file in yaml format stream = open(args[1] + ".yaml", "w") stream.write(a.serialize_yaml()) yaml.dump(b, stream) # Dump output into a file in XML file stream = open(args[1] + ".xml", "w") stream.write(a.serialize_xml()) stream.writelines(xmlSerialize(b)[173:].replace("\t", " ").replace("\n</plist>", "")) elif self.type == "contestlist": # Generate a sample election and output it to the location # specified by the user in the command line, in yaml and xml # formats. b = {} # Give each file its own audit header, generate data a = audit_header.AuditHeader() a.set_fields( "precinct_contestlist", "Pito Salas", "TTV Tabulator TAB02", "TTV Tabulator 0.1 JUL-1-2008", [] ) b = self.random_elec() b["type"] = "precinct_contestlist" # Dump output into a file in yaml format stream = open(args[1] + ".yaml", "w") stream.write(a.serialize_yaml()) yaml.dump(b, stream) # Dump output into a file in XML file stream = open(args[1] + ".xml", "w") stream.write(a.serialize_xml()) stream.writelines(xmlSerialize(b)[173:].replace("\t", " ").replace("\n</plist>", "")) elif self.type == "counts": # Load election specs from given file in yaml format stream = open(args[2] + ".yaml", "r") for i in range(0, 8): # Ignore the audit header stream.readline() e = yaml.load(stream) # Make the number of random ballot_info records specified by # the user. Use the loaded election specs as a template, # assign the vote counts of each candidate a value between # 0 and 99. b_list = [] for i in range(int(args[1])): b = {} for key in e.keys(): if key != "contests" and key != "precinct_list": b[key] = e[key] b["contests"] = [] b["type"] = "ballot_counter_total" prec_num = random.randint(1, 8) b["prec_id"] = "PREC-" + str(prec_num) if e.has_key("precinct_list"): b["registered_voters"] = e["precinct_list"][prec_num - 1]["registered_voters"] for j in range(len(e["contests"])): # If the template was a jurisdiction_slate, then # do not include contests that were not generated # in a district in this precinct. if e.has_key("precinct_list"): valid_contest = False for prec in e["precinct_list"]: if prec["prec_id"] == b["prec_id"]: for dist in prec["districts"]: if dist["ident"] == e["contests"][j]["district_id"]: valid_contest = True break if not valid_contest: continue b["contests"].append(copy.deepcopy(e["contests"][j])) for cand in b["contests"][-1]["candidates"]: if cand["display_name"] == "Write-In Votes": cand["count"] = random.randint(1, 9) else: cand["count"] = random.randint(1, 99) b["contests"][-1]["total_votes"] += cand["count"] b["contests"][-1]["uncounted_ballots"]["blank_votes"] = random.randint(1, 10) b["contests"][-1]["uncounted_ballots"]["over_votes"] = random.randint(1, 10) # Generate a random polling type for this session r = random.randint(0, 3) if r == 0: b["vote_type"] = "Polling" elif r == 1: b["vote_type"] = "Early Voting" elif r == 2: b["vote_type"] = "Absentee" else: b["vote_type"] = "Other" b_list.append(b) # Give each file its own audit header a = audit_header.AuditHeader() a.set_fields( "ballot_counter_total", "Pito Salas", "TTV Tabulator TAB02", "TTV Tabulator 0.1 JUL-1-2008", [] ) # Dump output into a file in yaml format stream = open(args[3] + ".yaml", "w") stream.write(a.serialize_yaml()) yaml.dump_all(b_list, stream) # Dump output into a file in XML file stream = open(args[3] + ".xml", "w") stream.write(a.serialize_xml()) for record in b_list: stream.writelines(xmlSerialize(record)[173:].replace("\t", " ").replace("\n</plist>", "")) else: exit()
def __init__(self, election_file, record_file1, record_file2, merge_output_file, report_output_file): # Open a stream to a report file that will be written to during # the course of tabulation. self.rstream = open(report_output_file, 'w') # Load ballot records from yaml file try: stream = open(record_file1 + '.yaml', 'r') except: self.rstream.write('Unable to open ' + record_file1 + '\n') print('Unable to open ' + record_file1 + '\n') exit() else: a = audit_header.AuditHeader() a.load_from_file(stream) guid1 = a.file_id prov1 = a.provenance self.b1 = [] self.b1 = yaml.load_all(stream) try: stream = open(record_file2 + '.yaml', 'r') except: self.rstream.write('Unable to open ' + record_file2 + '\n') print('Unable to open ' + record_file2 + '\n') exit() else: a = audit_header.AuditHeader() a.load_from_file(stream) guid2 = a.file_id prov2 = a.provenance self.b2 = [] self.b2 = yaml.load_all(stream) # Get the election specs from file stream = open(election_file + '.yaml', 'r') for i in range(0,8): # Ignore the audit header stream.readline() self.e = yaml.load(stream) # Verify that ballot records match the election specs in file self.verify_match_record(election_file) # Generators were already iterated over by verify_match_record, # so reload. stream = open(record_file1 + '.yaml', 'r') for i in range(0,8): # Ignore the audit header stream.readline() self.b1 = yaml.load_all(stream) stream = open(record_file2 + '.yaml', 'r') for i in range(0,8): # Ignore the audit header stream.readline() self.b2 = yaml.load_all(stream) # Combine provenances and guids from input files new_prov = [] new_prov.extend(prov1) new_prov.extend(prov2) new_prov.append(guid1) new_prov.append(guid2) # If the same GUID appears twice, then abort the merge for guid in new_prov: if new_prov.count(guid) > 1: print "Input files contain the same ballot record, merge aborted\n" self.rstream.write("Input files contain the same ballot record, merge aborted\n") exit() # Add the vote counts of candidates with the same ID# using # merge(). Write the vote totals for each candidate to the # report stream. s = self.sumation() self.rstream.write('\n') for key in s.keys(): self.rstream.write(str(s[key]) + ' votes found for ' + key + '\n') # Generators were already iterated over by merge, so reload. read_stream = open(record_file1 + '.yaml', 'r') for i in range(0,8): # Ignore the audit header read_stream.readline() self.b1 = yaml.load_all(read_stream) read_stream = open(record_file2 + '.yaml', 'r') for i in range(0,8): # Ignore the audit header read_stream.readline() self.b2 = yaml.load_all(read_stream) # Create an audit header for merge file a = audit_header.AuditHeader() a.set_fields('precinct_contestlist', 'Pito Salas', 'TTV Tabulator TAB02', 'TTV Tabulator 1.2 JUL-1-2008', new_prov) # Dump merge into a file in yaml format stream = open(merge_output_file + '.yaml', 'w') stream.write(a.serialize()) yaml.dump_all(self.b1, stream) stream.write('---\n') yaml.dump_all(self.b2, stream) # I really gotta figure out how to reset generators .... read_stream = open(record_file1 + '.yaml', 'r') for i in range(0,8): # Ignore the audit header read_stream.readline() self.b1 = yaml.load_all(read_stream) read_stream = open(record_file2 + '.yaml', 'r') for i in range(0,8): # Ignore the audit header read_stream.readline() self.b2 = yaml.load_all(read_stream) # Dump merge into a file in xml format stream = open(merge_output_file + '.xml', 'w') stream.write(a.serialize()) print self.b1 for record in self.b1: print xmlSerialize(record) + '\n\n' stream.writelines(xmlSerialize(record)[173:]. \ replace('\t', ' ').replace('\n</plist>', '')) for record in self.b2: print xmlSerialize(record) + '\n\n' stream.writelines(xmlSerialize(record)[173:]. \ replace('\t', ' ').replace('\n</plist>', '')) stream.close() self.rstream.close()