diff --git a/src/electos/ballotmaker/cli.py b/src/electos/ballotmaker/cli.py index 7006395..ec9a832 100644 --- a/src/electos/ballotmaker/cli.py +++ b/src/electos/ballotmaker/cli.py @@ -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 @@ -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) +log = logging.getLogger(__name__) + def version_callback(value: bool): if value: @@ -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)}" ) return make_ballots_result @@ -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 diff --git a/src/electos/ballotmaker/make_ballots.py b/src/electos/ballotmaker/make_ballots.py index dbf8efb..678863a 100644 --- a/src/electos/ballotmaker/make_ballots.py +++ b/src/electos/ballotmaker/make_ballots.py @@ -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 @@ -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? diff --git a/src/electos/ballotmaker/validate_edf.py b/src/electos/ballotmaker/validate_edf.py index 3a78106..8740a24 100644 --- a/src/electos/ballotmaker/validate_edf.py +++ b/src/electos/ballotmaker/validate_edf.py @@ -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, @@ -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 diff --git a/tests/test_cli.py b/tests/test_cli.py index 8d366a7..df69232 100644 --- a/tests/test_cli.py +++ b/tests/test_cli.py @@ -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 diff --git a/tests/test_make_ballots.py b/tests/test_make_ballots.py index 3fe9e5a..7163e5f 100644 --- a/tests/test_make_ballots.py +++ b/tests/test_make_ballots.py @@ -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