From b9068eebe4990fbb6d1e4d156325cac529208bf2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Maximilian=20Ke=C3=9Fler?= Date: Wed, 5 Jul 2023 09:33:55 +0200 Subject: [PATCH] implement cli command to init database --- hanabi/database/init_database.py | 19 ++++++++++ hanabi/hanabi_cli.py | 60 +++++++++++++++++++++++--------- test.py | 4 +-- 3 files changed, 65 insertions(+), 18 deletions(-) diff --git a/hanabi/database/init_database.py b/hanabi/database/init_database.py index 7f749e8..891ec5b 100644 --- a/hanabi/database/init_database.py +++ b/hanabi/database/init_database.py @@ -9,6 +9,25 @@ from hanabi import constants from .database import cur, conn +def get_existing_tables(): + cur.execute( + " SELECT tablename FROM pg_tables" + " WHERE" + " schemaname = 'public' AND " + " tablename IN (" + " 'seeds'," + " 'games'," + " 'suits'," + " 'colors'," + " 'suit_colors'," + " 'variants'," + " 'variant_suits'," + " 'variant_game_downloads'" + " )" + ) + return [table for (table,) in cur.fetchall()] + + def init_database_tables(): this = Path(__file__) logger.verbose("Initialising games and seeds tables...") diff --git a/hanabi/hanabi_cli.py b/hanabi/hanabi_cli.py index 513f590..c7715bd 100755 --- a/hanabi/hanabi_cli.py +++ b/hanabi/hanabi_cli.py @@ -8,6 +8,7 @@ from hanabi import logger, logger_manager from hanabi.live import check_game from hanabi.live import download_data from hanabi.live import compress +from hanabi.database import init_database """ init db + populate tables @@ -17,11 +18,52 @@ analyze single game """ +def subcommand_analyze(game_id: int, download: bool = False): + if download: + download_data.detailed_export_game(game_id) + logger.info('Analyzing game {}'.format(game_id)) + turn, sol = check_game.check_game(game_id) + if turn == 0: + logger.info('Instance is unfeasible') + else: + logger.info('Game was first lost after {} turns.'.format(turn)) + logger.info( + 'A replay achieving perfect score from the previous turn onwards is: {}#{}' + .format(compress.link(sol), turn) + ) + + +def subcommand_init(force: bool, populate: bool): + tables = init_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 + init_database.init_database_tables() + logger.info("Successfully initialized database tables") + if populate: + init_database.populate_static_tables() + + def add_init_subparser(subparsers): parser = subparsers.add_parser( 'init', help='Init database tables, retrieve variant and suit information from hanab.live' ) + parser.add_argument('--force', '-f', help='Force initialization (Drops existing tables)', action='store_true') + parser.add_argument('--no-populate-tables', '-n', + help='Do not download variant and suit information from hanab.live', + action='store_true', + dest='populate') def add_download_subparser(subparsers): @@ -37,21 +79,6 @@ def add_analyze_subparser(subparsers): parser.add_argument('--download', '-d', help='Download game if not in database', action='store_true') -def analyze_game(game_id: int, download: bool = False): - if download: - download_data.detailed_export_game(game_id) - logger.info('Analyzing game {}'.format(game_id)) - turn, sol = check_game.check_game(game_id) - if turn == 0: - logger.info('Instance is unfeasible') - else: - logger.info('Game was first lost after {} turns.'.format(turn)) - logger.info( - 'A replay achieving perfect score from the previous turn onwards is: {}#{}' - .format(compress.link(sol), turn) - ) - - def main_parser() -> argparse.ArgumentParser: parser = argparse.ArgumentParser( prog='hanabi_suite', @@ -70,7 +97,8 @@ def main_parser() -> argparse.ArgumentParser: def hanabi_cli(): args = main_parser().parse_args() switcher = { - 'analyze': analyze_game + 'analyze': subcommand_analyze, + 'init': subcommand_init } if args.verbose: logger_manager.set_console_level(verboselogs.VERBOSE) diff --git a/test.py b/test.py index 74413df..4a8420e 100644 --- a/test.py +++ b/test.py @@ -66,9 +66,9 @@ def export_all_seeds(): if __name__ == "__main__": - init_database.init_database_tables() - init_database.populate_static_tables() hanabi_cli() + # init_database.init_database_tables() +# init_database.populate_static_tables() exit(0) find_double_dark_games() exit(0)