@@ -20,9 +20,7 @@ def __init__(self, schema_file_path: str, root_path: str, slots: Dict):
2020 self .slots = slots
2121
2222 def _target_file_without_extension (self ) -> str :
23- return os .path .join (
24- self .version , * self .relative_path_without_extension
25- )
23+ return os .path .join (self .version , * self .relative_path_without_extension )
2624
2725 def _resolve_string_property (self , property ) -> Dict :
2826 format_map = { # see https://linkml.io/linkml-registry/
@@ -141,7 +139,7 @@ def _translate_property_specifications(
141139 "float" : self ._resolve_number_property ,
142140 "integer" : self ._resolve_number_property ,
143141 "object" : self ._resolve_object_property ,
144- "array" : self ._resolve_array_property
142+ "array" : self ._resolve_array_property ,
145143 }
146144
147145 if isinstance (prop_type , list ):
@@ -207,44 +205,52 @@ def update_slots(self):
207205 if value != slot_definition ["range" ]:
208206 slot_definition ["any_of" ] = [
209207 {"range" : slot_definition .pop ("range" )},
210- {"range" : value }
208+ {"range" : value },
211209 ]
212210 elif "any_of" in slot_definition :
213- allowed_values = [item ["range" ] for item in slot_definition ["any_of" ]]
211+ allowed_values = [
212+ item ["range" ] for item in slot_definition ["any_of" ]
213+ ]
214214 if value not in allowed_values :
215215 slot_definition ["any_of" ].append ({"range" : value })
216216 else :
217217 slot_definition ["range" ] = value
218218 elif key == "any_of" :
219219 if "range" in slot_definition :
220- slot_definition ["any_of" ] = value + [{"range" : slot_definition .pop ("range" )}]
220+ slot_definition ["any_of" ] = value + [
221+ {"range" : slot_definition .pop ("range" )}
222+ ]
221223 elif "any_of" in slot_definition :
222- allowed_values = [item ["range" ] for item in slot_definition ["any_of" ]]
224+ allowed_values = [
225+ item ["range" ] for item in slot_definition ["any_of" ]
226+ ]
223227 used_values = [item ["range" ] for item in slot_usage ["any_of" ]]
224- slot_definition ["any_of" ] = [{"range" : item } for item in set (allowed_values + used_values )]
228+ slot_definition ["any_of" ] = [
229+ {"range" : item }
230+ for item in set (allowed_values + used_values )
231+ ]
225232 else :
226233 slot_definition ["any_of" ] = value
227234 else :
228235 if key in slot_definition :
229- if isinstance (slot_definition [key ], dict ) and "ALTERNATES" in slot_definition [key ]:
236+ if (
237+ isinstance (slot_definition [key ], dict )
238+ and "ALTERNATES" in slot_definition [key ]
239+ ):
230240 slot_definition [key ]["ALTERNATES" ].append (value )
231241 elif slot_definition [key ] == value :
232242 pass
233243 else :
234- slot_definition [key ] = {"ALTERNATES" : [slot_definition [key ], value ]}
244+ slot_definition [key ] = {
245+ "ALTERNATES" : [slot_definition [key ], value ]
246+ }
235247 else :
236248 slot_definition [key ] = value
237249
238250 def build (self ):
239- target_file = os .path .join (
240- "target" , "schemas" , f"{ self ._target_file_without_extension ()} .yaml"
241- )
242- os .makedirs (os .path .dirname (target_file ), exist_ok = True )
243251 short_type = get_short_name (self ._schema_payload ["_type" ])
244252 self .translate ()
245-
246- with open (target_file , "w" ) as fp :
247- yaml .dump ({short_type : self ._translated_schema }, fp )
253+ return {short_type : self ._translated_schema }
248254
249255
250256class LinkMLEnumBuilder (object ):
@@ -260,11 +266,8 @@ def __init__(self, schema_file_path: str, root_path: str, instances: Dict):
260266 self ._schema_payload = json .load (schema_f )
261267 self .instances = instances
262268
263-
264269 def _target_file_without_extension (self ) -> str :
265- return os .path .join (
266- * self .relative_path_without_extension
267- )
270+ return os .path .join (* self .relative_path_without_extension )
268271
269272 def translate (self ):
270273 def build_enum (instance ):
@@ -273,27 +276,47 @@ def build_enum(instance):
273276 enum ["description" ] = instance ["definition" ]
274277 "meaning"
275278 return enum
279+
276280 instances_payload = self .instances [self ._schema_payload ["_type" ]]
277281 self ._translated_schema = {
278282 "enum_uri" : self ._schema_payload ["_type" ],
279283 "title" : self ._schema_payload ["label" ],
280- "permissible_values" : {
281- instance ["name" ]: build_enum (instance ) for instance in instances_payload
282- }
283284 }
284285 if "description" in self ._schema_payload :
285286 self ._translated_schema ["description" ] = self ._schema_payload ["description" ]
287+ self ._translated_schema ["permissible_values" ] = {
288+ instance ["name" ]: build_enum (instance ) for instance in instances_payload
289+ }
286290
287291 def build (self ):
288292 target_file = os .path .join (
289- "target" , "schemas" , self .version , "enums" , f"{ self ._target_file_without_extension ()} .yaml"
293+ "target" ,
294+ "schemas" ,
295+ self .version ,
296+ "enums" ,
297+ f"{ self ._target_file_without_extension ()} .yaml" ,
290298 )
291299 os .makedirs (os .path .dirname (target_file ), exist_ok = True )
292300 short_type = get_short_name (self ._schema_payload ["_type" ])
293301 self .translate ()
294302
303+ enum_schema = {
304+ "id" : f"https://openminds.ebrains.eu/schemas/latest/enums/{ short_type } ?format=linkml" ,
305+ "name" : f"openMINDS-enums-{ short_type } " ,
306+ "title" : f"OpenMINDS enum for { short_type } " ,
307+ "description" : f"OpenMINDS enum for { short_type } " ,
308+ "license" : "https://spdx.org/licenses/MIT.html" ,
309+ "prefixes" : {
310+ "linkml" : "https://w3id.org/linkml/" ,
311+ "schema" : "http://schema.org/" ,
312+ "omi" : "https://openminds.ebrains.eu" ,
313+ },
314+ "default_prefix" : "omi" ,
315+ "enums" : {short_type : self ._translated_schema },
316+ }
317+
295318 with open (target_file , "w" ) as fp :
296- yaml .dump ({ short_type : self . _translated_schema } , fp )
319+ yaml .dump (enum_schema , fp , sort_keys = False )
297320
298321
299322class LinkMLSlotBuilder :
@@ -315,7 +338,9 @@ def build(self):
315338 slot ["range" ] = get_short_name (target_classes [0 ])
316339 elif len (target_classes ) > 1 :
317340 assert "range" not in slot
318- slot ["any_of" ] = [{"range" : get_short_name (tc )} for tc in target_classes ]
341+ slot ["any_of" ] = [
342+ {"range" : get_short_name (tc )} for tc in target_classes
343+ ]
319344 if "asString" in property :
320345 # can't do anything yet, because string can be used to represent various LinkML types
321346 # todo: fix this
0 commit comments