Skip to content

Commit

Permalink
Merge pull request #5 from kikkomep/enhancement/non-text-format-output
Browse files Browse the repository at this point in the history
enhancement: validation report for non-text formats
  • Loading branch information
kikkomep authored Sep 18, 2024
2 parents a6397d4 + 5f27c2d commit e9de132
Show file tree
Hide file tree
Showing 4 changed files with 49 additions and 22 deletions.
50 changes: 31 additions & 19 deletions rocrate_validator/cli/commands/validate.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@
from InquirerPy import prompt
from InquirerPy.base.control import Choice
from rich.align import Align
from rich.console import Console
from rich.layout import Layout
from rich.live import Live
from rich.markdown import Markdown
Expand All @@ -38,7 +37,7 @@
from rocrate_validator import services
from rocrate_validator.cli.commands.errors import handle_error
from rocrate_validator.cli.main import cli, click
from rocrate_validator.cli.utils import get_app_header_rule
from rocrate_validator.cli.utils import Console, get_app_header_rule
from rocrate_validator.colors import get_severity_color
from rocrate_validator.events import Event, EventType, Subscriber
from rocrate_validator.models import (LevelCollection, Profile, Severity,
Expand Down Expand Up @@ -256,7 +255,8 @@ def validate(ctx,
}

# Print the application header
console.print(get_app_header_rule())
if output_format == "text" and output_file is None:
console.print(get_app_header_rule())

# Get the available profiles
available_profiles = services.get_profiles(profiles_path)
Expand Down Expand Up @@ -329,27 +329,39 @@ def validate(ctx,
report_layout = ValidationReportLayout(console, validation_settings, profile_stats, None)

# Validate RO-Crate against the profile and get the validation result
result: ValidationResult = report_layout.live(
lambda: services.validate(
validation_settings,
subscribers=[report_layout.progress_monitor]
result: ValidationResult = None
if output_format == "text":
console.disabled = output_file is not None
result: ValidationResult = report_layout.live(
lambda: services.validate(
validation_settings,
subscribers=[report_layout.progress_monitor]
)
)
console.disabled = False
else:
result: ValidationResult = services.validate(
validation_settings
)
)

# store the cumulative validation result
is_valid = is_valid and result.passed(LevelCollection.get(requirement_severity).severity)

# Print the validation result
if not result.passed():
verbose_choice = "n"
if interactive and not verbose and enable_pager:
verbose_choice = get_single_char(console, choices=['y', 'n'],
message=(
"[bold] > Do you want to see the validation details? "
"([magenta]y/n[/magenta]): [/bold]"
))
if verbose_choice == "y" or verbose:
report_layout.show_validation_details(pager, enable_pager=enable_pager)
if output_format == "text" and not output_file:
if not result.passed():
verbose_choice = "n"
if interactive and not verbose and enable_pager:
verbose_choice = get_single_char(console, choices=['y', 'n'],
message=(
"[bold] > Do you want to see the validation details? "
"([magenta]y/n[/magenta]): [/bold]"
))
if verbose_choice == "y" or verbose:
report_layout.show_validation_details(pager, enable_pager=enable_pager)

if output_format == "json" and not output_file:
console.print(result.to_json())

if output_file:
# Print the validation report to a file
Expand All @@ -361,7 +373,7 @@ def validate(ctx,
c = Console(file=f, color_system=None, width=output_line_width, height=31)
c.print(report_layout.layout)
report_layout.console = c
if not result.passed():
if not result.passed() and verbose:
report_layout.show_validation_details(None, enable_pager=False)

# Interrupt the validation if the fail fast mode is enabled
Expand Down
3 changes: 1 addition & 2 deletions rocrate_validator/cli/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,9 @@
import sys

import rich_click as click
from rich.console import Console

import rocrate_validator.log as logging
from rocrate_validator.cli.utils import SystemPager
from rocrate_validator.cli.utils import Console, SystemPager
from rocrate_validator.utils import get_version

# set up logging
Expand Down
13 changes: 13 additions & 0 deletions rocrate_validator/cli/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
import textwrap
from typing import Any, Optional

from rich.console import Console as BaseConsole
from rich.padding import Padding
from rich.pager import Pager
from rich.rule import Rule
Expand Down Expand Up @@ -62,3 +63,15 @@ def _pager(self, content: str) -> Any:
def show(self, content: str) -> None:
"""Use the same pager used by pydoc."""
self._pager(content)


class Console(BaseConsole):
"""Rich console that can be disabled."""

def __init__(self, *args, disabled: bool = False, **kwargs):
super().__init__(*args, **kwargs)
self.disabled = disabled

def print(self, *args, **kwargs):
if not self.disabled:
super().print(*args, **kwargs)
5 changes: 4 additions & 1 deletion rocrate_validator/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -1138,9 +1138,12 @@ def __eq__(self, other: object) -> bool:
return self._issues == other._issues

def to_dict(self) -> dict:
allowed_properties = ["data_path", "profiles_path",
"profile_identifier", "inherit_profiles", "requirement_severity", "abort_on_first"]
return {
"rocrate": str(self.rocrate_path),
"validation_settings": self.validation_settings,
"validation_settings": {key: self.validation_settings[key]
for key in allowed_properties if key in self.validation_settings},
"passed": self.passed(self.context.settings["requirement_severity"]),
"issues": [issue.to_dict() for issue in self.issues]
}
Expand Down

0 comments on commit e9de132

Please # to comment.