Skip to content

Commit

Permalink
Add base-output-filename to examples
Browse files Browse the repository at this point in the history
  • Loading branch information
avillar committed Dec 18, 2023
1 parent 9a7b7d0 commit 6af07cb
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 2 deletions.
4 changes: 4 additions & 0 deletions ogc/bblocks/examples-schema.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,10 @@ items:
base-uri:
description: Base URI that will be used for semantic uplift (JSON -> JSON-LD -> Turtle).
type: string
base-output-filename:

This comment has been minimized.

Copy link
@avillar

avillar Dec 18, 2023

Author Collaborator

@rob-metalinkage, you can now use this property to control the output file name for an example.

description: |
Base filename that will be used for writing this example to its own file. Extension, if any, will be discarded.
type: string
snippets:
description: |
Collection of snippets to illustrate this example. Preferably, only one snippet per language
Expand Down
5 changes: 5 additions & 0 deletions ogc/bblocks/util.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@

import jsonschema
import networkx as nx
import pathvalidate
import requests
from ogc.na.annotate_schema import SchemaAnnotator, ContextBuilder
from ogc.na.util import load_yaml, dump_yaml, is_url
Expand Down Expand Up @@ -835,3 +836,7 @@ def walk(subschema: dict | list, schema_id: str | Path, is_properties: bool = Fa
{'$ref': f"{schema_url}#/x-defs/{root_ref_id}"}
]
}


def sanitize_filename(fn: str):
return pathvalidate.sanitize_filename(fn)
13 changes: 11 additions & 2 deletions ogc/bblocks/validate.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@
from rdflib.term import Node, URIRef, BNode
from yaml import MarkedYAMLError

from ogc.bblocks.util import BuildingBlock, BuildingBlockRegister
from ogc.bblocks.util import BuildingBlock, BuildingBlockRegister, sanitize_filename
import traceback
import pyshacl
import jsonref
Expand Down Expand Up @@ -697,12 +697,16 @@ def validate_test_resources(bblock: BuildingBlock,
output_dir.mkdir(parents=True, exist_ok=True)

all_results: list[ValidationReportItem] = []

output_base_filenames = set()

# Test resources
if bblock.tests_dir.is_dir():
for fn in sorted(bblock.tests_dir.resolve().iterdir()):
if fn.suffix not in ('.json', '.jsonld', '.ttl'):
continue
output_fn = output_dir / fn.name
output_base_filenames.add(fn.stem)

test_result = _validate_resource(
bblock, fn, output_fn,
Expand Down Expand Up @@ -755,7 +759,12 @@ def validate_test_resources(bblock: BuildingBlock,
if code and lang in ('json', 'jsonld', 'ttl', 'json-ld', 'turtle'):
fn = bblock.files_path / (f"example_{example_id + 1}_{snippet_id + 1}"
f".{FORMAT_ALIASES.get(snippet['language'], snippet['language'])}")
output_fn = output_dir / fn.name

output_fn = output_dir / sanitize_filename(example.get('base-output-filename', fn.name))
i = 0
while output_fn.stem in output_base_filenames:
i += 1
output_fn = output_fn.with_stem(f"{output_fn.stem}-{i}")

with open(output_fn, 'w') as f:
f.write(code)
Expand Down
1 change: 1 addition & 0 deletions requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -10,3 +10,4 @@ rdflib~=6.3.2
jsonref~=1.1.0
networkx~=3.1
jq~=1.6
pathvalidate==3.2.0

0 comments on commit 6af07cb

Please sign in to comment.