Skip to content

Commit 9babf15

Browse files
authored
Merge pull request #190 from gadomski/recursive-and-max-depth
Split `recursive` into `recursive` and `max_depth`
2 parents c230da6 + 53a5ba5 commit 9babf15

File tree

8 files changed

+73
-43
lines changed

8 files changed

+73
-43
lines changed

CHANGELOG.md

+19-1
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,12 @@ All notable changes to this project will be documented in this file.
44

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

7+
## [v2.5.0] - 2022-03-10
8+
### Changed
9+
10+
- Split the `--recursive` option into a `--recursive` flag and a `--max-depth` option
11+
- Renamed the entry point from `stac_validator` to `stac-validator`
12+
713
## [v2.4.3] - 2022-03-10
814
### Changed
915

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

89-
## [1.0.0] - 2020-09-01
95+
## [v1.0.1] - 2020-09-01
9096

9197
### Added
9298

@@ -111,3 +117,15 @@ The format is (loosely) based on [Keep a Changelog](http://keepachangelog.com/)
111117
- Updated core validation to use validation from pystac instead of jsonchema.
112118
- 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.
113119
- Logging. Various additions were made here depending on the options selected. This was done to help assist people to update their STAC collections.
120+
121+
[v2.5.0]: <https://github.com/sparkgeo/stac-validator/compare/v2.4.0..main>
122+
[v2.4.0]: <https://github.com/sparkgeo/stac-validator/compare/v2.3.0..v2.4.0>
123+
[v2.3.0]: <https://github.com/sparkgeo/stac-validator/compare/v2.2.0..v2.3.0>
124+
[v2.2.0]: <https://github.com/sparkgeo/stac-validator/compare/v2.1.0..v2.2.0>
125+
[v2.1.0]: <https://github.com/sparkgeo/stac-validator/compare/v2.0.0..v2.1.0>
126+
[v2.0.0]: <https://github.com/sparkgeo/stac-validator/compare/v1.0.1..v2.0.0>
127+
[v1.0.1]: <https://github.com/sparkgeo/stac-validator/compare/v0.5.0..v1.0.1>
128+
[v0.5.0]: <https://github.com/sparkgeo/stac-validator/compare/v0.1.3..v0.5.0>
129+
[v0.1.3]: <https://github.com/sparkgeo/stac-validator/compare/v0.1.1..v0.1.3>
130+
[v0.1.1]: <https://github.com/sparkgeo/stac-validator/compare/v0.1.0..v0.1.1>
131+
[v0.1.0]: <https://github.com/sparkgeo/stac-validator/releases/tag/v0.1.0>

README.md

+8-7
Original file line numberDiff line numberDiff line change
@@ -95,12 +95,12 @@ make help
9595
**Basic Usage**
9696

9797
```bash
98-
stac_validator --help
99-
100-
Usage: stac_validator [OPTIONS] STAC_FILE
98+
stac-validator --help
99+
Usage: stac-validator [OPTIONS] STAC_FILE
101100

102101
Options:
103-
--lint Use stac-check to lint stac object.
102+
--lint Use stac-check to lint stac object instead of
103+
validating it.
104104
--core Validate core stac object only without extensions.
105105
--extensions Validate extensions only.
106106
--links Additionally validate links. Only works with
@@ -109,8 +109,9 @@ Options:
109109
default mode.
110110
-c, --custom TEXT Validate against a custom schema (local filepath or
111111
remote schema).
112-
-r, --recursive INTEGER Recursively validate all related stac objects. A
113-
depth of -1 indicates full recursion.
112+
-r, --recursive Recursively validate all related stac objects.
113+
-m, --max-depth INTEGER Maximum depth to traverse when recursing. Ignored
114+
if `recursive == False`.
114115
-v, --verbose Enables verbose output for recursive mode.
115116
--no_output Do not print output to console.
116117
--log_file TEXT Save full recursive output to log file (local
@@ -277,7 +278,7 @@ stac_validator https://raw.githubusercontent.com/radiantearth/stac-spec/master/e
277278
**--recursive**
278279
279280
```bash
280-
stac_validator https://spot-canada-ortho.s3.amazonaws.com/catalog.json --recursive 1 --verbose
281+
stac_validator https://spot-canada-ortho.s3.amazonaws.com/catalog.json --recursive --max-depth 1 --verbose
281282
[
282283
{
283284
"version": "0.8.1",

requirements-dev.txt

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
black
12
pytest
23
pytest-mypy
3-
pre-commit
4+
pre-commit

setup.py

+3-3
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
from setuptools import setup
44

5-
__version__ = "2.4.3"
5+
__version__ = "2.5.0"
66

77
with open("README.md", "r") as fh:
88
long_description = fh.read()
@@ -29,7 +29,7 @@
2929
long_description=long_description,
3030
long_description_content_type="text/markdown",
3131
url="https://github.com/stac-utils/stac-validator",
32-
download_url="https://github.com/stac-utils/stac-validator/archive/v2.4.3.tar.gz",
32+
download_url="https://github.com/stac-utils/stac-validator/archive/v2.5.0.tar.gz",
3333
install_requires=[
3434
"requests>=2.19.1",
3535
"jsonschema>=3.2.0",
@@ -39,7 +39,7 @@
3939
],
4040
packages=["stac_validator"],
4141
entry_points={
42-
"console_scripts": ["stac_validator = stac_validator.stac_validator:main"]
42+
"console_scripts": ["stac-validator = stac_validator.stac_validator:main"]
4343
},
4444
python_requires=">=3.7",
4545
tests_require=["pytest"],

stac_validator/stac_validator.py

+15-4
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,11 @@
1010

1111
@click.command()
1212
@click.argument("stac_file")
13-
@click.option("--lint", is_flag=True, help="Use stac-check to lint stac object.")
13+
@click.option(
14+
"--lint",
15+
is_flag=True,
16+
help="Use stac-check to lint the stac object in addition to validating it.",
17+
)
1418
@click.option(
1519
"--core", is_flag=True, help="Validate core stac object only without extensions."
1620
)
@@ -34,9 +38,14 @@
3438
@click.option(
3539
"--recursive",
3640
"-r",
41+
is_flag=True,
42+
help="Recursively validate all related stac objects.",
43+
)
44+
@click.option(
45+
"--max-depth",
46+
"-m",
3747
type=int,
38-
default=-2,
39-
help="Recursively validate all related stac objects. A depth of -1 indicates full recursion.",
48+
help="Maximum depth to traverse when recursing. Ignored if `recursive == False`.",
4049
)
4150
@click.option(
4251
"-v", "--verbose", is_flag=True, help="Enables verbose output for recursive mode."
@@ -52,6 +61,7 @@ def main(
5261
stac_file,
5362
lint,
5463
recursive,
64+
max_depth,
5565
core,
5666
extensions,
5767
links,
@@ -69,6 +79,7 @@ def main(
6979
stac = StacValidate(
7080
stac_file=stac_file,
7181
recursive=recursive,
82+
max_depth=max_depth,
7283
core=core,
7384
links=links,
7485
assets=assets,
@@ -83,7 +94,7 @@ def main(
8394
if no_output is False:
8495
click.echo(json.dumps(stac.message, indent=4))
8596

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

89100

stac_validator/validate.py

+11-7
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import json
22
import os
33
from json.decoder import JSONDecodeError
4-
from typing import List
4+
from typing import List, Optional
55
from urllib.error import HTTPError, URLError
66

77
import click # type: ignore
@@ -22,7 +22,8 @@ class StacValidate:
2222
def __init__(
2323
self,
2424
stac_file: str = None,
25-
recursive: int = -2,
25+
recursive: bool = False,
26+
max_depth: Optional[int] = None,
2627
core: bool = False,
2728
links: bool = False,
2829
assets: bool = False,
@@ -38,6 +39,7 @@ def __init__(
3839
self.links = links
3940
self.assets = assets
4041
self.recursive = recursive
42+
self.max_depth = max_depth
4143
self.extensions = extensions
4244
self.core = core
4345
self.stac_content: dict = {}
@@ -203,8 +205,8 @@ def recursive_validator(self, stac_type: str):
203205
message["valid_stac"] = True
204206
self.message.append(message)
205207
self.depth = self.depth + 1
206-
if self.recursive > -1:
207-
if self.depth >= int(self.recursive):
208+
if self.max_depth:
209+
if self.depth >= self.max_depth:
208210
self.skip_val = True
209211
base_url = self.stac_file
210212
for link in self.stac_content["links"]:
@@ -245,7 +247,9 @@ def recursive_validator(self, stac_type: str):
245247

246248
if self.log != "":
247249
self.message.append(message)
248-
if self.recursive < 5:
250+
if (
251+
self.max_depth and self.max_depth < 5
252+
): # TODO this should be configurable, correct?
249253
self.message.append(message)
250254
if self.verbose is True:
251255
click.echo(json.dumps(message, indent=4))
@@ -272,7 +276,7 @@ def run(cls):
272276
message["schema"] = [cls.custom]
273277
cls.custom_validator()
274278
cls.valid = True
275-
elif cls.recursive > -2:
279+
elif cls.recursive:
276280
cls.recursive_validator(stac_type)
277281
cls.valid = True
278282
elif cls.extensions is True:
@@ -312,7 +316,7 @@ def run(cls):
312316

313317
message["valid_stac"] = cls.valid
314318

315-
if cls.recursive < -1:
319+
if not cls.recursive:
316320
cls.message.append(message)
317321

318322
if cls.log != "":

tests/test_recursion.py

+7-7
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99

1010
def test_recursive_lvl_3_v070():
1111
stac_file = "https://radarstac.s3.amazonaws.com/stac/catalog.json"
12-
stac = stac_validator.StacValidate(stac_file, recursive=4)
12+
stac = stac_validator.StacValidate(stac_file, recursive=True, max_depth=4)
1313
stac.run()
1414
assert stac.message == [
1515
{
@@ -97,7 +97,7 @@ def test_recursive_lvl_3_v070():
9797

9898
def test_recursive_local_v090():
9999
stac_file = "tests/test_data/v090/catalog.json"
100-
stac = stac_validator.StacValidate(stac_file, recursive=1)
100+
stac = stac_validator.StacValidate(stac_file, recursive=True, max_depth=1)
101101
stac.run()
102102
assert stac.message == [
103103
{
@@ -133,7 +133,7 @@ def test_recursive_local_v090():
133133

134134
def test_recursive_v1beta1():
135135
stac_file = "tests/test_data/1beta1/sentinel2.json"
136-
stac = stac_validator.StacValidate(stac_file, recursive=0)
136+
stac = stac_validator.StacValidate(stac_file, recursive=True, max_depth=0)
137137
stac.run()
138138
assert stac.message == [
139139
{
@@ -149,7 +149,7 @@ def test_recursive_v1beta1():
149149

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

168168
def test_recursion_collection_local_v1rc1():
169169
stac_file = "tests/test_data/1rc1/collection.json"
170-
stac = stac_validator.StacValidate(stac_file, recursive=1)
170+
stac = stac_validator.StacValidate(stac_file, recursive=True, max_depth=1)
171171
stac.run()
172172
assert stac.message == [
173173
{
@@ -219,7 +219,7 @@ def test_recursion_collection_local_v1rc1():
219219

220220
def test_recursion_collection_local_v1rc2():
221221
stac_file = "tests/test_data/1rc2/collection.json"
222-
stac = stac_validator.StacValidate(stac_file, recursive=1)
222+
stac = stac_validator.StacValidate(stac_file, recursive=True, max_depth=1)
223223
stac.run()
224224
assert stac.message == [
225225
{
@@ -272,7 +272,7 @@ def test_recursion_collection_local_v1rc2():
272272

273273
def test_recursion_collection_local_2_v1rc2():
274274
stac_file = "tests/test_data/1rc2/extensions-collection/collection.json"
275-
stac = stac_validator.StacValidate(stac_file, recursive=1)
275+
stac = stac_validator.StacValidate(stac_file, recursive=True, max_depth=1)
276276
stac.run()
277277
assert stac.message == [
278278
{

tests/test_sys_exit.py

+8-13
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,18 @@
11
import subprocess
22

3+
import pytest
4+
35

46
def test_correct_sys_exit_error_python():
5-
try:
7+
with pytest.raises(subprocess.CalledProcessError):
68
subprocess.run(
7-
["stac_validator", "tests/test_data/bad_data/bad_item_v090.json"],
9+
["stac-validator", "tests/test_data/bad_data/bad_item_v090.json"],
810
check=True,
911
)
10-
assert False
11-
except subprocess.CalledProcessError:
12-
assert True
1312

1413

1514
def test_false_sys_exit_error_python():
16-
try:
17-
subprocess.run(
18-
["stac_validator", "tests/test_data/v090/items/good_item_v090.json"],
19-
check=True,
20-
)
21-
assert True
22-
except subprocess.CalledProcessError:
23-
assert False
15+
subprocess.run(
16+
["stac-validator", "tests/test_data/v090/items/good_item_v090.json"],
17+
check=True,
18+
)

0 commit comments

Comments
 (0)