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 21, 2022
1 parent edc394d commit 132ea76
Show file tree
Hide file tree
Showing 8 changed files with 33,648 additions and 22 deletions.
33,593 changes: 33,593 additions & 0 deletions get-pip.py

Large diffs are not rendered by default.

28 changes: 18 additions & 10 deletions src/electionguard_cli/cli_steps/cli_step_base.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
from typing import Any, Optional
import click

#import click
from print_utils import Echo, Secho, Style

class CliStepBase:
"""
Expand All @@ -15,17 +15,25 @@ 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)
#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:
click.echo("")
click.secho(s, fg=self.section_color, bold=True)
#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:
click.echo(click.style(name + ": ") + click.style(value, fg=self.value_color))
#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:
click.secho(f"WARNING: {s}", fg=self.warning_color)
#click.secho(f"WARNING: {s}", fg=self.warning_color)
Secho(f"WARNING: {s}", fg=self.warning_color)
6 changes: 4 additions & 2 deletions src/electionguard_cli/cli_steps/decrypt_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.guardian import Guardian
from electionguard.utils import get_optional
from electionguard.ballot import SubmittedBallot
Expand Down Expand Up @@ -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)

Expand Down
9 changes: 6 additions & 3 deletions src/electionguard_cli/cli_steps/election_builder_step.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
from typing import Optional
import click
#import click
from print_utils import Echo
from electionguard.elgamal import ElGamalPublicKey
from electionguard.group import ElementModQ
from electionguard.utils import get_optional
Expand All @@ -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,
Expand All @@ -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)
6 changes: 4 additions & 2 deletions src/electionguard_cli/cli_steps/encrypt_votes_step.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
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
from electionguard.manifest import InternalManifest
Expand Down Expand Up @@ -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}")
#click.echo(f"Encrypting 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
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,6 +1,7 @@
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
from electionguard.ballot import CiphertextBallot
Expand All @@ -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())
9 changes: 7 additions & 2 deletions src/electionguard_cli/e2e/submit_votes_step.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
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
from electionguard.election import CiphertextElectionContext
Expand Down Expand Up @@ -52,7 +53,11 @@ def _cast_and_spoil(
else:
submitted_ballot = ballot_box.cast(ballot)

click.echo(
#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
10 changes: 10 additions & 0 deletions src/electionguard_cli/print_utils.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import click

def Style(output: str):
click.style(output)

def Echo(output: str):
click.echo(output)

def Secho(text: str, fg=None, bg=None, bold=None, dim=None, underline=None, blink=None, reverse=None, reset=True):
click.secho(text, fg, bg, bold, dim, underline, blink, reverse, reset)

0 comments on commit 132ea76

Please sign in to comment.