Skip to content

Commit 50940ae

Browse files
committed
Slightly beef up the loader to allow some subschemas to not have versions.
1 parent cbc9889 commit 50940ae

File tree

5 files changed

+56
-11
lines changed

5 files changed

+56
-11
lines changed

docs/requirements.txt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,13 +39,13 @@ pygments==2.17.2
3939
# sphinx
4040
pygments-github-lexers==0.0.5
4141
# via -r docs/requirements.in
42-
referencing==0.31.1
42+
referencing==0.32.0
4343
# via referencing-loaders
4444
file:.#egg=referencing-loaders
4545
# via -r docs/requirements.in
4646
requests==2.31.0
4747
# via sphinx
48-
rpds-py==0.13.1
48+
rpds-py==0.13.2
4949
# via referencing
5050
snowballstemmer==2.2.0
5151
# via sphinx

pyproject.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ classifiers = [
3838
]
3939
dynamic = ["version"]
4040
dependencies = [
41-
"referencing>=0.31.1",
41+
"referencing>=0.32.0",
4242
]
4343

4444
[project.urls]

referencing_loaders/__init__.py

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,21 +8,29 @@
88
import json
99
import os
1010

11-
from referencing import Resource
11+
from referencing import Resource, Specification
1212

1313
if TYPE_CHECKING:
1414
from referencing.typing import URI
1515

1616

17-
def from_path(path: Path) -> Iterable[tuple[URI, Resource[Any]]]:
17+
def from_path(root: Path) -> Iterable[tuple[URI, Resource[Any]]]:
1818
"""
1919
Load some resources recursively from a given directory path.
20+
21+
Subdirectories are defaulted to the first version seen (starting from
22+
the root) -- though it still is often a good idea to explicitly indicate
23+
what specification every resource is written for internally.
2024
"""
21-
for root, _, files in _walk(path):
25+
specification: Specification[Any] | None = None
26+
for dir, _, files in _walk(root):
2227
for file in files:
23-
path = root / file
28+
path = dir / file
2429
contents = json.loads(path.read_text())
25-
yield path.as_uri(), Resource.from_contents(contents)
30+
if specification is None:
31+
specification = Specification.detect(contents) # type: ignore[reportUnknownMemberType]
32+
resource = specification.detect(contents).create_resource(contents)
33+
yield path.as_uri(), resource
2634

2735

2836
def _walk(path: Path) -> Iterable[tuple[Path, Iterable[str], Iterable[str]]]:

referencing_loaders/tests/test_loaders.py

Lines changed: 38 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import json
22

33
from referencing import Registry
4-
from referencing.jsonschema import EMPTY_REGISTRY
4+
from referencing.jsonschema import DRAFT202012, EMPTY_REGISTRY
55

66
import referencing_loaders as loaders
77

@@ -44,6 +44,43 @@ def test_absolute_internally_identified(tmp_path):
4444
assert registry.crawl() == expected.crawl()
4545

4646

47+
def test_schema_is_inherited_downwards(tmp_path):
48+
root_path, root = tmp_path / "schema.json", {
49+
"$schema": "https://json-schema.org/draft/2020-12/schema",
50+
"$id": "http://example.com/",
51+
}
52+
son_path, son = tmp_path / "child/son.json", {
53+
"$id": "http://example.com/son",
54+
}
55+
daughter_path, daughter = tmp_path / "child/daughter.json", {
56+
"$schema": "https://json-schema.org/draft/2020-12/schema",
57+
"$id": "http://example.com/random/daughter",
58+
}
59+
grandchild_path, grandchild = tmp_path / "child/more/gc.json", {
60+
"$id": "http://example.com/also/a/grandchild",
61+
}
62+
63+
tmp_path.joinpath("child/more").mkdir(parents=True)
64+
root_path.write_text(json.dumps(root))
65+
son_path.write_text(json.dumps(son))
66+
daughter_path.write_text(json.dumps(daughter))
67+
grandchild_path.write_text(json.dumps(grandchild))
68+
69+
expected = Registry().with_resources(
70+
(each, DRAFT202012.create_resource(contents))
71+
for each, contents in [
72+
(root_path.as_uri(), root),
73+
(son_path.as_uri(), son),
74+
(daughter_path.as_uri(), daughter),
75+
(grandchild_path.as_uri(), grandchild),
76+
]
77+
)
78+
79+
resources = loaders.from_path(tmp_path)
80+
registry = EMPTY_REGISTRY.with_resources(resources)
81+
assert registry.crawl() == expected.crawl()
82+
83+
4784
def test_empty(tmp_path):
4885
registry = EMPTY_REGISTRY.with_resources(loaders.from_path(tmp_path))
4986
assert registry == EMPTY_REGISTRY

test-requirements.txt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,9 @@ pluggy==1.3.0
1414
# via pytest
1515
pytest==7.4.3
1616
# via -r test-requirements.in
17-
referencing==0.31.1
17+
referencing==0.32.0
1818
# via referencing-loaders
1919
file:.#egg=referencing-loaders
2020
# via -r test-requirements.in
21-
rpds-py==0.13.1
21+
rpds-py==0.13.2
2222
# via referencing

0 commit comments

Comments
 (0)