Skip to content
Snippets Groups Projects
Commit 033388c6 authored by Felix Blanke's avatar Felix Blanke
Browse files

Address linter remarks

parent 6f43998c
Branches
No related tags found
1 merge request!25Feature: Use JSON schema
Pipeline #275694 passed
import json
from pathlib import Path from pathlib import Path
import referencing.retrieval import referencing.retrieval
from jsonschema import Draft202012Validator from jsonschema import Draft202012Validator
from jsonschema.protocols import Validator from jsonschema.protocols import Validator
from referencing import Registry, Resource from referencing import Registry
from AKPlanning import settings from AKPlanning import settings
def _construct_schema_path(uri: str | Path) -> Path: def _construct_schema_path(uri: str | Path) -> Path:
"""Construct a schema URI.
This function also checks for unallowed directory traversals
out of the 'schema' subfolder.
"""
schema_base_path = Path(settings.BASE_DIR).resolve() schema_base_path = Path(settings.BASE_DIR).resolve()
uri_path = (schema_base_path / uri).resolve() uri_path = (schema_base_path / uri).resolve()
if not uri_path.is_relative_to(schema_base_path / "schemas"): if not uri_path.is_relative_to(schema_base_path / "schemas"):
...@@ -18,8 +22,8 @@ def _construct_schema_path(uri: str | Path) -> Path: ...@@ -18,8 +22,8 @@ def _construct_schema_path(uri: str | Path) -> Path:
@referencing.retrieval.to_cached_resource() @referencing.retrieval.to_cached_resource()
def retrieve_schema(uri: str) -> str: def retrieve_schema_from_disk(uri: str) -> str:
# avoid dictionary traversals """Retrieve schemas from disk by URI."""
uri_path = _construct_schema_path(uri) uri_path = _construct_schema_path(uri)
with uri_path.open("r") as ff: with uri_path.open("r") as ff:
return ff.read() return ff.read()
...@@ -31,7 +35,7 @@ def construct_schema_validator(schema: str | dict) -> Validator: ...@@ -31,7 +35,7 @@ def construct_schema_validator(schema: str | dict) -> Validator:
In particular, all schemas from the 'schemas' directory In particular, all schemas from the 'schemas' directory
are loaded into the registry. are loaded into the registry.
""" """
registry = Registry(retrieve=retrieve_schema) registry = Registry(retrieve=retrieve_schema_from_disk)
if isinstance(schema, str): if isinstance(schema, str):
schema_uri = str(Path("schemas") / schema) schema_uri = str(Path("schemas") / schema)
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment