-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor: move pipeline user input parsing to utility method
- Loading branch information
Showing
3 changed files
with
101 additions
and
39 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
# -*- coding: utf-8 -*- | ||
from typing import TYPE_CHECKING, Any, Mapping, MutableMapping | ||
|
||
from kiara.exceptions import KiaraException | ||
|
||
if TYPE_CHECKING: | ||
from kiara.models.module.pipeline import Pipeline | ||
|
||
|
||
def create_pipeline_render_inputs( | ||
pipeline: "Pipeline", pipeline_inputs_user: Mapping[str, Any] | ||
) -> Mapping[str, Any]: | ||
from kiara.defaults import SpecialValue | ||
|
||
invalid = [] | ||
for field_name in pipeline_inputs_user.keys(): | ||
if field_name not in pipeline.pipeline_inputs_schema.keys(): | ||
invalid.append(field_name) | ||
|
||
if invalid: | ||
msg = "Valid pipeline inputs:\n" | ||
for field_name, field in pipeline.pipeline_inputs_schema.items(): | ||
msg = f"{msg} - *{field_name}*: {field.doc.description}\n" | ||
raise KiaraException( | ||
msg=f"Invalid pipeline inputs: {', '.join(invalid)}.", details=msg | ||
) | ||
|
||
pipeline_inputs = {} | ||
for field_name, schema in pipeline.pipeline_inputs_schema.items(): | ||
if field_name in pipeline_inputs_user.keys(): | ||
value = pipeline_inputs_user[field_name] | ||
elif schema.default not in [SpecialValue.NOT_SET]: | ||
if callable(schema.default): | ||
value = schema.default() | ||
else: | ||
value = schema.default | ||
elif not schema.is_required(): | ||
value = None | ||
else: | ||
value = "<TODO_SET_INPUT>" | ||
|
||
if isinstance(value, str): | ||
value = f'"{value}"' | ||
pipeline_inputs[field_name] = value | ||
|
||
inputs: MutableMapping[str, Any] = {} | ||
inputs["pipeline"] = pipeline | ||
inputs["pipeline_inputs"] = pipeline_inputs | ||
return inputs |