Skip to content

Commit 92755a1

Browse files
committed
Normalize mime types for validation. RDF/XML support
1 parent 39d32ff commit 92755a1

File tree

5 files changed

+47
-12
lines changed

5 files changed

+47
-12
lines changed

ogc/bblocks/known-mimetypes.yaml

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,3 +36,12 @@ xml:
3636
- application/xml
3737
extensions:
3838
- xml
39+
40+
yaml:
41+
label: YAML
42+
mimeType: application/x-yaml
43+
aliases:
44+
- text/yaml
45+
extensions:
46+
- yaml
47+
- yml

ogc/bblocks/validate.py

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
from mako import exceptions as mako_exceptions, template as mako_template
1414
from ogc.na.util import is_url
1515

16+
from ogc.bblocks import mimetypes
1617
from ogc.bblocks.models import BuildingBlock, BuildingBlockRegister
1718
from ogc.bblocks.util import sanitize_filename
1819
from ogc.bblocks.validation import Validator, ValidationItemSourceType, ValidationReportSection, ValidationItemSource, \
@@ -151,7 +152,8 @@ def _validate_resource(bblock: BuildingBlock,
151152
require_fail: bool | None = None,
152153
resource_url: str | None = None,
153154
example_index: tuple[int, int] | None = None,
154-
prefixes: dict[str, str] | None = None) -> ValidationReportItem | None:
155+
prefixes: dict[str, str] | None = None,
156+
file_format: str | None = None) -> ValidationReportItem | None:
155157
if require_fail is None:
156158
require_fail = filename.stem.endswith('-fail') and not example_index
157159

@@ -165,7 +167,7 @@ def _validate_resource(bblock: BuildingBlock,
165167
filename=output_filename,
166168
example_index=int(example_idx),
167169
snippet_index=int(snippet_idx),
168-
language=filename.suffix[1:],
170+
language=file_format,
169171
source_url=resource_url,
170172
)
171173
else:
@@ -186,7 +188,8 @@ def _validate_resource(bblock: BuildingBlock,
186188
base_uri=base_uri,
187189
additional_shacl_closures=additional_shacl_closures,
188190
schema_ref=schema_ref,
189-
prefixes=prefixes)
191+
prefixes=prefixes,
192+
file_format=file_format)
190193
any_validator_run = any_validator_run or (result is not False)
191194

192195
except Exception as unknown_exc:
@@ -279,6 +282,7 @@ def validate_test_resources(bblock: BuildingBlock,
279282
resource_contents=extra_test_resource['contents'],
280283
require_fail=extra_test_resource.get('require-fail', False),
281284
resource_url=extra_test_resource['ref'] if isinstance(extra_test_resource['ref'], str) else None,
285+
file_format=mimetypes.from_extension(fn.suffix[1:]),
282286
)
283287
if test_result:
284288
all_results.append(test_result)
@@ -301,8 +305,9 @@ def validate_test_resources(bblock: BuildingBlock,
301305
elif not add_snippets_formats:
302306
add_snippets_formats = []
303307

308+
extension = FORMAT_ALIASES.get(snippet['language'], snippet['language']).replace('/', '.')
304309
fn = bblock.files_path / (f"example_{example_id + 1}_{snippet_id + 1}"
305-
f".{FORMAT_ALIASES.get(snippet['language'], snippet['language'])}")
310+
f".{extension}")
306311

307312
output_fn = output_dir / sanitize_filename(example.get('base-output-filename', fn.name))
308313
i = 0
@@ -314,6 +319,9 @@ def validate_test_resources(bblock: BuildingBlock,
314319
f.write(code)
315320

316321
snippet['path'] = output_fn
322+
snippet_language = snippet.get('language')
323+
if snippet_language:
324+
snippet_language = mimetypes.normalize(snippet_language)
317325

318326
example_result = _validate_resource(
319327
bblock=bblock,
@@ -327,6 +335,7 @@ def validate_test_resources(bblock: BuildingBlock,
327335
resource_url=snippet.get('ref'),
328336
require_fail=False,
329337
prefixes=example.get('prefixes'),
338+
file_format=snippet_language,
330339
)
331340
if example_result:
332341
all_results.append(example_result)

ogc/bblocks/validation/__init__.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,14 +16,17 @@ def __init__(self, bblock: BuildingBlock, register: BuildingBlockRegister):
1616
self.register = register
1717

1818
@abstractmethod
19-
def validate(self, filename: Path, output_filename: Path,
19+
def validate(self,
20+
filename: Path,
21+
output_filename: Path,
2022
report: ValidationReportItem,
2123
contents: str | None = None,
2224
schema_ref: str | None = None,
2325
base_uri: str | None = None,
2426
resource_url: str | None = None,
2527
require_fail: bool | None = None,
2628
prefixes: dict[str, str] | None = None,
29+
file_format: str | None = None,
2730
**kwargs) -> bool | None:
2831
raise NotImplementedError
2932

ogc/bblocks/validation/json_.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -88,9 +88,11 @@ def __init__(self, bblock: BuildingBlock, register: BuildingBlockRegister):
8888
def validate(self, filename: Path, output_filename: Path, report: ValidationReportItem,
8989
contents: str | None = None,
9090
schema_ref: str | None = None,
91+
file_format: str | None = None,
9192
**kwargs) -> bool | None:
9293

93-
if filename.suffix not in ('.json', '.jsonld', '.yaml', '.yml'):
94+
if filename.suffix not in ('.json', '.jsonld', '.yaml', '.yml')\
95+
and file_format not in ('application/json', 'application/x-yaml'):
9496
return False
9597

9698
file_from = 'examples' if report.source.type == ValidationItemSourceType.EXAMPLE else 'test resources'

ogc/bblocks/validation/rdf.py

Lines changed: 18 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,13 @@
2020
from ogc.bblocks.validation.uplift import Uplifter
2121

2222

23+
NATIVE_RDF_LANGS = {
24+
'application/ld+json': 'jsonld',
25+
'text/turtle': 'ttl',
26+
'application/rdf+xml': 'xml',
27+
}
28+
29+
2330
def shacl_validate(g: Graph, s: Graph, ont_graph: Graph | None = None) \
2431
-> tuple[bool, Graph, str, dict[pyshacl.Shape, Sequence[Node]]]:
2532
validator = pyshacl.Validator(g, shacl_graph=s, ont_graph=ont_graph, options={
@@ -131,9 +138,10 @@ def __init__(self, bblock: BuildingBlock, register: BuildingBlockRegister):
131138
def _load_graph(self, filename: Path, output_filename: Path, report: ValidationReportItem,
132139
contents: str | None = None,
133140
base_uri: str | None = None,
134-
prefixes: dict[str, str] | None = None) -> Graph | None | bool:
141+
prefixes: dict[str, str] | None = None,
142+
file_format: str | None = None) -> Graph | None | bool:
135143
graph = False
136-
if filename.suffix == '.json':
144+
if filename.suffix == '.json' or file_format == 'application/json':
137145
if self.jsonld_error:
138146
report.add_entry(ValidationReportEntry(
139147
section=ValidationReportSection.JSON_LD,
@@ -231,7 +239,7 @@ def _load_graph(self, filename: Path, output_filename: Path, report: ValidationR
231239
if graph:
232240
graph = self.uplifter.post_uplift(report, graph)
233241

234-
elif output_filename.suffix == '.jsonld':
242+
elif output_filename.suffix == '.jsonld' or file_format == 'application/ld+json':
235243

236244
if contents:
237245
jsonld_doc = load_yaml(content=contents)
@@ -258,9 +266,11 @@ def _load_graph(self, filename: Path, output_filename: Path, report: ValidationR
258266
graph = Graph().parse(data=json.dumps(jsonld_doc), format='json-ld', base=base_uri)
259267
graph = self.uplifter.post_uplift(report, graph)
260268

261-
elif output_filename.suffix in ('.ttl', '.jsonld'):
269+
elif (output_filename.suffix in ('.ttl', '.jsonld', '.rdf')
270+
or file_format in NATIVE_RDF_LANGS):
262271
file_from = 'examples' if report.source.type == ValidationItemSourceType.EXAMPLE else 'test resources'
263-
rdf_format = 'ttl' if output_filename.suffix == '.ttl' else 'json-ld'
272+
rdf_format = NATIVE_RDF_LANGS.get(file_format,
273+
'ttl' if output_filename.suffix == '.ttl' else 'json-ld')
264274
try:
265275
if contents:
266276
# Prepend prefixes
@@ -299,8 +309,10 @@ def validate(self, filename: Path, output_filename: Path, report: ValidationRepo
299309
base_uri: str | None = None,
300310
additional_shacl_closures: list[str | Path] | None = None,
301311
prefixes: dict[str, str] | None = None,
312+
file_format: str | None = None,
302313
**kwargs) -> bool | None:
303-
graph = self._load_graph(filename, output_filename, report, contents, base_uri, prefixes)
314+
graph = self._load_graph(filename, output_filename, report,
315+
contents, base_uri, prefixes, file_format=file_format)
304316

305317
if graph is False:
306318
return False

0 commit comments

Comments
 (0)