Skip to content

Commit b00e635

Browse files
authored
Merge pull request crs4#121 from kikkomep/CU-8699vt19v_108_Make-the-output-formats-accessible-from-the-Python-API-rebased
feat: enhance output rendering and restructure UI/IO layers
2 parents 0c0ab0a + d1af451 commit b00e635

26 files changed

Lines changed: 2239 additions & 765 deletions

docs/3_usage_api.rst

Lines changed: 71 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,77 @@ representing the metadata and validates it against a given validation profile.
5757
rocrate_metadata = json.load(f)
5858
5959
# validate the metadata dictionary
60-
result = validation_report = validate_metadata_as_dict(rocrate_metadata, settings=settings)
60+
result = validate_metadata_as_dict(rocrate_metadata, settings=settings)
6161
6262
# process the validation result as needed
6363
...
64+
65+
66+
Formatting Validation Results
67+
-----------------------------
68+
69+
Validation results can be rendered using different output formatters provided by
70+
the library. Two formatter types are available: *text* and *JSON*.
71+
Both rely on the ``rich`` Python library and integrate with the
72+
``rocrate_validator.io.output.console.Console`` class, which extends
73+
``rich.console.Console`` to support custom formatter registration.
74+
75+
To format results, create a ``Console`` instance, register one formatter,
76+
and then print any validation output object (e.g., the full report or the
77+
aggregated statistics).
78+
79+
80+
TextOutputFormatter
81+
~~~~~~~~~~~~~~~~~~~
82+
83+
``TextOutputFormatter`` renders validation reports as human-readable, styled text.
84+
It is typically used for console output, report generation, or writing results
85+
to a file.
86+
87+
.. code-block:: python
88+
89+
from rocrate_validator.io.output.console import Console
90+
from rocrate_validator.io.output.text import TextOutputFormatter
91+
92+
console = Console()
93+
console.register_formatter(TextOutputFormatter())
94+
95+
# Print the main validation result
96+
console.print(result)
97+
98+
# Print aggregated statistics (violations by severity, executed checks, etc.)
99+
console.print(result.statistics)
100+
101+
# Write the output to a file
102+
with open("validation_report.txt", "w") as f:
103+
file_console = Console(file=f)
104+
file_console.register_formatter(TextOutputFormatter())
105+
file_console.print(result.statistics)
106+
file_console.print(result)
107+
108+
109+
JSONOutputFormatter
110+
~~~~~~~~~~~~~~~~~~~
111+
112+
``JSONOutputFormatter`` produces JSON-structured output, suitable for logging,
113+
programmatic processing, or integration with external tools.
114+
115+
.. code-block:: python
116+
117+
from rocrate_validator.io.output.console import Console
118+
from rocrate_validator.io.output.json import JSONOutputFormatter
119+
120+
console = Console()
121+
console.register_formatter(JSONOutputFormatter())
122+
123+
# Print the main validation result as JSON
124+
console.print(result)
125+
126+
# Print the aggregated statistics
127+
console.print(result.statistics)
128+
129+
# Write the output to a file
130+
with open("validation_report.json", "w") as f:
131+
file_console = Console(file=f)
132+
file_console.register_formatter(JSONOutputFormatter())
133+
file_console.print(result)

rocrate_validator/cli/commands/profiles.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,9 +25,9 @@
2525
from rocrate_validator import services
2626
from rocrate_validator.cli.commands.errors import handle_error
2727
from rocrate_validator.cli.main import cli, click
28-
from rocrate_validator.cli.utils import get_app_header_rule
2928
from rocrate_validator.colors import get_severity_color
3029
from rocrate_validator.constants import DEFAULT_PROFILE_IDENTIFIER
30+
from rocrate_validator.io.output.text.layout.report import get_app_header_rule
3131
from rocrate_validator.models import (LevelCollection, RequirementLevel,
3232
Severity)
3333
from rocrate_validator.utils import get_profiles_path, shorten_path

0 commit comments

Comments
 (0)