-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Initial working Custom List Store + typos
- Loading branch information
Showing
14 changed files
with
276 additions
and
8 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
Empty file.
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,29 @@ | ||
""" | ||
mlte/custom_list/model.py | ||
Model implementation for a custom list. | ||
""" | ||
from __future__ import annotations | ||
|
||
from typing import List | ||
|
||
from mlte.model import BaseModel | ||
|
||
class CustomList(BaseModel): | ||
"""A model class representing a custom list.""" | ||
|
||
name: str | ||
"""An name to uniquely identify the list.""" | ||
|
||
entries: List[CustomListEntry] = [] | ||
"""A list of entries in the list.""" | ||
|
||
|
||
class CustomListEntry(BaseModel): | ||
"""A model class representing a custom list entry.""" | ||
|
||
namme: str | ||
"""A name to uniquely identify the entry.""" | ||
|
||
description: str | ||
"""A description of the the entry.""" |
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
Empty file.
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,29 @@ | ||
""" | ||
mlte/store/custom_list/factory.py | ||
Top-level functions for custom list store creation. | ||
""" | ||
|
||
from mlte.store.base import StoreType, StoreURI | ||
from mlte.store.custom_list.store import CustomListStore | ||
from mlte.store.custom_list.underlying.fs import FileSystemCustomListStore | ||
# from mlte.store.custom_list.underlying.memory import InMemoryCustomListStore | ||
# from mlte.store.custom_list.underlying.rdbs.store import RelationalDBCustomListStore | ||
|
||
def create_custom_list_store(uri: str) -> CustomListStore: | ||
""" | ||
Create a MLTE custom list store instance. | ||
:param uri: The URI for the store instance | ||
:return: The store instance | ||
""" | ||
parsed_uri = StoreURI.from_string(uri) | ||
if parsed_uri.type == StoreType.LOCAL_MEMORY: | ||
return InMemoryCustomListStore(parsed_uri) | ||
if parsed_uri.type == StoreType.RELATIONAL_DB: | ||
return RelationalDBCustomListStore(parsed_uri) | ||
if parsed_uri.type == StoreType.LOCAL_FILESYSTEM: | ||
return FileSystemCustomListStore(parsed_uri) | ||
else: | ||
raise Exception( | ||
f"Store can't be created, unknown or unsupported URI prefix received for uri {parsed_uri}" | ||
) |
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,31 @@ | ||
""" | ||
mlte/store/custom_list/store.py | ||
MLTE custom list store implementation | ||
""" | ||
|
||
from __future__ import annotations | ||
|
||
from mlte.store.base import Store, StoreSession | ||
|
||
# ----------------------------------------------------------------------------- | ||
# CustomListStore | ||
# ----------------------------------------------------------------------------- | ||
|
||
|
||
class CustomListStore(Store): | ||
""" | ||
An abstract custom list store | ||
""" | ||
|
||
def __init__(self, uri: StoreURI): | ||
"""Base constructor""" | ||
super().__init__(uri=uri) | ||
"""Store uri.""" | ||
|
||
def session(self) -> CustomListStoreSession: | ||
""" | ||
Return a session handle for the store instance. | ||
:return: The session handle | ||
""" | ||
raise NotImplementedError("Cannot get handle to abstract Store.") |
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,63 @@ | ||
""" | ||
mlte/store/custom_list/store_session.py | ||
MLTE custom list store interface implementation | ||
""" | ||
from __future__ import annotations | ||
|
||
from typing import List | ||
|
||
from mlte.store.base import ResourceMapper, StoreSession | ||
from mlte.custom_list.model import CustomList, CustomListEntry | ||
|
||
# ----------------------------------------------------------------------------- | ||
# CustomListStoreSession | ||
# ----------------------------------------------------------------------------- | ||
|
||
|
||
class CustomListStoreSession(StoreSession): | ||
"""The base class for all implementations of the MLTE custom list store session.""" | ||
|
||
custom_list_mapper: CustomListMapper | ||
"""Mapper for the custom list resource.""" | ||
|
||
custom_list_entry_mapper: CustomListEntryMapper | ||
"""Mapper for the custom list entry resource.""" | ||
|
||
|
||
class CustomListMapper(ResourceMapper): | ||
"""An interface for mapping CRUD actions to custom lists.""" | ||
|
||
def create(self, new_custom_list: CustomList) -> CustomList: | ||
raise NotImplementedError(self.NOT_IMPLEMENTED_ERROR_MSG) | ||
|
||
def edit(self, updated_custom_list: CustomList) -> CustomList: | ||
raise NotImplementedError(self.NOT_IMPLEMENTED_ERROR_MSG) | ||
|
||
def read(self, custom_list_name: str) -> CustomList: | ||
raise NotImplementedError(self.NOT_IMPLEMENTED_ERROR_MSG) | ||
|
||
def list(self) -> List[str]: | ||
raise NotImplementedError(self.NOT_IMPLEMENTED_ERROR_MSG) | ||
|
||
def delete(self, custom_list_name: str) -> CustomList: | ||
raise NotImplementedError(self.NOT_IMPLEMENTED_ERROR_MSG) | ||
|
||
|
||
class CustomListEntryMapper(ResourceMapper): | ||
"""An interface for mapping CRUD actions to custom list entries.""" | ||
|
||
def create(self, new_custom_list_entry: CustomListEntry) -> CustomListEntry: | ||
raise NotImplementedError(self.NOT_IMPLEMENTED_ERROR_MSG) | ||
|
||
def edit(self, updated_custom_list_entry: CustomListEntry) -> CustomListEntry: | ||
raise NotImplementedError(self.NOT_IMPLEMENTED_ERROR_MSG) | ||
|
||
def read(self, custom_list_entry_name: str) -> CustomListEntry: | ||
raise NotImplementedError(self.NOT_IMPLEMENTED_ERROR_MSG) | ||
|
||
def list(self) -> List[str]: | ||
raise NotImplementedError(self.NOT_IMPLEMENTED_ERROR_MSG) | ||
|
||
def delete(self, custom_list_entry_name: str) -> CustomListEntry: | ||
raise NotImplementedError(self.NOT_IMPLEMENTED_ERROR_MSG) |
Empty file.
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,103 @@ | ||
""" | ||
mlte/store/custom_list/underlying/fs.py | ||
Implementation of local file system custom list store. | ||
""" | ||
from __future__ import annotations | ||
|
||
from pathlib import Path | ||
|
||
from mlte.custom_list.model import CustomList, CustomListEntry | ||
from mlte.store.base import StoreURI | ||
from mlte.store.custom_list.store import CustomListStore | ||
from mlte.store.custom_list.store_session import CustomListStoreSession, CustomListMapper, CustomListEntryMapper | ||
from mlte.store.common.fs_storage import FileSystemStorage | ||
|
||
# ----------------------------------------------------------------------------- | ||
# FileSystemCustomListStore | ||
# ----------------------------------------------------------------------------- | ||
|
||
|
||
class FileSystemCustomListStore(CustomListStore): | ||
"""A local file system implementation of the MLTE custom list store.""" | ||
|
||
BASE_CUSTOM_LIST_FOLDER = "custom_lists" | ||
"""Base folder to store custom lists in.""" | ||
|
||
def __init__(self, uri: StoreURI) -> None: | ||
self.storage = FileSystemStorage( | ||
uri=uri, sub_folder=self.BASE_CUSTOM_LIST_FOLDER | ||
) | ||
"""Underlying storage.""" | ||
|
||
# Initialize defaults. | ||
super().__init__(uri=uri) | ||
|
||
def session(self) -> FileSystemCustomListStoreSession: | ||
""" | ||
Return a session handle for the store instance. | ||
:return: The session handle | ||
""" | ||
return FileSystemCustomListStoreSession(storage=self.storage) | ||
|
||
|
||
# ----------------------------------------------------------------------------- | ||
# FileSystemCustomLilstStoreSession | ||
# ----------------------------------------------------------------------------- | ||
|
||
|
||
class FileSystemCustomListStoreSession(CustomListStoreSession): | ||
"""A local file-system implementation of the MLTE custom list store.""" | ||
|
||
def __init__(self, storage: FileSystemStorage) -> None: | ||
self.custom_list_mapper = FileSystemCustomListMapper(storage) | ||
"""The mapper to custom list CRUD.""" | ||
|
||
self.custom_list_entry_mapper = FileSystemCustomListEntryMapper(storage) | ||
"""The mapper to custom list entry CRUD.""" | ||
|
||
def close(self) -> None: | ||
"""Close the session.""" | ||
# Closing a local FS session is a no-op. | ||
pass | ||
|
||
|
||
# ----------------------------------------------------------------------------- | ||
# FileSystemCustomListMappper | ||
# ----------------------------------------------------------------------------- | ||
|
||
|
||
class FileSystemCustomListMapper(CustomListMapper): | ||
"""FS mapper for the custom list resource.""" | ||
|
||
CUSTOM_LIST_FOLDER = "custom_lists" | ||
"""Subfolder for custom lists.""" | ||
|
||
def __init__( | ||
self, storage: FileSystemStorage | ||
) -> None: | ||
self.storage = storage.clone() | ||
"""A reference to underlying storage.""" | ||
|
||
self.storage.set_base_path( | ||
Path(FileSystemCustomListStore.BASE_CUSTOM_LIST_FOLDER, self.CUSTOM_LIST_FOLDER) | ||
) | ||
"""Set the subfodler for this resource.""" | ||
|
||
def create(self, custom_list: CustomList) -> CustomList: | ||
self.storage.ensure_resource_does_not_exist(custom_list.name) | ||
return self._write_entry(custom_list) | ||
|
||
def _write_entry(self, custom_list: CustomList) -> CustomList: | ||
"""Writes a entry to storage.""" | ||
self.storage.write_resource(custom_list.name, custom_list.model_dump()) | ||
return self._read_entry(custom_list.name) | ||
|
||
|
||
# ----------------------------------------------------------------------------- | ||
# FileSystemCustomListEntryMappper | ||
# ----------------------------------------------------------------------------- | ||
|
||
|
||
class FileSystemCustomListEntryMapper(CustomListEntryMapper): | ||
pass |
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