Skip to content

Commit

Permalink
ENH: initial skeleton for Client and Backend
Browse files Browse the repository at this point in the history
  • Loading branch information
tangkong committed May 1, 2024
1 parent 707f995 commit eb124f4
Show file tree
Hide file tree
Showing 3 changed files with 86 additions and 0 deletions.
Empty file added superscore/backends/__init__.py
Empty file.
37 changes: 37 additions & 0 deletions superscore/backends/core.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
"""
Base superscore data storage backend interface
"""
from typing import Any, Generator
from uuid import UUID

from ..model import Entry


class _Backend:
"""
Base class for data storage backend.
"""
def get_entry(self, meta_id: UUID) -> Entry:
"""Get entry with ``meta_id``."""
raise NotImplementedError

def save_entry(self, entry: Entry):
"""Save ``entry`` into the database"""
raise NotImplementedError

def delete_entry(self, entry: Entry) -> None:
"""
Delete ``entry`` from the system (all instances)
"""
raise NotImplementedError

def update_entry(self, entry: Entry) -> None:
"""
Update ``entry`` in the backend.
Throws BackendError if ``entry`` does not already exist
"""
raise NotImplementedError

def search(self, **search_kwargs) -> Generator[Any, None, None]:
"""Yield a Entry objects corresponding matching ``search_kwargs``"""
raise NotImplementedError
49 changes: 49 additions & 0 deletions superscore/client.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
"""Client for superscore. Used for programmatic interactions with superscore"""
from typing import Any, List

from superscore.backends.core import _Backend
from superscore.model import Entry


class Client:
backend: _Backend

def __init__(self, backend=None, **kwargs) -> None:
# if backend is None, startup default filestore backend
return

@classmethod
def from_config(cls, cfg=None):
raise NotImplementedError

def search(self, **post) -> List[Entry]:
"""Search by key-value pair. Can search by any field, including id"""
return self.backend.search(**post)

def save(self, entry: Entry):
"""Save information in ``entry`` to database"""
# validate entry is valid
self.backend.save_entry(entry)

def delete(self, entry: Entry) -> None:
"""Remove item from backend, depending on backend"""
# check for references to ``entry`` in other objects?
self.backend.delete_entry(entry)

def compare(self, entry_l: Entry, entry_r: Entry) -> Any:
"""Compare two entries. Should be of same type, and return a diff"""
raise NotImplementedError

def apply(self, entry: Entry):
"""Apply settings found in ``entry``. If no values found, no-op"""
raise NotImplementedError

def validate(self, entry: Entry):
"""
Validate ``entry`` is properly formed and able to be inserted into
the backend. Includes checks the following:
- dataclass is valid
- reachable from root
- references are not cyclical, and type-correct
"""
raise NotImplementedError

0 comments on commit eb124f4

Please sign in to comment.