Skip to content

Commit b83fe16

Browse files
committed
refactor: split into recursive and max-depth
This makes it more clear how to enable/disable recursion.
1 parent 0a4eecb commit b83fe16

File tree

4 files changed

+36
-24
lines changed

4 files changed

+36
-24
lines changed

README.md

Lines changed: 8 additions & 7 deletions
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",

stac_validator/stac_validator.py

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -38,9 +38,14 @@
3838
@click.option(
3939
"--recursive",
4040
"-r",
41+
is_flag=True,
42+
help="Recursively validate all related stac objects.",
43+
)
44+
@click.option(
45+
"--max-depth",
46+
"-m",
4147
type=int,
42-
default=-2,
43-
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`.",
4449
)
4550
@click.option(
4651
"-v", "--verbose", is_flag=True, help="Enables verbose output for recursive mode."
@@ -56,6 +61,7 @@ def main(
5661
stac_file,
5762
lint,
5863
recursive,
64+
max_depth,
5965
core,
6066
extensions,
6167
links,
@@ -73,6 +79,7 @@ def main(
7379
stac = StacValidate(
7480
stac_file=stac_file,
7581
recursive=recursive,
82+
max_depth=max_depth,
7683
core=core,
7784
links=links,
7885
assets=assets,
@@ -87,7 +94,7 @@ def main(
8794
if no_output is False:
8895
click.echo(json.dumps(stac.message, indent=4))
8996

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

93100

stac_validator/validate.py

Lines changed: 11 additions & 7 deletions
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
@@ -21,7 +21,8 @@ class StacValidate:
2121
def __init__(
2222
self,
2323
stac_file: str = None,
24-
recursive: int = -2,
24+
recursive: bool = False,
25+
max_depth: Optional[int] = None,
2526
core: bool = False,
2627
links: bool = False,
2728
assets: bool = False,
@@ -37,6 +38,7 @@ def __init__(
3738
self.links = links
3839
self.assets = assets
3940
self.recursive = recursive
41+
self.max_depth = max_depth
4042
self.extensions = extensions
4143
self.core = core
4244
self.stac_content: dict = {}
@@ -202,8 +204,8 @@ def recursive_validator(self, stac_type: str):
202204
message["valid_stac"] = True
203205
self.message.append(message)
204206
self.depth = self.depth + 1
205-
if self.recursive > -1:
206-
if self.depth >= int(self.recursive):
207+
if self.max_depth:
208+
if self.depth >= self.max_depth:
207209
self.skip_val = True
208210
base_url = self.stac_file
209211
for link in self.stac_content["links"]:
@@ -244,7 +246,9 @@ def recursive_validator(self, stac_type: str):
244246

245247
if self.log != "":
246248
self.message.append(message)
247-
if self.recursive < 5:
249+
if (
250+
self.max_depth and self.max_depth < 5
251+
): # TODO this should be configurable, correct?
248252
self.message.append(message)
249253
if self.verbose is True:
250254
click.echo(json.dumps(message, indent=4))
@@ -271,7 +275,7 @@ def run(cls):
271275
message["schema"] = [cls.custom]
272276
cls.custom_validator()
273277
cls.valid = True
274-
elif cls.recursive > -2:
278+
elif cls.recursive:
275279
cls.recursive_validator(stac_type)
276280
cls.valid = True
277281
elif cls.extensions is True:
@@ -311,7 +315,7 @@ def run(cls):
311315

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

314-
if cls.recursive < -1:
318+
if not cls.recursive:
315319
cls.message.append(message)
316320

317321
if cls.log != "":

tests/test_recursion.py

Lines changed: 7 additions & 7 deletions
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
{

0 commit comments

Comments
 (0)