From 7b1021fa99edcbf972a60080230caba08074fc13 Mon Sep 17 00:00:00 2001 From: "Michael R. Crusoe" Date: Wed, 25 Dec 2024 10:11:04 -0800 Subject: [PATCH] pycodegen: idfields can never be None --- schema_salad/cpp_codegen.py | 2 +- schema_salad/metaschema.py | 24 ++++++++++++++++-------- schema_salad/python_codegen.py | 8 ++++++++ 3 files changed, 25 insertions(+), 9 deletions(-) diff --git a/schema_salad/cpp_codegen.py b/schema_salad/cpp_codegen.py index 374f0691..b4848483 100644 --- a/schema_salad/cpp_codegen.py +++ b/schema_salad/cpp_codegen.py @@ -19,8 +19,8 @@ # g++ versions older than version 10 may need "--std=c++2a" instead of "--std=c++20" """ -import re import os +import re from typing import IO, Any, Optional, Union, cast from . import _logger diff --git a/schema_salad/metaschema.py b/schema_salad/metaschema.py index f28d490f..41727318 100644 --- a/schema_salad/metaschema.py +++ b/schema_salad/metaschema.py @@ -1162,6 +1162,7 @@ class RecordField(Documented): A field of a record. """ + name: str class_uri = "https://w3id.org/cwl/salad#RecordField" def __init__( @@ -1181,7 +1182,7 @@ def __init__( else: self.loadingOptions = LoadingOptions() self.doc = doc - self.name = name + self.name = name if name is not None else "_:" + str(_uuid__.uuid4()) self.type_ = type_ def __eq__(self, other: Any) -> bool: @@ -1636,6 +1637,7 @@ class EnumSchema(Saveable): """ + name: str class_uri = "https://w3id.org/cwl/salad#EnumSchema" def __init__( @@ -1654,7 +1656,7 @@ def __init__( self.loadingOptions = loadingOptions else: self.loadingOptions = LoadingOptions() - self.name = name + self.name = name if name is not None else "_:" + str(_uuid__.uuid4()) self.symbols = symbols self.type_ = type_ @@ -3479,6 +3481,7 @@ class SaladRecordField(RecordField): A field of a record. """ + name: str class_uri = "https://w3id.org/cwl/salad#SaladRecordField" def __init__( @@ -3500,7 +3503,7 @@ def __init__( else: self.loadingOptions = LoadingOptions() self.doc = doc - self.name = name + self.name = name if name is not None else "_:" + str(_uuid__.uuid4()) self.type_ = type_ self.jsonldPredicate = jsonldPredicate self.default = default @@ -3862,6 +3865,7 @@ def save( class SaladRecordSchema(NamedType, RecordSchema, SchemaDefinedType): + name: str class_uri = "https://w3id.org/cwl/salad#SaladRecordSchema" def __init__( @@ -3890,7 +3894,7 @@ def __init__( self.loadingOptions = loadingOptions else: self.loadingOptions = LoadingOptions() - self.name = name + self.name = name if name is not None else "_:" + str(_uuid__.uuid4()) self.inVocab = inVocab self.fields = fields self.type_ = type_ @@ -4725,6 +4729,7 @@ class SaladEnumSchema(NamedType, EnumSchema, SchemaDefinedType): """ + name: str class_uri = "https://w3id.org/cwl/salad#SaladEnumSchema" def __init__( @@ -4751,7 +4756,7 @@ def __init__( self.loadingOptions = loadingOptions else: self.loadingOptions = LoadingOptions() - self.name = name + self.name = name if name is not None else "_:" + str(_uuid__.uuid4()) self.inVocab = inVocab self.symbols = symbols self.type_ = type_ @@ -5468,6 +5473,7 @@ class SaladMapSchema(NamedType, MapSchema, SchemaDefinedType): """ + name: str class_uri = "https://w3id.org/cwl/salad#SaladMapSchema" def __init__( @@ -5493,7 +5499,7 @@ def __init__( self.loadingOptions = loadingOptions else: self.loadingOptions = LoadingOptions() - self.name = name + self.name = name if name is not None else "_:" + str(_uuid__.uuid4()) self.inVocab = inVocab self.type_ = type_ self.values = values @@ -6155,6 +6161,7 @@ class SaladUnionSchema(NamedType, UnionSchema, DocType): """ + name: str class_uri = "https://w3id.org/cwl/salad#SaladUnionSchema" def __init__( @@ -6179,7 +6186,7 @@ def __init__( self.loadingOptions = loadingOptions else: self.loadingOptions = LoadingOptions() - self.name = name + self.name = name if name is not None else "_:" + str(_uuid__.uuid4()) self.inVocab = inVocab self.names = names self.type_ = type_ @@ -6783,6 +6790,7 @@ class Documentation(NamedType, DocType): """ + name: str class_uri = "https://w3id.org/cwl/salad#Documentation" def __init__( @@ -6805,7 +6813,7 @@ def __init__( self.loadingOptions = loadingOptions else: self.loadingOptions = LoadingOptions() - self.name = name + self.name = name if name is not None else "_:" + str(_uuid__.uuid4()) self.inVocab = inVocab self.doc = doc self.docParent = docParent diff --git a/schema_salad/python_codegen.py b/schema_salad/python_codegen.py index 0b207130..333451c7 100644 --- a/schema_salad/python_codegen.py +++ b/schema_salad/python_codegen.py @@ -164,6 +164,9 @@ def begin_class( self.out.write(" pass\n\n\n") return + idfield_safe_name = self.safe_name(idfield) if idfield != "" else None + if idfield_safe_name is not None: + self.out.write(f" {idfield_safe_name}: str\n") self.out.write(f' class_uri = "{class_uri}"\n\n') required_field_names = [f for f in field_names if f not in optional_fields] @@ -203,6 +206,11 @@ def begin_class( """.format( classname ) + elif name == idfield_safe_name: + field_inits += """ self.{0} = {0} if {0} is not None else "_:" + str(_uuid__.uuid4()) +""".format( + self.safe_name(name) + ) else: field_inits += """ self.{0} = {0} """.format(