From f4c8da1d79cab86189032427c8ee88ef36629e32 Mon Sep 17 00:00:00 2001 From: John Chilton Date: Mon, 26 Aug 2024 10:41:00 -0400 Subject: [PATCH] Refactor input_models -> parameters (fix TODO, a lot cleaner) --- lib/galaxy/tool_util/models.py | 2 +- lib/galaxy/tool_util/parameters/case.py | 2 +- lib/galaxy/tool_util/parameters/factory.py | 4 +- lib/galaxy/tool_util/parameters/models.py | 21 +++++----- lib/galaxy/tool_util/parameters/state.py | 38 +++++++++---------- lib/galaxy/tool_util/parameters/visitor.py | 2 +- .../tool_util/unittest_utils/parameters.py | 2 +- lib/galaxy/tool_util/verify/parse.py | 2 +- 8 files changed, 36 insertions(+), 37 deletions(-) diff --git a/lib/galaxy/tool_util/models.py b/lib/galaxy/tool_util/models.py index 4ee1c8c70b9c..fc4341099ad2 100644 --- a/lib/galaxy/tool_util/models.py +++ b/lib/galaxy/tool_util/models.py @@ -47,7 +47,7 @@ def parse_tool(tool_source: ToolSource) -> ParsedTool: version = tool_source.parse_version() name = tool_source.parse_name() description = tool_source.parse_description() - inputs = input_models_for_tool_source(tool_source).input_models + inputs = input_models_for_tool_source(tool_source).parameters outputs = from_tool_source(tool_source) citations = tool_source.parse_citations() license = tool_source.parse_license() diff --git a/lib/galaxy/tool_util/parameters/case.py b/lib/galaxy/tool_util/parameters/case.py index 15c029fc68ff..3c0459d033a3 100644 --- a/lib/galaxy/tool_util/parameters/case.py +++ b/lib/galaxy/tool_util/parameters/case.py @@ -299,6 +299,6 @@ def validate_test_cases_for_tool_source( test_cases: List[ToolSourceTest] = tool_source.parse_tests_to_dict()["tests"] results_by_test: List[TestCaseStateValidationResult] = [] for test_case in test_cases: - validation_result = test_case_validation(test_case, tool_parameter_bundle.input_models, profile) + validation_result = test_case_validation(test_case, tool_parameter_bundle.parameters, profile) results_by_test.append(validation_result) return results_by_test diff --git a/lib/galaxy/tool_util/parameters/factory.py b/lib/galaxy/tool_util/parameters/factory.py index de7d567458c5..a636b13a17e6 100644 --- a/lib/galaxy/tool_util/parameters/factory.py +++ b/lib/galaxy/tool_util/parameters/factory.py @@ -319,7 +319,7 @@ def _from_input_source_cwl(input_source: CwlInputSource) -> ToolParameterT: def input_models_from_json(json: List[Dict[str, Any]]) -> ToolParameterBundle: - return ToolParameterBundleModel(input_models=json) + return ToolParameterBundleModel(parameters=json) def tool_parameter_bundle_from_json(json: Dict[str, Any]) -> ToolParameterBundleModel: @@ -328,7 +328,7 @@ def tool_parameter_bundle_from_json(json: Dict[str, Any]) -> ToolParameterBundle def input_models_for_tool_source(tool_source: ToolSource) -> ToolParameterBundleModel: pages = tool_source.parse_input_pages() - return ToolParameterBundleModel(input_models=input_models_for_pages(pages)) + return ToolParameterBundleModel(parameters=input_models_for_pages(pages)) def input_models_for_pages(pages: PagesSource) -> List[ToolParameterT]: diff --git a/lib/galaxy/tool_util/parameters/models.py b/lib/galaxy/tool_util/parameters/models.py index f83b0bd0d9e0..976195ef440f 100644 --- a/lib/galaxy/tool_util/parameters/models.py +++ b/lib/galaxy/tool_util/parameters/models.py @@ -1164,12 +1164,11 @@ class ToolParameterModel(RootModel): class ToolParameterBundle(Protocol): """An object having a dictionary of input models (i.e. a 'Tool')""" - # TODO: rename to parameters to align with ConditionalWhen and Repeat. - input_models: List[ToolParameterT] + parameters: List[ToolParameterT] class ToolParameterBundleModel(BaseModel): - input_models: List[ToolParameterT] + parameters: List[ToolParameterT] def to_simple_model(input_parameter: Union[ToolParameterModel, ToolParameterT]) -> ToolParameterT: @@ -1181,9 +1180,9 @@ def to_simple_model(input_parameter: Union[ToolParameterModel, ToolParameterT]) def simple_input_models( - input_models: Union[List[ToolParameterModel], List[ToolParameterT]] + parameters: Union[List[ToolParameterModel], List[ToolParameterT]] ) -> Iterable[ToolParameterT]: - return [to_simple_model(m) for m in input_models] + return [to_simple_model(m) for m in parameters] def create_model_strict(*args, **kwd) -> Type[BaseModel]: @@ -1194,27 +1193,27 @@ def create_model_strict(*args, **kwd) -> Type[BaseModel]: def create_request_model(tool: ToolParameterBundle, name: str = "DynamicModelForTool") -> Type[BaseModel]: - return create_field_model(tool.input_models, name, "request") + return create_field_model(tool.parameters, name, "request") def create_request_internal_model(tool: ToolParameterBundle, name: str = "DynamicModelForTool") -> Type[BaseModel]: - return create_field_model(tool.input_models, name, "request_internal") + return create_field_model(tool.parameters, name, "request_internal") def create_job_internal_model(tool: ToolParameterBundle, name: str = "DynamicModelForTool") -> Type[BaseModel]: - return create_field_model(tool.input_models, name, "job_internal") + return create_field_model(tool.parameters, name, "job_internal") def create_test_case_model(tool: ToolParameterBundle, name: str = "DynamicModelForTool") -> Type[BaseModel]: - return create_field_model(tool.input_models, name, "test_case_xml") + return create_field_model(tool.parameters, name, "test_case_xml") def create_workflow_step_model(tool: ToolParameterBundle, name: str = "DynamicModelForTool") -> Type[BaseModel]: - return create_field_model(tool.input_models, name, "workflow_step") + return create_field_model(tool.parameters, name, "workflow_step") def create_workflow_step_linked_model(tool: ToolParameterBundle, name: str = "DynamicModelForTool") -> Type[BaseModel]: - return create_field_model(tool.input_models, name, "workflow_step_linked") + return create_field_model(tool.parameters, name, "workflow_step_linked") def create_field_model( diff --git a/lib/galaxy/tool_util/parameters/state.py b/lib/galaxy/tool_util/parameters/state.py index c21163d1e786..94eb48b5de52 100644 --- a/lib/galaxy/tool_util/parameters/state.py +++ b/lib/galaxy/tool_util/parameters/state.py @@ -39,8 +39,8 @@ def __init__(self, input_state: Dict[str, Any]): def _validate(self, pydantic_model: Type[BaseModel]) -> None: validate_against_model(pydantic_model, self.input_state) - def validate(self, input_models: HasToolParameters) -> None: - base_model = self.parameter_model_for(input_models) + def validate(self, parameters: HasToolParameters) -> None: + base_model = self.parameter_model_for(parameters) if base_model is None: raise NotImplementedError( f"Validating tool state against state representation {self.state_representation} is not implemented." @@ -53,17 +53,17 @@ def state_representation(self) -> StateRepresentationT: """Get state representation of the inputs.""" @classmethod - def parameter_model_for(cls, input_models: HasToolParameters) -> Type[BaseModel]: + def parameter_model_for(cls, parameters: HasToolParameters) -> Type[BaseModel]: bundle: ToolParameterBundle - if isinstance(input_models, list): - bundle = ToolParameterBundleModel(input_models=input_models) + if isinstance(parameters, list): + bundle = ToolParameterBundleModel(parameters=parameters) else: - bundle = input_models + bundle = parameters return cls._parameter_model_for(bundle) @classmethod @abstractmethod - def _parameter_model_for(cls, input_models: ToolParameterBundle) -> Type[BaseModel]: + def _parameter_model_for(cls, parameters: ToolParameterBundle) -> Type[BaseModel]: """Return a model type for this tool state kind.""" @@ -71,46 +71,46 @@ class RequestToolState(ToolState): state_representation: Literal["request"] = "request" @classmethod - def _parameter_model_for(cls, input_models: ToolParameterBundle) -> Type[BaseModel]: - return create_request_model(input_models) + def _parameter_model_for(cls, parameters: ToolParameterBundle) -> Type[BaseModel]: + return create_request_model(parameters) class RequestInternalToolState(ToolState): state_representation: Literal["request_internal"] = "request_internal" @classmethod - def _parameter_model_for(cls, input_models: ToolParameterBundle) -> Type[BaseModel]: - return create_request_internal_model(input_models) + def _parameter_model_for(cls, parameters: ToolParameterBundle) -> Type[BaseModel]: + return create_request_internal_model(parameters) class JobInternalToolState(ToolState): state_representation: Literal["job_internal"] = "job_internal" @classmethod - def _parameter_model_for(cls, input_models: ToolParameterBundle) -> Type[BaseModel]: - return create_job_internal_model(input_models) + def _parameter_model_for(cls, parameters: ToolParameterBundle) -> Type[BaseModel]: + return create_job_internal_model(parameters) class TestCaseToolState(ToolState): state_representation: Literal["test_case_xml"] = "test_case_xml" @classmethod - def _parameter_model_for(cls, input_models: ToolParameterBundle) -> Type[BaseModel]: + def _parameter_model_for(cls, parameters: ToolParameterBundle) -> Type[BaseModel]: # implement a test case model... - return create_test_case_model(input_models) + return create_test_case_model(parameters) class WorkflowStepToolState(ToolState): state_representation: Literal["workflow_step"] = "workflow_step" @classmethod - def _parameter_model_for(cls, input_models: ToolParameterBundle) -> Type[BaseModel]: - return create_workflow_step_model(input_models) + def _parameter_model_for(cls, parameters: ToolParameterBundle) -> Type[BaseModel]: + return create_workflow_step_model(parameters) class WorkflowStepLinkedToolState(ToolState): state_representation: Literal["workflow_step_linked"] = "workflow_step_linked" @classmethod - def _parameter_model_for(cls, input_models: ToolParameterBundle) -> Type[BaseModel]: - return create_workflow_step_linked_model(input_models) + def _parameter_model_for(cls, parameters: ToolParameterBundle) -> Type[BaseModel]: + return create_workflow_step_linked_model(parameters) diff --git a/lib/galaxy/tool_util/parameters/visitor.py b/lib/galaxy/tool_util/parameters/visitor.py index 363dd9298bc0..5b8e059f2895 100644 --- a/lib/galaxy/tool_util/parameters/visitor.py +++ b/lib/galaxy/tool_util/parameters/visitor.py @@ -34,7 +34,7 @@ def visit_input_values( no_replacement_value=VISITOR_NO_REPLACEMENT, ) -> Dict[str, Any]: return _visit_input_values( - simple_input_models(input_models.input_models), + simple_input_models(input_models.parameters), tool_state.input_state, callback=callback, no_replacement_value=no_replacement_value, diff --git a/lib/galaxy/tool_util/unittest_utils/parameters.py b/lib/galaxy/tool_util/unittest_utils/parameters.py index 07ec28ffcb70..906dc4e5cfcd 100644 --- a/lib/galaxy/tool_util/unittest_utils/parameters.py +++ b/lib/galaxy/tool_util/unittest_utils/parameters.py @@ -17,7 +17,7 @@ class ParameterBundle(ToolParameterBundle): def __init__(self, parameter: ToolParameterT): - self.input_models = [parameter] + self.parameters = [parameter] def parameter_bundle(parameter: ToolParameterT) -> ParameterBundle: diff --git a/lib/galaxy/tool_util/verify/parse.py b/lib/galaxy/tool_util/verify/parse.py index 52f1a9af052b..a3aee97eef0c 100644 --- a/lib/galaxy/tool_util/verify/parse.py +++ b/lib/galaxy/tool_util/verify/parse.py @@ -67,7 +67,7 @@ def parse_tool_test_descriptions( if validate_on_load: tool_parameter_bundle = input_models_for_tool_source(tool_source) try: - case_state(raw_test_dict, tool_parameter_bundle.input_models, profile, validate=True) + case_state(raw_test_dict, tool_parameter_bundle.parameters, profile, validate=True) except Exception as e: # TOOD: restrict types of validation exceptions a bit probably? validation_exception = e