From 8379684d162e950101cfad886f46a998ae24b1ff Mon Sep 17 00:00:00 2001 From: JKnight777 Date: Fri, 22 Jul 2022 16:34:15 -0700 Subject: [PATCH 1/6] Co-authored-by: Antonio Lopez Jr Co-authored-by: Pedro Rangel --- src/electionguard_cli/print_utils.py | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) create mode 100644 src/electionguard_cli/print_utils.py diff --git a/src/electionguard_cli/print_utils.py b/src/electionguard_cli/print_utils.py new file mode 100644 index 00000000..b582e365 --- /dev/null +++ b/src/electionguard_cli/print_utils.py @@ -0,0 +1,19 @@ +import click + +def print_header(text: str, header_color="green"): + click.echo("") + click.secho(f"{'-'*40}", fg=header_color) + click.echo(text, fg=header_color) + click.echo(f"{'-'*40}", header_color) + +def print_message(text: str, underlined=False): + click.secho(f"{text}", fg="white", underline=underlined) + +def print_warning(text: str, warning_color="bright_red") -> None: + click.secho(f"WARNING: {text}", fg=warning_color, bold=True) + +def print_error(text: str, error_color="bright_red"): + click.secho(f"Error: {text}", fg=error_color, bold=True, italic=True) + +def cleanup(): + click.clear() \ No newline at end of file From be8085618c3379fab994947dfb6956827ef79ee5 Mon Sep 17 00:00:00 2001 From: JKnight777 Date: Fri, 22 Jul 2022 16:34:25 -0700 Subject: [PATCH 2/6] Rough Draft: Added a new python file called print_utils.py to replace cli_step_base.py and all of the click imports in other files. --- .../cli_steps/cli_step_base.py | 19 ++++++++++--------- .../cli_steps/decrypt_step.py | 6 ++++-- .../cli_steps/election_builder_step.py | 7 +++++-- .../cli_steps/encrypt_votes_step.py | 6 ++++-- .../cli_steps/input_retrieval_step_base.py | 6 ++++-- .../cli_steps/submit_ballots_step.py | 9 ++++++--- .../e2e/submit_votes_step.py | 9 +++++---- 7 files changed, 38 insertions(+), 24 deletions(-) diff --git a/src/electionguard_cli/cli_steps/cli_step_base.py b/src/electionguard_cli/cli_steps/cli_step_base.py index f95ba3f6..1c97640b 100644 --- a/src/electionguard_cli/cli_steps/cli_step_base.py +++ b/src/electionguard_cli/cli_steps/cli_step_base.py @@ -1,5 +1,6 @@ from typing import Any, Optional -import click +#import click +from print_utils import * class CliStepBase: @@ -15,17 +16,17 @@ class CliStepBase: VERIFICATION_URL_NAME = "verification_url" def print_header(self, s: str) -> None: - click.echo("") - click.secho(f"{'-'*40}", fg=self.header_color) - click.secho(s, fg=self.header_color) - click.secho(f"{'-'*40}", fg=self.header_color) + Echo("") + Secho(f"{'-'*40}", fg=self.header_color) + Secho(s, fg=self.header_color) + Secho(f"{'-'*40}", self.header_color) def print_section(self, s: Optional[str]) -> None: - click.echo("") - click.secho(s, fg=self.section_color, bold=True) + Echo("") + Secho(s, fg=self.section_color, bold=True) def print_value(self, name: str, value: Any) -> None: - click.echo(click.style(name + ": ") + click.style(value, fg=self.value_color)) + Echo(click.style(name + ": ") + click.style(value, fg=self.value_color)) def print_warning(self, s: str) -> None: - click.secho(f"WARNING: {s}", fg=self.warning_color) + Secho(f"WARNING: {s}", fg=self.warning_color) \ No newline at end of file diff --git a/src/electionguard_cli/cli_steps/decrypt_step.py b/src/electionguard_cli/cli_steps/decrypt_step.py index 662d098d..fed31b25 100644 --- a/src/electionguard_cli/cli_steps/decrypt_step.py +++ b/src/electionguard_cli/cli_steps/decrypt_step.py @@ -1,5 +1,5 @@ from typing import List -import click +#import click from electionguard.guardian import Guardian from electionguard.utils import get_optional from electionguard.ballot import SubmittedBallot @@ -10,6 +10,7 @@ from ..cli_models import BuildElectionResults, CliDecryptResults from .cli_step_base import CliStepBase +from print_utils import Echo class DecryptStep(CliStepBase): @@ -54,7 +55,8 @@ def decrypt( ballot_shares = guardian.compute_ballot_shares(spoiled_ballots, context) decryption_mediator.announce(guardian_key, tally_share, ballot_shares) count += 1 - click.echo(f"Guardian Present: {guardian.id}") + #click.echo(f"Guardian Present: {guardian.id}") + Echo(f"Guardian Present: {guardian.id}") lagrange_coefficients = self._get_lagrange_coefficients(decryption_mediator) diff --git a/src/electionguard_cli/cli_steps/election_builder_step.py b/src/electionguard_cli/cli_steps/election_builder_step.py index 29bc0a71..ddaea688 100644 --- a/src/electionguard_cli/cli_steps/election_builder_step.py +++ b/src/electionguard_cli/cli_steps/election_builder_step.py @@ -7,6 +7,7 @@ from ..cli_models import CliElectionInputsBase, BuildElectionResults from .cli_step_base import CliStepBase +from print_utils import Echo class ElectionBuilderStep(CliStepBase): @@ -21,7 +22,8 @@ def _build_election( ) -> BuildElectionResults: self.print_header("Building election") - click.echo("Initializing public key and commitment hash") + #click.echo("Initializing public key and commitment hash") + Echo("Initializing public key and commitment hash") election_builder = ElectionBuilder( election_inputs.guardian_count, election_inputs.quorum, @@ -33,7 +35,8 @@ def _build_election( election_builder.add_extended_data_field( self.VERIFICATION_URL_NAME, verification_url ) - click.echo("Creating context and internal manifest") + #click.echo("Creating context and internal manifest") + Echo("Creating context and internal manifest") build_result = election_builder.build() internal_manifest, context = get_optional(build_result) return BuildElectionResults(internal_manifest, context) diff --git a/src/electionguard_cli/cli_steps/encrypt_votes_step.py b/src/electionguard_cli/cli_steps/encrypt_votes_step.py index ed301f16..3f735b6a 100644 --- a/src/electionguard_cli/cli_steps/encrypt_votes_step.py +++ b/src/electionguard_cli/cli_steps/encrypt_votes_step.py @@ -1,5 +1,6 @@ from typing import List, Tuple -import click +#import click +from print_utils import Echo from electionguard.encrypt import EncryptionDevice, EncryptionMediator from electionguard.election import CiphertextElectionContext @@ -62,7 +63,8 @@ def _encrypt_ballots( ) -> List[CiphertextBallot]: ciphertext_ballots: List[CiphertextBallot] = [] for plaintext_ballot in plaintext_ballots: - click.echo(f"Encrypting ballot: {plaintext_ballot.object_id}") + #clicho(f"Encryptick.eng ballot: {plaintext_ballot.object_id}") + Echo(f"Encrypting ballot: {plaintext_ballot.object_id}") encrypted_ballot = encrypter.encrypt(plaintext_ballot) ciphertext_ballots.append(get_optional(encrypted_ballot)) return ciphertext_ballots diff --git a/src/electionguard_cli/cli_steps/input_retrieval_step_base.py b/src/electionguard_cli/cli_steps/input_retrieval_step_base.py index e61db78a..021b6a45 100644 --- a/src/electionguard_cli/cli_steps/input_retrieval_step_base.py +++ b/src/electionguard_cli/cli_steps/input_retrieval_step_base.py @@ -2,7 +2,8 @@ from os.path import isfile, isdir, join from os import listdir from io import TextIOWrapper -from click import echo +#from click import echo +from print_utils import Echo from electionguard.election import CiphertextElectionContext from electionguard.manifest import InternationalizedText, Manifest @@ -67,5 +68,6 @@ def _get_ballots(ballots_path: str, ballot_type: Type[_T]) -> List[_T]: @staticmethod def _get_ballot(ballots_dir: str, filename: str, ballot_type: Type[_T]) -> _T: full_file = join(ballots_dir, filename) - echo(f"Importing {filename}") + #echo(f"Importing {filename}") + Echo(f"Importing {filename}") return from_file(ballot_type, full_file) diff --git a/src/electionguard_cli/cli_steps/submit_ballots_step.py b/src/electionguard_cli/cli_steps/submit_ballots_step.py index f6452c2c..364c8ba1 100644 --- a/src/electionguard_cli/cli_steps/submit_ballots_step.py +++ b/src/electionguard_cli/cli_steps/submit_ballots_step.py @@ -1,5 +1,6 @@ from typing import List -import click +#import click +from print_utils import Echo from electionguard.data_store import DataStore from electionguard.ballot_box import BallotBox @@ -27,10 +28,12 @@ def submit( for ballot in cast_ballots: ballot_box.cast(ballot) - click.echo(f"Cast Ballot Id: {ballot.object_id}") + #click.echo(f"Cast Ballot Id: {ballot.object_id}") + Echo(f"Cast Ballot Id: {ballot.object_id}") for ballot in spoil_ballots: ballot_box.spoil(ballot) - click.echo(f"Spoilt Ballot Id: {ballot.object_id}") + #click.echo(f"Spoilt Ballot Id: {ballot.object_id}") + Echo(f"Spoilt Ballot Id: {ballot.object_id}") return SubmitResults(ballot_store.all()) diff --git a/src/electionguard_cli/e2e/submit_votes_step.py b/src/electionguard_cli/e2e/submit_votes_step.py index 99a448ae..debe8ade 100644 --- a/src/electionguard_cli/e2e/submit_votes_step.py +++ b/src/electionguard_cli/e2e/submit_votes_step.py @@ -1,5 +1,5 @@ from typing import List -import click +#import click from electionguard.data_store import DataStore from electionguard.ballot_box import BallotBox @@ -13,6 +13,8 @@ from ..cli_models import BuildElectionResults, EncryptResults from ..cli_steps import CliStepBase from .e2e_inputs import E2eInputs +from print_utils.py import Echo + class SubmitVotesStep(CliStepBase): @@ -52,7 +54,6 @@ def _cast_and_spoil( else: submitted_ballot = ballot_box.cast(ballot) - click.echo( - f"Submitted Ballot Id: {ballot.object_id} state: {get_optional(submitted_ballot).state}" - ) + #click.echo(f"Submitted Ballot Id: {ballot.object_id} state: {get_optional(submitted_ballot).state}") + Echo(f"Submitted Ballot Id: {ballot.object_id} state: {get_optional(submitted_ballot).state}") return ballot_store From 8d2d198f88bdbeb9df994524d4c35828331d3587 Mon Sep 17 00:00:00 2001 From: JKnight777 Date: Fri, 22 Jul 2022 17:31:57 -0700 Subject: [PATCH 3/6] Rough Draft for print_utils.py: Changed the implementation of click in all electionguard_cli files that import click and only use echo/secho, to a new file called print_utils.py. print_utils.py has functions print_message, print_header, print_error, and print_warning that utilizes click.secho and click.echo to replace the click.echo and click.secho functions in other files. --- .../cli_steps/cli_step_base.py | 19 +++++++++---------- .../cli_steps/decrypt_step.py | 5 ++--- .../cli_steps/election_builder_step.py | 8 ++++---- .../cli_steps/encrypt_votes_step.py | 5 ++--- .../cli_steps/input_retrieval_step_base.py | 4 ++-- .../e2e/submit_votes_step.py | 5 ++--- .../import_ballots_publish_step.py | 5 +++-- .../mark_ballots/mark_ballots_publish_step.py | 5 +++-- src/electionguard_cli/print_utils.py | 4 ++-- .../submit_ballots_publish_step.py | 5 +++-- 10 files changed, 32 insertions(+), 33 deletions(-) diff --git a/src/electionguard_cli/cli_steps/cli_step_base.py b/src/electionguard_cli/cli_steps/cli_step_base.py index 1c97640b..f95ba3f6 100644 --- a/src/electionguard_cli/cli_steps/cli_step_base.py +++ b/src/electionguard_cli/cli_steps/cli_step_base.py @@ -1,6 +1,5 @@ from typing import Any, Optional -#import click -from print_utils import * +import click class CliStepBase: @@ -16,17 +15,17 @@ class CliStepBase: VERIFICATION_URL_NAME = "verification_url" def print_header(self, s: str) -> None: - Echo("") - Secho(f"{'-'*40}", fg=self.header_color) - Secho(s, fg=self.header_color) - Secho(f"{'-'*40}", self.header_color) + click.echo("") + click.secho(f"{'-'*40}", fg=self.header_color) + click.secho(s, fg=self.header_color) + click.secho(f"{'-'*40}", fg=self.header_color) def print_section(self, s: Optional[str]) -> None: - Echo("") - Secho(s, fg=self.section_color, bold=True) + click.echo("") + click.secho(s, fg=self.section_color, bold=True) def print_value(self, name: str, value: Any) -> None: - Echo(click.style(name + ": ") + click.style(value, fg=self.value_color)) + click.echo(click.style(name + ": ") + click.style(value, fg=self.value_color)) def print_warning(self, s: str) -> None: - Secho(f"WARNING: {s}", fg=self.warning_color) \ No newline at end of file + click.secho(f"WARNING: {s}", fg=self.warning_color) diff --git a/src/electionguard_cli/cli_steps/decrypt_step.py b/src/electionguard_cli/cli_steps/decrypt_step.py index fed31b25..5a2df2bc 100644 --- a/src/electionguard_cli/cli_steps/decrypt_step.py +++ b/src/electionguard_cli/cli_steps/decrypt_step.py @@ -10,7 +10,7 @@ from ..cli_models import BuildElectionResults, CliDecryptResults from .cli_step_base import CliStepBase -from print_utils import Echo +from print_utils import print_message class DecryptStep(CliStepBase): @@ -55,8 +55,7 @@ def decrypt( ballot_shares = guardian.compute_ballot_shares(spoiled_ballots, context) decryption_mediator.announce(guardian_key, tally_share, ballot_shares) count += 1 - #click.echo(f"Guardian Present: {guardian.id}") - Echo(f"Guardian Present: {guardian.id}") + print_message(f"Guardian Present: {guardian.id}") lagrange_coefficients = self._get_lagrange_coefficients(decryption_mediator) diff --git a/src/electionguard_cli/cli_steps/election_builder_step.py b/src/electionguard_cli/cli_steps/election_builder_step.py index ddaea688..e6861af2 100644 --- a/src/electionguard_cli/cli_steps/election_builder_step.py +++ b/src/electionguard_cli/cli_steps/election_builder_step.py @@ -1,5 +1,5 @@ from typing import Optional -import click +#import click from electionguard.elgamal import ElGamalPublicKey from electionguard.group import ElementModQ from electionguard.utils import get_optional @@ -7,7 +7,7 @@ from ..cli_models import CliElectionInputsBase, BuildElectionResults from .cli_step_base import CliStepBase -from print_utils import Echo +from print_utils import print_message class ElectionBuilderStep(CliStepBase): @@ -23,7 +23,7 @@ def _build_election( self.print_header("Building election") #click.echo("Initializing public key and commitment hash") - Echo("Initializing public key and commitment hash") + print_message("Initializing public key and commitment hash") election_builder = ElectionBuilder( election_inputs.guardian_count, election_inputs.quorum, @@ -36,7 +36,7 @@ def _build_election( self.VERIFICATION_URL_NAME, verification_url ) #click.echo("Creating context and internal manifest") - Echo("Creating context and internal manifest") + print_message("Creating context and internal manifest") build_result = election_builder.build() internal_manifest, context = get_optional(build_result) return BuildElectionResults(internal_manifest, context) diff --git a/src/electionguard_cli/cli_steps/encrypt_votes_step.py b/src/electionguard_cli/cli_steps/encrypt_votes_step.py index 3f735b6a..279419ab 100644 --- a/src/electionguard_cli/cli_steps/encrypt_votes_step.py +++ b/src/electionguard_cli/cli_steps/encrypt_votes_step.py @@ -1,6 +1,6 @@ from typing import List, Tuple #import click -from print_utils import Echo +from print_utils import print_message from electionguard.encrypt import EncryptionDevice, EncryptionMediator from electionguard.election import CiphertextElectionContext @@ -63,8 +63,7 @@ def _encrypt_ballots( ) -> List[CiphertextBallot]: ciphertext_ballots: List[CiphertextBallot] = [] for plaintext_ballot in plaintext_ballots: - #clicho(f"Encryptick.eng ballot: {plaintext_ballot.object_id}") - Echo(f"Encrypting ballot: {plaintext_ballot.object_id}") + print_message(f"Encrypting ballot: {plaintext_ballot.object_id}") encrypted_ballot = encrypter.encrypt(plaintext_ballot) ciphertext_ballots.append(get_optional(encrypted_ballot)) return ciphertext_ballots diff --git a/src/electionguard_cli/cli_steps/input_retrieval_step_base.py b/src/electionguard_cli/cli_steps/input_retrieval_step_base.py index 021b6a45..19da5323 100644 --- a/src/electionguard_cli/cli_steps/input_retrieval_step_base.py +++ b/src/electionguard_cli/cli_steps/input_retrieval_step_base.py @@ -3,7 +3,7 @@ from os import listdir from io import TextIOWrapper #from click import echo -from print_utils import Echo +from print_utils import print_message from electionguard.election import CiphertextElectionContext from electionguard.manifest import InternationalizedText, Manifest @@ -69,5 +69,5 @@ def _get_ballots(ballots_path: str, ballot_type: Type[_T]) -> List[_T]: def _get_ballot(ballots_dir: str, filename: str, ballot_type: Type[_T]) -> _T: full_file = join(ballots_dir, filename) #echo(f"Importing {filename}") - Echo(f"Importing {filename}") + print_message(f"Importing {filename}") return from_file(ballot_type, full_file) diff --git a/src/electionguard_cli/e2e/submit_votes_step.py b/src/electionguard_cli/e2e/submit_votes_step.py index debe8ade..93d21a25 100644 --- a/src/electionguard_cli/e2e/submit_votes_step.py +++ b/src/electionguard_cli/e2e/submit_votes_step.py @@ -13,7 +13,7 @@ from ..cli_models import BuildElectionResults, EncryptResults from ..cli_steps import CliStepBase from .e2e_inputs import E2eInputs -from print_utils.py import Echo +from print_utils.py import print_message @@ -54,6 +54,5 @@ def _cast_and_spoil( else: submitted_ballot = ballot_box.cast(ballot) - #click.echo(f"Submitted Ballot Id: {ballot.object_id} state: {get_optional(submitted_ballot).state}") - Echo(f"Submitted Ballot Id: {ballot.object_id} state: {get_optional(submitted_ballot).state}") + print_message(f"Submitted Ballot Id: {ballot.object_id} state: {get_optional(submitted_ballot).state}") return ballot_store diff --git a/src/electionguard_cli/import_ballots/import_ballots_publish_step.py b/src/electionguard_cli/import_ballots/import_ballots_publish_step.py index 25be57bd..d16907b2 100644 --- a/src/electionguard_cli/import_ballots/import_ballots_publish_step.py +++ b/src/electionguard_cli/import_ballots/import_ballots_publish_step.py @@ -2,7 +2,8 @@ from shutil import make_archive from os.path import splitext from tempfile import TemporaryDirectory -from click import echo +#from click import echo +from print_utils import print_message from electionguard.encrypt import EncryptionDevice from electionguard.constants import get_constants @@ -49,4 +50,4 @@ def publish( ) file_name = splitext(election_inputs.output_record)[0] make_archive(file_name, self._COMPRESSION_FORMAT, temp_dir) - echo(f"Exported election record to '{election_inputs.output_record}'") + print_message(f"Exported election record to '{election_inputs.output_record}'") diff --git a/src/electionguard_cli/mark_ballots/mark_ballots_publish_step.py b/src/electionguard_cli/mark_ballots/mark_ballots_publish_step.py index 380e54f6..810e924b 100644 --- a/src/electionguard_cli/mark_ballots/mark_ballots_publish_step.py +++ b/src/electionguard_cli/mark_ballots/mark_ballots_publish_step.py @@ -1,4 +1,5 @@ -from click import echo +#from click import echo +from print_utils import print_message from electionguard import to_file @@ -15,5 +16,5 @@ def publish(self, marked_ballots: MarkResults, out_dir: str) -> None: self.print_header("Writing Marked Ballots") for ballot in marked_ballots.plaintext_ballots: ballot_file = to_file(ballot, ballot.object_id, out_dir) - echo(f"Writing {ballot_file}") + print_message(f"Writing {ballot_file}") self.print_value("Marked ballots", len(marked_ballots.plaintext_ballots)) diff --git a/src/electionguard_cli/print_utils.py b/src/electionguard_cli/print_utils.py index b582e365..92b8b5c7 100644 --- a/src/electionguard_cli/print_utils.py +++ b/src/electionguard_cli/print_utils.py @@ -6,8 +6,8 @@ def print_header(text: str, header_color="green"): click.echo(text, fg=header_color) click.echo(f"{'-'*40}", header_color) -def print_message(text: str, underlined=False): - click.secho(f"{text}", fg="white", underline=underlined) +def print_message(text: str, color="white", underlined=False, bolded=False): + click.secho(f"{text}", fg=color, underline=underlined, bold=bolded) def print_warning(text: str, warning_color="bright_red") -> None: click.secho(f"WARNING: {text}", fg=warning_color, bold=True) diff --git a/src/electionguard_cli/submit_ballots/submit_ballots_publish_step.py b/src/electionguard_cli/submit_ballots/submit_ballots_publish_step.py index 56094906..9d09329c 100644 --- a/src/electionguard_cli/submit_ballots/submit_ballots_publish_step.py +++ b/src/electionguard_cli/submit_ballots/submit_ballots_publish_step.py @@ -1,4 +1,5 @@ -from click import echo +#from click import echo +from print_utils import print_message from electionguard import to_file @@ -15,5 +16,5 @@ def publish(self, submit_results: SubmitResults, out_dir: str) -> None: self.print_header("Writing Submitted Ballots") for ballot in submit_results.submitted_ballots: ballot_file = to_file(ballot, ballot.object_id, out_dir) - echo(f"Writing {ballot_file}") + print_message(f"Writing {ballot_file}") self.print_value("Submitted ballots", len(submit_results.submitted_ballots)) From 45a01f89b6c95fded7d43e05164e83e28ce27faa Mon Sep 17 00:00:00 2001 From: JKnight777 Date: Sat, 23 Jul 2022 13:20:31 -0700 Subject: [PATCH 4/6] Co-authored-by: Pedro Rangel Co-authored-by: Antonio Lopez Jr --- src/electionguard_cli/cli_steps/cli_step_base.py | 14 +++++--------- src/electionguard_cli/cli_steps/decrypt_step.py | 5 ++--- .../cli_steps/election_builder_step.py | 8 ++------ .../cli_steps/encrypt_votes_step.py | 1 - .../cli_steps/input_retrieval_step_base.py | 5 +---- src/electionguard_cli/e2e/submit_votes_step.py | 2 +- .../mark_ballots/mark_ballots_publish_step.py | 3 --- src/electionguard_cli/print_utils.py | 16 +++++++++++++--- .../submit_ballots_publish_step.py | 3 --- 9 files changed, 24 insertions(+), 33 deletions(-) diff --git a/src/electionguard_cli/cli_steps/cli_step_base.py b/src/electionguard_cli/cli_steps/cli_step_base.py index f95ba3f6..90909247 100644 --- a/src/electionguard_cli/cli_steps/cli_step_base.py +++ b/src/electionguard_cli/cli_steps/cli_step_base.py @@ -1,5 +1,5 @@ from typing import Any, Optional -import click +import print_utils class CliStepBase: @@ -15,17 +15,13 @@ class CliStepBase: VERIFICATION_URL_NAME = "verification_url" def print_header(self, s: str) -> None: - click.echo("") - click.secho(f"{'-'*40}", fg=self.header_color) - click.secho(s, fg=self.header_color) - click.secho(f"{'-'*40}", fg=self.header_color) + print_utils.print_header(s) def print_section(self, s: Optional[str]) -> None: - click.echo("") - click.secho(s, fg=self.section_color, bold=True) + print_utils.print_section(s) def print_value(self, name: str, value: Any) -> None: - click.echo(click.style(name + ": ") + click.style(value, fg=self.value_color)) + print_utils.print_value(name, value) def print_warning(self, s: str) -> None: - click.secho(f"WARNING: {s}", fg=self.warning_color) + print_utils.print_warning(s) diff --git a/src/electionguard_cli/cli_steps/decrypt_step.py b/src/electionguard_cli/cli_steps/decrypt_step.py index 5a2df2bc..2b5de206 100644 --- a/src/electionguard_cli/cli_steps/decrypt_step.py +++ b/src/electionguard_cli/cli_steps/decrypt_step.py @@ -1,5 +1,4 @@ from typing import List -#import click from electionguard.guardian import Guardian from electionguard.utils import get_optional from electionguard.ballot import SubmittedBallot @@ -10,7 +9,6 @@ from ..cli_models import BuildElectionResults, CliDecryptResults from .cli_step_base import CliStepBase -from print_utils import print_message class DecryptStep(CliStepBase): @@ -46,6 +44,7 @@ def decrypt( self.print_value("Spoiled ballots", ciphertext_tally.spoiled()) self.print_value("Total ballots", len(ciphertext_tally)) + count = 0 for guardian in guardians: guardian_key = guardian.share_key() @@ -55,7 +54,7 @@ def decrypt( ballot_shares = guardian.compute_ballot_shares(spoiled_ballots, context) decryption_mediator.announce(guardian_key, tally_share, ballot_shares) count += 1 - print_message(f"Guardian Present: {guardian.id}") + self.print_value(f"Guardian Present: {guardian.id}") lagrange_coefficients = self._get_lagrange_coefficients(decryption_mediator) diff --git a/src/electionguard_cli/cli_steps/election_builder_step.py b/src/electionguard_cli/cli_steps/election_builder_step.py index e6861af2..e1a618c2 100644 --- a/src/electionguard_cli/cli_steps/election_builder_step.py +++ b/src/electionguard_cli/cli_steps/election_builder_step.py @@ -1,5 +1,4 @@ from typing import Optional -#import click from electionguard.elgamal import ElGamalPublicKey from electionguard.group import ElementModQ from electionguard.utils import get_optional @@ -7,10 +6,9 @@ from ..cli_models import CliElectionInputsBase, BuildElectionResults from .cli_step_base import CliStepBase -from print_utils import print_message -class ElectionBuilderStep(CliStepBase): +class ElectionBuilderStep(): #(CliStepBase): """Responsible for creating a manifest and context for use in an election.""" def _build_election( @@ -22,9 +20,8 @@ def _build_election( ) -> BuildElectionResults: self.print_header("Building election") - #click.echo("Initializing public key and commitment hash") print_message("Initializing public key and commitment hash") - election_builder = ElectionBuilder( + election_builder = ElectionBuilder( election_inputs.guardian_count, election_inputs.quorum, election_inputs.manifest, @@ -35,7 +32,6 @@ def _build_election( election_builder.add_extended_data_field( self.VERIFICATION_URL_NAME, verification_url ) - #click.echo("Creating context and internal manifest") print_message("Creating context and internal manifest") build_result = election_builder.build() internal_manifest, context = get_optional(build_result) diff --git a/src/electionguard_cli/cli_steps/encrypt_votes_step.py b/src/electionguard_cli/cli_steps/encrypt_votes_step.py index 279419ab..350d568c 100644 --- a/src/electionguard_cli/cli_steps/encrypt_votes_step.py +++ b/src/electionguard_cli/cli_steps/encrypt_votes_step.py @@ -1,5 +1,4 @@ from typing import List, Tuple -#import click from print_utils import print_message from electionguard.encrypt import EncryptionDevice, EncryptionMediator diff --git a/src/electionguard_cli/cli_steps/input_retrieval_step_base.py b/src/electionguard_cli/cli_steps/input_retrieval_step_base.py index 19da5323..fb655b8e 100644 --- a/src/electionguard_cli/cli_steps/input_retrieval_step_base.py +++ b/src/electionguard_cli/cli_steps/input_retrieval_step_base.py @@ -2,8 +2,6 @@ from os.path import isfile, isdir, join from os import listdir from io import TextIOWrapper -#from click import echo -from print_utils import print_message from electionguard.election import CiphertextElectionContext from electionguard.manifest import InternationalizedText, Manifest @@ -17,7 +15,7 @@ _T = TypeVar("_T") -class InputRetrievalStepBase(CliStepBase): +class InputRetrievalStepBase("""CliStepBase"""): """A common base class for all CLI commands that accept user input""" def _get_manifest(self, manifest_file: TextIOWrapper) -> Manifest: @@ -68,6 +66,5 @@ def _get_ballots(ballots_path: str, ballot_type: Type[_T]) -> List[_T]: @staticmethod def _get_ballot(ballots_dir: str, filename: str, ballot_type: Type[_T]) -> _T: full_file = join(ballots_dir, filename) - #echo(f"Importing {filename}") print_message(f"Importing {filename}") return from_file(ballot_type, full_file) diff --git a/src/electionguard_cli/e2e/submit_votes_step.py b/src/electionguard_cli/e2e/submit_votes_step.py index 93d21a25..753007c4 100644 --- a/src/electionguard_cli/e2e/submit_votes_step.py +++ b/src/electionguard_cli/e2e/submit_votes_step.py @@ -13,7 +13,7 @@ from ..cli_models import BuildElectionResults, EncryptResults from ..cli_steps import CliStepBase from .e2e_inputs import E2eInputs -from print_utils.py import print_message +from print_utils import print_message diff --git a/src/electionguard_cli/mark_ballots/mark_ballots_publish_step.py b/src/electionguard_cli/mark_ballots/mark_ballots_publish_step.py index 810e924b..79bdd5e3 100644 --- a/src/electionguard_cli/mark_ballots/mark_ballots_publish_step.py +++ b/src/electionguard_cli/mark_ballots/mark_ballots_publish_step.py @@ -1,6 +1,3 @@ -#from click import echo -from print_utils import print_message - from electionguard import to_file from ..cli_models import MarkResults diff --git a/src/electionguard_cli/print_utils.py b/src/electionguard_cli/print_utils.py index 92b8b5c7..f7106b98 100644 --- a/src/electionguard_cli/print_utils.py +++ b/src/electionguard_cli/print_utils.py @@ -1,12 +1,15 @@ import click +from typing import Any, Optional -def print_header(text: str, header_color="green"): +VERIFICATION_URL_NAME = "verification_url" + +def print_header(text: str, header_color="green") -> None: click.echo("") click.secho(f"{'-'*40}", fg=header_color) click.echo(text, fg=header_color) click.echo(f"{'-'*40}", header_color) -def print_message(text: str, color="white", underlined=False, bolded=False): +def print_message(text: str, color="white", underlined=False, bolded=False) -> None: click.secho(f"{text}", fg=color, underline=underlined, bold=bolded) def print_warning(text: str, warning_color="bright_red") -> None: @@ -15,5 +18,12 @@ def print_warning(text: str, warning_color="bright_red") -> None: def print_error(text: str, error_color="bright_red"): click.secho(f"Error: {text}", fg=error_color, bold=True, italic=True) -def cleanup(): +def print_value(name: str, value: Any, value_color="yellow") -> None: + click.echo(click.style(name + ": ") + click.style(value, fg=self.value_color)) + +def print_section(text: Optional[str], section_color="bright-white") -> None: + click.echo("") + click.secho(text, fg=section_color, bold=True) + +def cleanup() -> None: click.clear() \ No newline at end of file diff --git a/src/electionguard_cli/submit_ballots/submit_ballots_publish_step.py b/src/electionguard_cli/submit_ballots/submit_ballots_publish_step.py index 9d09329c..ac6bd228 100644 --- a/src/electionguard_cli/submit_ballots/submit_ballots_publish_step.py +++ b/src/electionguard_cli/submit_ballots/submit_ballots_publish_step.py @@ -1,6 +1,3 @@ -#from click import echo -from print_utils import print_message - from electionguard import to_file from ..cli_models import SubmitResults From 9ed7a11a0f157a09011b7925a33eecf493d17bd0 Mon Sep 17 00:00:00 2001 From: JKnight777 Date: Sat, 23 Jul 2022 13:31:40 -0700 Subject: [PATCH 5/6] Rough Draft for print_utils.py Finished print_utils.py, and updated cli_step_base.py to incorporate it. --- src/electionguard_cli/cli_steps/election_builder_step.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/electionguard_cli/cli_steps/election_builder_step.py b/src/electionguard_cli/cli_steps/election_builder_step.py index e1a618c2..fe90dc10 100644 --- a/src/electionguard_cli/cli_steps/election_builder_step.py +++ b/src/electionguard_cli/cli_steps/election_builder_step.py @@ -8,7 +8,7 @@ from .cli_step_base import CliStepBase -class ElectionBuilderStep(): #(CliStepBase): +class ElectionBuilderStep(CliStepBase): """Responsible for creating a manifest and context for use in an election.""" def _build_election( From bf7d69f24e2aa6d383707b94bbf03f0ca7a56f75 Mon Sep 17 00:00:00 2001 From: JKnight777 Date: Tue, 26 Jul 2022 09:15:33 -0700 Subject: [PATCH 6/6] Changed some cli files based on the reviews --- src/electionguard_cli/cli_steps/cli_step_base.py | 5 +---- src/electionguard_cli/cli_steps/input_retrieval_step_base.py | 2 +- src/electionguard_cli/e2e/submit_votes_step.py | 1 - .../import_ballots/import_ballots_publish_step.py | 1 - 4 files changed, 2 insertions(+), 7 deletions(-) diff --git a/src/electionguard_cli/cli_steps/cli_step_base.py b/src/electionguard_cli/cli_steps/cli_step_base.py index 90909247..0cf2ec0c 100644 --- a/src/electionguard_cli/cli_steps/cli_step_base.py +++ b/src/electionguard_cli/cli_steps/cli_step_base.py @@ -2,16 +2,13 @@ import print_utils + class CliStepBase: """ Responsible for providing common functionality to the individual steps within an end-to-end election command from the CLI. """ - header_color = "green" - value_color = "yellow" - warning_color = "bright_red" - section_color = "bright_white" VERIFICATION_URL_NAME = "verification_url" def print_header(self, s: str) -> None: diff --git a/src/electionguard_cli/cli_steps/input_retrieval_step_base.py b/src/electionguard_cli/cli_steps/input_retrieval_step_base.py index fb655b8e..2a3f0119 100644 --- a/src/electionguard_cli/cli_steps/input_retrieval_step_base.py +++ b/src/electionguard_cli/cli_steps/input_retrieval_step_base.py @@ -15,7 +15,7 @@ _T = TypeVar("_T") -class InputRetrievalStepBase("""CliStepBase"""): +class InputRetrievalStepBase(CliStepBase): """A common base class for all CLI commands that accept user input""" def _get_manifest(self, manifest_file: TextIOWrapper) -> Manifest: diff --git a/src/electionguard_cli/e2e/submit_votes_step.py b/src/electionguard_cli/e2e/submit_votes_step.py index 753007c4..37cb79c3 100644 --- a/src/electionguard_cli/e2e/submit_votes_step.py +++ b/src/electionguard_cli/e2e/submit_votes_step.py @@ -1,5 +1,4 @@ from typing import List -#import click from electionguard.data_store import DataStore from electionguard.ballot_box import BallotBox diff --git a/src/electionguard_cli/import_ballots/import_ballots_publish_step.py b/src/electionguard_cli/import_ballots/import_ballots_publish_step.py index d16907b2..697d0eba 100644 --- a/src/electionguard_cli/import_ballots/import_ballots_publish_step.py +++ b/src/electionguard_cli/import_ballots/import_ballots_publish_step.py @@ -2,7 +2,6 @@ from shutil import make_archive from os.path import splitext from tempfile import TemporaryDirectory -#from click import echo from print_utils import print_message from electionguard.encrypt import EncryptionDevice