forked from Hanabi/hanabi-league
73 lines
2 KiB
Python
73 lines
2 KiB
Python
|
import argparse
|
||
|
|
||
|
import verboselogs
|
||
|
|
||
|
import config
|
||
|
import constants
|
||
|
import database
|
||
|
import log_setup
|
||
|
|
||
|
from log_setup import logger
|
||
|
|
||
|
|
||
|
def subcommand_init(force: bool):
|
||
|
tables = database.get_existing_tables()
|
||
|
if len(tables) > 0 and not force:
|
||
|
logger.info(
|
||
|
'Database tables "{}" exist already, aborting. To force re-initialization, use the --force options'
|
||
|
.format(", ".join(tables))
|
||
|
)
|
||
|
return
|
||
|
if len(tables) > 0:
|
||
|
logger.info(
|
||
|
"WARNING: This will drop all existing tables from the database and re-initialize them."
|
||
|
)
|
||
|
response = input("Do you wish to continue? [y/N] ")
|
||
|
if response not in ["y", "Y", "yes"]:
|
||
|
return
|
||
|
database.init_database()
|
||
|
logger.info("Successfully initialized database tables")
|
||
|
|
||
|
|
||
|
def subcommand_generate_config():
|
||
|
config.create_db_config()
|
||
|
|
||
|
|
||
|
def get_parser() -> argparse.ArgumentParser:
|
||
|
parser = argparse.ArgumentParser(
|
||
|
prog=constants.APP_NAME,
|
||
|
description="Data collection and analysis tool for the Hanabi League"
|
||
|
)
|
||
|
parser.add_argument('--verbose', '-v', help='Enable verbose logging to console', action='store_true')
|
||
|
|
||
|
subparsers = parser.add_subparsers(dest='command', required=True, help='select subcommand')
|
||
|
|
||
|
init_parser = subparsers.add_parser('init', help='Initialize database.')
|
||
|
init_parser.add_argument('--force', '-f', help='Force initialization (Drops existing tables)', action='store_true')
|
||
|
|
||
|
subparsers.add_parser('generate-config', help='Generate config file at default location')
|
||
|
|
||
|
return parser
|
||
|
|
||
|
|
||
|
def main():
|
||
|
parser = get_parser()
|
||
|
args = parser.parse_args()
|
||
|
|
||
|
subcommand_func = {
|
||
|
'init': subcommand_init,
|
||
|
'generate-config': subcommand_generate_config
|
||
|
}[args.command]
|
||
|
|
||
|
if args.verbose:
|
||
|
log_setup.logger_manager.set_console_level(verboselogs.VERBOSE)
|
||
|
|
||
|
del args.command
|
||
|
del args.verbose
|
||
|
|
||
|
subcommand_func(**vars(args))
|
||
|
|
||
|
|
||
|
if __name__ == "__main__":
|
||
|
main()
|