File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff line change 11""" The BallotMaker Command Line Interface (CLI)"""
2+ import logging
3+ import sys
24from os import strerror
35from pathlib import Path
46from typing import Optional
1214STYLE_HELP = "Stylesheet file for ballot generation"
1315VERSION_HELP = "Print the version number."
1416
17+ # configure logging
18+ logging .basicConfig (stream = sys .stdout , level = logging .DEBUG )
19+ log = logging .getLogger (__name__ )
20+
1521
1622def version_callback (value : bool ):
1723 if value :
@@ -45,8 +51,8 @@ def make(
4551 """Make ballots from EDF file"""
4652 make_ballots_result = make_ballots .make_ballots (edf , output_dir , style )
4753 if make_ballots_result != NO_ERRORS :
48- typer . echo (
49- f"Error { make_ballots_result } making ballots: { strerror (make_ballots_result )} "
54+ log . error (
55+ f"Code { make_ballots_result } in make - { strerror (make_ballots_result )} "
5056 )
5157 return make_ballots_result
5258
@@ -61,8 +67,8 @@ def validate(
6167 """Validate data in EDF file"""
6268 validate_edf_result = validate_edf .validate_edf (edf )
6369 if validate_edf_result != NO_ERRORS :
64- typer . echo (
65- f"Error { validate_edf_result } validating EDF: { strerror (validate_edf_result )} "
70+ log . error (
71+ f"Code { validate_edf_result } in validate - { strerror (validate_edf_result )} "
6672 )
6773 return validate_edf_result
6874
Original file line number Diff line number Diff line change 1+ import logging
12from pathlib import Path
23
34from electos .ballotmaker .constants import NO_ERRORS , NO_FILE
45
6+ log = logging .getLogger (__name__ )
7+
58
69def make_ballots (
710 _edf : Path , _output_dir : Path = None , _styles : Path = None
@@ -13,8 +16,12 @@ def make_ballots(
1316 Output directory for generated PDF files
1417 Styles file for ballot formatting
1518 """
16- # is the EDF a JSON file?
19+ # is the EDF a file?
1720 if _edf is None :
21+ log .debug ("No EDF file provided to make ballots." )
22+ return NO_FILE
23+ if not _edf .is_file ():
24+ log .debug (f"Can't make ballots, EDF { _edf } is not a file" )
1825 return NO_FILE
1926 # was a valid output directory provided?
2027 # was a styles file provided?
Original file line number Diff line number Diff line change 1+ import logging
12from pathlib import Path
23
34from electos .ballotmaker .constants import NO_DATA , NO_ERRORS , NO_FILE
45from electos .ballotmaker .read_edf import read_edf
56
7+ log = logging .getLogger (__name__ )
8+
69
710def validate_edf (
811 _edf : Path ,
@@ -13,10 +16,12 @@ def validate_edf(
1316 """
1417 # is the EDF a file?
1518 if _edf is None :
19+ log .debug ("No EDF file provided for validation." )
1620 return NO_FILE
1721 if not _edf .is_file ():
22+ log .debug (f"Can't validate, EDF { _edf } is not a file" )
1823 return NO_FILE
1924
2025 ballot_style_count = read_edf (_edf )
21- print (f"Found { ballot_style_count } ballots in { _edf } " )
26+ log . info (f"Found { ballot_style_count } ballots in { _edf } " )
2227 return NO_ERRORS
Original file line number Diff line number Diff line change @@ -34,7 +34,7 @@ def test_make():
3434 # bypass mandatory CLI option to force error
3535 assert cli .make (edf = None ) == NO_FILE
3636 # any old path will satisfy current tests
37- assert cli .make (imaginary_file ) == NO_ERRORS
37+ assert cli .make (imaginary_file ) == NO_FILE
3838 # check CLI errors: no options for make
3939 result = runner .invoke (cli .app , ["make" ])
4040 assert result .exit_code == NO_FILE
Original file line number Diff line number Diff line change 11from pathlib import Path
22
3- from electos .ballotmaker import make_ballots
43from electos .ballotmaker .constants import NO_ERRORS , NO_FILE
4+ from electos .ballotmaker .make_ballots import make_ballots
55
6- test_file = Path ("empty.json" )
6+ imaginary_file = Path ("imaginary_file.json" )
7+ test_dir = Path (__file__ ).parent .resolve ()
8+ test_file = Path ("june_test_case.json" )
9+ full_test_path = Path (test_dir , test_file )
710
811
912def test_make_ballots ():
1013 # force a no file error with Path = None
11- assert make_ballots .make_ballots (_edf = None ) == NO_FILE
12- assert make_ballots .make_ballots (test_file ) == NO_ERRORS
14+ assert make_ballots (_edf = None ) == NO_FILE
15+ # ensure imaginary_file is actually not a file
16+ assert not imaginary_file .is_file ()
17+ assert make_ballots (imaginary_file ) == NO_FILE
18+ assert full_test_path .is_file ()
19+ assert make_ballots (full_test_path ) == NO_ERRORS
You can’t perform that action at this time.
0 commit comments