From 1e95836f54904d8f269248924d98379193a9430f Mon Sep 17 00:00:00 2001 From: Nicola Soranzo Date: Thu, 7 Mar 2024 13:12:40 +0000 Subject: [PATCH] Split ``raw_to_galaxy()`` trans requirement to app and history Marginal improvement, but might be nice not to be bound to `trans.history`. Co-authored-by: Marius van den Beek --- lib/galaxy/tools/parameters/basic.py | 26 ++++++++++++++------------ lib/galaxy/workflow/modules.py | 2 +- lib/galaxy/workflow/run.py | 6 ++++-- 3 files changed, 19 insertions(+), 15 deletions(-) diff --git a/lib/galaxy/tools/parameters/basic.py b/lib/galaxy/tools/parameters/basic.py index b5a8c30a8754..35ab932a3ec4 100644 --- a/lib/galaxy/tools/parameters/basic.py +++ b/lib/galaxy/tools/parameters/basic.py @@ -65,7 +65,12 @@ if TYPE_CHECKING: from sqlalchemy.orm import Session + from galaxy.model import ( + History, + HistoryItem, + ) from galaxy.security.idencoding import IdEncodingHelper + from galaxy.structured_app import MinimalApp log = logging.getLogger(__name__) @@ -2116,7 +2121,7 @@ def from_json(self, value, trans, other_values=None): raise ParameterValueError("specify a dataset of the required format / build for parameter", self.name) if value in [None, "None", ""]: if self.default_object: - return raw_to_galaxy(trans, self.default_object) + return raw_to_galaxy(trans.app, trans.history, self.default_object) return None if isinstance(value, MutableMapping) and "values" in value: value = self.to_python(value, trans.app) @@ -2456,7 +2461,7 @@ def from_json(self, value, trans, other_values=None): raise ParameterValueError("specify a dataset collection of the correct type", self.name) if value in [None, "None"]: if self.default_object: - return raw_to_galaxy(trans, self.default_object) + return raw_to_galaxy(trans.app, trans.history, self.default_object) return None if isinstance(value, MutableMapping) and "values" in value: value = self.to_python(value, trans.app) @@ -2672,10 +2677,7 @@ def to_text(self, value): # Code from CWL branch to massage in order to be shared across tools and workflows, # and for CWL artifacts as well as Galaxy ones. -def raw_to_galaxy(trans, as_dict_value): - app = trans.app - history = trans.history - +def raw_to_galaxy(app: "MinimalApp", history: "History", as_dict_value: Dict[str, Any]) -> "HistoryItem": object_class = as_dict_value["class"] if object_class == "File": # TODO: relative_to = "/" @@ -2714,15 +2716,15 @@ def raw_to_galaxy(trans, as_dict_value): dbkey="?", dataset=dataset, flush=False, - sa_session=trans.sa_session, + sa_session=app.model.session, ) primary_data.state = Dataset.states.DEFERRED permissions = app.security_agent.history_get_default_permissions(history) app.security_agent.set_all_dataset_permissions(primary_data.dataset, permissions, new=True, flush=False) - trans.sa_session.add(primary_data) + app.model.session.add(primary_data) history.stage_addition(primary_data) history.add_pending_items() - trans.sa_session.flush() + app.model.session.flush() return primary_data else: name = as_dict_value.get("name") @@ -2741,7 +2743,7 @@ def write_elements_to_collection(has_elements, collection_builder): element_class = element_dict["class"] identifier = element_dict["identifier"] if element_class == "File": - hda = raw_to_galaxy(trans, element_dict) + hda = raw_to_galaxy(app, history, element_dict) collection_builder.add_dataset(identifier, hda) else: subcollection_builder = collection_builder.get_level(identifier) @@ -2750,8 +2752,8 @@ def write_elements_to_collection(has_elements, collection_builder): collection_builder = builder.BoundCollectionBuilder(collection) write_elements_to_collection(as_dict_value, collection_builder) collection_builder.populate() - trans.sa_session.add(hdca) - trans.sa_session.flush() + app.model.session.add(hdca) + app.model.session.flush() return hdca diff --git a/lib/galaxy/workflow/modules.py b/lib/galaxy/workflow/modules.py index a6138d2588d2..7783cfb40f05 100644 --- a/lib/galaxy/workflow/modules.py +++ b/lib/galaxy/workflow/modules.py @@ -942,7 +942,7 @@ def execute( if input_value is None: default_value = step.get_input_default_value(NO_REPLACEMENT) if default_value is not NO_REPLACEMENT: - input_value = raw_to_galaxy(trans, default_value) + input_value = raw_to_galaxy(trans.app, trans.history, default_value) step_outputs = dict(output=input_value) diff --git a/lib/galaxy/workflow/run.py b/lib/galaxy/workflow/run.py index 4d1c024fbdd7..c008848a3ee3 100644 --- a/lib/galaxy/workflow/run.py +++ b/lib/galaxy/workflow/run.py @@ -45,6 +45,7 @@ if TYPE_CHECKING: from galaxy.model import ( + HistoryItem, Workflow, WorkflowOutput, WorkflowStep, @@ -422,6 +423,7 @@ def replacement_for_input(self, trans, step: "WorkflowStep", input_dict: Dict[st modules.NoReplacement, model.DatasetCollectionInstance, List[model.DatasetCollectionInstance], + "HistoryItem", ] = modules.NO_REPLACEMENT prefixed_name = input_dict["name"] multiple = input_dict["multiple"] @@ -446,7 +448,7 @@ def replacement_for_input(self, trans, step: "WorkflowStep", input_dict: Dict[st for step_input in step.inputs: if step_input.name == prefixed_name and step_input.default_value_set: if is_data: - replacement = raw_to_galaxy(trans, step_input.default_value) + replacement = raw_to_galaxy(trans.app, trans.history, step_input.default_value) return replacement def replacement_for_connection(self, connection: "WorkflowStepConnection", is_data: bool = True): @@ -713,7 +715,7 @@ def subworkflow_progress( ) def raw_to_galaxy(self, value: dict): - return raw_to_galaxy(self.module_injector.trans, value) + return raw_to_galaxy(self.module_injector.trans.app, self.module_injector.trans.history, value) def _recover_mapping(self, step_invocation: WorkflowInvocationStep) -> None: try: