From 42b1e5bec1ec7e613f5189b1661a07daa4d320c4 Mon Sep 17 00:00:00 2001 From: Alejandro Villar Date: Mon, 18 Nov 2024 16:04:28 +0100 Subject: [PATCH] Support rdfData entries in bblock.json --- ogc/bblocks/models.py | 50 ++++++++++++++++++++------ ogc/bblocks/postprocess.py | 5 +++ ogc/bblocks/schemas/bblock.schema.yaml | 9 ++++- 3 files changed, 52 insertions(+), 12 deletions(-) diff --git a/ogc/bblocks/models.py b/ogc/bblocks/models.py index e0a2ebc..7f59f51 100644 --- a/ogc/bblocks/models.py +++ b/ogc/bblocks/models.py @@ -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) @@ -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(): diff --git a/ogc/bblocks/postprocess.py b/ogc/bblocks/postprocess.py index 67cb8a4..1372a01 100644 --- a/ogc/bblocks/postprocess.py +++ b/ogc/bblocks/postprocess.py @@ -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) diff --git a/ogc/bblocks/schemas/bblock.schema.yaml b/ogc/bblocks/schemas/bblock.schema.yaml index 251c0aa..a319482 100644 --- a/ogc/bblocks/schemas/bblock.schema.yaml +++ b/ogc/bblocks/schemas/bblock.schema.yaml @@ -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