# Path: pyani/pyani_orm.py
# class PyaniORMException(PyaniException):
# class LabelTuple(NamedTuple):
# class Label(Base):
# class BlastDB(Base):
# class Genome(Base):
# class Run(Base):
# class Comparison(Base):
#     def __str__(self) -> str:
#     def __repr__(self) -> str:
#     def __str__(self) -> str:
#     def __repr__(self) -> str:
#     def __str__(self) -> str:
#     def __repr__(self) -> str:
#     def __str__(self) -> str:
#     def __repr__(self) -> str:
#     def __str__(self) -> str:
#     def __repr__(self) -> str:
# def create_db(dbpath: Path) -> None:
# def get_session(dbpath: Path) -> Any:
# def get_comparison_dict(session: Any) -> Dict[Tuple, Any]:
# def get_matrix_labels_for_run(session: Any, run_id: int) -> Dict:
# def get_matrix_classes_for_run(session: Any, run_id: int) -> Dict[str, List]:
# def filter_existing_comparisons(
#     session,
#     run,
#     comparisons,
#     program,
#     version,
#     fragsize: Optional[int] = None,
#     maxmatch: Optional[bool] = False,
#     kmersize: Optional[int] = None,
#     minmatch: Optional[float] = None,
# ) -> List:
# def add_run(session, method, cmdline, date, status, name):
# def add_run_genomes(
#     session, run, indir: Path, classpath: Path, labelpath: Path, **kwargs
# ) -> List:
# def update_comparison_matrices(session, run) -> None:


import logging
from argparse import Namespace
from pyani import pyani_orm
"""Provides the createdb subcommand for pyani."""





def subcmd_createdb(args: Namespace) -> int:
    """Create an empty pyani database.

    :param args:  Namespace, command-line arguments
    :param logger:  logging object
    """
    # Create logger
    logger = logging.getLogger(__name__)

    # If the database exists, raise an error rather than overwrite
    if args.dbpath.is_file():
        if not args.force:
            logger.error("Database %s already exists (exiting)", args.dbpath)
            raise SystemError(1)
        logger.warning("Database %s already exists - overwriting", args.dbpath)
        args.dbpath.unlink()

    # If the path to the database doesn't exist, create it
    if not args.dbpath.parent.is_dir():
        logger.info("Creating database directory %s", args.dbpath.parent)
        args.dbpath.parent.mkdir(parents=True, exist_ok=True)

    # Create the empty database
    logger.info("Creating pyani database at %s", args.dbpath)

# Path: pyani/pyani_orm.py
# class PyaniORMException(PyaniException):
# class LabelTuple(NamedTuple):
# class Label(Base):
# class BlastDB(Base):
# class Genome(Base):
# class Run(Base):
# class Comparison(Base):
#     def __str__(self) -> str:
#     def __repr__(self) -> str:
#     def __str__(self) -> str:
#     def __repr__(self) -> str:
#     def __str__(self) -> str:
#     def __repr__(self) -> str:
#     def __str__(self) -> str:
#     def __repr__(self) -> str:
#     def __str__(self) -> str:
#     def __repr__(self) -> str:
# def create_db(dbpath: Path) -> None:
# def get_session(dbpath: Path) -> Any:
# def get_comparison_dict(session: Any) -> Dict[Tuple, Any]:
# def get_matrix_labels_for_run(session: Any, run_id: int) -> Dict:
# def get_matrix_classes_for_run(session: Any, run_id: int) -> Dict[str, List]:
# def filter_existing_comparisons(
#     session,
#     run,
#     comparisons,
#     program,
#     version,
#     fragsize: Optional[int] = None,
#     maxmatch: Optional[bool] = False,
#     kmersize: Optional[int] = None,
#     minmatch: Optional[float] = None,
# ) -> List:
# def add_run(session, method, cmdline, date, status, name):
# def add_run_genomes(
#     session, run, indir: Path, classpath: Path, labelpath: Path, **kwargs
# ) -> List:
# def update_comparison_matrices(session, run) -> None:


# Path: pyani/scripts/subcommands/subcmd_createdb.py
import logging
from argparse import Namespace
from pyani import pyani_orm

"""Provides the createdb subcommand for pyani."""





def subcmd_createdb(args: Namespace) -> int:
    """Create an empty pyani database.

    :param args:  Namespace, command-line arguments
    :param logger:  logging object
    """
    # Create logger
    logger = logging.getLogger(__name__)

    # If the database exists, raise an error rather than overwrite
    if args.dbpath.is_file():
        if not args.force:
            logger.error("Database %s already exists (exiting)", args.dbpath)
            raise SystemError(1)
        logger.warning("Database %s already exists - overwriting", args.dbpath)
        args.dbpath.unlink()

    # If the path to the database doesn't exist, create it
    if not args.dbpath.parent.is_dir():
        logger.info("Creating database directory %s", args.dbpath.parent)
        args.dbpath.parent.mkdir(parents=True, exist_ok=True)

    # Create the empty database
    logger.info("Creating pyani database at %s", args.dbpath)

    pyani_orm.create_db(args.dbpath)