Skip to content

Commit

Permalink
Improved tool API.
Browse files Browse the repository at this point in the history
  • Loading branch information
jmchilton committed Jul 8, 2024
1 parent 66d2a66 commit 60e89b1
Show file tree
Hide file tree
Showing 13 changed files with 1,307 additions and 127 deletions.
1,029 changes: 1,022 additions & 7 deletions client/src/api/schema/schema.ts

Large diffs are not rendered by default.

6 changes: 3 additions & 3 deletions lib/galaxy/config/schemas/tool_shed_config_schema.yml
Original file line number Diff line number Diff line change
Expand Up @@ -102,12 +102,12 @@ mapping:
the repositories and tools within the Tool Shed given that you specify
the following two config options.
tool_state_cache_dir:
model_cache_dir:
type: str
default: database/tool_state_cache
default: database/model_cache
required: false
desc: |
Cache directory for tool state.
Cache directory for Pydantic model objects.
repo_name_boost:
type: float
Expand Down
25 changes: 18 additions & 7 deletions lib/galaxy/tool_util/parameters/state.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,10 @@
from typing import (
Any,
Dict,
List,
Optional,
Type,
Union,
)

from pydantic import BaseModel
Expand All @@ -17,9 +19,13 @@
create_request_model,
StateRepresentationT,
ToolParameterBundle,
ToolParameterBundleModel,
ToolParameterT,
validate_against_model,
)

HasToolParameters = Union[List[ToolParameterT], ToolParameterBundle]


class ToolState(ABC):
input_state: Dict[str, Any]
Expand All @@ -30,7 +36,7 @@ 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: ToolParameterBundle) -> None:
def validate(self, input_models: HasToolParameters) -> None:
base_model = self.parameter_model_for(input_models)
if base_model is None:
raise NotImplementedError(
Expand All @@ -44,31 +50,36 @@ def state_representation(self) -> StateRepresentationT:
"""Get state representation of the inputs."""

@classmethod
def parameter_model_for(cls, input_models: ToolParameterBundle) -> Optional[Type[BaseModel]]:
return None
def parameter_model_for(cls, input_models: HasToolParameters) -> Optional[Type[BaseModel]]:
bundle: ToolParameterBundle
if isinstance(input_models, list):
bundle = ToolParameterBundleModel(input_models=input_models)
else:
bundle = input_models
return cls._parameter_model_for(bundle)


class RequestToolState(ToolState):
state_representation: Literal["request"] = "request"

@classmethod
def parameter_model_for(cls, input_models: ToolParameterBundle) -> Type[BaseModel]:
def _parameter_model_for(cls, input_models: ToolParameterBundle) -> Type[BaseModel]:
return create_request_model(input_models)


class RequestInternalToolState(ToolState):
state_representation: Literal["request_internal"] = "request_internal"

@classmethod
def parameter_model_for(cls, input_models: ToolParameterBundle) -> Type[BaseModel]:
def _parameter_model_for(cls, input_models: ToolParameterBundle) -> Type[BaseModel]:
return create_request_internal_model(input_models)


class JobInternalToolState(ToolState):
state_representation: Literal["job_internal"] = "job_internal"

@classmethod
def parameter_model_for(cls, input_models: ToolParameterBundle) -> Type[BaseModel]:
def _parameter_model_for(cls, input_models: ToolParameterBundle) -> Type[BaseModel]:
# implement a job model...
return create_request_internal_model(input_models)

Expand All @@ -77,6 +88,6 @@ class TestCaseToolState(ToolState):
state_representation: Literal["test_case"] = "test_case"

@classmethod
def parameter_model_for(cls, input_models: ToolParameterBundle) -> Type[BaseModel]:
def _parameter_model_for(cls, input_models: ToolParameterBundle) -> Type[BaseModel]:
# implement a test case model...
return create_request_internal_model(input_models)
64 changes: 64 additions & 0 deletions lib/tool_shed/managers/model_cache.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
import json
import os
from typing import (
Any,
Dict,
Optional,
Type,
TypeVar,
)

from pydantic import BaseModel

from galaxy.util.hash_util import md5_hash_str

RAW_CACHED_JSON = Dict[str, Any]


def hash_model(model_class: Type[BaseModel]) -> str:
return md5_hash_str(json.dumps(model_class.model_json_schema()))


MODEL_HASHES: Dict[Type[BaseModel], str] = {}


M = TypeVar("M", bound=BaseModel)


def ensure_model_has_hash(model_class: Type[BaseModel]) -> None:
if model_class not in MODEL_HASHES:
MODEL_HASHES[model_class] = hash_model(model_class)


class ModelCache:
_cache_directory: str

def __init__(self, cache_directory: str):
if not os.path.exists(cache_directory):
os.makedirs(cache_directory)
self._cache_directory = cache_directory

def _cache_target(self, model_class: Type[M], tool_id: str, tool_version: str) -> str:
ensure_model_has_hash(model_class)
# consider breaking this into multiple directories...
cache_target = os.path.join(self._cache_directory, MODEL_HASHES[model_class], tool_id, tool_version)
return cache_target

def get_cache_entry_for(self, model_class: Type[M], tool_id: str, tool_version: str) -> Optional[M]:
cache_target = self._cache_target(model_class, tool_id, tool_version)
if not os.path.exists(cache_target):
return None
with open(cache_target) as f:
return model_class.model_validate(json.load(f))

def has_cached_entry_for(self, model_class: Type[M], tool_id: str, tool_version: str) -> bool:
cache_target = self._cache_target(model_class, tool_id, tool_version)
return os.path.exists(cache_target)

def insert_cache_entry_for(self, model_object: M, tool_id: str, tool_version: str) -> None:
cache_target = self._cache_target(model_object.__class__, tool_id, tool_version)
parent_directory = os.path.dirname(cache_target)
if not os.path.exists(parent_directory):
os.makedirs(parent_directory)
with open(cache_target, "w") as f:
json.dump(model_object.dict(), f)
42 changes: 0 additions & 42 deletions lib/tool_shed/managers/tool_state_cache.py

This file was deleted.

77 changes: 63 additions & 14 deletions lib/tool_shed/managers/tools.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@
Tuple,
)

from pydantic import BaseModel

from galaxy import exceptions
from galaxy.exceptions import (
InternalServerError,
Expand All @@ -21,13 +23,16 @@
)
from galaxy.tool_util.parameters import (
input_models_for_tool_source,
tool_parameter_bundle_from_json,
ToolParameterBundleModel,
ToolParameterT,
)
from galaxy.tool_util.parser import (
get_tool_source,
ToolSource,
)
from galaxy.tool_util.parser.interface import (
Citation,
XrefDict,
)
from galaxy.tools.stock import stock_tool_sources
from tool_shed.context import (
ProvidesRepositoriesContext,
Expand All @@ -41,6 +46,50 @@
STOCK_TOOL_SOURCES: Optional[Dict[str, Dict[str, ToolSource]]] = None


# parse the tool source with galaxy.util abstractions to provide a bit richer
# information about the tool than older tool shed abstractions.
class ParsedTool(BaseModel):
id: str
version: Optional[str]
name: str
description: Optional[str]
inputs: List[ToolParameterT]
citations: List[Citation]
license: Optional[str]
profile: Optional[str]
edam_operations: List[str]
edam_topics: List[str]
xrefs: List[XrefDict]


def _parse_tool(tool_source: ToolSource) -> ParsedTool:
id = tool_source.parse_id()
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
citations = tool_source.parse_citations()
license = tool_source.parse_license()
profile = tool_source.parse_profile()
edam_operations = tool_source.parse_edam_operations()
edam_topics = tool_source.parse_edam_topics()
xrefs = tool_source.parse_xrefs()

return ParsedTool(
id=id,
version=version,
name=name,
description=description,
profile=profile,
inputs=inputs,
license=license,
citations=citations,
edam_operations=edam_operations,
edam_topics=edam_topics,
xrefs=xrefs,
)


def search(trans: SessionRequestContext, q: str, page: int = 1, page_size: int = 10) -> dict:
"""
Perform the search over TS tools index.
Expand Down Expand Up @@ -97,23 +146,23 @@ def get_repository_metadata_tool_dict(
raise ObjectNotFound()


def tool_input_models_cached_for(
def parsed_tool_model_cached_for(
trans: ProvidesRepositoriesContext, trs_tool_id: str, tool_version: str, repository_clone_url: Optional[str] = None
) -> ToolParameterBundleModel:
tool_state_cache = trans.app.tool_state_cache
raw_json = tool_state_cache.get_cache_entry_for(trs_tool_id, tool_version)
if raw_json is not None:
return tool_parameter_bundle_from_json(raw_json)
bundle = tool_input_models_for(trans, trs_tool_id, tool_version, repository_clone_url=repository_clone_url)
tool_state_cache.insert_cache_entry_for(trs_tool_id, tool_version, bundle.dict())
return bundle
) -> ParsedTool:
model_cache = trans.app.model_cache
parsed_tool = model_cache.get_cache_entry_for(ParsedTool, trs_tool_id, tool_version)
if parsed_tool is not None:
return parsed_tool
parsed_tool = parsed_tool_model_for(trans, trs_tool_id, tool_version, repository_clone_url=repository_clone_url)
model_cache.insert_cache_entry_for(parsed_tool, trs_tool_id, tool_version)
return parsed_tool


def tool_input_models_for(
def parsed_tool_model_for(
trans: ProvidesRepositoriesContext, trs_tool_id: str, tool_version: str, repository_clone_url: Optional[str] = None
) -> ToolParameterBundleModel:
) -> ParsedTool:
tool_source = tool_source_for(trans, trs_tool_id, tool_version, repository_clone_url=repository_clone_url)
return input_models_for_tool_source(tool_source)
return _parse_tool(tool_source)


def tool_source_for(
Expand Down
4 changes: 2 additions & 2 deletions lib/tool_shed/structured_app.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
from galaxy.structured_app import BasicSharedApp

if TYPE_CHECKING:
from tool_shed.managers.tool_state_cache import ToolStateCache
from tool_shed.managers.model_cache import ModelCache
from tool_shed.repository_registry import Registry as RepositoryRegistry
from tool_shed.repository_types.registry import Registry as RepositoryTypesRegistry
from tool_shed.util.hgweb_config import HgWebConfigManager
Expand All @@ -17,4 +17,4 @@ class ToolShedApp(BasicSharedApp):
repository_registry: "RepositoryRegistry"
hgweb_config_manager: "HgWebConfigManager"
security_agent: "CommunityRBACAgent"
tool_state_cache: "ToolStateCache"
model_cache: "ModelCache"
17 changes: 8 additions & 9 deletions lib/tool_shed/webapp/api2/tools.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,12 @@
from galaxy.tool_util.parameters import (
RequestToolState,
to_json_schema_string,
ToolParameterBundleModel,
)
from tool_shed.context import SessionRequestContext
from tool_shed.managers.tools import (
parsed_tool_model_cached_for,
ParsedTool,
search,
tool_input_models_cached_for,
)
from tool_shed.managers.trs import (
get_tool,
Expand Down Expand Up @@ -144,17 +144,17 @@ def trs_get_versions(
return get_tool(trans, tool_id).versions

@router.get(
"/api/tools/{tool_id}/versions/{tool_version}/parameter_model",
"/api/tools/{tool_id}/versions/{tool_version}",
operation_id="tools__parameter_model",
summary="Return Galaxy's meta model description of the tool's inputs",
)
def tool_parameters_meta_model(
def show_tool(
self,
trans: SessionRequestContext = DependsOnTrans,
tool_id: str = TOOL_ID_PATH_PARAM,
tool_version: str = TOOL_VERSION_PATH_PARAM,
) -> ToolParameterBundleModel:
return tool_input_models_cached_for(trans, tool_id, tool_version)
) -> ParsedTool:
return parsed_tool_model_cached_for(trans, tool_id, tool_version)

@router.get(
"/api/tools/{tool_id}/versions/{tool_version}/parameter_request_schema",
Expand All @@ -168,6 +168,5 @@ def tool_state(
tool_id: str = TOOL_ID_PATH_PARAM,
tool_version: str = TOOL_VERSION_PATH_PARAM,
) -> Response:
return json_schema_response(
RequestToolState.parameter_model_for(tool_input_models_cached_for(trans, tool_id, tool_version))
)
parsed_tool = parsed_tool_model_cached_for(trans, tool_id, tool_version)
return json_schema_response(RequestToolState.parameter_model_for(parsed_tool.inputs))
Loading

0 comments on commit 60e89b1

Please sign in to comment.