sinapsis_core 1.0.0__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- sinapsis_core/__init__.py +0 -0
- sinapsis_core/agent/__init__.py +5 -0
- sinapsis_core/agent/agent.py +362 -0
- sinapsis_core/agent/agent_helpers.py +112 -0
- sinapsis_core/agent/base_models.py +361 -0
- sinapsis_core/agent/builder.py +112 -0
- sinapsis_core/agent/profiler_agent.py +117 -0
- sinapsis_core/cli/__init__.py +0 -0
- sinapsis_core/cli/arg_parser.py +29 -0
- sinapsis_core/cli/display_template_info.py +202 -0
- sinapsis_core/cli/run_agent_from_config.py +40 -0
- sinapsis_core/context_managers/__init__.py +0 -0
- sinapsis_core/context_managers/env_var_override_context_manager.py +39 -0
- sinapsis_core/context_managers/working_dir_context_manager.py +57 -0
- sinapsis_core/data_containers/__init__.py +0 -0
- sinapsis_core/data_containers/annotations.py +152 -0
- sinapsis_core/data_containers/data_packet.py +364 -0
- sinapsis_core/data_containers/model_types/__init__.py +38 -0
- sinapsis_core/data_containers/model_types/_asr_base_models.py +39 -0
- sinapsis_core/data_containers/model_types/llm_types.py +189 -0
- sinapsis_core/exceptions/agent_exceptions.py +2 -0
- sinapsis_core/hooks/__init__.py +15 -0
- sinapsis_core/hooks/pre_init_hook_registry.py +225 -0
- sinapsis_core/template_base/__init__.py +10 -0
- sinapsis_core/template_base/base_models.py +59 -0
- sinapsis_core/template_base/copy_data_container.py +46 -0
- sinapsis_core/template_base/dynamic_template.py +169 -0
- sinapsis_core/template_base/dynamic_template_factory.py +454 -0
- sinapsis_core/template_base/dynamic_template_helpers/__init__.py +0 -0
- sinapsis_core/template_base/dynamic_template_helpers/builder_methods.py +92 -0
- sinapsis_core/template_base/dynamic_template_helpers/signature_parser.py +625 -0
- sinapsis_core/template_base/empty_data_container.py +23 -0
- sinapsis_core/template_base/input_template.py +8 -0
- sinapsis_core/template_base/merge_data_flow.py +33 -0
- sinapsis_core/template_base/multi_execute_template.py +65 -0
- sinapsis_core/template_base/output_template.py +8 -0
- sinapsis_core/template_base/split_data_flow.py +50 -0
- sinapsis_core/template_base/template.py +292 -0
- sinapsis_core/template_base/transfer_data_container.py +13 -0
- sinapsis_core/utils/__init__.py +0 -0
- sinapsis_core/utils/base_model_updater.py +148 -0
- sinapsis_core/utils/doc_string_generator.py +20 -0
- sinapsis_core/utils/env_var_keys.py +249 -0
- sinapsis_core/utils/env_var_utils.py +6 -0
- sinapsis_core/utils/logging_utils.py +53 -0
- sinapsis_core-1.0.0.dist-info/METADATA +239 -0
- sinapsis_core-1.0.0.dist-info/RECORD +50 -0
- sinapsis_core-1.0.0.dist-info/WHEEL +4 -0
- sinapsis_core-1.0.0.dist-info/licenses/LICENSE-APACHE +202 -0
- sinapsis_core-1.0.0.dist-info/licenses/LICENSE-MIT +21 -0
|
@@ -0,0 +1,65 @@
|
|
|
1
|
+
# -*- coding: utf-8 -*-
|
|
2
|
+
|
|
3
|
+
from functools import wraps
|
|
4
|
+
from inspect import getdoc
|
|
5
|
+
from typing import Type
|
|
6
|
+
|
|
7
|
+
from sinapsis_core.data_containers.data_packet import DataContainer
|
|
8
|
+
from sinapsis_core.template_base import Template
|
|
9
|
+
from sinapsis_core.template_base.base_models import TemplateAttributeType
|
|
10
|
+
|
|
11
|
+
|
|
12
|
+
def execute_template_n_times_wrapper(cls: Type[Template]) -> Type[Template]:
|
|
13
|
+
"""
|
|
14
|
+
Decorator function to repeat a template execution n times.
|
|
15
|
+
This wrapper should be used whenever we want to execute a template
|
|
16
|
+
a finite number of times, as opposed to every time an execute call
|
|
17
|
+
is made.
|
|
18
|
+
"""
|
|
19
|
+
|
|
20
|
+
@wraps(cls, updated=())
|
|
21
|
+
class RepeatExecute(cls):
|
|
22
|
+
class AttributesBaseModel(cls.AttributesBaseModel): # type: ignore [name-defined]
|
|
23
|
+
"""
|
|
24
|
+
n_times_execute determines the number of times the template's execute
|
|
25
|
+
method is called. By default, it only gets called once. Further calls
|
|
26
|
+
to execute are bypassed.
|
|
27
|
+
"""
|
|
28
|
+
|
|
29
|
+
n_times_execute: int = 1
|
|
30
|
+
|
|
31
|
+
def __init__(self, attributes: TemplateAttributeType) -> None:
|
|
32
|
+
super().__init__(attributes)
|
|
33
|
+
self.counter = 0
|
|
34
|
+
|
|
35
|
+
def execute(self, container: DataContainer) -> DataContainer:
|
|
36
|
+
if self.counter < self.attributes.n_times_execute:
|
|
37
|
+
container = super().execute(container)
|
|
38
|
+
self.counter += 1
|
|
39
|
+
return container
|
|
40
|
+
|
|
41
|
+
RepeatExecute.__doc__ = f"{getdoc(cls)}, \n{getdoc(RepeatExecute.AttributesBaseModel)}"
|
|
42
|
+
return RepeatExecute
|
|
43
|
+
|
|
44
|
+
|
|
45
|
+
def execute_template_on_trigger(cls: Type[Template]) -> Type[Template]:
|
|
46
|
+
"""
|
|
47
|
+
Decorator function to trigger a template execution when another
|
|
48
|
+
template has set a generic data field.
|
|
49
|
+
"""
|
|
50
|
+
|
|
51
|
+
@wraps(cls, updated=())
|
|
52
|
+
class ExecuteOnTrigger(cls):
|
|
53
|
+
class AttributesBaseModel(cls.AttributesBaseModel): # type: ignore [name-defined]
|
|
54
|
+
generic_key: str
|
|
55
|
+
|
|
56
|
+
def __init__(self, attributes: TemplateAttributeType) -> None:
|
|
57
|
+
super().__init__(attributes)
|
|
58
|
+
self.counter = 0
|
|
59
|
+
|
|
60
|
+
def execute(self, container: DataContainer) -> DataContainer:
|
|
61
|
+
if self._get_generic_data(container, self.attributes.generic_key):
|
|
62
|
+
container = super().execute(container)
|
|
63
|
+
return container
|
|
64
|
+
|
|
65
|
+
return ExecuteOnTrigger
|
|
@@ -0,0 +1,50 @@
|
|
|
1
|
+
# -*- coding: utf-8 -*-
|
|
2
|
+
from typing import List
|
|
3
|
+
|
|
4
|
+
from sinapsis_core.data_containers.data_packet import DataContainer, get_uuid
|
|
5
|
+
from sinapsis_core.template_base.base_models import TemplateAttributes
|
|
6
|
+
from sinapsis_core.template_base.copy_data_container import CopyDataContainer
|
|
7
|
+
|
|
8
|
+
|
|
9
|
+
class SplitDataFlow(CopyDataContainer):
|
|
10
|
+
"""
|
|
11
|
+
Template used to split data flow among Agent definition.
|
|
12
|
+
|
|
13
|
+
It's the only template allowed to produce a list of DataContainer objects as output. The number of elements in the
|
|
14
|
+
output list is determined by the attribute number_of_outputs.
|
|
15
|
+
"""
|
|
16
|
+
|
|
17
|
+
class AttributesBaseModel(CopyDataContainer.AttributesBaseModel):
|
|
18
|
+
"""
|
|
19
|
+
Attributes for SplitDataFlow template.
|
|
20
|
+
|
|
21
|
+
number_of_outputs (int): Define the number of allowed output connections. It must be specified according to the
|
|
22
|
+
number of templates that will receive data from this template.
|
|
23
|
+
|
|
24
|
+
"""
|
|
25
|
+
|
|
26
|
+
number_of_outputs: int
|
|
27
|
+
|
|
28
|
+
def execute(self, container: DataContainer) -> List[DataContainer]:
|
|
29
|
+
output_data_container_list = []
|
|
30
|
+
|
|
31
|
+
for _ in range(self.attributes.number_of_outputs):
|
|
32
|
+
container = self.get_data_container_copy(container)
|
|
33
|
+
container.container_id = get_uuid()
|
|
34
|
+
output_data_container_list.append(container)
|
|
35
|
+
|
|
36
|
+
return output_data_container_list
|
|
37
|
+
|
|
38
|
+
|
|
39
|
+
class MPISplitDataFlow(SplitDataFlow):
|
|
40
|
+
"""
|
|
41
|
+
This template is not exactly used as a normal template. It gets replaced
|
|
42
|
+
by MPI routines. Its main purpose is to avoid user mistakes when defining
|
|
43
|
+
MPI workflows by being explicit about data flow and how data containers are
|
|
44
|
+
to be distributed to workers.
|
|
45
|
+
"""
|
|
46
|
+
|
|
47
|
+
class AttributesBaseModel(TemplateAttributes):
|
|
48
|
+
recv_from_front_branch: bool = True
|
|
49
|
+
send_container_to_parallel_workers: bool = True
|
|
50
|
+
workers_stop_receiving_upon_first_empty_container: bool = False
|
|
@@ -0,0 +1,292 @@
|
|
|
1
|
+
# -*- coding: utf-8 -*-
|
|
2
|
+
import atexit
|
|
3
|
+
import gc
|
|
4
|
+
from abc import ABC, abstractmethod
|
|
5
|
+
from typing import Any
|
|
6
|
+
|
|
7
|
+
from sinapsis_core.data_containers.data_packet import DataContainer
|
|
8
|
+
from sinapsis_core.template_base.base_models import (
|
|
9
|
+
TemplateAttributes,
|
|
10
|
+
TemplateAttributeType,
|
|
11
|
+
UIPropertiesMetadata,
|
|
12
|
+
)
|
|
13
|
+
from sinapsis_core.utils.base_model_updater import update_nested_attribute
|
|
14
|
+
from sinapsis_core.utils.logging_utils import sinapsis_logger
|
|
15
|
+
|
|
16
|
+
|
|
17
|
+
class Template(ABC):
|
|
18
|
+
"""
|
|
19
|
+
Base class for all Templates in Sinapsis. A template is a reusable blueprint for implementing Agent executable
|
|
20
|
+
components. In summary, templates can encapsulate a series of operations, ranging from simple methods to
|
|
21
|
+
sophisticated orchestrations of multiple functions or other templates.
|
|
22
|
+
|
|
23
|
+
Properties:
|
|
24
|
+
* Highly parameterized through Template attributes.
|
|
25
|
+
* Single input and output design.
|
|
26
|
+
* Standalone functionality.
|
|
27
|
+
* Enable the creation of modular and scalable agents.
|
|
28
|
+
* Can be dynamically created from existing python methods or classes.
|
|
29
|
+
"""
|
|
30
|
+
|
|
31
|
+
AttributesBaseModel = TemplateAttributes
|
|
32
|
+
UIProperties = UIPropertiesMetadata
|
|
33
|
+
|
|
34
|
+
def _make_logger(self) -> Any:
|
|
35
|
+
"""
|
|
36
|
+
Creates the Sinapsis template logger.
|
|
37
|
+
|
|
38
|
+
Returns:
|
|
39
|
+
Any: Sinapsis logger bound with agent name, template name and template class name.
|
|
40
|
+
"""
|
|
41
|
+
return sinapsis_logger.bind(
|
|
42
|
+
agent_name=self.agent_name,
|
|
43
|
+
template_name=self.instance_name,
|
|
44
|
+
template_class_name=self.class_name,
|
|
45
|
+
)
|
|
46
|
+
|
|
47
|
+
@property
|
|
48
|
+
def instance_name(self) -> str | None:
|
|
49
|
+
"""
|
|
50
|
+
Property to retrieve the name of the template instance.
|
|
51
|
+
|
|
52
|
+
Returns:
|
|
53
|
+
str | None: Template instance name.
|
|
54
|
+
"""
|
|
55
|
+
if self.attributes.metadata:
|
|
56
|
+
return self.attributes.metadata.template_instance_name
|
|
57
|
+
|
|
58
|
+
sinapsis_logger.warning("Template has no metadata associated")
|
|
59
|
+
return None
|
|
60
|
+
|
|
61
|
+
@property
|
|
62
|
+
def agent_name(self) -> str | None:
|
|
63
|
+
"""
|
|
64
|
+
Property to retrieve the name of the agent.
|
|
65
|
+
|
|
66
|
+
Returns:
|
|
67
|
+
str | None: Agent name.
|
|
68
|
+
"""
|
|
69
|
+
if self.attributes.metadata:
|
|
70
|
+
return self.attributes.metadata.agent_name
|
|
71
|
+
sinapsis_logger.warning("Template has no metadata associated")
|
|
72
|
+
return None
|
|
73
|
+
|
|
74
|
+
def __init__(self, attributes: TemplateAttributeType) -> None:
|
|
75
|
+
"""
|
|
76
|
+
Base Template constructor method. Set attributes, logger and blocking_mode fields. Additionally, if implemented
|
|
77
|
+
it set shut_down method to be run at termination of the Template execution.
|
|
78
|
+
|
|
79
|
+
Args:
|
|
80
|
+
attributes (TemplateAttributeType): Template Attributes specified in the Agent definition.
|
|
81
|
+
"""
|
|
82
|
+
self.attributes = self.parse_instance_atts(attributes)
|
|
83
|
+
self.logger = self._make_logger()
|
|
84
|
+
self.__blocking_mode: bool = False
|
|
85
|
+
atexit.register(self._shut_down)
|
|
86
|
+
|
|
87
|
+
def update_attribute(self, attribute_name: str, attribute_value: Any) -> None:
|
|
88
|
+
"""Updates template attributes supporting nested paths while preserving non-updated attribute states.
|
|
89
|
+
|
|
90
|
+
Handles both dictionary and BaseModel nested structures. Validates the entire path before
|
|
91
|
+
updating and maintains type safety through Pydantic validation.
|
|
92
|
+
|
|
93
|
+
Args:
|
|
94
|
+
attribute_name (str): The full path to the attribute (e.g. "config.style.font.size")
|
|
95
|
+
attribute_value (Any): The value to set at the specified path
|
|
96
|
+
|
|
97
|
+
Notes:
|
|
98
|
+
- For non-existent paths, logs a warning and returns without updating
|
|
99
|
+
- Supports arbitrary nesting levels in both BaseModels and dictionaries
|
|
100
|
+
- Preserves state of non-updated attributes during updates
|
|
101
|
+
|
|
102
|
+
Limitations:
|
|
103
|
+
- Cannot serialize/deserialize complex objects like cv2.VideoCapture,
|
|
104
|
+
numpy.ndarray, or custom library objects during model_dump(), can will raise error.
|
|
105
|
+
"""
|
|
106
|
+
|
|
107
|
+
self.attributes, successful_update = update_nested_attribute(
|
|
108
|
+
base_model=self.attributes,
|
|
109
|
+
attribute_name=attribute_name,
|
|
110
|
+
attribute_value=attribute_value,
|
|
111
|
+
model_class=self.AttributesBaseModel,
|
|
112
|
+
instance_name=self.instance_name,
|
|
113
|
+
)
|
|
114
|
+
if successful_update:
|
|
115
|
+
log_msg = f"Successfully updated attribute {attribute_name} to {attribute_value}"
|
|
116
|
+
else:
|
|
117
|
+
log_msg = f"Failed to update attribute {attribute_name} to {attribute_value}"
|
|
118
|
+
self.logger.info(log_msg)
|
|
119
|
+
|
|
120
|
+
@classmethod
|
|
121
|
+
def class_name(cls) -> str:
|
|
122
|
+
"""
|
|
123
|
+
Method to retrieve the Template class name.
|
|
124
|
+
|
|
125
|
+
Returns:
|
|
126
|
+
str: Template class name.
|
|
127
|
+
"""
|
|
128
|
+
return cls.__class__.__name__
|
|
129
|
+
|
|
130
|
+
def _shut_down(self) -> None:
|
|
131
|
+
"""
|
|
132
|
+
Method to be implement in child Template classes. It must be used to implement a series of operations to be
|
|
133
|
+
performed at termination of Template execution.
|
|
134
|
+
"""
|
|
135
|
+
|
|
136
|
+
async def _async_shut_down(self) -> None:
|
|
137
|
+
"""
|
|
138
|
+
Method to be implement in child async Template classes. It must be used to implement a series of operations
|
|
139
|
+
to be performed at termination of Template execution.
|
|
140
|
+
"""
|
|
141
|
+
|
|
142
|
+
def _load_from_check_point(self) -> None:
|
|
143
|
+
pass
|
|
144
|
+
|
|
145
|
+
@classmethod
|
|
146
|
+
def parse_instance_atts(cls, attributes: TemplateAttributeType) -> TemplateAttributes:
|
|
147
|
+
"""
|
|
148
|
+
Initialize attributes pydantic object and raise PydanticValidationError
|
|
149
|
+
on validation failure.
|
|
150
|
+
"""
|
|
151
|
+
if isinstance(attributes, dict):
|
|
152
|
+
return cls.AttributesBaseModel(**attributes)
|
|
153
|
+
elif isinstance(attributes, cls.AttributesBaseModel):
|
|
154
|
+
return attributes
|
|
155
|
+
raise TypeError("Unexpected type for attributes")
|
|
156
|
+
|
|
157
|
+
def reset_state(self, template_name: str | None = None) -> None:
|
|
158
|
+
"""
|
|
159
|
+
This method is intended to be called from the agent, which in turn can be called
|
|
160
|
+
externally. It should be used to reset state
|
|
161
|
+
The method re-instantiates the class with the original attributes, resetting the
|
|
162
|
+
internal instance state.
|
|
163
|
+
|
|
164
|
+
Args:
|
|
165
|
+
template_name (str|None): The name of the template instance to reset.
|
|
166
|
+
|
|
167
|
+
Note:
|
|
168
|
+
More optimized ways to reset the state of the template are possible.
|
|
169
|
+
|
|
170
|
+
"""
|
|
171
|
+
_ = template_name
|
|
172
|
+
gc.collect()
|
|
173
|
+
new_instance = self.__class__(self.attributes.model_dump())
|
|
174
|
+
self.__dict__.update(new_instance.__dict__)
|
|
175
|
+
self.logger.info(f"Reset template instance `{self.instance_name}`")
|
|
176
|
+
|
|
177
|
+
@property
|
|
178
|
+
def blocking_mode(self) -> bool:
|
|
179
|
+
"""
|
|
180
|
+
Getter for blocking mode. When set to True a Template execute
|
|
181
|
+
method will block until done.
|
|
182
|
+
"""
|
|
183
|
+
return self.__blocking_mode
|
|
184
|
+
|
|
185
|
+
@blocking_mode.setter
|
|
186
|
+
def blocking_mode(self, block: bool) -> None:
|
|
187
|
+
"""
|
|
188
|
+
Setter for blocking mode. When set to True a Template execute
|
|
189
|
+
method will block until done.
|
|
190
|
+
"""
|
|
191
|
+
self.__blocking_mode = block
|
|
192
|
+
|
|
193
|
+
def __call__(self, container: DataContainer) -> DataContainer:
|
|
194
|
+
"""
|
|
195
|
+
Simply calls execute. Allows calling templates without explicitly calling
|
|
196
|
+
my_template_instance.execute(...). Refer to execute method for more information.
|
|
197
|
+
"""
|
|
198
|
+
if not container:
|
|
199
|
+
raise AttributeError(f"Template {self.instance_name} was called without a data container.")
|
|
200
|
+
|
|
201
|
+
return self.execute(container)
|
|
202
|
+
|
|
203
|
+
async def async_execute(self, container: DataContainer) -> DataContainer:
|
|
204
|
+
# self.logger.debug("async_execute called on non async template, will call sync execute")
|
|
205
|
+
return self.execute(container)
|
|
206
|
+
|
|
207
|
+
@abstractmethod
|
|
208
|
+
def execute(self, container: DataContainer) -> DataContainer:
|
|
209
|
+
"""
|
|
210
|
+
Execute is the main interface of a template to carry out a given task.
|
|
211
|
+
Any template calls should be made through execute.
|
|
212
|
+
|
|
213
|
+
Args:
|
|
214
|
+
container (DataContainer): Data container which can be empty or
|
|
215
|
+
which has been modified by previous templates. Any new data
|
|
216
|
+
produced by the template is appended to this container for
|
|
217
|
+
downstream use. Likewise, data added by a previous template
|
|
218
|
+
can be modified by this template.
|
|
219
|
+
|
|
220
|
+
Returns:
|
|
221
|
+
container (DataContainer): In most cases this is the same instance
|
|
222
|
+
as the received containers. Templates rarely create new instances
|
|
223
|
+
of data container, with the exception being MergeDataContainers.
|
|
224
|
+
"""
|
|
225
|
+
|
|
226
|
+
def num_elements(self) -> int:
|
|
227
|
+
"""
|
|
228
|
+
This method is intended to be used by 'producer' templates. That is, templates
|
|
229
|
+
which create new data packets which are appended to the data container flowing
|
|
230
|
+
through the agent. It is meant to have the same functionality as __len__ and
|
|
231
|
+
Templates that define len, should simply return self.__len__() instead of -1.
|
|
232
|
+
|
|
233
|
+
Returns:
|
|
234
|
+
len (int): Number of elements to be processed by the template, or -1. If
|
|
235
|
+
the latter, the agent will not check if the template has_finished. Instead,
|
|
236
|
+
it will assume that if `producer` containers are finished, agent execution
|
|
237
|
+
can be stopped (if in generator mode).
|
|
238
|
+
"""
|
|
239
|
+
_ = self
|
|
240
|
+
return -1
|
|
241
|
+
|
|
242
|
+
def _get_generic_data(self, container: DataContainer, generic_key: str | None = None) -> dict[str, Any]:
|
|
243
|
+
"""
|
|
244
|
+
|
|
245
|
+
Retrieves data values from a data container generic_data dictionary. The intended
|
|
246
|
+
use is to append data without native support (Packets) to the data container for
|
|
247
|
+
downstream use.
|
|
248
|
+
|
|
249
|
+
Args:
|
|
250
|
+
container (DataContainer): Data container flowing through the agent.
|
|
251
|
+
generic_key (str | None): Optional key to access the data from. Templates
|
|
252
|
+
should define generic_key as a required field in their ATTRIBUTE_CLS.
|
|
253
|
+
|
|
254
|
+
Returns:
|
|
255
|
+
(dict): Dictionary containing the generic key value pairs, if any, else empty dict.
|
|
256
|
+
|
|
257
|
+
"""
|
|
258
|
+
default_return_value: dict = {}
|
|
259
|
+
|
|
260
|
+
# if user didn't provide a generic key, get it from attributes
|
|
261
|
+
if not generic_key and hasattr(self.attributes, "generic_key"):
|
|
262
|
+
generic_key = getattr(self.attributes, "generic_key", None)
|
|
263
|
+
|
|
264
|
+
if not isinstance(generic_key, str):
|
|
265
|
+
self.logger.debug(
|
|
266
|
+
"Attempted to read from generic_data but generic_key was not given nor is it in attributes. "
|
|
267
|
+
"An empty dict will be returned."
|
|
268
|
+
)
|
|
269
|
+
return default_return_value
|
|
270
|
+
|
|
271
|
+
return container.generic_data.get(generic_key, default_return_value)
|
|
272
|
+
|
|
273
|
+
def _set_generic_data(self, container: DataContainer, data_item: Any) -> None:
|
|
274
|
+
"""
|
|
275
|
+
|
|
276
|
+
Appends data_item to the data container generic_data dictionary with self.instance_name
|
|
277
|
+
as the key.
|
|
278
|
+
|
|
279
|
+
Args:
|
|
280
|
+
container (DataContainer): Data container flowing through the agent.
|
|
281
|
+
data_item (Any): Data item to be appended to the data container generic_data dictionary.
|
|
282
|
+
"""
|
|
283
|
+
container.generic_data[self.instance_name] = data_item
|
|
284
|
+
|
|
285
|
+
def cancel_execution(self) -> None:
|
|
286
|
+
pass
|
|
287
|
+
|
|
288
|
+
# def pre_execute_initialization(self) -> None:
|
|
289
|
+
# pass
|
|
290
|
+
#
|
|
291
|
+
# async def async_pre_execute_initialization(self) -> None:
|
|
292
|
+
# pass
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
# -*- coding: utf-8 -*-
|
|
2
|
+
from sinapsis_core.data_containers.data_packet import DataContainer
|
|
3
|
+
from sinapsis_core.template_base.template import Template
|
|
4
|
+
|
|
5
|
+
|
|
6
|
+
class TransferDataContainer(Template):
|
|
7
|
+
"""Base class for DataContainer templates"""
|
|
8
|
+
|
|
9
|
+
def execute(self, container: DataContainer) -> DataContainer:
|
|
10
|
+
"""Propagates received container to the next template"""
|
|
11
|
+
|
|
12
|
+
_ = self
|
|
13
|
+
return container
|
|
File without changes
|
|
@@ -0,0 +1,148 @@
|
|
|
1
|
+
# -*- coding: utf-8 -*-
|
|
2
|
+
from typing import Any
|
|
3
|
+
|
|
4
|
+
from pydantic import BaseModel
|
|
5
|
+
|
|
6
|
+
from sinapsis_core.utils.logging_utils import sinapsis_logger
|
|
7
|
+
|
|
8
|
+
|
|
9
|
+
def _log_missing_attribute(attribute_path: str, instance_name: str) -> None:
|
|
10
|
+
"""Logs a warning message for missing attributes.
|
|
11
|
+
|
|
12
|
+
Args:
|
|
13
|
+
attribute_path (str): The path to the attribute that was not found
|
|
14
|
+
instance_name (str): Name of the instance being updated
|
|
15
|
+
"""
|
|
16
|
+
sinapsis_logger.warning(f"No attribute found: `{attribute_path}` on instance `{instance_name}`")
|
|
17
|
+
|
|
18
|
+
|
|
19
|
+
def validate_path_exists(current_value: Any, path_part: str, full_path: str, instance_name: str) -> bool:
|
|
20
|
+
"""Validates if a path component exists in the current object and is accessible.
|
|
21
|
+
|
|
22
|
+
Checks whether the given path_part exists in the current object, which can be either
|
|
23
|
+
a dictionary or a BaseModel. For dictionaries, checks if the key exists. For BaseModels,
|
|
24
|
+
checks if the attribute exists. For other types, logs a warning as they cannot contain
|
|
25
|
+
nested attributes.
|
|
26
|
+
|
|
27
|
+
Args:
|
|
28
|
+
current_value (Any): The current object being traversed (dict or BaseModel)
|
|
29
|
+
path_part (str): The specific part of the path being validated
|
|
30
|
+
full_path (str): The complete attribute path for error reporting
|
|
31
|
+
instance_name (str): Name of the template instance
|
|
32
|
+
|
|
33
|
+
Returns:
|
|
34
|
+
bool: True if the path exists and is valid, False otherwise
|
|
35
|
+
"""
|
|
36
|
+
|
|
37
|
+
if isinstance(current_value, dict):
|
|
38
|
+
if path_part not in current_value:
|
|
39
|
+
_log_missing_attribute(full_path, instance_name)
|
|
40
|
+
return False
|
|
41
|
+
elif isinstance(current_value, BaseModel):
|
|
42
|
+
if not hasattr(current_value, path_part):
|
|
43
|
+
_log_missing_attribute(full_path, instance_name)
|
|
44
|
+
return False
|
|
45
|
+
else:
|
|
46
|
+
sinapsis_logger.warning(f"Cannot set nested attribute on `{type(current_value).__name__}`: `{full_path}`")
|
|
47
|
+
return False
|
|
48
|
+
return True
|
|
49
|
+
|
|
50
|
+
|
|
51
|
+
def get_nested_value(source_object: Any, attribute_name: str) -> Any:
|
|
52
|
+
"""Retrieves a nested value from either a dictionary or BaseModel.
|
|
53
|
+
|
|
54
|
+
Provides unified access to nested values regardless of container type.
|
|
55
|
+
For dictionaries, it uses key access. For BaseModels, it uses attribute access.
|
|
56
|
+
|
|
57
|
+
Args:
|
|
58
|
+
source_object (Any): The container object (dict or BaseModel) to get the value from
|
|
59
|
+
attribute_name (str): The attribute/key name to retrieve
|
|
60
|
+
|
|
61
|
+
Returns:
|
|
62
|
+
Any: The value stored at the specified attribute/key
|
|
63
|
+
"""
|
|
64
|
+
if isinstance(source_object, dict):
|
|
65
|
+
return source_object[attribute_name]
|
|
66
|
+
return getattr(source_object, attribute_name)
|
|
67
|
+
|
|
68
|
+
|
|
69
|
+
def set_nested_value(target_object: Any, attribute_name: str, attribute_value: Any) -> None:
|
|
70
|
+
"""Sets a nested value in either a dictionary or BaseModel.
|
|
71
|
+
|
|
72
|
+
Provides a unified way to set values regardless of container type.
|
|
73
|
+
For dictionaries, it uses key assignment. For BaseModels, it uses attribute assignment.
|
|
74
|
+
|
|
75
|
+
Args:
|
|
76
|
+
target_object (Any): The container object (dict or BaseModel) to set the value in
|
|
77
|
+
attribute_name (str): The attribute/key name where to set the value
|
|
78
|
+
attribute_value (Any): The value to set
|
|
79
|
+
"""
|
|
80
|
+
if isinstance(target_object, dict):
|
|
81
|
+
target_object[attribute_name] = attribute_value
|
|
82
|
+
else:
|
|
83
|
+
setattr(target_object, attribute_name, attribute_value)
|
|
84
|
+
|
|
85
|
+
|
|
86
|
+
def update_nested_attribute(
|
|
87
|
+
base_model: BaseModel,
|
|
88
|
+
attribute_name: str,
|
|
89
|
+
attribute_value: Any,
|
|
90
|
+
model_class: type[BaseModel],
|
|
91
|
+
instance_name: str,
|
|
92
|
+
) -> tuple[BaseModel, bool]:
|
|
93
|
+
"""Updates template attributes, supporting nested paths with validation.
|
|
94
|
+
|
|
95
|
+
Allows updating both top-level and nested attributes in the attribute structure.
|
|
96
|
+
Handles both dictionary and BaseModel nested structures. Validates the entire path before
|
|
97
|
+
updating and maintains type safety through Pydantic validation.
|
|
98
|
+
|
|
99
|
+
Args:
|
|
100
|
+
base_model (BaseModel): The base model instance to update
|
|
101
|
+
attribute_name (str): The full path to the attribute (e.g. "config.style.font.size")
|
|
102
|
+
attribute_value (Any): The value to set at the specified path
|
|
103
|
+
model_class (type[BaseModel]): The BaseModel class to use for validation
|
|
104
|
+
instance_name (str): Name of the instance being updated
|
|
105
|
+
|
|
106
|
+
Returns:
|
|
107
|
+
BaseModel: Updated base model instance
|
|
108
|
+
|
|
109
|
+
Notes:
|
|
110
|
+
- For non-existent paths, logs a warning and returns original model
|
|
111
|
+
- Validates through Pydantic after update to ensure type safety
|
|
112
|
+
- Supports arbitrary nesting levels in both BaseModels and dictionaries
|
|
113
|
+
"""
|
|
114
|
+
successful_update = False
|
|
115
|
+
if "." not in attribute_name:
|
|
116
|
+
if hasattr(base_model, attribute_name):
|
|
117
|
+
setattr(base_model, attribute_name, attribute_value)
|
|
118
|
+
successful_update = True
|
|
119
|
+
else:
|
|
120
|
+
_log_missing_attribute(attribute_name, instance_name)
|
|
121
|
+
return base_model, successful_update
|
|
122
|
+
|
|
123
|
+
parts = attribute_name.split(".")
|
|
124
|
+
root = parts[0]
|
|
125
|
+
|
|
126
|
+
if not hasattr(base_model, root):
|
|
127
|
+
_log_missing_attribute(attribute_name, instance_name)
|
|
128
|
+
return base_model, successful_update
|
|
129
|
+
|
|
130
|
+
current = getattr(base_model, root)
|
|
131
|
+
|
|
132
|
+
for part in parts[1:-1]:
|
|
133
|
+
if not validate_path_exists(current, part, attribute_name, instance_name):
|
|
134
|
+
return base_model, successful_update
|
|
135
|
+
current = get_nested_value(current, part)
|
|
136
|
+
|
|
137
|
+
if not validate_path_exists(current, parts[-1], attribute_name, instance_name):
|
|
138
|
+
return base_model, successful_update
|
|
139
|
+
|
|
140
|
+
new_attrs = base_model.model_dump()
|
|
141
|
+
target = new_attrs[root]
|
|
142
|
+
|
|
143
|
+
for part in parts[1:-1]:
|
|
144
|
+
target = get_nested_value(target, part)
|
|
145
|
+
|
|
146
|
+
set_nested_value(target, parts[-1], attribute_value)
|
|
147
|
+
successful_update = True
|
|
148
|
+
return model_class(**new_attrs), successful_update
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
# -*- coding: utf-8 -*-
|
|
2
|
+
|
|
3
|
+
from types import ModuleType
|
|
4
|
+
|
|
5
|
+
from sinapsis_core.template_base.template import Template
|
|
6
|
+
|
|
7
|
+
|
|
8
|
+
def make_template_module_docs(template_module: ModuleType) -> str:
|
|
9
|
+
"""Generates the docstrings for the template_module and returns the string representation
|
|
10
|
+
Args:
|
|
11
|
+
template_module (ModuleType): Module where the templates exist
|
|
12
|
+
Returns:
|
|
13
|
+
str : The string representation of the documentation in the templates.
|
|
14
|
+
"""
|
|
15
|
+
aggregate_doc_str: str = f"""{template_module.__name__}"""
|
|
16
|
+
separator: str = "\n" * 3
|
|
17
|
+
for template_name in template_module.__all__:
|
|
18
|
+
template_cls: Template = getattr(template_module, template_name)
|
|
19
|
+
aggregate_doc_str += f"""{template_name}\n {template_cls.__doc__}{separator}"""
|
|
20
|
+
return aggregate_doc_str
|