|
26 | 26 | from space_packet_parser import ccsds |
27 | 27 | from space_packet_parser.ccsds import ccsds_generator |
28 | 28 | from space_packet_parser.xtce.definitions import DEFAULT_ROOT_CONTAINER, XtcePacketDefinition |
| 29 | +from space_packet_parser.xtce.validation import validate_xtce |
29 | 30 |
|
30 | 31 | # Initialize a console instance for rich output |
31 | 32 | console = Console() |
@@ -208,3 +209,55 @@ def parse( |
208 | 209 | # Limit the number of packets and variables printed |
209 | 210 | # also limit the length of strings (binary data can be long) |
210 | 211 | pretty.pprint(packets, indent_guides=False, max_length=max_items, max_string=max_string) |
| 212 | + |
| 213 | + |
| 214 | +@spp.command() |
| 215 | +@click.argument("file_path", type=click.Path(exists=True, path_type=Path)) |
| 216 | +@click.option( |
| 217 | + "--level", |
| 218 | + type=click.Choice(["schema", "structure", "all"], case_sensitive=False), |
| 219 | + default="all", |
| 220 | + help="Validation level to perform", |
| 221 | +) |
| 222 | +@click.option("--timeout", type=int, default=30, help="Timeout in seconds for schema downloads") |
| 223 | +@click.option("--local-xsd", type=click.Path(exists=True, path_type=Path), help="Local XSD file for schema validation") |
| 224 | +def validate(file_path: Path, level: str, timeout: int, local_xsd: Path) -> None: |
| 225 | + """Validate an XTCE document.""" |
| 226 | + logging.info(f"Validating XTCE file: {file_path}") |
| 227 | + logging.info(f"Validation level: {level}") |
| 228 | + logging.debug(f"Timeout: {timeout}") |
| 229 | + logging.debug(f"Local XSD: {local_xsd}") |
| 230 | + |
| 231 | + result = validate_xtce( |
| 232 | + file_path, |
| 233 | + level=level.lower(), |
| 234 | + timeout=timeout, |
| 235 | + print_results=False, |
| 236 | + raise_on_error=False, |
| 237 | + local_xsd=local_xsd, |
| 238 | + ) |
| 239 | + |
| 240 | + # Display results in rich format (complementing the print_results from validate_xtce) |
| 241 | + if result.valid: |
| 242 | + console.print(f"[bold green]✓ VALID[/bold green] ({result.validation_level.value} level)") |
| 243 | + else: |
| 244 | + console.print(f"[bold red]✗ INVALID[/bold red] ({result.validation_level.value} level)") |
| 245 | + |
| 246 | + if result.schema_location: |
| 247 | + console.print(f"Schema: {result.schema_location}") |
| 248 | + if result.schema_version: |
| 249 | + console.print(f"Version: {result.schema_version}") |
| 250 | + |
| 251 | + if result.validation_time_ms: |
| 252 | + console.print(f"Validation time: {result.validation_time_ms:.1f}ms") |
| 253 | + |
| 254 | + if result.errors: |
| 255 | + console.print(f"\n[bold red]Errors ({len(result.errors)}):[/bold red]") |
| 256 | + for error in result.errors: |
| 257 | + console.print(f" {error}") |
| 258 | + if error.context: |
| 259 | + console.print(f" - Additional context: {error.context}") |
| 260 | + |
| 261 | + # Exit with error code if validation failed (unless raise_on_error is True, which already raised) |
| 262 | + if not result.valid: |
| 263 | + raise click.ClickException("Validation failed") |
0 commit comments