Skip to content

Commit

Permalink
Move uplift configuration to semantic-uplift.yaml
Browse files Browse the repository at this point in the history
  • Loading branch information
avillar committed Aug 13, 2024
1 parent 3f25532 commit 5ce42fc
Show file tree
Hide file tree
Showing 7 changed files with 81 additions and 61 deletions.
2 changes: 1 addition & 1 deletion ogc/bblocks/examples-schema.yaml
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
"$schema": https://json-schema.org/draft/2020-12/schema
$schema: https://json-schema.org/draft/2020-12/schema
title: OGC Building Blocks examples schema
$defs:

Expand Down
38 changes: 3 additions & 35 deletions ogc/bblocks/metadata-schema.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -234,38 +234,6 @@ properties:
type: string
pattern: '^https?://.*'
semanticUplift:
description: Additional configuration for semantic uplift of resources
type: object
properties:
additionalSteps:
description: List of additional transformation steps to perform before or after the actual semantic uplift
type: array
items:
type: object
properties:
type:
description: |
Type of transformation step. The type will also determine whether the step will be run before or after
the uplift
enum:
- jq
- shacl
- sparql-construct
- sparql-update
code:
description: Code contents of this transformation step (e.g., jq script or SHACL rules file).
type: string
pattern: '[^\s]+'
ref:
description: |
Location of a file with the code contents of this transformation ste (instead of
providing them inline through the "code" property).
type: string
pattern: '[^\s]+'
required:
- type
oneOf:
- required:
- code
- required:
- ref
description: Deprecated - configuration should go in semantic-uplift.yaml
not: true

16 changes: 16 additions & 0 deletions ogc/bblocks/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -217,6 +217,22 @@ def output_openapi_contents(self):
self._lazy_properties['output_openapi_contents'] = load_file(self.output_openapi, self.remote_cache_dir)
return self._lazy_properties['output_openapi_contents']

@property
def semantic_uplift(self):
if 'semantic_uplift' not in self._lazy_properties:
fn = self.files_path / 'semantic_uplift.yaml'
semantic_uplift = {}
if fn.is_file():
semantic_uplift = load_yaml(fn)
if not semantic_uplift:
semantic_uplift = {}
try:
jsonschema.validate(semantic_uplift, get_schema('semantic-uplift'))
except Exception as e:
raise BuildingBlockError(f'Error validating semantic uplift metadata for {self.identifier}') from e
self._lazy_properties['semantic_uplift'] = semantic_uplift
return self._lazy_properties['semantic_uplift']

def get_extra_test_resources(self) -> Generator[dict, None, None]:
extra_tests_file = self.files_path / 'tests.yaml'
if extra_tests_file.is_file():
Expand Down
11 changes: 5 additions & 6 deletions ogc/bblocks/postprocess.py
Original file line number Diff line number Diff line change
Expand Up @@ -191,12 +191,11 @@ def do_postprocess(bblock: BuildingBlock, light: bool = False) -> bool:
transform['ref'] = urljoin(bblock.metadata['sourceFiles'], transform['ref'])
bblock.metadata['transforms'].append({k: v for k, v in transform.items() if k != 'code'})

if bblock.semanticUplift and bblock.semanticUplift.get('additionalSteps'):
for step in bblock.semanticUplift['additionalSteps']:
if step.get('ref'):
step['ref'] = PathOrUrl(bblock.files_path).resolve_ref(step['ref']).with_base_url(
base_url, cwd if base_url else output_file_root
)
for step in bblock.semantic_uplift.get('additionalSteps', ()):
if step.get('ref'):
step['ref'] = PathOrUrl(bblock.files_path).resolve_ref(step['ref']).with_base_url(
base_url, cwd if base_url else output_file_root
)

if not light and (not steps or 'doc' in steps):
print(f" > Generating documentation for {bblock.identifier}", file=sys.stderr)
Expand Down
36 changes: 36 additions & 0 deletions ogc/bblocks/semantic-uplift-schema.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
$schema: https://json-schema.org/draft/2020-12/schema
title: Additional configuration for semantic uplift of resources
type: object
properties:
additionalSteps:
description: List of additional transformation steps to perform before or after the actual semantic uplift
type: array
items:
type: object
properties:
type:
description: |
Type of transformation step. The type will also determine whether the step will be run before or after
the uplift
enum:
- jq
- shacl
- sparql-construct
- sparql-update
code:
description: Code contents of this transformation step (e.g., jq script or SHACL rules file).
type: string
pattern: '[^\s]+'
ref:
description: |
Location of a file with the code contents of this transformation ste (instead of
providing them inline through the "code" property).
type: string
pattern: '[^\s]+'
required:
- type
oneOf:
- required:
- code
- required:
- ref
2 changes: 2 additions & 0 deletions ogc/bblocks/templates/json-full/index.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ if bblock.example_prefixes:
output['examplePrefixes'] = bblock.example_prefixes
if bblock.annotated_schema:
output['annotatedSchema'] = bblock.annotated_schema_contents
if bblock.semantic_uplift:
output['semanticUplift'] = bblock.semantic_uplift
if git_repo:
output['gitRepository'] = git_repo
output['gitPath'] = git_path
Expand Down
37 changes: 18 additions & 19 deletions ogc/bblocks/validation/uplift.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,25 +16,24 @@ def __init__(self, bblock: BuildingBlock):
self.bblock_files = PathOrUrl(bblock.files_path)

def _run_steps(self, stage: str, report: ValidationReportItem, input_data: Any, *args):
if self.bblock.semanticUplift and self.bblock.semanticUplift.get('additionalSteps'):
for idx, step in enumerate(self.bblock.semanticUplift['additionalSteps']):
func_name = f"_{stage}_{step['type'].replace('-', '_')}"
if hasattr(self, func_name):
code = step.get('code')
report_source = 'inline code'
if not code:
if step.get('ref'):
code = load_file(self.bblock_files.resolve_ref(step['ref']), self.bblock.remote_cache_dir)
report_source = step['ref']
else:
raise ValueError(
f'No code or ref found for semanticUplift step {idx} in {self.bblock.identifier}')
step['stage'] = stage
report.add_entry(ValidationReportEntry(
section=ValidationReportSection.SEMANTIC_UPLIFT,
message=f"Running {stage}-uplift {step['type']} transform step from {report_source}",
))
input_data = getattr(self, func_name)(code, input_data, *args)
for idx, step in enumerate(self.bblock.semantic_uplift.get('additionalSteps', ())):
func_name = f"_{stage}_{step['type'].replace('-', '_')}"
if hasattr(self, func_name):
code = step.get('code')
report_source = 'inline code'
if not code:
if step.get('ref'):
code = load_file(self.bblock_files.resolve_ref(step['ref']), self.bblock.remote_cache_dir)
report_source = step['ref']
else:
raise ValueError(
f'No code or ref found for semanticUplift step {idx} in {self.bblock.identifier}')
step['stage'] = stage
report.add_entry(ValidationReportEntry(
section=ValidationReportSection.SEMANTIC_UPLIFT,
message=f"Running {stage}-uplift {step['type']} transform step from {report_source}",
))
input_data = getattr(self, func_name)(code, input_data, *args)
return input_data

def pre_uplift(self, report: ValidationReportItem, json_doc: dict | list):
Expand Down

0 comments on commit 5ce42fc

Please sign in to comment.