Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

#601 Creation of print_utils.py #716

Open
wants to merge 7 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 6 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 6 additions & 13 deletions src/electionguard_cli/cli_steps/cli_step_base.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
from typing import Any, Optional
import click
import print_utils

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You can remove the header_color and other colors that are unused in this file




class CliStepBase:
Expand All @@ -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)
4 changes: 2 additions & 2 deletions src/electionguard_cli/cli_steps/decrypt_step.py
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -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()
Expand All @@ -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)

Expand Down
7 changes: 3 additions & 4 deletions src/electionguard_cli/cli_steps/election_builder_step.py
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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,
Expand All @@ -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)
4 changes: 2 additions & 2 deletions src/electionguard_cli/cli_steps/encrypt_votes_step.py
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -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
3 changes: 1 addition & 2 deletions src/electionguard_cli/cli_steps/input_retrieval_step_base.py
Original file line number Diff line number Diff line change
Expand Up @@ -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 InternationalizedText, Manifest
Expand Down Expand Up @@ -67,5 +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)
9 changes: 6 additions & 3 deletions src/electionguard_cli/cli_steps/submit_ballots_step.py
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -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())
7 changes: 3 additions & 4 deletions src/electionguard_cli/e2e/submit_votes_step.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
from typing import List
import click

from electionguard.data_store import DataStore
from electionguard.ballot_box import BallotBox
Expand All @@ -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):
Expand Down Expand Up @@ -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
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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}'")
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
from click import echo

from electionguard import to_file

from ..cli_models import MarkResults
Expand All @@ -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))
29 changes: 29 additions & 0 deletions src/electionguard_cli/print_utils.py
Original file line number Diff line number Diff line change
@@ -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:

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can you confirm that the warning color and error color are the same?

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()
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
from click import echo

from electionguard import to_file

from ..cli_models import SubmitResults
Expand All @@ -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))