2023-03-03 06:02:00 +00:00
|
|
|
from abc import ABC, abstractmethod
|
2023-07-12 15:14:22 +00:00
|
|
|
from typing import Callable, Generic, Optional, TypeVar
|
2022-12-01 05:33:20 +00:00
|
|
|
|
2023-09-24 05:19:57 +00:00
|
|
|
from pydantic import BaseModel
|
2023-03-03 06:02:00 +00:00
|
|
|
|
2023-09-24 05:19:57 +00:00
|
|
|
from invokeai.app.services.shared.pagination import PaginatedResults
|
2022-12-01 05:33:20 +00:00
|
|
|
|
2023-09-24 05:19:57 +00:00
|
|
|
T = TypeVar("T", bound=BaseModel)
|
2023-07-27 14:54:01 +00:00
|
|
|
|
2022-12-01 05:33:20 +00:00
|
|
|
|
|
|
|
class ItemStorageABC(ABC, Generic[T]):
|
|
|
|
_on_changed_callbacks: list[Callable[[T], None]]
|
|
|
|
_on_deleted_callbacks: list[Callable[[str], None]]
|
|
|
|
|
|
|
|
def __init__(self) -> None:
|
|
|
|
self._on_changed_callbacks = list()
|
|
|
|
self._on_deleted_callbacks = list()
|
|
|
|
|
|
|
|
"""Base item storage class"""
|
2023-03-03 06:02:00 +00:00
|
|
|
|
2022-12-01 05:33:20 +00:00
|
|
|
@abstractmethod
|
|
|
|
def get(self, item_id: str) -> T:
|
2023-07-12 15:14:22 +00:00
|
|
|
"""Gets the item, parsing it into a Pydantic model"""
|
|
|
|
pass
|
|
|
|
|
|
|
|
@abstractmethod
|
|
|
|
def get_raw(self, item_id: str) -> Optional[str]:
|
|
|
|
"""Gets the raw item as a string, skipping Pydantic parsing"""
|
2022-12-01 05:33:20 +00:00
|
|
|
pass
|
|
|
|
|
|
|
|
@abstractmethod
|
|
|
|
def set(self, item: T) -> None:
|
2023-07-12 15:14:22 +00:00
|
|
|
"""Sets the item"""
|
2022-12-01 05:33:20 +00:00
|
|
|
pass
|
|
|
|
|
|
|
|
@abstractmethod
|
|
|
|
def list(self, page: int = 0, per_page: int = 10) -> PaginatedResults[T]:
|
2023-07-12 15:14:22 +00:00
|
|
|
"""Gets a paginated list of items"""
|
2022-12-01 05:33:20 +00:00
|
|
|
pass
|
|
|
|
|
|
|
|
@abstractmethod
|
2023-03-03 06:02:00 +00:00
|
|
|
def search(self, query: str, page: int = 0, per_page: int = 10) -> PaginatedResults[T]:
|
2022-12-01 05:33:20 +00:00
|
|
|
pass
|
|
|
|
|
|
|
|
def on_changed(self, on_changed: Callable[[T], None]) -> None:
|
|
|
|
"""Register a callback for when an item is changed"""
|
|
|
|
self._on_changed_callbacks.append(on_changed)
|
|
|
|
|
|
|
|
def on_deleted(self, on_deleted: Callable[[str], None]) -> None:
|
|
|
|
"""Register a callback for when an item is deleted"""
|
|
|
|
self._on_deleted_callbacks.append(on_deleted)
|
|
|
|
|
|
|
|
def _on_changed(self, item: T) -> None:
|
|
|
|
for callback in self._on_changed_callbacks:
|
|
|
|
callback(item)
|
2023-03-03 06:02:00 +00:00
|
|
|
|
2022-12-01 05:33:20 +00:00
|
|
|
def _on_deleted(self, item_id: str) -> None:
|
|
|
|
for callback in self._on_deleted_callbacks:
|
|
|
|
callback(item_id)
|