Skip to content

Commit

Permalink
Support rdfData entries in bblock.json
Browse files Browse the repository at this point in the history
  • Loading branch information
avillar committed Nov 18, 2024
1 parent 92755a1 commit 42b1e5b
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 12 deletions.
50 changes: 39 additions & 11 deletions ogc/bblocks/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -103,24 +103,41 @@ def __init__(self, identifier: str, metadata_file: Path,

self.transforms_path = fp / 'transforms.yaml'

self.rdf_data_paths: list[PathOrUrl] = self._find_path_or_url('rdfData', ('data.ttl',))
if not isinstance(self.rdf_data_paths, list):
self.rdf_data_paths = [self.rdf_data_paths]

self.remote_cache_dir = self.annotated_path / 'remote_cache'

def _find_path_or_url(self, metadata_property: str, default_filenames: tuple[str, ...]):
ref = self.metadata.get(metadata_property)
if ref:
if is_url(ref):
result = ref
else:
result = self.files_path.joinpath(ref).resolve()
else:
result = default_filenames[0]
def _find_path_or_url(self, metadata_property: str,
default_filenames: tuple[str, ...]) -> PathOrUrl | list[PathOrUrl] | None:
value = self.metadata.get(metadata_property)
result = []
single = True
if value:
single = isinstance(value, str)
if single:
value = [value]
for ref in value:
if ref:
if is_url(ref):
result.append(ref)
else:
result.append(self.files_path.joinpath(ref).resolve())

if not result:
result = [default_filenames[0]]
for fn in default_filenames:
f = self.files_path / fn
if f.is_file():
result = f
result[0] = f
break

return PathOrUrl(result)
if not result:
return None

result = [PathOrUrl(r) for r in result]
return result[0] if single else result

def __getattr__(self, item):
return self.metadata.get(item)
Expand Down Expand Up @@ -257,6 +274,17 @@ def transforms(self) -> list:
self._lazy_properties['transforms'] = transforms
return self._lazy_properties['transforms']

@property
def rdf_data(self) -> Graph | None:
if 'rdf_data' not in self._lazy_properties:
rdf_data = None
if self.rdf_data_paths:
rdf_data = Graph()
for rdf_data_path in self.rdf_data_paths:
rdf_data.parse(rdf_data_path.value)
self._lazy_properties['rdf_data'] = rdf_data
return self._lazy_properties['rdf_data']

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
5 changes: 5 additions & 0 deletions ogc/bblocks/postprocess.py
Original file line number Diff line number Diff line change
Expand Up @@ -154,6 +154,11 @@ def do_postprocess(bblock: BuildingBlock, light: bool = False) -> bool:
base_url, cwd if base_url else output_file_root
) + '/'

if bblock.rdf_data_paths:
bblock.metadata['rdfData'] = [
p.with_base_url(base_url, cwd if base_url else output_file_root) for p in bblock.rdf_data_paths
]

if not light:
if not steps or 'tests' in steps:
print(f" > Running tests for {bblock.identifier}", file=sys.stderr)
Expand Down
9 changes: 8 additions & 1 deletion ogc/bblocks/schemas/bblock.schema.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -232,4 +232,11 @@ properties:
semanticUplift:
description: Deprecated - configuration should go in semantic-uplift.yaml
not: true

rdfData:
description: |
Array of file names (relative to the Building Block directory) or URLs with RDF content for this Building Block,
such as ontology sources, profile descriptions, etc.
If the data file is named `data.ttl`, this property can be omitted.
type: array
items:
type: string

0 comments on commit 42b1e5b

Please sign in to comment.