implement cli command to init database

This commit is contained in:
Maximilian Keßler 2023-07-05 09:33:55 +02:00
parent 184129fca0
commit b9068eebe4
Signed by: max
GPG key ID: BCC5A619923C0BA5
3 changed files with 65 additions and 18 deletions

View file

@ -9,6 +9,25 @@ from hanabi import constants
from .database import cur, conn 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(): def init_database_tables():
this = Path(__file__) this = Path(__file__)
logger.verbose("Initialising games and seeds tables...") logger.verbose("Initialising games and seeds tables...")

View file

@ -8,6 +8,7 @@ from hanabi import logger, logger_manager
from hanabi.live import check_game from hanabi.live import check_game
from hanabi.live import download_data from hanabi.live import download_data
from hanabi.live import compress from hanabi.live import compress
from hanabi.database import init_database
""" """
init db + populate tables 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): def add_init_subparser(subparsers):
parser = subparsers.add_parser( parser = subparsers.add_parser(
'init', 'init',
help='Init database tables, retrieve variant and suit information from hanab.live' 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): 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') 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: def main_parser() -> argparse.ArgumentParser:
parser = argparse.ArgumentParser( parser = argparse.ArgumentParser(
prog='hanabi_suite', prog='hanabi_suite',
@ -70,7 +97,8 @@ def main_parser() -> argparse.ArgumentParser:
def hanabi_cli(): def hanabi_cli():
args = main_parser().parse_args() args = main_parser().parse_args()
switcher = { switcher = {
'analyze': analyze_game 'analyze': subcommand_analyze,
'init': subcommand_init
} }
if args.verbose: if args.verbose:
logger_manager.set_console_level(verboselogs.VERBOSE) logger_manager.set_console_level(verboselogs.VERBOSE)

View file

@ -66,9 +66,9 @@ def export_all_seeds():
if __name__ == "__main__": if __name__ == "__main__":
init_database.init_database_tables()
init_database.populate_static_tables()
hanabi_cli() hanabi_cli()
# init_database.init_database_tables()
# init_database.populate_static_tables()
exit(0) exit(0)
find_double_dark_games() find_double_dark_games()
exit(0) exit(0)