1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354
|
import json
import sys
import time
from typing import Any, Dict, List, Optional, Tuple
import click # type: ignore
from .validate import StacValidate
def _print_summary(
title: str, valid_count: int, total_count: int, obj_type: str = "STAC objects"
) -> None:
"""Helper function to print a consistent summary line.
Args:
title (str): Title of the summary section
valid_count (int): Number of valid items
total_count (int): Total number of items
obj_type (str): Type of objects being counted (e.g., 'items', 'collections')
"""
click.secho()
click.secho(f"{title}:", bold=True)
if total_count > 0:
percentage = (valid_count / total_count) * 100
click.secho(
f" {obj_type.capitalize()} passed: {valid_count}/{total_count} ({percentage:.1f}%)"
)
else:
click.secho(f" No {obj_type} found to validate")
def format_duration(seconds: float) -> str:
"""Format duration in seconds to a human-readable string.
Args:
seconds (float): Duration in seconds
Returns:
str: Formatted duration string (e.g., '1m 23.45s' or '456.78ms')
"""
if seconds < 1.0:
return f"{seconds * 1000:.2f}ms"
minutes, seconds = divmod(seconds, 60)
if minutes > 0:
return f"{int(minutes)}m {seconds:.2f}s"
return f"{seconds:.2f}s"
def print_update_message(version: str) -> None:
"""Prints an update message for `stac-validator` based on the version of the
STAC file being validated.
Args:
version (str): The version of the STAC file being validated.
Returns:
None
"""
click.secho()
if version != "1.1.0":
click.secho(
f"Please upgrade from version {version} to version 1.1.0!", fg="red"
)
else:
click.secho("Thanks for using STAC version 1.1.0!", fg="green")
click.secho()
def item_collection_summary(message: List[Dict[str, Any]]) -> None:
"""Prints a summary of the validation results for an item collection response.
Args:
message (List[Dict[str, Any]]): The validation results for the item collection.
Returns:
None
"""
valid_count = sum(1 for item in message if item.get("valid_stac") is True)
_print_summary("-- Item Collection Summary", valid_count, len(message), "items")
def collections_summary(message: List[Dict[str, Any]]) -> None:
"""Prints a summary of the validation results for a collections response.
Args:
message (List[Dict[str, Any]]): The validation results for the collections.
Returns:
None
"""
valid_count = sum(1 for coll in message if coll.get("valid_stac") is True)
_print_summary("-- Collections Summary", valid_count, len(message), "collections")
def recursive_validation_summary(message: List[Dict[str, Any]]) -> None:
"""Prints a summary of the recursive validation results.
Args:
message (List[Dict[str, Any]]): The validation results from recursive validation.
Returns:
None
"""
# Count valid and total objects by type
type_counts = {}
total_valid = 0
for item in message:
if not isinstance(item, dict):
continue
obj_type = item.get("asset_type", "unknown").lower()
is_valid = item.get("valid_stac", False) is True
if obj_type not in type_counts:
type_counts[obj_type] = {"valid": 0, "total": 0}
type_counts[obj_type]["total"] += 1
if is_valid:
type_counts[obj_type]["valid"] += 1
total_valid += 1
# Print overall summary
_print_summary("-- Recursive Validation Summary", total_valid, len(message))
# Print breakdown by type if there are multiple types
if len(type_counts) > 1:
click.secho("\n Breakdown by type:")
for obj_type, counts in sorted(type_counts.items()):
percentage = (
(counts["valid"] / counts["total"]) * 100 if counts["total"] > 0 else 0
)
click.secho(
f" {obj_type.capitalize()}: {counts['valid']}/{counts['total']} ({percentage:.1f}%)"
)
@click.command()
@click.argument("stac_file")
@click.option(
"--core", is_flag=True, help="Validate core stac object only without extensions."
)
@click.option("--extensions", is_flag=True, help="Validate extensions only.")
@click.option(
"--links",
is_flag=True,
help="Additionally validate links. Only works with default mode.",
)
@click.option(
"--assets",
is_flag=True,
help="Additionally validate assets. Only works with default mode.",
)
@click.option(
"--custom",
"-c",
default="",
help="Validate against a custom schema (local filepath or remote schema).",
)
@click.option(
"--schema-config",
"-sc",
default="",
help="Validate against a custom schema config (local filepath or remote schema config).",
)
@click.option(
"--schema-map",
"-s",
type=(str, str),
multiple=True,
help="Schema path to replaced by (local) schema path during validation. Can be used multiple times.",
)
@click.option(
"--recursive",
"-r",
is_flag=True,
help="Recursively validate all related stac objects.",
)
@click.option(
"--max-depth",
"-m",
type=int,
help="Maximum depth to traverse when recursing. Omit this argument to get full recursion. Ignored if `recursive == False`.",
)
@click.option(
"--collections",
is_flag=True,
help="Validate /collections response.",
)
@click.option(
"--item-collection",
is_flag=True,
help="Validate item collection response. Can be combined with --pages. Defaults to one page.",
)
@click.option(
"--no-assets-urls",
is_flag=True,
help="Disables the opening of href links when validating assets (enabled by default).",
)
@click.option(
"--header",
type=(str, str),
multiple=True,
help="HTTP header to include in the requests. Can be used multiple times.",
)
@click.option(
"--pages",
"-p",
type=int,
help="Maximum number of pages to validate via --item-collection. Defaults to one page.",
)
@click.option(
"-t",
"--trace-recursion",
is_flag=True,
help="Enables verbose output for recursive mode.",
)
@click.option("--no_output", is_flag=True, help="Do not print output to console.")
@click.option(
"--log_file",
default="",
help="Save full recursive output to log file (local filepath).",
)
@click.option(
"--pydantic",
is_flag=True,
help="Validate using stac-pydantic models for enhanced type checking and validation.",
)
@click.option(
"--verbose",
is_flag=True,
help="Enable verbose output. This will output additional information during validation.",
)
def main(
stac_file: str,
collections: bool,
item_collection: bool,
no_assets_urls: bool,
header: list,
pages: int,
recursive: bool,
max_depth: int,
core: bool,
extensions: bool,
links: bool,
assets: bool,
custom: str,
schema_config: str,
schema_map: List[Tuple],
trace_recursion: bool,
no_output: bool,
log_file: str,
pydantic: bool,
verbose: bool = False,
):
"""Main function for the `stac-validator` command line tool. Validates a STAC file
against the STAC specification and prints the validation results to the console as JSON.
Args:
stac_file (str): Path to the STAC file to be validated.
collections (bool): Validate response from /collections endpoint.
item_collection (bool): Whether to validate item collection responses.
no_assets_urls (bool): Whether to open href links when validating assets
(enabled by default).
headers (dict): HTTP headers to include in the requests.
pages (int): Maximum number of pages to validate via `item_collection`.
recursive (bool): Whether to recursively validate all related STAC objects.
max_depth (int): Maximum depth to traverse when recursing.
core (bool): Whether to validate core STAC objects only.
extensions (bool): Whether to validate extensions only.
links (bool): Whether to additionally validate links. Only works with default mode.
assets (bool): Whether to additionally validate assets. Only works with default mode.
custom (str): Path to a custom schema file to validate against.
schema_config (str): Path to a custom schema config file to validate against.
schema_map (list(tuple)): List of tuples each having two elememts. First element is the schema path to be replaced by the path in the second element.
trace_recursion (bool): Whether to enable verbose output for recursive mode.
no_output (bool): Whether to print output to console.
log_file (str): Path to a log file to save full recursive output.
pydantic (bool): Whether to validate using stac-pydantic models for enhanced type checking and validation.
verbose (bool): Whether to enable verbose output. This will output additional information during validation.
Returns:
None
Raises:
SystemExit: Exits the program with a status code of 0 if the STAC file is valid,
or 1 if it is invalid.
"""
start_time = time.time()
valid = True
if schema_map == ():
schema_map_dict: Optional[Dict[str, str]] = None
else:
schema_map_dict = dict(schema_map)
stac = StacValidate(
stac_file=stac_file,
collections=collections,
item_collection=item_collection,
pages=pages,
recursive=recursive,
max_depth=max_depth,
core=core,
links=links,
assets=assets,
assets_open_urls=not no_assets_urls,
headers=dict(header),
extensions=extensions,
custom=custom,
schema_config=schema_config,
schema_map=schema_map_dict,
trace_recursion=trace_recursion,
log=log_file,
pydantic=pydantic,
verbose=verbose,
)
try:
if not item_collection and not collections:
valid = stac.run()
elif collections:
stac.validate_collections()
else:
stac.validate_item_collection()
message = stac.message
if "version" in message[0]:
print_update_message(message[0]["version"])
if no_output is False:
click.echo(json.dumps(message, indent=4))
# Print appropriate summary based on validation mode
if item_collection:
item_collection_summary(message)
elif collections:
collections_summary(message)
elif recursive:
recursive_validation_summary(message)
finally:
# Always print the duration, even if validation fails
duration = time.time() - start_time
click.secho(
f"\nValidation completed in {format_duration(duration)}", fg="green"
)
click.secho()
sys.exit(0 if valid else 1)
if __name__ == "__main__":
main()
|