From a240aa60ce01e140c4ce167b14fc013ffb99074f Mon Sep 17 00:00:00 2001 From: Manuel Bovo Date: Fri, 29 Nov 2024 11:38:47 +0100 Subject: [PATCH] Fix json output (#221) * Fix json output * Update devbox lock * Bump version * Fixing json output --- devbox.json | 9 ++++++++- devbox.lock | 2 +- pyproject.toml | 2 +- src/pdh/output.py | 24 +++++++++++++++++------- tests/test_main.py | 8 +++++++- 5 files changed, 34 insertions(+), 11 deletions(-) diff --git a/devbox.json b/devbox.json index 27da456..0f495cc 100644 --- a/devbox.json +++ b/devbox.json @@ -8,5 +8,12 @@ "pre-commit@3.7.1", "cachix@1.7.4", "statix@latest" - ] + ], + "shell":{ + "init_hook": [ + "pre-commit install", + "source $VENV_DIR/bin/activate && poetry install", + "echo 'source $VENV_DIR/bin/activate' to activate the virtual environment" + ] + } } diff --git a/devbox.lock b/devbox.lock index 6fd1bd0..7b343e2 100644 --- a/devbox.lock +++ b/devbox.lock @@ -308,7 +308,7 @@ }, "python@3.11.9": { "last_modified": "2024-06-12T20:55:33Z", - "plugin_version": "0.0.3", + "plugin_version": "0.0.4", "resolved": "github:NixOS/nixpkgs/a9858885e197f984d92d7fe64e9fff6b2e488d40#python3", "source": "devbox-search", "version": "3.11.9", diff --git a/pyproject.toml b/pyproject.toml index 7f77706..eb01e50 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -22,7 +22,7 @@ ignore = [ [tool.poetry] name = "pdh" -version = "0.6.0" +version = "0.6.1" description = "Pagerduty CLI for Humans" authors = ["Manuel Bovo "] license = "GPL-3.0-or-later" diff --git a/src/pdh/output.py b/src/pdh/output.py index 147d219..fedfe86 100644 --- a/src/pdh/output.py +++ b/src/pdh/output.py @@ -27,7 +27,9 @@ class Output(object): def plain(self, **kwargs) -> None: items = kwargs["items"] if "items" in kwargs else [] plain_print_f = kwargs["print_f"] if "print_f" in kwargs else None - console = kwargs["console"] if "console" in kwargs else Console() + console = kwargs["console"] if "console" in kwargs else None + if console is None: + console = Console() for i in items: if plain_print_f: plain_print_f(i) @@ -36,22 +38,30 @@ def plain(self, **kwargs) -> None: def raw(self, **kwargs) -> None: items = kwargs["items"] if "items" in kwargs else [] - console = kwargs["console"] if "console" in kwargs else Console() + console = kwargs["console"] if "console" in kwargs else None + if console is None: + console = Console() console.print(items) def yaml(self, **kwargs) -> None: items = kwargs["items"] if "items" in kwargs else [] - console = kwargs["console"] if "console" in kwargs else Console() + console = kwargs["console"] if "console" in kwargs else None + if console is None: + console = Console() console.print(yaml.safe_dump(items)) def json(self, **kwargs) -> None: items = kwargs["items"] if "items" in kwargs else [] - console = kwargs["console"] if "console" in kwargs else Console() - console.print(json.dumps(items)) + console = kwargs["console"] if "console" in kwargs else None + if console is None: + console = Console() + console.print_json(json.dumps(items)) def table(self, **kwargs) -> None: items = kwargs["items"] if "items" in kwargs else [] - console = kwargs["console"] if "console" in kwargs else Console() + console = kwargs["console"] if "console" in kwargs else None + if console is None: + console = Console() skip_columns = kwargs["skip_columns"] if "skip_columns" in kwargs else [] odd_color = kwargs["odd_color"] if "odd_color" in kwargs else "grey93 on black" even_color = kwargs["even_color"] if "even_color" in kwargs else "grey50 on black" @@ -78,7 +88,7 @@ def print_items( output, skip_columns: list = [], plain_print_f = None, - console: Console = Console(), + console: Console|None = None, odd_color: str = "grey93 on black", even_color: str = "grey50 on black" ) -> None: diff --git a/tests/test_main.py b/tests/test_main.py index 63b8504..8cc40b7 100644 --- a/tests/test_main.py +++ b/tests/test_main.py @@ -55,7 +55,13 @@ def test_print_items_yaml(): def test_print_items_json(): items = [{"Title": "text", "Url": "https://localhost"}] - expected = '[{"Title": "text", "Url": "https://localhost"}]\n' + expected = """[ + { + "Title": "text", + "Url": "https://localhost" + } +] +""" console = Console(force_terminal=True, file=io.StringIO(), _environ={}, color_system=None) main.print_items(items, output="json", console=console) assert console.file.getvalue() == expected