Skip to content

Commit

Permalink
Refactor RuntimeValue utils for re-use
Browse files Browse the repository at this point in the history
Moving this to tools.parameters.workflow_utils avoids a circular
dependency when importing is_runtime_value in
tools.parameters.dynamic_options.
  • Loading branch information
mvdbeek committed Apr 4, 2024
1 parent d22ed48 commit 2ca4b2e
Show file tree
Hide file tree
Showing 16 changed files with 81 additions and 61 deletions.
6 changes: 4 additions & 2 deletions lib/galaxy/managers/workflows.py
Original file line number Diff line number Diff line change
Expand Up @@ -90,12 +90,14 @@
visit_input_values,
)
from galaxy.tools.parameters.basic import (
ConnectedValue,
DataCollectionToolParameter,
DataToolParameter,
)
from galaxy.tools.parameters.workflow_utils import (
ConnectedValue,
RuntimeValue,
workflow_building_modes,
)
from galaxy.tools.parameters.workflow_building_modes import workflow_building_modes
from galaxy.util.hash_util import md5_hash_str
from galaxy.util.json import (
safe_dumps,
Expand Down
2 changes: 1 addition & 1 deletion lib/galaxy/tools/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,7 @@
)
from galaxy.tools.parameters.input_translation import ToolInputTranslator
from galaxy.tools.parameters.meta import expand_meta_parameters
from galaxy.tools.parameters.workflow_building_modes import workflow_building_modes
from galaxy.tools.parameters.workflow_utils import workflow_building_modes
from galaxy.tools.parameters.wrapped_json import json_wrap
from galaxy.tools.test import parse_tests
from galaxy.util import (
Expand Down
2 changes: 1 addition & 1 deletion lib/galaxy/tools/actions/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,9 +33,9 @@
from galaxy.tools.parameters.basic import (
DataCollectionToolParameter,
DataToolParameter,
RuntimeValue,
SelectToolParameter,
)
from galaxy.tools.parameters.workflow_utils import RuntimeValue
from galaxy.tools.parameters.wrapped import (
LegacyUnprefixedDict,
WrappedParameters,
Expand Down
2 changes: 1 addition & 1 deletion lib/galaxy/tools/execute.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@
on_text_for_names,
ToolExecutionCache,
)
from galaxy.tools.parameters.basic import is_runtime_value
from galaxy.tools.parameters.workflow_utils import is_runtime_value

if typing.TYPE_CHECKING:
from galaxy.tools import Tool
Expand Down
6 changes: 4 additions & 2 deletions lib/galaxy/tools/parameters/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,7 @@
from .basic import (
DataCollectionToolParameter,
DataToolParameter,
is_runtime_value,
ParameterValueError,
runtime_to_json,
SelectToolParameter,
ToolParameter,
)
Expand All @@ -29,6 +27,10 @@
Section,
UploadDataset,
)
from .workflow_utils import (
is_runtime_value,
runtime_to_json,
)
from .wrapped import flat_to_nested_state

REPLACE_ON_TRUTHY = object()
Expand Down
44 changes: 7 additions & 37 deletions lib/galaxy/tools/parameters/basic.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@
from galaxy.model.dataset_collections import builder
from galaxy.schema.fetch_data import FilesPayload
from galaxy.tool_util.parser import get_input_source as ensure_input_source
from galaxy.tools.parameters.workflow_building_modes import workflow_building_modes
from galaxy.tools.parameters.workflow_utils import workflow_building_modes
from galaxy.util import (
sanitize_param,
string_as_bool,
Expand All @@ -61,6 +61,12 @@
)
from .dataset_matcher import get_dataset_matcher_factory
from .sanitize import ToolParameterSanitizer
from .workflow_utils import (
is_runtime_value,
runtime_to_json,
runtime_to_object,
RuntimeValue,
)

if TYPE_CHECKING:
from sqlalchemy.orm import Session
Expand All @@ -87,12 +93,6 @@ def contains_workflow_parameter(value, search=False):
return False


def is_runtime_value(value):
return isinstance(value, RuntimeValue) or (
isinstance(value, MutableMapping) and value.get("__class__") in ["RuntimeValue", "ConnectedValue"]
)


def is_runtime_context(trans, other_values):
if trans.workflow_building_mode:
return True
Expand Down Expand Up @@ -2777,36 +2777,6 @@ def write_elements_to_collection(has_elements, collection_builder):
)


def runtime_to_json(runtime_value):
if isinstance(runtime_value, ConnectedValue) or (
isinstance(runtime_value, MutableMapping) and runtime_value["__class__"] == "ConnectedValue"
):
return {"__class__": "ConnectedValue"}
else:
return {"__class__": "RuntimeValue"}


def runtime_to_object(runtime_value):
if isinstance(runtime_value, ConnectedValue) or (
isinstance(runtime_value, MutableMapping) and runtime_value["__class__"] == "ConnectedValue"
):
return ConnectedValue()
else:
return RuntimeValue()


class RuntimeValue:
"""
Wrapper to note a value that is not yet set, but will be required at runtime.
"""


class ConnectedValue(RuntimeValue):
"""
Wrapper to note a value that is not yet set, but will be inferred from a connection.
"""


def history_item_dict_to_python(value, app, name):
if isinstance(value, MutableMapping) and "src" in value:
if value["src"] not in ("hda", "dce", "ldda", "hdca"):
Expand Down
7 changes: 6 additions & 1 deletion lib/galaxy/tools/parameters/dynamic_options.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,10 @@
User,
)
from galaxy.tools.expressions import do_eval
from galaxy.tools.parameters.workflow_building_modes import workflow_building_modes
from galaxy.tools.parameters.workflow_utils import (
is_runtime_value,
workflow_building_modes,
)
from galaxy.util import (
Element,
string_as_bool,
Expand Down Expand Up @@ -969,6 +972,8 @@ def _get_ref_data(other_values, ref_name):
list,
),
):
if is_runtime_value(ref):
return []
raise ValueError
if isinstance(ref, DatasetCollectionElement) and ref.hda:
ref = ref.hda
Expand Down
4 changes: 0 additions & 4 deletions lib/galaxy/tools/parameters/workflow_building_modes.py

This file was deleted.

43 changes: 43 additions & 0 deletions lib/galaxy/tools/parameters/workflow_utils.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
from collections.abc import MutableMapping


class workflow_building_modes:
DISABLED = False
ENABLED = True
USE_HISTORY = 1


def runtime_to_json(runtime_value):
if isinstance(runtime_value, ConnectedValue) or (
isinstance(runtime_value, MutableMapping) and runtime_value["__class__"] == "ConnectedValue"
):
return {"__class__": "ConnectedValue"}
else:
return {"__class__": "RuntimeValue"}


def runtime_to_object(runtime_value):
if isinstance(runtime_value, ConnectedValue) or (
isinstance(runtime_value, MutableMapping) and runtime_value["__class__"] == "ConnectedValue"
):
return ConnectedValue()
else:
return RuntimeValue()


class RuntimeValue:
"""
Wrapper to note a value that is not yet set, but will be required at runtime.
"""


class ConnectedValue(RuntimeValue):
"""
Wrapper to note a value that is not yet set, but will be inferred from a connection.
"""


def is_runtime_value(value):
return isinstance(value, RuntimeValue) or (
isinstance(value, MutableMapping) and value.get("__class__") in ["RuntimeValue", "ConnectedValue"]
)
2 changes: 1 addition & 1 deletion lib/galaxy/tools/recommendations.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
import yaml

from galaxy.tools.parameters import populate_state
from galaxy.tools.parameters.workflow_building_modes import workflow_building_modes
from galaxy.tools.parameters.workflow_utils import workflow_building_modes
from galaxy.util import DEFAULT_SOCKET_TIMEOUT
from galaxy.workflow.modules import module_factory

Expand Down
2 changes: 1 addition & 1 deletion lib/galaxy/webapps/galaxy/api/workflows.py
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@
from galaxy.tool_shed.galaxy_install.install_manager import InstallRepositoryManager
from galaxy.tools import recommendations
from galaxy.tools.parameters import populate_state
from galaxy.tools.parameters.workflow_building_modes import workflow_building_modes
from galaxy.tools.parameters.workflow_utils import workflow_building_modes
from galaxy.util.sanitize_html import sanitize_html
from galaxy.version import VERSION
from galaxy.web import (
Expand Down
2 changes: 1 addition & 1 deletion lib/galaxy/webapps/galaxy/controllers/workflow.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
)
from galaxy.model.base import transaction
from galaxy.model.item_attrs import UsesItemRatings
from galaxy.tools.parameters.workflow_building_modes import workflow_building_modes
from galaxy.tools.parameters.workflow_utils import workflow_building_modes
from galaxy.util import FILENAME_VALID_CHARS
from galaxy.util.sanitize_html import sanitize_html
from galaxy.web import url_for
Expand Down
10 changes: 6 additions & 4 deletions lib/galaxy/workflow/modules.py
Original file line number Diff line number Diff line change
Expand Up @@ -68,16 +68,13 @@
BaseDataToolParameter,
BooleanToolParameter,
ColorToolParameter,
ConnectedValue,
DataCollectionToolParameter,
DataToolParameter,
FloatToolParameter,
HiddenToolParameter,
IntegerToolParameter,
is_runtime_value,
parameter_types,
raw_to_galaxy,
runtime_to_json,
SelectToolParameter,
TextToolParameter,
)
Expand All @@ -86,7 +83,12 @@
ConditionalWhen,
)
from galaxy.tools.parameters.history_query import HistoryQuery
from galaxy.tools.parameters.workflow_building_modes import workflow_building_modes
from galaxy.tools.parameters.workflow_utils import (
ConnectedValue,
is_runtime_value,
runtime_to_json,
workflow_building_modes,
)
from galaxy.tools.parameters.wrapped import make_dict_copy
from galaxy.util import (
listify,
Expand Down
4 changes: 2 additions & 2 deletions lib/galaxy/workflow/refactor/execute.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,9 @@

from galaxy.exceptions import RequestParameterInvalidException
from galaxy.tools.parameters import visit_input_values
from galaxy.tools.parameters.basic import (
from galaxy.tools.parameters.basic import contains_workflow_parameter
from galaxy.tools.parameters.workflow_utils import (
ConnectedValue,
contains_workflow_parameter,
runtime_to_json,
)
from .schema import (
Expand Down
2 changes: 1 addition & 1 deletion test/integration/test_workflow_refactoring.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
WorkflowStepConnection,
)
from galaxy.model.base import transaction
from galaxy.tools.parameters.workflow_building_modes import workflow_building_modes
from galaxy.tools.parameters.workflow_utils import workflow_building_modes
from galaxy.workflow.refactor.schema import RefactorActionExecutionMessageTypeEnum
from galaxy_test.base.populators import WorkflowPopulator
from galaxy_test.base.uses_shed_api import UsesShedApi
Expand Down
4 changes: 2 additions & 2 deletions test/unit/app/tools/test_select_parameters.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

from galaxy import model
from galaxy.model.base import transaction
from galaxy.tools.parameters import basic
from galaxy.tools.parameters.workflow_utils import RuntimeValue
from .util import BaseParameterTestCase


Expand Down Expand Up @@ -36,7 +36,7 @@ def test_unvalidated_datasets(self):
self.options_xml = """<options><filter type="data_meta" ref="input_bam" key="dbkey"/></options>"""
self.trans.workflow_building_mode = True
assert isinstance(
self.param.from_json(model.HistoryDatasetAssociation(), self.trans, {"input_bam": basic.RuntimeValue()}),
self.param.from_json(model.HistoryDatasetAssociation(), self.trans, {"input_bam": RuntimeValue()}),
model.HistoryDatasetAssociation,
)

Expand Down

0 comments on commit 2ca4b2e

Please sign in to comment.