Skip to content

Commit

Permalink
Merge pull request #190 from gadomski/recursive-and-max-depth
Browse files Browse the repository at this point in the history
Split `recursive` into `recursive` and `max_depth`
  • Loading branch information
jonhealy1 authored Mar 10, 2022
2 parents c230da6 + 53a5ba5 commit 9babf15
Show file tree
Hide file tree
Showing 8 changed files with 73 additions and 43 deletions.
20 changes: 19 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,12 @@ All notable changes to this project will be documented in this file.

The format is (loosely) based on [Keep a Changelog](http://keepachangelog.com/) and this project adheres to [Semantic Versioning](http://semver.org/).

## [v2.5.0] - 2022-03-10
### Changed

- Split the `--recursive` option into a `--recursive` flag and a `--max-depth` option
- Renamed the entry point from `stac_validator` to `stac-validator`

## [v2.4.3] - 2022-03-10
### Changed

Expand Down Expand Up @@ -86,7 +92,7 @@ The format is (loosely) based on [Keep a Changelog](http://keepachangelog.com/)
- stac versions where a `stac_version` field is not present are
no longer supported.

## [1.0.0] - 2020-09-01
## [v1.0.1] - 2020-09-01

### Added

Expand All @@ -111,3 +117,15 @@ The format is (loosely) based on [Keep a Changelog](http://keepachangelog.com/)
- Updated core validation to use validation from pystac instead of jsonchema.
- With the newest version - 1.0.0-beta.2 - items will run through jsonchema validation before the PySTAC validation. The reason for this is that jsonschema will give more informative error messages. This should be addressed better in the future. This is not the case with the --recursive option as time can be a concern here with larger collections.
- Logging. Various additions were made here depending on the options selected. This was done to help assist people to update their STAC collections.

[v2.5.0]: <https://github.com/sparkgeo/stac-validator/compare/v2.4.0..main>
[v2.4.0]: <https://github.com/sparkgeo/stac-validator/compare/v2.3.0..v2.4.0>
[v2.3.0]: <https://github.com/sparkgeo/stac-validator/compare/v2.2.0..v2.3.0>
[v2.2.0]: <https://github.com/sparkgeo/stac-validator/compare/v2.1.0..v2.2.0>
[v2.1.0]: <https://github.com/sparkgeo/stac-validator/compare/v2.0.0..v2.1.0>
[v2.0.0]: <https://github.com/sparkgeo/stac-validator/compare/v1.0.1..v2.0.0>
[v1.0.1]: <https://github.com/sparkgeo/stac-validator/compare/v0.5.0..v1.0.1>
[v0.5.0]: <https://github.com/sparkgeo/stac-validator/compare/v0.1.3..v0.5.0>
[v0.1.3]: <https://github.com/sparkgeo/stac-validator/compare/v0.1.1..v0.1.3>
[v0.1.1]: <https://github.com/sparkgeo/stac-validator/compare/v0.1.0..v0.1.1>
[v0.1.0]: <https://github.com/sparkgeo/stac-validator/releases/tag/v0.1.0>
15 changes: 8 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -95,12 +95,12 @@ make help
**Basic Usage**

```bash
stac_validator --help

Usage: stac_validator [OPTIONS] STAC_FILE
stac-validator --help
Usage: stac-validator [OPTIONS] STAC_FILE

Options:
--lint Use stac-check to lint stac object.
--lint Use stac-check to lint stac object instead of
validating it.
--core Validate core stac object only without extensions.
--extensions Validate extensions only.
--links Additionally validate links. Only works with
Expand All @@ -109,8 +109,9 @@ Options:
default mode.
-c, --custom TEXT Validate against a custom schema (local filepath or
remote schema).
-r, --recursive INTEGER Recursively validate all related stac objects. A
depth of -1 indicates full recursion.
-r, --recursive Recursively validate all related stac objects.
-m, --max-depth INTEGER Maximum depth to traverse when recursing. Ignored
if `recursive == False`.
-v, --verbose Enables verbose output for recursive mode.
--no_output Do not print output to console.
--log_file TEXT Save full recursive output to log file (local
Expand Down Expand Up @@ -277,7 +278,7 @@ stac_validator https://raw.githubusercontent.com/radiantearth/stac-spec/master/e
**--recursive**
```bash
stac_validator https://spot-canada-ortho.s3.amazonaws.com/catalog.json --recursive 1 --verbose
stac_validator https://spot-canada-ortho.s3.amazonaws.com/catalog.json --recursive --max-depth 1 --verbose
[
{
"version": "0.8.1",
Expand Down
3 changes: 2 additions & 1 deletion requirements-dev.txt
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
black
pytest
pytest-mypy
pre-commit
pre-commit
6 changes: 3 additions & 3 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

from setuptools import setup

__version__ = "2.4.3"
__version__ = "2.5.0"

with open("README.md", "r") as fh:
long_description = fh.read()
Expand All @@ -29,7 +29,7 @@
long_description=long_description,
long_description_content_type="text/markdown",
url="https://github.com/stac-utils/stac-validator",
download_url="https://github.com/stac-utils/stac-validator/archive/v2.4.3.tar.gz",
download_url="https://github.com/stac-utils/stac-validator/archive/v2.5.0.tar.gz",
install_requires=[
"requests>=2.19.1",
"jsonschema>=3.2.0",
Expand All @@ -39,7 +39,7 @@
],
packages=["stac_validator"],
entry_points={
"console_scripts": ["stac_validator = stac_validator.stac_validator:main"]
"console_scripts": ["stac-validator = stac_validator.stac_validator:main"]
},
python_requires=">=3.7",
tests_require=["pytest"],
Expand Down
19 changes: 15 additions & 4 deletions stac_validator/stac_validator.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,11 @@

@click.command()
@click.argument("stac_file")
@click.option("--lint", is_flag=True, help="Use stac-check to lint stac object.")
@click.option(
"--lint",
is_flag=True,
help="Use stac-check to lint the stac object in addition to validating it.",
)
@click.option(
"--core", is_flag=True, help="Validate core stac object only without extensions."
)
Expand All @@ -34,9 +38,14 @@
@click.option(
"--recursive",
"-r",
is_flag=True,
help="Recursively validate all related stac objects.",
)
@click.option(
"--max-depth",
"-m",
type=int,
default=-2,
help="Recursively validate all related stac objects. A depth of -1 indicates full recursion.",
help="Maximum depth to traverse when recursing. Ignored if `recursive == False`.",
)
@click.option(
"-v", "--verbose", is_flag=True, help="Enables verbose output for recursive mode."
Expand All @@ -52,6 +61,7 @@ def main(
stac_file,
lint,
recursive,
max_depth,
core,
extensions,
links,
Expand All @@ -69,6 +79,7 @@ def main(
stac = StacValidate(
stac_file=stac_file,
recursive=recursive,
max_depth=max_depth,
core=core,
links=links,
assets=assets,
Expand All @@ -83,7 +94,7 @@ def main(
if no_output is False:
click.echo(json.dumps(stac.message, indent=4))

if recursive == -2 and stac.message[0]["valid_stac"] is False:
if not recursive and stac.message[0]["valid_stac"] is False:
sys.exit(1)


Expand Down
18 changes: 11 additions & 7 deletions stac_validator/validate.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import json
import os
from json.decoder import JSONDecodeError
from typing import List
from typing import List, Optional
from urllib.error import HTTPError, URLError

import click # type: ignore
Expand All @@ -22,7 +22,8 @@ class StacValidate:
def __init__(
self,
stac_file: str = None,
recursive: int = -2,
recursive: bool = False,
max_depth: Optional[int] = None,
core: bool = False,
links: bool = False,
assets: bool = False,
Expand All @@ -38,6 +39,7 @@ def __init__(
self.links = links
self.assets = assets
self.recursive = recursive
self.max_depth = max_depth
self.extensions = extensions
self.core = core
self.stac_content: dict = {}
Expand Down Expand Up @@ -203,8 +205,8 @@ def recursive_validator(self, stac_type: str):
message["valid_stac"] = True
self.message.append(message)
self.depth = self.depth + 1
if self.recursive > -1:
if self.depth >= int(self.recursive):
if self.max_depth:
if self.depth >= self.max_depth:
self.skip_val = True
base_url = self.stac_file
for link in self.stac_content["links"]:
Expand Down Expand Up @@ -245,7 +247,9 @@ def recursive_validator(self, stac_type: str):

if self.log != "":
self.message.append(message)
if self.recursive < 5:
if (
self.max_depth and self.max_depth < 5
): # TODO this should be configurable, correct?
self.message.append(message)
if self.verbose is True:
click.echo(json.dumps(message, indent=4))
Expand All @@ -272,7 +276,7 @@ def run(cls):
message["schema"] = [cls.custom]
cls.custom_validator()
cls.valid = True
elif cls.recursive > -2:
elif cls.recursive:
cls.recursive_validator(stac_type)
cls.valid = True
elif cls.extensions is True:
Expand Down Expand Up @@ -312,7 +316,7 @@ def run(cls):

message["valid_stac"] = cls.valid

if cls.recursive < -1:
if not cls.recursive:
cls.message.append(message)

if cls.log != "":
Expand Down
14 changes: 7 additions & 7 deletions tests/test_recursion.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@

def test_recursive_lvl_3_v070():
stac_file = "https://radarstac.s3.amazonaws.com/stac/catalog.json"
stac = stac_validator.StacValidate(stac_file, recursive=4)
stac = stac_validator.StacValidate(stac_file, recursive=True, max_depth=4)
stac.run()
assert stac.message == [
{
Expand Down Expand Up @@ -97,7 +97,7 @@ def test_recursive_lvl_3_v070():

def test_recursive_local_v090():
stac_file = "tests/test_data/v090/catalog.json"
stac = stac_validator.StacValidate(stac_file, recursive=1)
stac = stac_validator.StacValidate(stac_file, recursive=True, max_depth=1)
stac.run()
assert stac.message == [
{
Expand Down Expand Up @@ -133,7 +133,7 @@ def test_recursive_local_v090():

def test_recursive_v1beta1():
stac_file = "tests/test_data/1beta1/sentinel2.json"
stac = stac_validator.StacValidate(stac_file, recursive=0)
stac = stac_validator.StacValidate(stac_file, recursive=True, max_depth=0)
stac.run()
assert stac.message == [
{
Expand All @@ -149,7 +149,7 @@ def test_recursive_v1beta1():

def test_recursive_v1beta2():
stac_file = "https://raw.githubusercontent.com/stac-utils/pystac/main/tests/data-files/examples/1.0.0-beta.2/collection-spec/examples/sentinel2.json"
stac = stac_validator.StacValidate(stac_file, recursive=0)
stac = stac_validator.StacValidate(stac_file, recursive=True, max_depth=0)
stac.run()
assert stac.message == [
{
Expand All @@ -167,7 +167,7 @@ def test_recursive_v1beta2():

def test_recursion_collection_local_v1rc1():
stac_file = "tests/test_data/1rc1/collection.json"
stac = stac_validator.StacValidate(stac_file, recursive=1)
stac = stac_validator.StacValidate(stac_file, recursive=True, max_depth=1)
stac.run()
assert stac.message == [
{
Expand Down Expand Up @@ -219,7 +219,7 @@ def test_recursion_collection_local_v1rc1():

def test_recursion_collection_local_v1rc2():
stac_file = "tests/test_data/1rc2/collection.json"
stac = stac_validator.StacValidate(stac_file, recursive=1)
stac = stac_validator.StacValidate(stac_file, recursive=True, max_depth=1)
stac.run()
assert stac.message == [
{
Expand Down Expand Up @@ -272,7 +272,7 @@ def test_recursion_collection_local_v1rc2():

def test_recursion_collection_local_2_v1rc2():
stac_file = "tests/test_data/1rc2/extensions-collection/collection.json"
stac = stac_validator.StacValidate(stac_file, recursive=1)
stac = stac_validator.StacValidate(stac_file, recursive=True, max_depth=1)
stac.run()
assert stac.message == [
{
Expand Down
21 changes: 8 additions & 13 deletions tests/test_sys_exit.py
Original file line number Diff line number Diff line change
@@ -1,23 +1,18 @@
import subprocess

import pytest


def test_correct_sys_exit_error_python():
try:
with pytest.raises(subprocess.CalledProcessError):
subprocess.run(
["stac_validator", "tests/test_data/bad_data/bad_item_v090.json"],
["stac-validator", "tests/test_data/bad_data/bad_item_v090.json"],
check=True,
)
assert False
except subprocess.CalledProcessError:
assert True


def test_false_sys_exit_error_python():
try:
subprocess.run(
["stac_validator", "tests/test_data/v090/items/good_item_v090.json"],
check=True,
)
assert True
except subprocess.CalledProcessError:
assert False
subprocess.run(
["stac-validator", "tests/test_data/v090/items/good_item_v090.json"],
check=True,
)

0 comments on commit 9babf15

Please sign in to comment.