From 7e834918a3d93503a11fb63ba5d082d4c8fb740c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Matthias=20B=C3=BCschelberger?= Date: Thu, 19 Dec 2024 16:15:36 +0100 Subject: [PATCH] move function to sectionize metadata to sdk --- dsms/knowledge/utils.py | 41 ++++++++++++++++++++++++++++++++++++--- dsms/knowledge/webform.py | 10 +++++----- 2 files changed, 43 insertions(+), 8 deletions(-) diff --git a/dsms/knowledge/utils.py b/dsms/knowledge/utils.py index 35b7518..ea3301d 100644 --- a/dsms/knowledge/utils.py +++ b/dsms/knowledge/utils.py @@ -935,11 +935,11 @@ def _make_misc_section(custom_properties: dict): If the ktype_id is not found, return the custom_properties dictionary as is, wrapped in a section named "Misc". """ - section = {"id": id_generator(), "name": "Misc", "entries": []} + section = {"id": generate_id(), "name": "Misc", "entries": []} for key, value in custom_properties.items(): section["entries"].append( { - "id": id_generator(), + "id": generate_id(), "label": key, "value": value, "type": _map_data_type_to_widget(value), @@ -966,7 +966,42 @@ def _map_data_type_to_widget(value): return widget -def id_generator(prefix: str = "id") -> str: +def sectionize_metadata(metadata: List[Dict[str, Any]]) -> dict: + """ + Convert a list of dictionaries representing metadata + entries into a DSMS schema dict. + + The input should be a list of dictionaries, + where each dictionary represents a metadata entry. + The output is a dictionary in the DSMS schema, + with a single section named "General", + containing the given metadata entries. + + If the input is empty, the function will + return an empty dictionary. + + :param metadata: The metadata list to convert. + :return: A dictionary in the DSMS schema. + """ + if metadata: + for metadatum in metadata: + metadatum["id"] = generate_id() + metadata = { + "sections": [ + { + "id": generate_id(), + "name": "General", + "entries": metadata, + } + ] + } + else: + metadata = {} + + return metadata + + +def generate_id(prefix: str = "id") -> str: # Generate a unique part using time and random characters """ Generates a unique id using a combination of the current time and 6 random characters. diff --git a/dsms/knowledge/webform.py b/dsms/knowledge/webform.py index f6c9c06..5250fb9 100644 --- a/dsms/knowledge/webform.py +++ b/dsms/knowledge/webform.py @@ -18,7 +18,7 @@ from dsms.knowledge.utils import ( # isort:skip _map_data_type_to_widget, - id_generator, + generate_id, print_model, ) @@ -264,7 +264,7 @@ class Input(BaseWebformModel): """Input fields in the sections in webform""" id: Optional[str] = Field( - default_factory=id_generator, description="ID of the input" + default_factory=generate_id, description="ID of the input" ) label: Optional[str] = Field(None, description="Label of the input") widget: Optional[Widget] = Field(None, description="Widget of the input") @@ -309,7 +309,7 @@ class Section(BaseWebformModel): """Section in webform""" id: Optional[str] = Field( - default_factory=id_generator, description="ID of the section" + default_factory=generate_id, description="ID of the section" ) name: Optional[str] = Field(None, description="Name of the section") inputs: List[Input] = Field( @@ -417,7 +417,7 @@ class Entry(BaseWebformModel): Entry in a custom properties section """ - id: str = Field(default_factory=id_generator) + id: str = Field(default_factory=generate_id) type: Optional[Widget] = Field(None, description="Type of the entry") label: str = Field(..., description="Label of the entry") value: Optional[ @@ -611,7 +611,7 @@ class CustomPropertiesSection(BaseWebformModel): Section for custom properties """ - id: Optional[str] = Field(default_factory=id_generator) + id: Optional[str] = Field(default_factory=generate_id) name: str = Field(..., description="Name of the section") entries: List[Entry] = Field([], description="Entries of the section")