Skip to content

Commit

Permalink
adding support for directly viewing the versioned receipt
Browse files Browse the repository at this point in the history
  • Loading branch information
windoverwater committed Apr 17, 2024
1 parent 5313412 commit 8ea2154
Show file tree
Hide file tree
Showing 3 changed files with 72 additions and 19 deletions.
13 changes: 11 additions & 2 deletions src/vtp/core/webapi.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
"""A kitchen sync for VTP classes for the moment"""

# standard imports
import csv
import json
import os
import re
Expand Down Expand Up @@ -116,7 +117,15 @@ def get_guid_based_edf_dir(guid: str) -> str:
return os.path.join(edf_path, dirs[0])

@staticmethod
def convert_git_log_to_json(stdout: list, json_errors: list = None):
def convert_csv_to_2d_list(ballot_check_cvs: list) -> list[list[str]]:
"""Convert a 1-D csv format list to a 2-D list of list format"""
my_list = []
for row in csv.reader(ballot_check_cvs, delimiter=",", quotechar='"'):
my_list.append(row)
return my_list

@staticmethod
def convert_git_log_to_json(stdout: list, json_errors: list = None) -> dict:
"""
Will convert the STDOUT of git log -1 <digest> to a dictionary
"""
Expand All @@ -140,5 +149,5 @@ def convert_git_log_to_json(stdout: list, json_errors: list = None):
log_string += line.strip()
output["Log"] = json.loads(log_string)
if json_errors:
output["Errors"] = json_errors
output["backend_errors"] = json_errors
return output
11 changes: 2 additions & 9 deletions src/vtp/ops/accept_ballot_operation.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@

# Standard imports
import base64
import csv
import io
import os
import random
Expand All @@ -41,6 +40,7 @@
from vtp.core.ballot import Ballot
from vtp.core.common import Globals
from vtp.core.election_config import ElectionConfig
from vtp.core.webapi import WebAPI
from vtp.ops.merge_contests_operation import MergeContestsOperation

# Local imports
Expand Down Expand Up @@ -374,13 +374,6 @@ def inner_loop():
# return all three
return ballot_receipt, voters_row, receipt_file

def convert_csv_to_2d_list(self, ballot_check_cvs: list) -> list[list[str]]:
"""Convert a 1-D csv format list to a 2-D list of list format"""
my_list = []
for row in csv.reader(ballot_check_cvs, delimiter=",", quotechar='"'):
my_list.append(row)
return my_list

def main_handle_contests(
self,
a_ballot: dict,
Expand Down Expand Up @@ -711,7 +704,7 @@ def run(
qr_img.save(stream=safe_image)
base64_image = base64.b64encode(safe_image.getvalue()).decode()
return (
self.convert_csv_to_2d_list(ballot_check),
WebAPI.convert_csv_to_2d_list(ballot_check),
index,
base64_image,
receipt_digest,
Expand Down
67 changes: 59 additions & 8 deletions src/vtp/ops/show_contests_operation.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
"""Logic of operation for showing contests."""

# Standard imports
import os

# Project imports
from vtp.core.election_config import ElectionConfig
Expand Down Expand Up @@ -99,8 +100,15 @@ def validate_digests(
return json_errors

# pylint: disable=duplicate-code
def run(self, contest_check: str = "", webapi: bool = False) -> list:
"""Main function - see -h for more info"""
def run(
self, contest_check: str = "", webapi: bool = False, receipt: bool = False
) -> dict:
"""
Main function - see -h for more info. If receipt is True,
contest_check is interpreted as the commit digest of versioned
ballot receipt in which case the contents of the the versioned
file is returned.
"""

# Create a VTP ElectionData object if one does not already exist
the_election_config = ElectionConfig.configure_election(
Expand All @@ -115,11 +123,32 @@ def run(self, contest_check: str = "", webapi: bool = False) -> list:
valid_digests = [
digest for digest in contest_check.split(",") if digest not in error_digests
]
# show/log the digests
if not receipt:
# show/log the digests
with self.changed_cwd(the_election_config.get("git_rootdir")):
output_lines = (
self.shell_out(
["git", "show", "-s"] + valid_digests,
incoming_printlevel=5,
text=True,
check=True,
capture_output=True,
)
.stdout.strip()
.splitlines()
)
for line in output_lines:
self.imprimir(line)
# return a dictionary
return WebAPI.convert_git_log_to_json(output_lines, json_errors)
# get the contents of the file via the commit digest.
# This appears to require two git commands TBD
receipt_digest = valid_digests[0]
with self.changed_cwd(the_election_config.get("git_rootdir")):
# get the filename
output_lines = (
self.shell_out(
["git", "show", "-s"] + valid_digests,
["git", "show", "--name-only", "--oneline", receipt_digest],
incoming_printlevel=5,
text=True,
check=True,
Expand All @@ -128,10 +157,32 @@ def run(self, contest_check: str = "", webapi: bool = False) -> list:
.stdout.strip()
.splitlines()
)
for line in output_lines:
self.imprimir(line)
# return a dictionary
return WebAPI.convert_git_log_to_json(output_lines, json_errors)
# minimal error checking
if len(output_lines) != 2:
json_errors.append(
"invalid 'git show ...' results - did not return 2 lines"
)
elif os.path.basename(output_lines[1]) != "receipt.csv":
json_errors.append("'git show ...' did not return a receipt.csv file")
else:
# get the contents
ballot_check = (
self.shell_out(
["git", "show", receipt_digest + ":" + output_lines[1]],
incoming_printlevel=5,
text=True,
check=True,
capture_output=True,
)
.stdout.strip()
.splitlines()
)
# convert this to an array of arrays
# import pdb; pdb.set_trace()
return {
"ballot_check": WebAPI.convert_csv_to_2d_list(ballot_check),
"backend_errors": json_errors,
}


# For future reference just in case . . .
Expand Down

0 comments on commit 8ea2154

Please sign in to comment.