Skip to content

Commit

Permalink
✨ NEW: Add local config in .samudra
Browse files Browse the repository at this point in the history
  • Loading branch information
Thaza-Kun committed Oct 21, 2022
1 parent 740d38a commit 79b6a76
Show file tree
Hide file tree
Showing 5 changed files with 74 additions and 50 deletions.
1 change: 1 addition & 0 deletions samudra/cli/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
from . import database
47 changes: 47 additions & 0 deletions samudra/cli/database.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
from pathlib import Path

import typer
from typer import Typer
from rich import print

from samudra.models import create_tables
from samudra.conf.local import save_db, list_db
from samudra.conf.database.options import DatabaseEngine
from samudra.conf.database.core import get_database

app = Typer()


@app.command()
def create(
name: str = typer.Argument(..., rich_help_panel="Name of database."),
path: str = typer.Option(".", rich_help_panel="Path to store database (SQLite)"),
experimental: bool = typer.Option(
False, rich_help_panel="Include experimental tables"
),
engine: DatabaseEngine = typer.Option(
DatabaseEngine.SQLite.value,
"--engine",
"-e",
rich_help_panel="Engine to use for database.",
),
) -> None:
"""Creates a new database"""
database = get_database(db_name=name, engine=engine, path=path)
tables = create_tables(
database=database,
auth=False,
experimental=experimental,
)
save_db(db_name=name, path=Path(path))
print(
f"`samudra.db` has been created in {Path(path, name).resolve()} with the following tables:"
)
[print("-", table) for table in tables]


@app.command()
def list():
"""Lists available databases"""
# TODO Print as tables
print(list_db()["databases"])
23 changes: 23 additions & 0 deletions samudra/conf/local.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
from pathlib import Path
from typing import List, Dict

import pytomlpp as toml

HOME: Path = Path("~")

dotconfig = Path(HOME, ".samudra").expanduser()

if not dotconfig.exists():
dotconfig.mkdir()


def save_db(db_name: str, path: Path):
databases: List[Dict] = list()
databases.append({"name": db_name, "path": f"{path.resolve()}"})
with open(Path(dotconfig, "databases.toml"), mode="a") as f:
f.write(toml.dumps({"databases": databases}))


def list_db():
with open(Path(dotconfig, "databases.toml"), mode="r") as f:
return toml.load(f)
50 changes: 2 additions & 48 deletions samudra/main.py
Original file line number Diff line number Diff line change
@@ -1,54 +1,8 @@
from pathlib import Path

import typer
from typer import Typer
from rich import print

from samudra.models import create_tables
from samudra.conf.database.options import DatabaseEngine
from samudra.conf.database.core import get_database
from samudra.cli import database

app = Typer()


@app.command()
def new(
name: str = typer.Argument(..., rich_help_panel="Name of database."),
path: str = typer.Option(".", rich_help_panel="Path to store database (SQLite)"),
experimental: bool = typer.Option(
False, rich_help_panel="Include experimental tables"
),
engine: DatabaseEngine = typer.Option(
DatabaseEngine.SQLite.value,
"--engine",
"-e",
rich_help_panel="Engine to use for database.",
),
) -> None:
"""Creates a new database"""
if path == "." and DatabaseEngine[engine] == DatabaseEngine.SQLite:
db_path = input(
"Please specify the folder to store database. Database will be stored in 'path/name/samudra.db'\nDefaults to '.':"
)
else:
db_path = path
database = get_database(db_name=name, engine=engine, path=db_path)
tables = create_tables(
database=database,
auth=False,
experimental=experimental,
)
# TODO Jot this down in some sort of internal list
print(
f"`samudra.db` has been created in {Path(db_path, name).resolve()} with the following tables:"
)
[print("-", table) for table in tables]


@app.command()
def newer():
pass

app.add_typer(database.app, name="db")

if __name__ == "__main__":
app()
3 changes: 1 addition & 2 deletions samudra/models/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
- [BaseStrictDataTable][samudra.models.base.BaseStrictDataTable]
"""
from collections import defaultdict
from dataclasses import field
from typing import List, Dict, Tuple, Type

import peewee
Expand Down Expand Up @@ -61,7 +60,7 @@ def connects_to(cls, other: BaseDataTable, through: BaseRelationshipTable) -> No
other (BaseDataTable): The data to attach the connection
through (BaseRelationshipTable): The bridge that holds the many-to-many connections
"""
cls.connection_table: Dict[str, Type[BaseRelationshipTable]] = defaultdict(
cls.connection_table: Dict[str, BaseRelationshipTable] = defaultdict(
BaseRelationshipTable
)
cls.connection_table[other._meta.table_name] = through
Expand Down

0 comments on commit 79b6a76

Please sign in to comment.