Skip to content

Commit

Permalink
Add uplifted snippets to output doc
Browse files Browse the repository at this point in the history
  • Loading branch information
avillar committed Jul 25, 2023
1 parent 3770650 commit 1bc7ceb
Show file tree
Hide file tree
Showing 4 changed files with 55 additions and 14 deletions.
9 changes: 9 additions & 0 deletions ogc/bblocks/examples-schema.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,15 @@ items:
type: string
base-uri:
type: string
doc-uplift-formats:
oneOf:
- type: string
enum: [jsonld, ttl]
- type: array
items:
type: string
enum: [jsonld, ttl]
- type: 'null'
oneOf:
- required:
- code
Expand Down
1 change: 1 addition & 0 deletions ogc/bblocks/templates/slate/index.html.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ title: ${bblock.name} (${bblock.itemClass.capitalize()})
'java': 'Java',
'python': 'Python',
'javascript': 'Javascript',
'jsonld': 'JSON-LD',
}
langs = {snippet['language']: True for example in bblock.examples for snippet in example.get('snippets', [])}
%>
Expand Down
47 changes: 38 additions & 9 deletions ogc/bblocks/validate.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,13 +22,19 @@
import traceback

OUTPUT_SUBDIR = 'output'
FORMAT_ALIASES = {
'turtle': 'ttl',
'json-ld': 'jsonld',
}
DEFAULT_UPLIFT_FORMATS = ['jsonld', 'ttl']


class ValidationReport:

def __init__(self):
self._errors = False
self._sections: dict[str, list[str]] = {}
self.uplifted_files: dict[str, str] = {}

def add_info(self, section, text):
self._sections.setdefault(section, []).append(text)
Expand Down Expand Up @@ -122,8 +128,10 @@ def validate_inner():
else:
jsonld_uplifted['@context'] = jsonld_url
jsonld_fn = output_filename.with_suffix('.jsonld')
jsonld_contents = json.dumps(jsonld_uplifted, indent=2)
with open(jsonld_fn, 'w') as f:
json.dump(jsonld_uplifted, f, indent=2)
f.write(jsonld_contents)
report.uplifted_files['jsonld'] = jsonld_contents
report.add_info('Files', f'Output JSON-LD {jsonld_fn.name} created')

elif output_filename.suffix == '.jsonld':
Expand Down Expand Up @@ -152,7 +160,8 @@ def validate_inner():
if graph is not None and (resource_contents or filename.suffix != '.ttl'):
ttl_fn = output_filename.with_suffix('.ttl')
graph.serialize(ttl_fn, format='ttl')
if not graph:
report.uplifted_files['ttl'] = graph.serialize(format='ttl')
if graph:
report.add_info('Files', f'Output Turtle {ttl_fn.name} created')
else:
report.add_info('Files', f'*Empty* output Turtle {ttl_fn.name} created')
Expand Down Expand Up @@ -195,16 +204,16 @@ def validate_inner():
if e.args:
query_lines = e.args[0].splitlines()
max_line_digits = len(str(len(query_lines)))
query_error = '\nfor SPARQL query\n' + '\n'.join(f"{str(i+1).rjust(max_line_digits)}: {line}"
for i, line in enumerate(query_lines))
query_error = '\nfor SPARQL query\n' + '\n'.join(f"{str(i + 1).rjust(max_line_digits)}: {line}"
for i, line in enumerate(query_lines))
else:
query_error = ''
report.add_error('SHACL', f"Error parsing SHACL validator: {e}{query_error}")

try:
validate_inner()
except Exception as e:
report.add_error('Unknown errors', ','.join(traceback.format_exception(e)))
except Exception as unknown_exc:
report.add_error('Unknown errors', ','.join(traceback.format_exception(unknown_exc)))

report.write(output_filename)

Expand Down Expand Up @@ -279,16 +288,24 @@ def validate_test_resources(bblock: BuildingBlock,
if bblock.examples:
for example_id, example in enumerate(bblock.examples):
example_base_uri = example.get('base-uri')
for snippet_id, snippet in enumerate(example.get('snippets', ())):
snippets = example.get('snippets', ())
snippet_langs = set(snippet.get('language') for snippet in snippets)
add_snippets = {}
for snippet_id, snippet in enumerate(snippets):
code, lang = snippet.get('code'), snippet.get('language')
add_snippets_formats = snippet.get('doc-uplift-formats', DEFAULT_UPLIFT_FORMATS)
if isinstance(add_snippets_formats, str):
add_snippets_formats = [add_snippets_formats]
elif not add_snippets_formats:
add_snippets_formats = []
if code and lang in ('json', 'jsonld', 'ttl'):
fn = bblock.tests_dir / f"example_{example_id + 1}_{snippet_id + 1}.{snippet['language']}"
output_fn = output_dir / fn.name

with open(output_fn, 'w') as f:
f.write(code)

result = not _validate_resource(
example_result = _validate_resource(
fn, output_fn,
resource_contents=code,
schema_url=schema_url,
Expand All @@ -299,9 +316,21 @@ def validate_test_resources(bblock: BuildingBlock,
json_error=json_error,
shacl_error=shacl_error,
base_uri=snippet.get('base-uri', example_base_uri),
shacl_files=shacl_files).has_errors and result
shacl_files=shacl_files)
result = result and not example_result.has_errors
for file_format, file_contents in example_result.uplifted_files.items():
if file_format not in snippet_langs and file_format in add_snippets_formats:
add_snippets[file_format] = file_contents
test_count += 1

if add_snippets:
snippets = example.setdefault('snippets', [])
for lang, code in add_snippets.items():
snippets.append({
'language': lang,
'code': code,
})

return result, test_count


Expand Down
12 changes: 7 additions & 5 deletions requirements.txt
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
asciidoc
pyyaml
Mako
pyyaml~=6.0
Mako~=1.2.4
jsonschema==4.17.3
ogc-na>=0.3.2
rdflib
requests
pyld
rdflib~=6.3.2
requests~=2.31.0
pyld~=2.0.3

pyparsing~=3.0.9

0 comments on commit 1bc7ceb

Please sign in to comment.