-
Notifications
You must be signed in to change notification settings - Fork 1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
10 changed files
with
210 additions
and
84 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
from pydantic import ( | ||
BaseModel, | ||
ConfigDict, | ||
) | ||
|
||
from tool_shed.managers.model_cache import ( | ||
hash_model, | ||
ModelCache, | ||
) | ||
|
||
|
||
class Moo(BaseModel): | ||
foo: int | ||
|
||
|
||
class MooLike(BaseModel): | ||
model_config = ConfigDict(title="Moo") | ||
foo: int | ||
|
||
|
||
class NewMoo(BaseModel): | ||
model_config = ConfigDict(title="Moo") | ||
foo: int | ||
new_prop: str | ||
|
||
|
||
def test_hash(): | ||
hash_moo_1 = hash_model(Moo) | ||
hash_moo_2 = hash_model(Moo) | ||
assert hash_moo_1 == hash_moo_2 | ||
|
||
|
||
def test_hash_by_value(): | ||
hash_moo_1 = hash_model(Moo) | ||
hash_moo_like = hash_model(MooLike) | ||
assert hash_moo_1 == hash_moo_like | ||
|
||
|
||
def test_hash_different_on_updates(): | ||
hash_moo_1 = hash_model(Moo) | ||
hash_moo_new = hash_model(NewMoo) | ||
assert hash_moo_1 != hash_moo_new | ||
|
||
|
||
def cache_dict(tmp_path): | ||
model_cache = ModelCache(tmp_path) | ||
assert not model_cache.has_cached_entry_for(Moo, "moo", "1.0") | ||
assert None is model_cache.get_cache_entry_for(Moo, "moo", "1.0") | ||
model_cache.insert_cache_entry_for(Moo(foo=4), "moo", "1.0") | ||
moo = model_cache.get_cache_entry_for(Moo, "moo", "1.0") | ||
assert moo | ||
assert moo.foo == 4 | ||
assert model_cache.has_cached_entry_for(Moo, "moo", "1.0") |
Oops, something went wrong.