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

Add logging to Dev make flat branch #67

Merged
merged 5 commits into from
Aug 16, 2022
Merged
Show file tree
Hide file tree
Changes from all 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
14 changes: 10 additions & 4 deletions src/electos/ballotmaker/cli.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
""" The BallotMaker Command Line Interface (CLI)"""
import logging
import sys
from os import strerror
from pathlib import Path
from typing import Optional
Expand All @@ -12,6 +14,10 @@
STYLE_HELP = "Stylesheet file for ballot generation"
VERSION_HELP = "Print the version number."

# configure logging
logging.basicConfig(stream=sys.stdout, level=logging.DEBUG)
stratofax marked this conversation as resolved.
Show resolved Hide resolved
log = logging.getLogger(__name__)


def version_callback(value: bool):
if value:
Expand Down Expand Up @@ -45,8 +51,8 @@ def make(
"""Make ballots from EDF file"""
make_ballots_result = make_ballots.make_ballots(edf, output_dir, style)
if make_ballots_result != NO_ERRORS:
typer.echo(
f"Error {make_ballots_result} making ballots: {strerror(make_ballots_result)}"
log.error(
f"Code {make_ballots_result} in make - {strerror(make_ballots_result)}"
stratofax marked this conversation as resolved.
Show resolved Hide resolved
)
return make_ballots_result

Expand All @@ -61,8 +67,8 @@ def validate(
"""Validate data in EDF file"""
validate_edf_result = validate_edf.validate_edf(edf)
if validate_edf_result != NO_ERRORS:
typer.echo(
f"Error {validate_edf_result} validating EDF: {strerror(validate_edf_result)}"
log.error(
f"Code {validate_edf_result} in validate - {strerror(validate_edf_result)}"
)
return validate_edf_result

Expand Down
9 changes: 8 additions & 1 deletion src/electos/ballotmaker/make_ballots.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
import logging
from pathlib import Path

from electos.ballotmaker.constants import NO_ERRORS, NO_FILE

log = logging.getLogger(__name__)


def make_ballots(
_edf: Path, _output_dir: Path = None, _styles: Path = None
Expand All @@ -13,8 +16,12 @@ def make_ballots(
Output directory for generated PDF files
Styles file for ballot formatting
"""
# is the EDF a JSON file?
# is the EDF a file?
if _edf is None:
log.debug("No EDF file provided to make ballots.")
return NO_FILE
if not _edf.is_file():
log.debug(f"Can't make ballots, EDF {_edf} is not a file")
return NO_FILE
# was a valid output directory provided?
# was a styles file provided?
Expand Down
7 changes: 6 additions & 1 deletion src/electos/ballotmaker/validate_edf.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
import logging
from pathlib import Path

from electos.ballotmaker.constants import NO_DATA, NO_ERRORS, NO_FILE
from electos.ballotmaker.read_edf import read_edf

log = logging.getLogger(__name__)


def validate_edf(
_edf: Path,
Expand All @@ -13,10 +16,12 @@ def validate_edf(
"""
# is the EDF a file?
if _edf is None:
log.debug("No EDF file provided for validation.")
return NO_FILE
if not _edf.is_file():
log.debug(f"Can't validate, EDF {_edf} is not a file")
return NO_FILE

ballot_style_count = read_edf(_edf)
print(f"Found {ballot_style_count} ballots in {_edf}")
log.info(f"Found {ballot_style_count} ballots in {_edf}")
return NO_ERRORS
2 changes: 1 addition & 1 deletion tests/test_cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ def test_make():
# bypass mandatory CLI option to force error
assert cli.make(edf=None) == NO_FILE
# any old path will satisfy current tests
assert cli.make(imaginary_file) == NO_ERRORS
assert cli.make(imaginary_file) == NO_FILE
# check CLI errors: no options for make
result = runner.invoke(cli.app, ["make"])
assert result.exit_code == NO_FILE
Expand Down
15 changes: 11 additions & 4 deletions tests/test_make_ballots.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,19 @@
from pathlib import Path

from electos.ballotmaker import make_ballots
from electos.ballotmaker.constants import NO_ERRORS, NO_FILE
from electos.ballotmaker.make_ballots import make_ballots

test_file = Path("empty.json")
imaginary_file = Path("imaginary_file.json")
test_dir = Path(__file__).parent.resolve()
test_file = Path("june_test_case.json")
full_test_path = Path(test_dir, test_file)


def test_make_ballots():
# force a no file error with Path = None
assert make_ballots.make_ballots(_edf=None) == NO_FILE
assert make_ballots.make_ballots(test_file) == NO_ERRORS
assert make_ballots(_edf=None) == NO_FILE
# ensure imaginary_file is actually not a file
assert not imaginary_file.is_file()
assert make_ballots(imaginary_file) == NO_FILE
assert full_test_path.is_file()
assert make_ballots(full_test_path) == NO_ERRORS