From dcee0e098f646b59bdb8c91f11fb14e3802590ab Mon Sep 17 00:00:00 2001 From: Goffi Date: Mon, 15 Dec 2025 11:35:26 +0100 Subject: [PATCH] cli (output): Add a `OUTPUT_MODEL` for Pydantic models: As Pydantic becomes more and more used in Libervia, it is now important to have a dedicated output in the CLI. This patch also fixes `json` and `json_raw` outputs which were incorrectly calling `console.print_json`. rel 473 --- libervia/cli/constants.py | 4 +++- libervia/cli/output_std.py | 14 ++++++++++++-- 2 files changed, 15 insertions(+), 3 deletions(-) diff --git a/libervia/cli/constants.py b/libervia/cli/constants.py index 10f8c08d..b2708cfd 100644 --- a/libervia/cli/constants.py +++ b/libervia/cli/constants.py @@ -44,6 +44,7 @@ class Const(constants.Const): OUTPUT_LIST_XML = "list_xml" # list of XML nodes (as unicode strings) OUTPUT_XMLUI = "xmlui" # XMLUI as unicode string OUTPUT_LIST_XMLUI = "list_xmlui" # list of XMLUI (as unicode strings) + OUTPUT_MODEL = "model" # Pydantic model OUTPUT_TYPES = ( OUTPUT_CUSTOM, OUTPUT_TEXT, @@ -57,6 +58,7 @@ class Const(constants.Const): OUTPUT_LIST_XML, OUTPUT_XMLUI, OUTPUT_LIST_XMLUI, + OUTPUT_MODEL, ) OUTPUT_NAME_SIMPLE = "simple" OUTPUT_NAME_XML = "xml" @@ -81,7 +83,7 @@ class Const(constants.Const): A_SUCCESS = A.BOLD + A.FG_GREEN A_FAILURE = A.BOLD + A.FG_RED A_WARNING = A.BOLD + A.FG_RED - #  A_PROMPT_* is for shell + # A_PROMPT_* is for shell A_PROMPT_PATH = A.BOLD + A.FG_CYAN A_PROMPT_SUF = A.BOLD # Files diff --git a/libervia/cli/output_std.py b/libervia/cli/output_std.py index 6e5688ce..d580657e 100644 --- a/libervia/cli/output_std.py +++ b/libervia/cli/output_std.py @@ -130,9 +130,19 @@ class Json: host.register_output(C.OUTPUT_MESS, C.OUTPUT_NAME_JSON_RAW, self.dump) host.register_output(C.OUTPUT_COMPLEX, C.OUTPUT_NAME_JSON, self.dump_pretty) host.register_output(C.OUTPUT_COMPLEX, C.OUTPUT_NAME_JSON_RAW, self.dump) + host.register_output( + C.OUTPUT_MODEL, + C.OUTPUT_NAME_JSON, + lambda model: self.dump_pretty(model.model_dump_json()), + ) + host.register_output( + C.OUTPUT_MODEL, + C.OUTPUT_NAME_JSON_RAW, + lambda model: self.dump(model.model_dump_json()), + ) def dump(self, data): - self.host.console.print_json(data=data, indent=None, highlight=False, default=str) + self.host.console.print_json(json=data, indent=None, highlight=False, default=str) def dump_pretty(self, data): - self.host.console.print_json(data=data, indent=4, default=str) + self.host.console.print_json(json=data, indent=4, default=str)