Skip to content
Snippets Groups Projects
Verified Commit 797817e0 authored by Daniel Hornung's avatar Daniel Hornung
Browse files

MAINT: Renamed a few functions.

parent a4e990c8
No related branches found
No related tags found
2 merge requests!89ENH: JsonSchemaExporter accepts do_not_create parameter.,!81F schema export references
...@@ -13,7 +13,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ...@@ -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 * Parsing from YAML now allows to give an existing model to which the YAML data model shall be
added. added.
* The `json_schema_exporter` module which introduces tools to create a json * 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 ### ### Changed ###
......
...@@ -34,7 +34,8 @@ Optional h5-crawler: ...@@ -34,7 +34,8 @@ Optional h5-crawler:
## Run Unit Tests ## Run Unit Tests
- All tests: `tox` - 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 ## Run Integration Tests Locally
......
...@@ -33,10 +33,10 @@ def _make_required_list(rt: db.RecordType): ...@@ -33,10 +33,10 @@ def _make_required_list(rt: db.RecordType):
if rt.get_importance(prop.name) == db.OBLIGATORY] if rt.get_importance(prop.name) == db.OBLIGATORY]
def _make_prop_from_prop(prop: db.Property, additional_properties: bool, def _make_segment_from_prop(prop: db.Property, additional_properties: bool,
name_and_description_in_properties: bool, name_and_description_in_properties: bool,
additional_options_for_text_props: Optional[dict], additional_options_for_text_props: Optional[dict],
units_in_description: bool): units_in_description: bool):
"""Return the JSON Schema segment for the given property """Return the JSON Schema segment for the given property
Parameters Parameters
...@@ -44,7 +44,7 @@ def _make_prop_from_prop(prop: db.Property, additional_properties: bool, ...@@ -44,7 +44,7 @@ def _make_prop_from_prop(prop: db.Property, additional_properties: bool,
prop : db.Property prop : db.Property
the property to be transformed the property to be transformed
additional_properties : bool, optional 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. schema. Optional, default is True.
name_and_description_in_properties : bool, optional name_and_description_in_properties : bool, optional
Whether to include name and description in the `properties` section of 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, ...@@ -99,7 +99,7 @@ def _make_prop_from_prop(prop: db.Property, additional_properties: bool,
json_prop["type"] = "array" json_prop["type"] = "array"
list_element_prop = db.Property( list_element_prop = db.Property(
name=prop.name, datatype=get_list_datatype(prop.datatype, strict=True)) 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, list_element_prop, additional_properties,
name_and_description_in_properties, additional_options_for_text_props, name_and_description_in_properties, additional_options_for_text_props,
units_in_description units_in_description
...@@ -116,10 +116,10 @@ def _make_prop_from_prop(prop: db.Property, additional_properties: bool, ...@@ -116,10 +116,10 @@ def _make_prop_from_prop(prop: db.Property, additional_properties: bool,
else: else:
values = _retrieve_enum_values(f"RECORD '{prop.datatype}'") values = _retrieve_enum_values(f"RECORD '{prop.datatype}'")
rt = db.execute_query(f"FIND RECORDTYPE WITH name='{prop.datatype}'", unique=True) rt = db.execute_query(f"FIND RECORDTYPE WITH name='{prop.datatype}'", unique=True)
subschema = _treat_recordtype(rt, additional_properties, subschema = _make_segment_from_recordtype(rt, additional_properties,
name_and_description_in_properties, name_and_description_in_properties,
additional_options_for_text_props, additional_options_for_text_props,
units_in_description) units_in_description)
json_prop["oneOf"] = [ json_prop["oneOf"] = [
{"enum": values}, {"enum": values},
subschema subschema
...@@ -167,9 +167,7 @@ def _retrieve_enum_values(role: str): ...@@ -167,9 +167,7 @@ def _retrieve_enum_values(role: str):
possible_values = db.execute_query(f"SELECT name, id FROM {role}") possible_values = db.execute_query(f"SELECT name, id FROM {role}")
vals = [] vals = []
for val in possible_values: for val in possible_values:
if val.name: if val.name:
vals.append(f"{val.id}, {val.name}") vals.append(f"{val.id}, {val.name}")
else: else:
...@@ -178,10 +176,12 @@ def _retrieve_enum_values(role: str): ...@@ -178,10 +176,12 @@ def _retrieve_enum_values(role: str):
return vals return vals
def _treat_recordtype(rt: db.RecordType, additional_properties: bool = True, def _make_segment_from_recordtype(rt: db.RecordType, additional_properties: bool = True,
name_and_description_in_properties: bool = False, name_and_description_in_properties: bool = False,
additional_options_for_text_props: Optional[dict] = None, additional_options_for_text_props: Optional[dict] = None,
units_in_description: bool = True): units_in_description: bool = True):
"""Return a Json schema segment for the given RecordType.
"""
schema = { schema = {
"type": "object" "type": "object"
} }
...@@ -201,7 +201,7 @@ def _treat_recordtype(rt: db.RecordType, additional_properties: bool = True, ...@@ -201,7 +201,7 @@ def _treat_recordtype(rt: db.RecordType, additional_properties: bool = True,
"Creating a schema for multi-properties is not specified. " "Creating a schema for multi-properties is not specified. "
f"Property {prop.name} occurs more than once." 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, prop, additional_properties, name_and_description_in_properties,
additional_options_for_text_props, units_in_description) additional_options_for_text_props, units_in_description)
...@@ -246,8 +246,10 @@ def recordtype_to_json_schema(rt: db.RecordType, additional_properties: bool = T ...@@ -246,8 +246,10 @@ def recordtype_to_json_schema(rt: db.RecordType, additional_properties: bool = T
if additional_options_for_text_props is None: if additional_options_for_text_props is None:
additional_options_for_text_props = {} additional_options_for_text_props = {}
schema = _treat_recordtype(rt, additional_properties, name_and_description_in_properties, schema = _make_segment_from_recordtype(rt, additional_properties,
additional_options_for_text_props, units_in_description) name_and_description_in_properties,
additional_options_for_text_props,
units_in_description)
schema["$schema"] = "https://json-schema.org/draft/2019-09/schema" schema["$schema"] = "https://json-schema.org/draft/2019-09/schema"
if rt.name: if rt.name:
schema["title"] = rt.name schema["title"] = rt.name
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment