-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* chores_lint_errors_and_path_issues * feature_db_py * fix_audio_issue * chores_lint_error
- Loading branch information
Serhii Ofii
authored
Sep 24, 2024
1 parent
be71261
commit 60ad27f
Showing
7 changed files
with
242 additions
and
1 deletion.
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 |
---|---|---|
@@ -1,4 +1,5 @@ | ||
"""it is entity for debugging""" | ||
|
||
import uvicorn | ||
|
||
from linguaphoto.main import app | ||
|
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,110 @@ | ||
"""Defines base tools for interacting with the database.""" | ||
|
||
import argparse | ||
import asyncio | ||
import logging | ||
from typing import AsyncGenerator, Literal, Self | ||
|
||
from linguaphoto.crud.base import TABLE_NAME, BaseCrud | ||
from linguaphoto.crud.collection import CollectionCrud | ||
from linguaphoto.crud.image import ImageCrud | ||
from linguaphoto.crud.user import UserCrud | ||
|
||
|
||
class Crud( | ||
CollectionCrud, | ||
ImageCrud, | ||
UserCrud, | ||
BaseCrud, | ||
): | ||
"""Composes the various CRUD classes into a single class.""" | ||
|
||
@classmethod | ||
async def get(cls) -> AsyncGenerator[Self, None]: | ||
async with cls() as crud: | ||
yield crud | ||
|
||
|
||
async def create_tables(crud: Crud | None = None, deletion_protection: bool = False) -> None: | ||
"""Initializes all of the database tables. | ||
Args: | ||
crud: The top-level CRUD class. | ||
deletion_protection: Whether to enable deletion protection on the tables. | ||
""" | ||
logging.basicConfig(level=logging.INFO) | ||
|
||
if crud is None: | ||
async with Crud() as new_crud: | ||
await create_tables(new_crud) | ||
|
||
else: | ||
gsis_set = crud.get_gsis() | ||
gsis: list[tuple[str, str, Literal["S", "N", "B"], Literal["HASH", "RANGE"]]] = [ | ||
(Crud.get_gsi_index_name(g), g, "S", "HASH") for g in gsis_set | ||
] | ||
|
||
await asyncio.gather( | ||
crud._create_dynamodb_table( | ||
name=TABLE_NAME, | ||
keys=[ | ||
("id", "S", "HASH"), | ||
], | ||
gsis=gsis, | ||
deletion_protection=deletion_protection, | ||
), | ||
crud._create_s3_bucket(), | ||
) | ||
|
||
|
||
async def delete_tables(crud: Crud | None = None) -> None: | ||
"""Deletes all of the database tables. | ||
Args: | ||
crud: The top-level CRUD class. | ||
""" | ||
logging.basicConfig(level=logging.INFO) | ||
|
||
if crud is None: | ||
async with Crud() as new_crud: | ||
await delete_tables(new_crud) | ||
|
||
else: | ||
await crud._delete_dynamodb_table(TABLE_NAME) | ||
await crud._delete_s3_bucket() | ||
|
||
|
||
async def populate_with_dummy_data(crud: Crud | None = None) -> None: | ||
"""Populates the database with dummy data. | ||
Args: | ||
crud: The top-level CRUD class. | ||
""" | ||
if crud is None: | ||
async with Crud() as new_crud: | ||
await populate_with_dummy_data(new_crud) | ||
|
||
else: | ||
raise NotImplementedError("This function is not yet implemented.") | ||
|
||
|
||
async def main() -> None: | ||
parser = argparse.ArgumentParser() | ||
parser.add_argument("action", choices=["create", "delete", "populate"]) | ||
args = parser.parse_args() | ||
|
||
async with Crud() as crud: | ||
match args.action: | ||
case "create": | ||
await create_tables(crud) | ||
case "delete": | ||
await delete_tables(crud) | ||
case "populate": | ||
await populate_with_dummy_data(crud) | ||
case _: | ||
raise ValueError(f"Invalid action: {args.action}") | ||
|
||
|
||
if __name__ == "__main__": | ||
# python -m store.app.db | ||
asyncio.run(main()) |
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,12 @@ | ||
"""Defines package-wide utility functions.""" | ||
|
||
from linguaphoto.settings import settings | ||
|
||
LOCALHOST_URLS = [ | ||
"http://127.0.0.1:3000", | ||
"http://localhost:3000", | ||
] | ||
|
||
|
||
def get_cors_origins() -> list[str]: | ||
return list({settings.homepage_url, *LOCALHOST_URLS}) |