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
This commit is contained in:
Goffi 2025-12-15 11:35:26 +01:00
commit dcee0e098f
2 changed files with 15 additions and 3 deletions

View file

@ -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

View file

@ -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)