Skip to content

Commit 3628118

Browse files
committed
Allow bblocks:// URIs in bblock.json "schema" for reuse
1 parent be330b7 commit 3628118

File tree

1 file changed

+39
-27
lines changed

1 file changed

+39
-27
lines changed

ogc/bblocks/util.py

+39-27
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,6 @@ def __init__(self, value: str | Path):
100100
else:
101101
self.url = self.value
102102
self.is_url = not self.is_path
103-
self.exists = self.is_url or value.is_file()
104103
self.parsed_url = None if self.is_path else urlparse(self.value)
105104

106105
def __str__(self) -> str:
@@ -173,6 +172,10 @@ def load_yaml(self):
173172
else:
174173
return load_yaml(filename=self.path)
175174

175+
@property
176+
def exists(self):
177+
return self.is_url or self.path.is_file()
178+
176179

177180
class RegisterSchemaResolver(SchemaResolver):
178181
"""
@@ -427,22 +430,6 @@ def __init__(self,
427430
annotated_path=annotated_path)
428431
self.bblocks[bblock_id] = bblock
429432
self.bblock_paths[bblock.files_path] = bblock
430-
431-
if bblock.schema.exists:
432-
if bblock.schema.is_url:
433-
self.local_bblock_schemas[bblock.schema.url] = bblock_id
434-
else:
435-
rel = os.path.relpath(bblock.schema.path)
436-
self.local_bblock_schemas[rel] = bblock_id
437-
if base_url:
438-
self.local_bblock_schemas[f"{base_url}{rel}"] = bblock_id
439-
440-
for s in (bblock.annotated_schema, bblock.annotated_schema.with_suffix('.json')):
441-
rel = os.path.relpath(s)
442-
self.local_bblock_schemas[rel] = bblock_id
443-
if base_url:
444-
self.local_bblock_schemas[f"{base_url}{rel}"] = bblock_id
445-
446433
except Exception as e:
447434
if fail_on_error:
448435
raise
@@ -451,9 +438,42 @@ def __init__(self,
451438
traceback.print_exception(e, file=sys.stderr)
452439
print('=========', file=sys.stderr)
453440

441+
dep_graph = nx.DiGraph()
442+
443+
for bblock_id, bblock in self.bblocks.items():
444+
if bblock.schema.is_url and bblock.schema.url.startswith('bblocks://'):
445+
ref_bblock_id = bblock.schema.url[len('bblocks://'):]
446+
if ref_bblock_id == bblock_id:
447+
raise ValueError(f'Circular dependency detected on schema for {bblock_id}')
448+
if ref_bblock_id in self.bblocks:
449+
bblock.schema = PathOrUrl(self.bblocks[ref_bblock_id].annotated_schema)
450+
elif ref_bblock_id in self.imported_bblocks:
451+
bblock.schema = PathOrUrl(
452+
self.imported_bblocks[ref_bblock_id]['schema']['application/yaml']
453+
)
454+
else:
455+
raise ValueError(f'Unknown schema reference to {ref_bblock_id} in bblock {bblock_id}')
456+
dep_graph.add_node(ref_bblock_id)
457+
dep_graph.add_edge(ref_bblock_id, bblock_id)
458+
continue
459+
460+
if bblock.schema.exists:
461+
if bblock.schema.is_url:
462+
self.local_bblock_schemas[bblock.schema.url] = bblock_id
463+
else:
464+
rel = os.path.relpath(bblock.schema.path)
465+
self.local_bblock_schemas[rel] = bblock_id
466+
if base_url:
467+
self.local_bblock_schemas[f"{base_url}{rel}"] = bblock_id
468+
469+
for s in (bblock.annotated_schema, bblock.annotated_schema.with_suffix('.json')):
470+
rel = os.path.relpath(s)
471+
self.local_bblock_schemas[rel] = bblock_id
472+
if base_url:
473+
self.local_bblock_schemas[f"{base_url}{rel}"] = bblock_id
474+
454475
# Map of schema URLs for imported bblocks, including source and annotated ones
455476
self.imported_bblock_schemas: dict[str, str] = {}
456-
dep_graph = nx.DiGraph()
457477
for identifier, imported_bblock in self.imported_bblocks.items():
458478
dep_graph.add_node(identifier)
459479
dep_graph.add_edges_from([(d, identifier) for d in imported_bblock.get('dependsOn', ())])
@@ -732,16 +752,8 @@ def annotate_schema(bblock: BuildingBlock,
732752
result = []
733753
schema_fn = None
734754
schema_url = None
735-
metadata_schemas = bblock.metadata.get('schema')
736755

737-
if isinstance(metadata_schemas, Sequence):
738-
# Take only first, if more than one
739-
ref_schema = metadata_schemas if isinstance(metadata_schemas, str) else metadata_schemas[0]
740-
if is_url(ref_schema, http_only=True):
741-
schema_url = ref_schema
742-
else:
743-
schema_fn = bblock.files_path.joinpath(ref_schema).resolve()
744-
elif bblock.schema.is_url:
756+
if bblock.schema.is_url:
745757
schema_url = bblock.schema.value
746758
elif bblock.schema.exists:
747759
schema_fn = bblock.schema.value

0 commit comments

Comments
 (0)