# core imports
import sys
import base, data, filenames

# based on meeting2, and meeting3 for the ballots
import meeting1, meeting2, meeting3
election = meeting1.election
ballots, cast_ballots = meeting3.ballots, meeting3.ballots_with_codes

# spoiled ballots codes
spoiled_ballots_codes_xml = base.file_in_dir(base.DATA_PATH, filenames.SPOILED_BALLOTS_CODES, 'Spoiled Ballots Codes')
spoiled_ballots = data.parse_ballot_table(spoiled_ballots_codes_xml)

# spoiled ballots mixnet
spoiled_ballots_mixnet_xml = base.file_in_dir(base.DATA_PATH, filenames.SPOILED_BALLOTS_MIXNET, 'Spoiled Ballots Mixnet')
spoiled_p_table, spoiled_partitions = data.parse_database(spoiled_ballots_mixnet_xml)

def verify(output_stream, codes_output_stream=None):

  if codes_output_stream:
    BALLOTS = {}
    def new_code(webSerial, pid, q_id, s_id, confirmation_code):
      if not BALLOTS.has_key(webSerial):
        BALLOTS[webSerial] = {'pid': pid, 'questions': {}}
        
      if not BALLOTS[webSerial]['questions'].has_key(q_id):
        BALLOTS[webSerial]['questions'][q_id] = []
      
      BALLOTS[webSerial]['questions'][q_id].append(confirmation_code)
  else:
    new_code = None
python meeting-one.py <DATA_PATH>

data path should NOT have a trailing slash
"""

import sys
import base, data, filenames

# base election params
from electionparams import *

# get the election data
meeting_one_out_xml = base.file_in_dir(base.DATA_PATH, filenames.MEETING_ONE_OUT, "Meeting One Out")

# get the p table and d tables
p_table, partitions = data.parse_database(meeting_one_out_xml)

# are we actually running meeting 1?
def verify(output_stream):
  # check that there are as many ballots in the P table as claimed
  assert len(p_table.rows) == election.num_ballots, "P Table has the wrong number of ballots, should be %s " % election.num_ballots

  num_d_tables = None

  # loop through partitions
  for p_id, partition in partitions.iteritems():
    this_num_d_tables = len(partition.values())
  
    # check that it's the same number of D tables
    if num_d_tables:
      assert this_num_d_tables == num_d_tables
# pull in certain data structures from meeting1
election, p_table, partitions = meeting1.election, meeting1.p_table, meeting1.partitions

# second meeting
meeting_two_in_xml = base.file_in_dir(base.DATA_PATH, filenames.MEETING_TWO_IN, 'Meeting Two In')
meeting_two_out_xml = base.file_in_dir(base.DATA_PATH, filenames.MEETING_TWO_OUT, "Meeting Two Out")
meeting_two_out_commitments_xml = base.file_in_dir(base.DATA_PATH, filenames.MEETING_TWO_OUT_COMMITMENTS, "Meeting Two Out Commitments")
meeting_two_random_data = base.file_in_dir(base.DATA_PATH, filenames.MEETING_TWO_RANDOM_DATA, "Random Data for Meeting Two Challenges", xml=False, correct_windows=True)

# get the challenges
challenge_p_table = data.PTable()
challenge_p_table.parse(meeting_two_in_xml.find('challenges/print'))

# get the response
response_p_table, response_partitions = data.parse_database(meeting_two_out_xml)

challenge_row_ids = challenge_p_table.rows.keys()

def verify_open_p_and_d_tables(election, committed_p_table, committed_partitions, open_p_table, open_partitions):
  # check P table commitments
  for row in open_p_table.rows.values():
    if not committed_p_table.check_full_row(row, election.constant):
      return False
  
  # Now we go through the partitions, the d tables within each partition,
  # and we look at the rows that are revealed. As we do this, we'll also
  # spot check that the permutations in a given d_table row match the p_table rows revealed
  
  # first we get the partition-and-question map for this election, which
  # is effectively a tree representation of how the questions are grouped
# core imports
import sys
import base, data, filenames

# based on meeting2, and meeting3 for the ballots
import meeting1, meeting2, meeting3
election = meeting1.election
ballots, cast_ballots = meeting3.ballots, meeting3.ballots_with_codes

# unused ballots codes
unused_ballots_codes_xml = base.file_in_dir(base.DATA_PATH, filenames.UNUSED_BALLOTS_CODES, 'Unused Ballots Codes')
unused_ballots = data.parse_ballot_table(unused_ballots_codes_xml)

# unused ballots mixnet
unused_ballots_mixnet_xml = base.file_in_dir(base.DATA_PATH, filenames.UNUSED_BALLOTS_MIXNET, 'Unused Ballots Mixnet')
unused_p_table, unused_partitions = data.parse_database(unused_ballots_mixnet_xml)

def verify(output_stream, codes_output_stream=None):

  if codes_output_stream:
    BALLOTS = {}
    def new_code(webSerial, pid, q_id, s_id, confirmation_code):
      if not BALLOTS.has_key(webSerial):
        BALLOTS[webSerial] = {'pid': pid, 'questions': {}}
        
      if not BALLOTS[webSerial]['questions'].has_key(q_id):
        BALLOTS[webSerial]['questions'][q_id] = []
      
      BALLOTS[webSerial]['questions'][q_id].append(confirmation_code)
  else:
    new_code = None