Skip to content

Commit

Permalink
Ignore hidden files (really only parse JSON files).
Browse files Browse the repository at this point in the history
  • Loading branch information
Julian committed May 1, 2024
1 parent 648bcae commit 643bf29
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 4 deletions.
8 changes: 4 additions & 4 deletions referencing_loaders/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,9 @@ def from_path(root: Path) -> Iterable[tuple[URI, Resource[Any]]]:
the root) -- though it still is often a good idea to explicitly indicate
what specification every resource is written for internally.
"""
return _from_walked(_walk(root))
return _from_walked(
each for each in _walk(root) if each.name.endswith(".json")
)


def _walk(path: Path) -> Iterable[Path]:
Expand Down Expand Up @@ -61,9 +63,7 @@ def from_traversable(root: Traversable) -> Iterable[tuple[URI, Resource[Any]]]:
(I.e. load schemas from data within a Python package.)
"""
return _from_walked(
each
for each in _walk_traversable(root)
if not each.name.endswith((".py", ".pyc", ".pyo"))
each for each in _walk_traversable(root) if each.name.endswith(".json")
)


Expand Down
17 changes: 17 additions & 0 deletions referencing_loaders/tests/test_loaders.py
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,23 @@ def test_schema_is_inherited_downwards(tmp_path):
assert registry.crawl() == expected.crawl()


def test_hidden_files_are_ignored(tmp_path):
schema_path, schema = tmp_path / "schema.json", {
"$schema": "https://json-schema.org/draft/2020-12/schema",
"$id": "http://example.com/",
}
hidden_path = tmp_path / ".hidden"

schema_path.write_text(json.dumps(schema))
hidden_path.write_text("total nonsense")

expected = Registry().with_contents([(schema_path.as_uri(), schema)])

resources = loaders.from_path(tmp_path)
registry = EMPTY_REGISTRY.with_resources(resources)
assert registry.crawl() == expected.crawl()


def test_empty(tmp_path):
registry = EMPTY_REGISTRY.with_resources(loaders.from_path(tmp_path))
assert registry == EMPTY_REGISTRY
Expand Down

0 comments on commit 643bf29

Please sign in to comment.