Revision 456ad6c0
Added by koszko over 1 year ago
src/hydrilla/util/_util.py | ||
---|---|---|
30 | 30 |
from pathlib import Path |
31 | 31 |
from typing import Optional |
32 | 32 |
|
33 |
from jsonschema import RefResolver, Draft7Validator |
|
34 |
|
|
33 | 35 |
here = Path(__file__).resolve().parent |
34 | 36 |
|
35 | 37 |
_strip_comment_re = re.compile(r''' |
... | ... | |
103 | 105 |
""" |
104 | 106 |
return '.'.join([str(n) for n in ver]) + ('' if rev is None else f'-{rev}') |
105 | 107 |
|
106 |
def load_schema(schema_filename: str) -> dict: |
|
107 |
"""Find schema JSON file in '../schemas' and json.load() it.""" |
|
108 |
with open(here.parent / 'schemas' / schema_filename, 'rt') as file_handle: |
|
109 |
return json.load(file_handle) |
|
108 |
schemas = {} |
|
109 |
for path in (here.parent / 'schemas').glob('*-1.schema.json'): |
|
110 |
schema = json.loads(path.read_text()) |
|
111 |
schemas[schema['$id']] = schema |
|
112 |
|
|
113 |
common_schema_filename = 'common_definitions-1.schema.json' |
|
114 |
common_schema_path = here.parent / "schemas" / common_schema_filename |
|
115 |
|
|
116 |
resolver = RefResolver( |
|
117 |
base_uri=f'file://{str(common_schema_path)}', |
|
118 |
referrer=f'https://hydrilla.koszko.org/{common_schema_filename}', |
|
119 |
store=schemas |
|
120 |
) |
|
121 |
|
|
122 |
def validator_for(schema_filename: str) -> Draft7Validator: |
|
123 |
""" |
|
124 |
Prepare a validator for one of the schemas in '../schemas'. |
|
125 |
|
|
126 |
This function is not thread-safe. |
|
127 |
""" |
|
128 |
return Draft7Validator(resolver.resolve(schema_filename)[1], |
|
129 |
resolver=resolver) |
Also available in: Unified diff
include new schemas in package and in tests