fixing an untyped method.

This commit is contained in:
Dan 2022-12-05 12:55:44 -05:00
parent 3a09b45765
commit e06500821e
1 changed files with 60 additions and 59 deletions

View File

@ -97,6 +97,7 @@ from spiffworkflow_backend.services.service_task_service import ServiceTaskDeleg
from spiffworkflow_backend.services.spec_file_service import SpecFileService from spiffworkflow_backend.services.spec_file_service import SpecFileService
from spiffworkflow_backend.services.user_service import UserService from spiffworkflow_backend.services.user_service import UserService
# Sorry about all this crap. I wanted to move this thing to another file, but # Sorry about all this crap. I wanted to move this thing to another file, but
# importing a bunch of types causes circular imports. # importing a bunch of types causes circular imports.
@ -178,16 +179,16 @@ class CustomBpmnScriptEngine(PythonScriptEngine): # type: ignore
) )
return Script.generate_augmented_list(script_attributes_context) return Script.generate_augmented_list(script_attributes_context)
def evaluate(self, task: SpiffTask, expression: str, external_methods=None) -> Any: def evaluate(self, task: SpiffTask, expression: str, external_methods: Any = None) -> Any:
"""Evaluate.""" """Evaluate."""
return self._evaluate(expression, task.data, task, external_methods) return self._evaluate(expression, task.data, task, external_methods)
def _evaluate( def _evaluate(
self, self,
expression: str, expression: str,
context: Dict[str, Union[Box, str]], context: Dict[str, Union[Box, str]],
task: Optional[SpiffTask] = None, task: Optional[SpiffTask] = None,
external_methods: Optional[Dict[str, Any]] = None, external_methods: Optional[Dict[str, Any]] = None,
) -> Any: ) -> Any:
"""_evaluate.""" """_evaluate."""
methods = self.__get_augment_methods(task) methods = self.__get_augment_methods(task)
@ -211,7 +212,7 @@ class CustomBpmnScriptEngine(PythonScriptEngine): # type: ignore
) from exception ) from exception
def execute( def execute(
self, task: SpiffTask, script: str, external_methods: Any = None self, task: SpiffTask, script: str, external_methods: Any = None
) -> None: ) -> None:
"""Execute.""" """Execute."""
try: try:
@ -225,10 +226,10 @@ class CustomBpmnScriptEngine(PythonScriptEngine): # type: ignore
raise WorkflowTaskExecException(task, f" {script}, {e}", e) from e raise WorkflowTaskExecException(task, f" {script}, {e}", e) from e
def call_service( def call_service(
self, self,
operation_name: str, operation_name: str,
operation_params: Dict[str, Any], operation_params: Dict[str, Any],
task_data: Dict[str, Any], task_data: Dict[str, Any],
) -> Any: ) -> Any:
"""CallService.""" """CallService."""
return ServiceTaskDelegate.call_connector( return ServiceTaskDelegate.call_connector(
@ -275,7 +276,7 @@ class ProcessInstanceProcessor:
# * get_spec, which returns a spec and any subprocesses (as IdToBpmnProcessSpecMapping dict) # * get_spec, which returns a spec and any subprocesses (as IdToBpmnProcessSpecMapping dict)
# * __get_bpmn_process_instance, which takes spec and subprocesses and instantiates and returns a BpmnWorkflow # * __get_bpmn_process_instance, which takes spec and subprocesses and instantiates and returns a BpmnWorkflow
def __init__( def __init__(
self, process_instance_model: ProcessInstanceModel, validate_only: bool = False self, process_instance_model: ProcessInstanceModel, validate_only: bool = False
) -> None: ) -> None:
"""Create a Workflow Processor based on the serialized information available in the process_instance model.""" """Create a Workflow Processor based on the serialized information available in the process_instance model."""
tld = current_app.config["THREAD_LOCAL_DATA"] tld = current_app.config["THREAD_LOCAL_DATA"]
@ -302,7 +303,7 @@ class ProcessInstanceProcessor:
) )
else: else:
bpmn_json_length = len(process_instance_model.bpmn_json.encode("utf-8")) bpmn_json_length = len(process_instance_model.bpmn_json.encode("utf-8"))
megabyte = float(1024**2) megabyte = float(1024 ** 2)
json_size = bpmn_json_length / megabyte json_size = bpmn_json_length / megabyte
if json_size > 1: if json_size > 1:
wf_json = json.loads(process_instance_model.bpmn_json) wf_json = json.loads(process_instance_model.bpmn_json)
@ -316,22 +317,22 @@ class ProcessInstanceProcessor:
len(json.dumps(test_spec).encode("utf-8")) / megabyte len(json.dumps(test_spec).encode("utf-8")) / megabyte
) )
message = ( message = (
"Workflow " "Workflow "
+ process_instance_model.process_model_identifier + process_instance_model.process_model_identifier
+ f" JSON Size is over 1MB:{json_size:.2f} MB" + f" JSON Size is over 1MB:{json_size:.2f} MB"
) )
message += f"\n Task Size: {task_size}" message += f"\n Task Size: {task_size}"
message += f"\n Spec Size: {spec_size}" message += f"\n Spec Size: {spec_size}"
current_app.logger.warning(message) current_app.logger.warning(message)
def check_sub_specs( def check_sub_specs(
test_spec: dict, indent: int = 0, show_all: bool = False test_spec: dict, indent: int = 0, show_all: bool = False
) -> None: ) -> None:
"""Check_sub_specs.""" """Check_sub_specs."""
for my_spec_name in test_spec["task_specs"]: for my_spec_name in test_spec["task_specs"]:
my_spec = test_spec["task_specs"][my_spec_name] my_spec = test_spec["task_specs"][my_spec_name]
my_spec_size = ( my_spec_size = (
len(json.dumps(my_spec).encode("utf-8")) / megabyte len(json.dumps(my_spec).encode("utf-8")) / megabyte
) )
if my_spec_size > 0.1 or show_all: if my_spec_size > 0.1 or show_all:
current_app.logger.warning( current_app.logger.warning(
@ -367,13 +368,13 @@ class ProcessInstanceProcessor:
raise ApiError( raise ApiError(
error_code="unexpected_process_instance_structure", error_code="unexpected_process_instance_structure",
message="Failed to deserialize process_instance" message="Failed to deserialize process_instance"
" '%s' due to a mis-placed or missing task '%s'" " '%s' due to a mis-placed or missing task '%s'"
% (self.process_model_identifier, str(ke)), % (self.process_model_identifier, str(ke)),
) from ke ) from ke
@classmethod @classmethod
def get_process_model_and_subprocesses( def get_process_model_and_subprocesses(
cls, process_model_identifier: str cls, process_model_identifier: str
) -> Tuple[BpmnProcessSpec, IdToBpmnProcessSpecMapping]: ) -> Tuple[BpmnProcessSpec, IdToBpmnProcessSpecMapping]:
"""Get_process_model_and_subprocesses.""" """Get_process_model_and_subprocesses."""
process_model_info = ProcessModelService.get_process_model( process_model_info = ProcessModelService.get_process_model(
@ -391,7 +392,7 @@ class ProcessInstanceProcessor:
@classmethod @classmethod
def get_bpmn_process_instance_from_process_model( def get_bpmn_process_instance_from_process_model(
cls, process_model_identifier: str cls, process_model_identifier: str
) -> BpmnWorkflow: ) -> BpmnWorkflow:
"""Get_all_bpmn_process_identifiers_for_process_model.""" """Get_all_bpmn_process_identifiers_for_process_model."""
(bpmn_process_spec, subprocesses) = cls.get_process_model_and_subprocesses( (bpmn_process_spec, subprocesses) = cls.get_process_model_and_subprocesses(
@ -416,7 +417,7 @@ class ProcessInstanceProcessor:
return current_user return current_user
def add_user_info_to_process_instance( def add_user_info_to_process_instance(
self, bpmn_process_instance: BpmnWorkflow self, bpmn_process_instance: BpmnWorkflow
) -> None: ) -> None:
"""Add_user_info_to_process_instance.""" """Add_user_info_to_process_instance."""
current_user = self.current_user() current_user = self.current_user()
@ -429,8 +430,8 @@ class ProcessInstanceProcessor:
@staticmethod @staticmethod
def get_bpmn_process_instance_from_workflow_spec( def get_bpmn_process_instance_from_workflow_spec(
spec: BpmnProcessSpec, spec: BpmnProcessSpec,
subprocesses: Optional[IdToBpmnProcessSpecMapping] = None, subprocesses: Optional[IdToBpmnProcessSpecMapping] = None,
) -> BpmnWorkflow: ) -> BpmnWorkflow:
"""Get_bpmn_process_instance_from_workflow_spec.""" """Get_bpmn_process_instance_from_workflow_spec."""
return BpmnWorkflow( return BpmnWorkflow(
@ -441,10 +442,10 @@ class ProcessInstanceProcessor:
@staticmethod @staticmethod
def __get_bpmn_process_instance( def __get_bpmn_process_instance(
process_instance_model: ProcessInstanceModel, process_instance_model: ProcessInstanceModel,
spec: Optional[BpmnProcessSpec] = None, spec: Optional[BpmnProcessSpec] = None,
validate_only: bool = False, validate_only: bool = False,
subprocesses: Optional[IdToBpmnProcessSpecMapping] = None, subprocesses: Optional[IdToBpmnProcessSpecMapping] = None,
) -> BpmnWorkflow: ) -> BpmnWorkflow:
"""__get_bpmn_process_instance.""" """__get_bpmn_process_instance."""
if process_instance_model.bpmn_json: if process_instance_model.bpmn_json:
@ -487,14 +488,14 @@ class ProcessInstanceProcessor:
self.save() self.save()
def raise_if_no_potential_owners( def raise_if_no_potential_owners(
self, potential_owner_ids: list[int], message: str self, potential_owner_ids: list[int], message: str
) -> None: ) -> None:
"""Raise_if_no_potential_owners.""" """Raise_if_no_potential_owners."""
if not potential_owner_ids: if not potential_owner_ids:
raise (NoPotentialOwnersForTaskError(message)) raise (NoPotentialOwnersForTaskError(message))
def get_potential_owner_ids_from_task( def get_potential_owner_ids_from_task(
self, task: SpiffTask self, task: SpiffTask
) -> PotentialOwnerIdList: ) -> PotentialOwnerIdList:
"""Get_potential_owner_ids_from_task.""" """Get_potential_owner_ids_from_task."""
task_spec = task.task_spec task_spec = task.task_spec
@ -666,7 +667,7 @@ class ProcessInstanceProcessor:
@staticmethod @staticmethod
def backfill_missing_spec_reference_records( def backfill_missing_spec_reference_records(
bpmn_process_identifier: str, bpmn_process_identifier: str,
) -> Optional[str]: ) -> Optional[str]:
"""Backfill_missing_spec_reference_records.""" """Backfill_missing_spec_reference_records."""
process_models = ProcessModelService.get_process_models(recursive=True) process_models = ProcessModelService.get_process_models(recursive=True)
@ -687,7 +688,7 @@ class ProcessInstanceProcessor:
@staticmethod @staticmethod
def bpmn_file_full_path_from_bpmn_process_identifier( def bpmn_file_full_path_from_bpmn_process_identifier(
bpmn_process_identifier: str, bpmn_process_identifier: str,
) -> str: ) -> str:
"""Bpmn_file_full_path_from_bpmn_process_identifier.""" """Bpmn_file_full_path_from_bpmn_process_identifier."""
if bpmn_process_identifier is None: if bpmn_process_identifier is None:
@ -697,8 +698,8 @@ class ProcessInstanceProcessor:
spec_reference = ( spec_reference = (
SpecReferenceCache.query.filter_by(identifier=bpmn_process_identifier) SpecReferenceCache.query.filter_by(identifier=bpmn_process_identifier)
.filter_by(type="process") .filter_by(type="process")
.first() .first()
) )
bpmn_file_full_path = None bpmn_file_full_path = None
if spec_reference is None: if spec_reference is None:
@ -717,15 +718,15 @@ class ProcessInstanceProcessor:
ApiError( ApiError(
error_code="could_not_find_bpmn_process_identifier", error_code="could_not_find_bpmn_process_identifier",
message="Could not find the the given bpmn process identifier from any sources: %s" message="Could not find the the given bpmn process identifier from any sources: %s"
% bpmn_process_identifier, % bpmn_process_identifier,
) )
) )
return os.path.abspath(bpmn_file_full_path) return os.path.abspath(bpmn_file_full_path)
@staticmethod @staticmethod
def update_spiff_parser_with_all_process_dependency_files( def update_spiff_parser_with_all_process_dependency_files(
parser: BpmnDmnParser, parser: BpmnDmnParser,
processed_identifiers: Optional[set[str]] = None, processed_identifiers: Optional[set[str]] = None,
) -> None: ) -> None:
"""Update_spiff_parser_with_all_process_dependency_files.""" """Update_spiff_parser_with_all_process_dependency_files."""
if processed_identifiers is None: if processed_identifiers is None:
@ -763,7 +764,7 @@ class ProcessInstanceProcessor:
@staticmethod @staticmethod
def get_spec( def get_spec(
files: List[File], process_model_info: ProcessModelInfo files: List[File], process_model_info: ProcessModelInfo
) -> Tuple[BpmnProcessSpec, IdToBpmnProcessSpecMapping]: ) -> Tuple[BpmnProcessSpec, IdToBpmnProcessSpecMapping]:
"""Returns a SpiffWorkflow specification for the given process_instance spec, using the files provided.""" """Returns a SpiffWorkflow specification for the given process_instance spec, using the files provided."""
parser = ProcessInstanceProcessor.get_parser() parser = ProcessInstanceProcessor.get_parser()
@ -777,14 +778,14 @@ class ProcessInstanceProcessor:
dmn: etree.Element = etree.fromstring(data) dmn: etree.Element = etree.fromstring(data)
parser.add_dmn_xml(dmn, filename=file.name) parser.add_dmn_xml(dmn, filename=file.name)
if ( if (
process_model_info.primary_process_id is None process_model_info.primary_process_id is None
or process_model_info.primary_process_id == "" or process_model_info.primary_process_id == ""
): ):
raise ( raise (
ApiError( ApiError(
error_code="no_primary_bpmn_error", error_code="no_primary_bpmn_error",
message="There is no primary BPMN process id defined for process_model %s" message="There is no primary BPMN process id defined for process_model %s"
% process_model_info.id, % process_model_info.id,
) )
) )
ProcessInstanceProcessor.update_spiff_parser_with_all_process_dependency_files( ProcessInstanceProcessor.update_spiff_parser_with_all_process_dependency_files(
@ -802,7 +803,7 @@ class ProcessInstanceProcessor:
raise ApiError( raise ApiError(
error_code="process_instance_validation_error", error_code="process_instance_validation_error",
message="Failed to parse the Workflow Specification. " message="Failed to parse the Workflow Specification. "
+ "Error is '%s.'" % str(ve), + "Error is '%s.'" % str(ve),
file_name=ve.filename, file_name=ve.filename,
task_id=ve.id, task_id=ve.id,
tag=ve.tag, tag=ve.tag,
@ -849,12 +850,12 @@ class ProcessInstanceProcessor:
message_correlations = [] message_correlations = []
for ( for (
message_correlation_key, message_correlation_key,
message_correlation_properties, message_correlation_properties,
) in bpmn_message.correlations.items(): ) in bpmn_message.correlations.items():
for ( for (
message_correlation_property_identifier, message_correlation_property_identifier,
message_correlation_property_value, message_correlation_property_value,
) in message_correlation_properties.items(): ) in message_correlation_properties.items():
message_correlation_property = ( message_correlation_property = (
MessageCorrelationPropertyModel.query.filter_by( MessageCorrelationPropertyModel.query.filter_by(
@ -946,7 +947,7 @@ class ProcessInstanceProcessor:
db.session.add(message_instance) db.session.add(message_instance)
for ( for (
spiff_correlation_property spiff_correlation_property
) in waiting_task.task_spec.event_definition.correlation_properties: ) in waiting_task.task_spec.event_definition.correlation_properties:
# NOTE: we may have to cycle through keys here # NOTE: we may have to cycle through keys here
# not sure yet if it's valid for a property to be associated with multiple keys # not sure yet if it's valid for a property to be associated with multiple keys
@ -956,9 +957,9 @@ class ProcessInstanceProcessor:
process_instance_id=self.process_instance_model.id, process_instance_id=self.process_instance_model.id,
name=correlation_key_name, name=correlation_key_name,
) )
.join(MessageCorrelationPropertyModel) .join(MessageCorrelationPropertyModel)
.filter_by(identifier=spiff_correlation_property.name) .filter_by(identifier=spiff_correlation_property.name)
.first() .first()
) )
message_correlation_message_instance = ( message_correlation_message_instance = (
MessageCorrelationMessageInstanceModel( MessageCorrelationMessageInstanceModel(
@ -1062,12 +1063,12 @@ class ProcessInstanceProcessor:
endtasks = [] endtasks = []
if self.bpmn_process_instance.is_completed(): if self.bpmn_process_instance.is_completed():
for task in SpiffTask.Iterator( for task in SpiffTask.Iterator(
self.bpmn_process_instance.task_tree, TaskState.ANY_MASK self.bpmn_process_instance.task_tree, TaskState.ANY_MASK
): ):
# Assure that we find the end event for this process_instance, and not for any sub-process_instances. # Assure that we find the end event for this process_instance, and not for any sub-process_instances.
if ( if (
isinstance(task.task_spec, EndEvent) isinstance(task.task_spec, EndEvent)
and task.workflow == self.bpmn_process_instance and task.workflow == self.bpmn_process_instance
): ):
endtasks.append(task) endtasks.append(task)
if len(endtasks) > 0: if len(endtasks) > 0:
@ -1103,8 +1104,8 @@ class ProcessInstanceProcessor:
return task return task
for task in ready_tasks: for task in ready_tasks:
if ( if (
self.bpmn_process_instance.last_task self.bpmn_process_instance.last_task
and task.parent == last_user_task.parent and task.parent == last_user_task.parent
): ):
return task return task
@ -1114,7 +1115,7 @@ class ProcessInstanceProcessor:
# and return that # and return that
next_task = None next_task = None
for task in SpiffTask.Iterator( for task in SpiffTask.Iterator(
self.bpmn_process_instance.task_tree, TaskState.NOT_FINISHED_MASK self.bpmn_process_instance.task_tree, TaskState.NOT_FINISHED_MASK
): ):
next_task = task next_task = task
return next_task return next_task
@ -1198,7 +1199,7 @@ class ProcessInstanceProcessor:
t t
for t in all_tasks for t in all_tasks
if not self.bpmn_process_instance._is_engine_task(t.task_spec) if not self.bpmn_process_instance._is_engine_task(t.task_spec)
and t.state in [TaskState.COMPLETED, TaskState.CANCELLED] and t.state in [TaskState.COMPLETED, TaskState.CANCELLED]
] ]
def get_all_waiting_tasks(self) -> list[SpiffTask]: def get_all_waiting_tasks(self) -> list[SpiffTask]:
@ -1213,7 +1214,7 @@ class ProcessInstanceProcessor:
@classmethod @classmethod
def get_task_by_bpmn_identifier( def get_task_by_bpmn_identifier(
cls, bpmn_task_identifier: str, bpmn_process_instance: BpmnWorkflow cls, bpmn_task_identifier: str, bpmn_process_instance: BpmnWorkflow
) -> Optional[SpiffTask]: ) -> Optional[SpiffTask]:
"""Get_task_by_id.""" """Get_task_by_id."""
all_tasks = bpmn_process_instance.get_tasks(TaskState.ANY_MASK) all_tasks = bpmn_process_instance.get_tasks(TaskState.ANY_MASK)