generated from bit-bots/bitbots_template_repository
-
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.
Merge pull request #26 from bit-bots/feature/cli
Dataset CLI for db/import tasks
- Loading branch information
Showing
10 changed files
with
325 additions
and
61 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 |
---|---|---|
|
@@ -30,4 +30,4 @@ jobs: | |
run: poetry install | ||
|
||
- name: Run create-db | ||
run: poetry run create-db | ||
run: poetry run cli db create_schema |
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,73 @@ | ||
import sys | ||
from enum import Enum | ||
from pathlib import Path | ||
|
||
from tap import Tap | ||
|
||
from ddlitlab2024 import DB_PATH | ||
|
||
|
||
class ImportType(str, Enum): | ||
ROS_BAG = "rosbag" | ||
|
||
|
||
class CLICommand(str, Enum): | ||
DB = "db" | ||
IMPORT = "import" | ||
|
||
|
||
class DBArgs(Tap): | ||
create_schema: bool = False | ||
|
||
def configure(self) -> None: | ||
self.add_argument( | ||
"create_schema", | ||
type=bool, | ||
help="Create the base database schema, if it doesn't exist", | ||
nargs="?", | ||
) | ||
|
||
|
||
class ImportArgs(Tap): | ||
import_type: ImportType | ||
file: Path | ||
|
||
def configure(self) -> None: | ||
self.add_argument( | ||
"import-type", | ||
type=ImportType, | ||
help="Type of import to perform", | ||
) | ||
self.add_argument( | ||
"file", | ||
type=Path, | ||
help="File to import", | ||
) | ||
|
||
|
||
class CLIArgs(Tap): | ||
dry_run: bool = False | ||
db_path: str = DB_PATH # Path to the sqlite database file | ||
version: bool = False # if set print version and exit | ||
|
||
def __init__(self): | ||
super().__init__( | ||
description="ddlitlab dataset CLI", | ||
underscores_to_dashes=True, | ||
) | ||
|
||
def configure(self) -> None: | ||
self.add_subparsers(dest="command", help="Command to run") | ||
self.add_subparser(CLICommand.DB.value, DBArgs, help="Database management commands") | ||
self.add_subparser(CLICommand.IMPORT.value, ImportArgs, help="Import data into the database") | ||
|
||
def print_help_and_exit(self) -> None: | ||
self.print_help() | ||
sys.exit(0) | ||
|
||
def process_args(self) -> None: | ||
if self.command == CLICommand.DB: | ||
all_args = (self.create_schema,) | ||
|
||
if not any(all_args): | ||
self.print_help_and_exit() |
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,33 @@ | ||
from sqlalchemy import Engine, create_engine | ||
from sqlalchemy.orm import Session, sessionmaker | ||
|
||
from ddlitlab2024.dataset import logger | ||
from ddlitlab2024.dataset.models import Base | ||
|
||
|
||
class Database: | ||
def __init__(self, db_path: str): | ||
self.db_path = db_path | ||
self.engine: Engine = self._setup_sqlite() | ||
self.session: Session | None = None | ||
|
||
def _setup_sqlite(self) -> Engine: | ||
return create_engine(f"sqlite:///{self.db_path}") | ||
|
||
def _create_schema(self) -> None: | ||
logger.info("Creating database schema") | ||
Base.metadata.create_all(self.engine) | ||
logger.info("Database schema created") | ||
|
||
def create_session(self, create_schema: bool = True) -> Session: | ||
logger.info("Setting up database session") | ||
if create_schema: | ||
self._create_schema() | ||
return sessionmaker(bind=self.engine)() | ||
|
||
def close_session(self) -> None: | ||
if self.session: | ||
self.session.close() | ||
logger.info("Database session closed") | ||
else: | ||
logger.warning("No database session to close") |
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,2 @@ | ||
class CLIArgumentError(Exception): | ||
"""Raised when the configuration of CLI arguments is not valid and execution is impossible""" |
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,39 @@ | ||
#!/usr/bin/env python3 | ||
|
||
import os | ||
import sys | ||
|
||
from rich.console import Console | ||
|
||
from ddlitlab2024 import __version__ | ||
from ddlitlab2024.dataset import logger | ||
from ddlitlab2024.dataset.cli import CLIArgs, CLICommand | ||
from ddlitlab2024.dataset.db import Database | ||
|
||
err_console = Console(stderr=True) | ||
|
||
|
||
def main(): | ||
debug_mode = os.getenv("LOGLEVEL") == "DEBUG" | ||
|
||
try: | ||
logger.debug("Parsing CLI args...") | ||
args: CLIArgs = CLIArgs().parse_args() | ||
if args.version: | ||
logger.info(f"running ddlitlab2024 CLI v{__version__}") | ||
sys.exit(0) | ||
|
||
if args.command == CLICommand.DB: | ||
db = Database(args.db_path).create_session(args.create_schema) | ||
logger.info(f"Database session created: {db}") | ||
|
||
logger.info(f"CLI args: {args}") | ||
sys.exit(0) | ||
except Exception as e: | ||
logger.error(e) | ||
err_console.print_exception(show_locals=debug_mode) | ||
sys.exit(1) | ||
|
||
|
||
if __name__ == "__main__": | ||
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
Oops, something went wrong.