-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
74 additions
and
50 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
from . import database |
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,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"]) |
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,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) |
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,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() |
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