Skip to content

Commit

Permalink
settings: Add OutputFormat enum
Browse files Browse the repository at this point in the history
This prepares for refactoring how we determine output format
  • Loading branch information
jherland committed Feb 20, 2023
1 parent 9a70499 commit 5375df9
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 10 deletions.
4 changes: 2 additions & 2 deletions fawltydeps/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
from fawltydeps import extract_imports
from fawltydeps.check import compare_imports_to_dependencies
from fawltydeps.extract_declared_dependencies import extract_declared_dependencies
from fawltydeps.settings import Action, Settings
from fawltydeps.settings import Action, OutputFormat, Settings
from fawltydeps.types import (
ArgParseError,
DeclaredDependency,
Expand Down Expand Up @@ -177,7 +177,7 @@ def main() -> int:
except ArgParseError as exc:
return parser.error(exc.msg) # exit code 2

if settings.json_output:
if settings.output_format == OutputFormat.JSON:
analysis.print_json(sys.stdout)
else:
analysis.print_human_readable(sys.stdout, details=settings.verbosity > 0)
Expand Down
14 changes: 11 additions & 3 deletions fawltydeps/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,13 @@ class Action(OrderedEnum):
REPORT_UNUSED = "check_unused"


class OutputFormat(OrderedEnum):
"""Output formats provided by the FawltyDeps application."""

HUMAN_READABLE = "human_readable"
JSON = "json"


def parse_path_or_stdin(arg: str) -> PathOrSpecial:
"""Convert --code argument into Path or "<stdin>"."""
if arg == "-":
Expand All @@ -91,7 +98,7 @@ class Settings(BaseSettings): # type: ignore
actions: Set[Action] = {Action.REPORT_UNDECLARED, Action.REPORT_UNUSED}
code: PathOrSpecial = Path(".")
deps: Path = Path(".")
json_output: bool = False
output_format: OutputFormat = OutputFormat.HUMAN_READABLE
ignore_undeclared: Set[str] = set()
ignore_unused: Set[str] = set()
verbosity: int = 0
Expand Down Expand Up @@ -216,8 +223,9 @@ def populate_parser_options(cls, parser: argparse._ActionsContainer) -> None:
)
parser.add_argument(
"--json",
dest="json_output",
action="store_true",
dest="output_format",
action="store_const",
const="json",
help="Generate JSON output instead of a human-readable report",
)
parser.add_argument(
Expand Down
6 changes: 3 additions & 3 deletions tests/test_cmdline.py
Original file line number Diff line number Diff line change
Expand Up @@ -155,7 +155,7 @@ def test_list_imports_json__from_py_file__prints_imports_from_file(write_tmp_fil
"actions": ["list_imports"],
"code": f"{tmp_path}/myfile.py",
"deps": ".",
"json_output": True,
"output_format": "json",
"ignore_undeclared": [],
"ignore_unused": [],
"verbosity": 0,
Expand Down Expand Up @@ -295,7 +295,7 @@ def test_list_deps_json__dir__prints_deps_from_requirements_txt(
"actions": ["list_deps"],
"code": ".",
"deps": f"{tmp_path}",
"json_output": True,
"output_format": "json",
"ignore_undeclared": [],
"ignore_unused": [],
"verbosity": 0,
Expand Down Expand Up @@ -472,7 +472,7 @@ def test_check_json__simple_project__can_report_both_undeclared_and_unused(
"actions": ["check_undeclared", "check_unused"],
"code": f"{tmp_path}",
"deps": f"{tmp_path}",
"json_output": True,
"output_format": "json",
"ignore_undeclared": [],
"ignore_unused": [],
"verbosity": 0,
Expand Down
4 changes: 2 additions & 2 deletions tests/test_settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
from pydantic import ValidationError
from pydantic.env_settings import SettingsError # pylint: disable=no-name-in-module

from fawltydeps.settings import Action, Settings
from fawltydeps.settings import Action, OutputFormat, Settings

if sys.version_info >= (3, 11):
from tomllib import TOMLDecodeError # pylint: disable=no-member
Expand All @@ -19,7 +19,7 @@
actions={Action.REPORT_UNDECLARED, Action.REPORT_UNUSED},
code=Path("."),
deps=Path("."),
json_output=False,
output_format=OutputFormat.HUMAN_READABLE,
ignore_undeclared=set(),
ignore_unused=set(),
verbosity=0,
Expand Down

0 comments on commit 5375df9

Please sign in to comment.