@@ -100,7 +100,6 @@ def __init__(self, value: str | Path):
100
100
else :
101
101
self .url = self .value
102
102
self .is_url = not self .is_path
103
- self .exists = self .is_url or value .is_file ()
104
103
self .parsed_url = None if self .is_path else urlparse (self .value )
105
104
106
105
def __str__ (self ) -> str :
@@ -173,6 +172,10 @@ def load_yaml(self):
173
172
else :
174
173
return load_yaml (filename = self .path )
175
174
175
+ @property
176
+ def exists (self ):
177
+ return self .is_url or self .path .is_file ()
178
+
176
179
177
180
class RegisterSchemaResolver (SchemaResolver ):
178
181
"""
@@ -427,22 +430,6 @@ def __init__(self,
427
430
annotated_path = annotated_path )
428
431
self .bblocks [bblock_id ] = bblock
429
432
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
-
446
433
except Exception as e :
447
434
if fail_on_error :
448
435
raise
@@ -451,9 +438,42 @@ def __init__(self,
451
438
traceback .print_exception (e , file = sys .stderr )
452
439
print ('=========' , file = sys .stderr )
453
440
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
+
454
475
# Map of schema URLs for imported bblocks, including source and annotated ones
455
476
self .imported_bblock_schemas : dict [str , str ] = {}
456
- dep_graph = nx .DiGraph ()
457
477
for identifier , imported_bblock in self .imported_bblocks .items ():
458
478
dep_graph .add_node (identifier )
459
479
dep_graph .add_edges_from ([(d , identifier ) for d in imported_bblock .get ('dependsOn' , ())])
@@ -732,16 +752,8 @@ def annotate_schema(bblock: BuildingBlock,
732
752
result = []
733
753
schema_fn = None
734
754
schema_url = None
735
- metadata_schemas = bblock .metadata .get ('schema' )
736
755
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 :
745
757
schema_url = bblock .schema .value
746
758
elif bblock .schema .exists :
747
759
schema_fn = bblock .schema .value
0 commit comments