diff --git a/src/electionguard_cli/cli_steps/cli_step_base.py b/src/electionguard_cli/cli_steps/cli_step_base.py index f95ba3f6..0cf2ec0c 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 print_utils + class CliStepBase: @@ -8,24 +9,16 @@ class CliStepBase: 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: - 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 662d098d..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 @@ -45,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() @@ -54,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 - click.echo(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 29bc0a71..fe90dc10 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 @@ -21,8 +20,8 @@ def _build_election( ) -> BuildElectionResults: self.print_header("Building election") - click.echo("Initializing public key and commitment hash") - election_builder = ElectionBuilder( + print_message("Initializing public key and commitment hash") + election_builder = ElectionBuilder( election_inputs.guardian_count, election_inputs.quorum, election_inputs.manifest, @@ -33,7 +32,7 @@ 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) 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..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,5 @@ from typing import List, Tuple -import click +from print_utils import print_message from electionguard.encrypt import EncryptionDevice, EncryptionMediator from electionguard.election import CiphertextElectionContext @@ -62,7 +62,7 @@ def _encrypt_ballots( ) -> List[CiphertextBallot]: ciphertext_ballots: List[CiphertextBallot] = [] for plaintext_ballot in plaintext_ballots: - click.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 b5a3cd58..97e57695 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,6 @@ from os.path import isfile, isdir, join from os import listdir from io import TextIOWrapper -from click import echo from electionguard.election import CiphertextElectionContext from electionguard.manifest import Manifest @@ -63,5 +62,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/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..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 @@ -13,6 +12,8 @@ from ..cli_models import BuildElectionResults, EncryptResults from ..cli_steps import CliStepBase from .e2e_inputs import E2eInputs +from print_utils import print_message + class SubmitVotesStep(CliStepBase): @@ -52,7 +53,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}" - ) + 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..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,7 @@ 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 from electionguard.constants import get_constants @@ -49,4 +49,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..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,5 +1,3 @@ -from click import echo - from electionguard import to_file from ..cli_models import MarkResults @@ -15,5 +13,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 new file mode 100644 index 00000000..f7106b98 --- /dev/null +++ b/src/electionguard_cli/print_utils.py @@ -0,0 +1,29 @@ +import click +from typing import Any, Optional + +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) -> None: + 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) + +def print_error(text: str, error_color="bright_red"): + click.secho(f"Error: {text}", fg=error_color, bold=True, italic=True) + +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 56094906..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,5 +1,3 @@ -from click import echo - from electionguard import to_file from ..cli_models import SubmitResults @@ -15,5 +13,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))