From 797817e0467a009fa22721a4fa16cb5e71161392 Mon Sep 17 00:00:00 2001 From: Daniel <d.hornung@indiscale.com> Date: Wed, 25 Oct 2023 17:19:32 +0200 Subject: [PATCH] MAINT: Renamed a few functions. --- CHANGELOG.md | 2 +- README_SETUP.md | 3 +- src/caosadvancedtools/json_schema_exporter.py | 40 ++++++++++--------- 3 files changed, 24 insertions(+), 21 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 108c0cd0..61c4d9c4 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -13,7 +13,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 * Parsing from YAML now allows to give an existing model to which the YAML data model shall be added. * The `json_schema_exporter` module which introduces tools to create a json - schema from a RecordType, e.g., for the useage in web forms. + schema from a RecordType, e.g., for the usage in web forms. ### Changed ### diff --git a/README_SETUP.md b/README_SETUP.md index bd3d1e1e..bf4f25d9 100644 --- a/README_SETUP.md +++ b/README_SETUP.md @@ -34,7 +34,8 @@ Optional h5-crawler: ## Run Unit Tests - All tests: `tox` -- One specific test: `tox -- -k expression` +- One specific test with tox: `tox -- unittests/test_myusecase.py -k expression` +- Or even using only pytest: `pytest unittests/test_myusecase.py -k expression` ## Run Integration Tests Locally diff --git a/src/caosadvancedtools/json_schema_exporter.py b/src/caosadvancedtools/json_schema_exporter.py index 2c283d67..39f660f0 100644 --- a/src/caosadvancedtools/json_schema_exporter.py +++ b/src/caosadvancedtools/json_schema_exporter.py @@ -33,10 +33,10 @@ def _make_required_list(rt: db.RecordType): if rt.get_importance(prop.name) == db.OBLIGATORY] -def _make_prop_from_prop(prop: db.Property, additional_properties: bool, - name_and_description_in_properties: bool, - additional_options_for_text_props: Optional[dict], - units_in_description: bool): +def _make_segment_from_prop(prop: db.Property, additional_properties: bool, + name_and_description_in_properties: bool, + additional_options_for_text_props: Optional[dict], + units_in_description: bool): """Return the JSON Schema segment for the given property Parameters @@ -44,7 +44,7 @@ def _make_prop_from_prop(prop: db.Property, additional_properties: bool, prop : db.Property the property to be transformed additional_properties : bool, optional - Whether additional propeties will be admitted in the resulting + Whether additional properties will be admitted in the resulting schema. Optional, default is True. name_and_description_in_properties : bool, optional Whether to include name and description in the `properties` section of @@ -99,7 +99,7 @@ def _make_prop_from_prop(prop: db.Property, additional_properties: bool, json_prop["type"] = "array" list_element_prop = db.Property( name=prop.name, datatype=get_list_datatype(prop.datatype, strict=True)) - json_prop["items"] = _make_prop_from_prop( + json_prop["items"] = _make_segment_from_prop( list_element_prop, additional_properties, name_and_description_in_properties, additional_options_for_text_props, units_in_description @@ -116,10 +116,10 @@ def _make_prop_from_prop(prop: db.Property, additional_properties: bool, else: values = _retrieve_enum_values(f"RECORD '{prop.datatype}'") rt = db.execute_query(f"FIND RECORDTYPE WITH name='{prop.datatype}'", unique=True) - subschema = _treat_recordtype(rt, additional_properties, - name_and_description_in_properties, - additional_options_for_text_props, - units_in_description) + subschema = _make_segment_from_recordtype(rt, additional_properties, + name_and_description_in_properties, + additional_options_for_text_props, + units_in_description) json_prop["oneOf"] = [ {"enum": values}, subschema @@ -167,9 +167,7 @@ def _retrieve_enum_values(role: str): possible_values = db.execute_query(f"SELECT name, id FROM {role}") vals = [] - for val in possible_values: - if val.name: vals.append(f"{val.id}, {val.name}") else: @@ -178,10 +176,12 @@ def _retrieve_enum_values(role: str): return vals -def _treat_recordtype(rt: db.RecordType, additional_properties: bool = True, - name_and_description_in_properties: bool = False, - additional_options_for_text_props: Optional[dict] = None, - units_in_description: bool = True): +def _make_segment_from_recordtype(rt: db.RecordType, additional_properties: bool = True, + name_and_description_in_properties: bool = False, + additional_options_for_text_props: Optional[dict] = None, + units_in_description: bool = True): + """Return a Json schema segment for the given RecordType. + """ schema = { "type": "object" } @@ -201,7 +201,7 @@ def _treat_recordtype(rt: db.RecordType, additional_properties: bool = True, "Creating a schema for multi-properties is not specified. " f"Property {prop.name} occurs more than once." ) - props[prop.name] = _make_prop_from_prop( + props[prop.name] = _make_segment_from_prop( prop, additional_properties, name_and_description_in_properties, additional_options_for_text_props, units_in_description) @@ -246,8 +246,10 @@ def recordtype_to_json_schema(rt: db.RecordType, additional_properties: bool = T if additional_options_for_text_props is None: additional_options_for_text_props = {} - schema = _treat_recordtype(rt, additional_properties, name_and_description_in_properties, - additional_options_for_text_props, units_in_description) + schema = _make_segment_from_recordtype(rt, additional_properties, + name_and_description_in_properties, + additional_options_for_text_props, + units_in_description) schema["$schema"] = "https://json-schema.org/draft/2019-09/schema" if rt.name: schema["title"] = rt.name -- GitLab