Skip to content

Commit bbbf58e

Browse files
committed
Trivial first filesystem loader.
Assumes every schema is internally identified and knows what spec it belongs to. Successive commits will loosen those constraints, as well as make this work for things which are even less Path-like (sigh, importlib.abc.Traversable).
1 parent 00525c0 commit bbbf58e

File tree

5 files changed

+73
-9
lines changed

5 files changed

+73
-9
lines changed

docs/requirements.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ pygments==2.17.2
3939
# sphinx
4040
pygments-github-lexers==0.0.5
4141
# via -r docs/requirements.in
42-
referencing==0.31.0
42+
referencing==0.31.1
4343
# via referencing-loaders
4444
file:.#egg=referencing-loaders
4545
# via -r docs/requirements.in

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.0",
41+
"referencing>=0.31.1",
4242
]
4343

4444
[project.urls]

referencing/loaders.py

Lines changed: 19 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,17 +3,32 @@
33
"""
44
from __future__ import annotations
55

6+
from pathlib import Path
67
from typing import TYPE_CHECKING, Any, Iterable
8+
import json
9+
import os
710

811
from referencing import Resource
912

1013
if TYPE_CHECKING:
11-
from pathlib import Path
14+
from referencing.typing import URI
1215

1316

14-
def from_path(path: Path) -> Iterable[Resource[Any]]:
17+
def from_path(path: Path) -> Iterable[tuple[URI, Resource[Any]]]:
1518
"""
1619
Load some resources recursively from a given directory path.
1720
"""
18-
for _, _, _ in path.walk():
19-
yield Resource.opaque(None)
21+
for root, _, files in _walk(path):
22+
for file in files:
23+
path = root / file
24+
contents = json.loads(path.read_text())
25+
yield path.as_uri(), Resource.from_contents(contents)
26+
27+
28+
def _walk(path: Path):
29+
walk = getattr(path, "walk", None)
30+
if walk is not None:
31+
yield from walk()
32+
return
33+
for root, dirs, files in os.walk(path):
34+
yield Path(root), dirs, files

referencing/tests/test_loaders.py

Lines changed: 51 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,51 @@
1-
def test_it_imports(tmp_path):
2-
import referencing.loaders # noqa: F401
1+
import json
2+
3+
from referencing import Registry, loaders
4+
from referencing.jsonschema import EMPTY_REGISTRY
5+
6+
7+
def test_absolute_internally_identified(tmp_path):
8+
root_path, root = tmp_path / "schema.json", {
9+
"$schema": "https://json-schema.org/draft/2020-12/schema",
10+
"$id": "http://example.com/",
11+
}
12+
son_path, son = tmp_path / "child/son.json", {
13+
"$schema": "https://json-schema.org/draft/2019-09/schema",
14+
"$id": "http://example.com/son",
15+
}
16+
daughter_path, daughter = tmp_path / "child/daughter.json", {
17+
"$schema": "https://json-schema.org/draft/2020-12/schema",
18+
"$id": "http://example.com/random/daughter",
19+
}
20+
grandchild_path, grandchild = tmp_path / "child/more/gc.json", {
21+
"$schema": "https://json-schema.org/draft/2019-09/schema",
22+
"$id": "http://example.com/also/a/grandchild",
23+
}
24+
25+
tmp_path.joinpath("child/more").mkdir(parents=True)
26+
root_path.write_text(json.dumps(root))
27+
son_path.write_text(json.dumps(son))
28+
daughter_path.write_text(json.dumps(daughter))
29+
grandchild_path.write_text(json.dumps(grandchild))
30+
31+
expected = Registry().with_contents(
32+
[
33+
(root_path.as_uri(), root),
34+
(son_path.as_uri(), son),
35+
(daughter_path.as_uri(), daughter),
36+
(grandchild_path.as_uri(), grandchild),
37+
],
38+
)
39+
40+
resources = loaders.from_path(tmp_path)
41+
registry = EMPTY_REGISTRY.with_resources(resources)
42+
assert registry.crawl() == expected.crawl()
43+
44+
45+
def test_empty(tmp_path):
46+
registry = EMPTY_REGISTRY.with_resources(loaders.from_path(tmp_path))
47+
assert registry == EMPTY_REGISTRY
48+
49+
50+
def test_custom_loads(tmp_path):
51+
pass

test-requirements.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ pluggy==1.3.0
1414
# via pytest
1515
pytest==7.4.3
1616
# via -r test-requirements.in
17-
referencing==0.31.0
17+
referencing==0.31.1
1818
# via referencing-loaders
1919
file:.#egg=referencing-loaders
2020
# via -r test-requirements.in

0 commit comments

Comments
 (0)