Skip to content

Commit

Permalink
♻️ Migrate CLI print methods to single module
Browse files Browse the repository at this point in the history
  • Loading branch information
Alopezjr2002 committed Jul 23, 2022
1 parent 35e900f commit 08f673c
Show file tree
Hide file tree
Showing 7 changed files with 31 additions and 26 deletions.
16 changes: 7 additions & 9 deletions src/electionguard_cli/cli_steps/cli_step_base.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
from typing import Any, Optional
from electionguard_cli.print_utils import print_header, print_warning_utils
#import click
from print_utils import Echo, Secho, Style
from print_utils import print_header_utils, print_message_utils, print_value_utils

class CliStepBase:
"""
Expand All @@ -15,25 +16,22 @@ class CliStepBase:
VERIFICATION_URL_NAME = "verification_url"

def print_header(self, s: str) -> None:
print_header_utils(s)
#click.echo("")
Echo("")
#click.secho(f"{'-'*40}", fg=self.header_color)
Secho(f"{'-'*40}", fg=self.header_color)
#click.secho(s, fg=self.header_color)
Secho(s, fg=self.header_color)
#click.secho(f"{'-'*40}", fg=self.header_color)
Secho(f"{'-'*40}", fg=self.header_color)

def print_section(self, s: Optional[str]) -> None:
print_message_utils(s)
#click.echo("")
Echo("")
#click.secho(s, fg=self.section_color, bold=True)
Secho(s, fg=self.section_color, bold=True)

def print_value(self, name: str, value: Any) -> None:
print_value_utils(name, value)
#click.echo(click.style(name + ": ") + click.style(value, fg=self.value_color))
Echo(Style(name + ": ") + Style(value, fg=self.value_color))

def print_warning(self, s: str) -> None:
print_warning_utils(s)
#click.secho(f"WARNING: {s}", fg=self.warning_color)
Secho(f"WARNING: {s}", fg=self.warning_color)

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,6 +1,6 @@
from typing import List
#import click
from print_utils import Echo
from print_utils import print_text_utils
from electionguard.guardian import Guardian
from electionguard.utils import get_optional
from electionguard.ballot import SubmittedBallot
Expand Down Expand Up @@ -56,7 +56,7 @@ def decrypt(
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_text_utils(f"Guardian Present: {guardian.id}")

lagrange_coefficients = self._get_lagrange_coefficients(decryption_mediator)

Expand Down
6 changes: 3 additions & 3 deletions src/electionguard_cli/cli_steps/election_builder_step.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
from typing import Optional
#import click
from print_utils import Echo
from print_utils import print_text_utils
from electionguard.elgamal import ElGamalPublicKey
from electionguard.group import ElementModQ
from electionguard.utils import get_optional
Expand All @@ -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_text_utils("Initializing public key and commitment hash")
election_builder = ElectionBuilder(
election_inputs.guardian_count,
election_inputs.quorum,
Expand All @@ -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_text_utils("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,7 +1,7 @@
from typing import List, Tuple
#import click

from print_utils import Echo
from print_utils import print_text_utils
from electionguard.encrypt import EncryptionDevice, EncryptionMediator
from electionguard.election import CiphertextElectionContext
from electionguard.manifest import InternalManifest
Expand Down Expand Up @@ -64,7 +64,7 @@ def _encrypt_ballots(
ciphertext_ballots: List[CiphertextBallot] = []
for plaintext_ballot in plaintext_ballots:
#click.echo(f"Encrypting ballot: {plaintext_ballot.object_id}")
Echo(f"Encrypting ballot: {plaintext_ballot.object_id}")
print_text_utils(f"Encrypting ballot: {plaintext_ballot.object_id}")
encrypted_ballot = encrypter.encrypt(plaintext_ballot)
ciphertext_ballots.append(get_optional(encrypted_ballot))
return ciphertext_ballots
6 changes: 3 additions & 3 deletions src/electionguard_cli/cli_steps/submit_ballots_step.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
from typing import List
#import click

from print_utils import Echo
from print_utils import print_text_utils
from electionguard.data_store import DataStore
from electionguard.ballot_box import BallotBox
from electionguard.ballot import CiphertextBallot
Expand Down Expand Up @@ -29,11 +29,11 @@ def submit(
for ballot in cast_ballots:
ballot_box.cast(ballot)
#click.echo(f"Cast Ballot Id: {ballot.object_id}")
Echo(f"Cast Ballot Id: {ballot.object_id}")
print_text_utils(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}")
Echo(f"Spoilt Ballot Id: {ballot.object_id}")
print_text_utils(f"Spoilt Ballot Id: {ballot.object_id}")

return SubmitResults(ballot_store.all())
4 changes: 2 additions & 2 deletions src/electionguard_cli/e2e/submit_votes_step.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
from typing import List
#import click

from print_utils import Echo
from print_utils import print_text_utils
from electionguard.data_store import DataStore
from electionguard.ballot_box import BallotBox
from electionguard.election import CiphertextElectionContext
Expand Down Expand Up @@ -57,7 +57,7 @@ def _cast_and_spoil(
#f"Submitted Ballot Id: {ballot.object_id} state: {get_optional(submitted_ballot).state}"
#)

Echo(
print_text_utils(
f"Submitted Ballot Id: {ballot.object_id} state: {get_optional(submitted_ballot).state}"
)
return ballot_store
17 changes: 12 additions & 5 deletions src/electionguard_cli/print_utils.py
Original file line number Diff line number Diff line change
@@ -1,19 +1,26 @@
import click
from typing import Any

def print_header(text: str, header_color="green"):
def print_header_utils(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):
def print_message_utils(text: str, underlined=False):
click.secho(f"{text}", fg="white", underline=underlined)

def print_warning(text: str, warning_color="bright_red") -> None:
def print_warning_utils(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"):
def print_error_utils(text: str, error_color="bright_red"):
click.secho(f"Error: {text}", fg=error_color, bold=True, italic=True)

def cleanup():
def print_value_utils(text: str, val: Any):
click.echo(click.style(text + ": ") + click.style(val, fg="yellow"))

def print_text_utils(text: Any):
click.echo(text)

def cleanup_utits():
click.clear()

0 comments on commit 08f673c

Please sign in to comment.