From b83e521653333d6e424a9174c595da991db9d22b Mon Sep 17 00:00:00 2001 From: Elizabeth Esswein Date: Tue, 26 Nov 2024 22:15:52 -0500 Subject: [PATCH] enforce some consistency in the script engine --- .../bpmn/script_engine/python_engine.py | 15 +++---- .../bpmn/script_engine/python_environment.py | 2 +- .../spiff/specs/mixins/service_task.py | 45 ++++++++----------- tests/SpiffWorkflow/spiff/ServiceTaskTest.py | 5 +-- .../spiff/ServiceTaskVariableTest.py | 5 +-- 5 files changed, 30 insertions(+), 42 deletions(-) diff --git a/SpiffWorkflow/bpmn/script_engine/python_engine.py b/SpiffWorkflow/bpmn/script_engine/python_engine.py index cb5b58d4..f3766717 100644 --- a/SpiffWorkflow/bpmn/script_engine/python_engine.py +++ b/SpiffWorkflow/bpmn/script_engine/python_engine.py @@ -20,7 +20,6 @@ import ast import sys import traceback -import warnings from SpiffWorkflow.exceptions import SpiffWorkflowException from SpiffWorkflow.bpmn.exceptions import WorkflowTaskException @@ -65,15 +64,13 @@ def execute(self, task, script, external_context=None): wte = self.create_task_exec_exception(task, script, err) raise wte - def call_service(self, operation_name, operation_params, task_data): + def call_service(self, task, **kwargs): """Override to control how external services are called from service tasks.""" - warnings.warn( - 'In the next release, implementation of this method will be moved to the scripting environment', - DeprecationWarning, - stacklevel=2, - ) - # Ideally, this method would look like call_service(self, task, operation_name, operation_params) - return self.environment.call_service(operation_name, operation_params, task_data) + try: + return self.environment.call_service(task.data, **kwargs) + except Exception as err: + wte = self.create_task_exec_exception(task, script, err) + raise wte def create_task_exec_exception(self, task, script, err): line_number, error_line = self.get_error_line_number_and_content(script, err) diff --git a/SpiffWorkflow/bpmn/script_engine/python_environment.py b/SpiffWorkflow/bpmn/script_engine/python_environment.py index bd4b4a4d..0147601b 100644 --- a/SpiffWorkflow/bpmn/script_engine/python_environment.py +++ b/SpiffWorkflow/bpmn/script_engine/python_environment.py @@ -30,7 +30,7 @@ def evaluate(self, expression, context, external_context=None): def execute(self, script, context, external_context=None): raise NotImplementedError("Subclass must implement this method") - def call_service(self, operation_name, operation_params, task_data): + def call_service(self, operation_name, operation_params, context): raise NotImplementedError("To call external services override the script engine and implement `call_service`.") diff --git a/SpiffWorkflow/spiff/specs/mixins/service_task.py b/SpiffWorkflow/spiff/specs/mixins/service_task.py index 85f4156b..30f9bbc2 100644 --- a/SpiffWorkflow/spiff/specs/mixins/service_task.py +++ b/SpiffWorkflow/spiff/specs/mixins/service_task.py @@ -29,32 +29,25 @@ def __init__(self, wf_spec, name, operation_name, operation_params, result_varia super().__init__(wf_spec, name, **kwargs) self.operation_name = operation_name self.operation_params = operation_params - self.result_variable = result_variable - - def _result_variable(self, task): - if self.result_variable is not None and len(self.result_variable) > 0: - return self.result_variable - - escaped_spec_name = task.task_spec.name.replace('-', '_') - - return f'spiff__{escaped_spec_name}_result' + if result_variable is None or result_variable == '': + self.result_variable = f'spiff__{name.replace("-", "_")}_result' + else: + self.result_variable = result_variable + + def evalutate_params(self, task): + evaluated_params = {} + for name, param in self.operation_params.items(): + evaluated_params[name] = { + 'value': task.workflow.script_engine.evaluate(task, param['value']), + 'type': param['type'], + } + return evaluated_params def _execute(self, task): - def evaluate(param): - param['value'] = task.workflow.script_engine.evaluate(task, param['value']) - return param - - operation_params_copy = deepcopy(self.operation_params) - evaluated_params = {k: evaluate(v) for k, v in operation_params_copy.items()} - - try: - result = task.workflow.script_engine.call_service(self.operation_name, - evaluated_params, task.data) - except Exception as e: - wte = WorkflowTaskException("Error executing Service Task", - task=task, exception=e) - wte.add_note(str(e)) - raise wte - parsed_result = json.loads(result) - task.data[self._result_variable(task)] = parsed_result + result = task.workflow.script_engine.call_service( + task, + self.operation_name, + self.evalutate_params(task), + ) + task.data[self.result_variable] = json.loads(result) return True diff --git a/tests/SpiffWorkflow/spiff/ServiceTaskTest.py b/tests/SpiffWorkflow/spiff/ServiceTaskTest.py index 6b779298..de0e8e2a 100644 --- a/tests/SpiffWorkflow/spiff/ServiceTaskTest.py +++ b/tests/SpiffWorkflow/spiff/ServiceTaskTest.py @@ -34,9 +34,8 @@ def call_connector(name, params, task_data): return json.dumps(sample_response) class ExampleCustomScriptEngine(PythonScriptEngine): - def call_service(self, operation_name, operation_params, task_data): - return ServiceTaskDelegate.call_connector(operation_name, operation_params, - task_data) + def call_service(self, task, operation_name, operation_params): + return ServiceTaskDelegate.call_connector(operation_name, operation_params, task.data) class ServiceTaskTest(BaseTestCase): diff --git a/tests/SpiffWorkflow/spiff/ServiceTaskVariableTest.py b/tests/SpiffWorkflow/spiff/ServiceTaskVariableTest.py index 2daae122..529c2953 100644 --- a/tests/SpiffWorkflow/spiff/ServiceTaskVariableTest.py +++ b/tests/SpiffWorkflow/spiff/ServiceTaskVariableTest.py @@ -25,9 +25,8 @@ def call_connector(name, params, task_data): return json.dumps(sample_response) class ExampleCustomScriptEngine(PythonScriptEngine): - def call_service(self, operation_name, operation_params, task_data): - return ServiceTaskDelegate.call_connector(operation_name, operation_params, - task_data) + def call_service(self, task, operation_name, operation_params): + return ServiceTaskDelegate.call_connector(operation_name, operation_params, task.data) class ServiceTaskVariableTest(BaseTestCase):