Skip to content

Commit

Permalink
move function to sectionize metadata to sdk
Browse files Browse the repository at this point in the history
  • Loading branch information
MBueschelberger committed Dec 19, 2024
1 parent c2170f6 commit 7e83491
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 8 deletions.
41 changes: 38 additions & 3 deletions dsms/knowledge/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -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),
Expand All @@ -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.
Expand Down
10 changes: 5 additions & 5 deletions dsms/knowledge/webform.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@

from dsms.knowledge.utils import ( # isort:skip
_map_data_type_to_widget,
id_generator,
generate_id,
print_model,
)

Expand Down Expand Up @@ -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")
Expand Down Expand Up @@ -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(
Expand Down Expand Up @@ -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[
Expand Down Expand Up @@ -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")

Expand Down

0 comments on commit 7e83491

Please sign in to comment.