From 01ebfa380deeb4e4260ee29e0b6b0593928a5f90 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Maximilian=20Ke=C3=9Fler?= Date: Wed, 22 Nov 2023 18:58:55 +0100 Subject: [PATCH] Add CLI option to fetch variants --- database.py | 52 ++++++++++++++++++++++++++-------------------------- main.py | 5 ++++- 2 files changed, 30 insertions(+), 27 deletions(-) diff --git a/database.py b/database.py index 0b81f45..e6f148d 100644 --- a/database.py +++ b/database.py @@ -72,6 +72,32 @@ def init_database(): logger.verbose("Initialized DB tables.") +def fetch_and_initialize_variants(): + response = requests.get(constants.VARIANTS_JSON_URL) + if not response.status_code == 200: + logger.error("Could not download variants.json file from github (tried url {})".format(constants.VARIANTS_JSON_URL)) + return + variants = json.loads(response.text) + + config = read_config() + + for variant in variants: + variant_id = variant['id'] + name = variant['name'] + clue_starved = variant.get('clueStarved', False) + num_suits = len(variant['suits']) + + if config.min_suit_count <= num_suits <= config.max_suit_count: + if any(var_name in name.lower() for var_name in config.excluded_variants): + continue + cur = conn_manager.get_new_cursor() + cur.execute( + "INSERT INTO variants (id, name, num_suits, clue_starved) VALUES (%s, %s, %s, %s)", + (variant_id, name, num_suits, clue_starved) + ) + conn_manager.get_connection().commit() + + def add_player_name(player_name: str): conn = conn_manager.get_connection() cur = conn.cursor() @@ -121,29 +147,3 @@ def add_user_name_to_player(hanabi_username: str, player_name: str): def add_player(player_name: str, user_name: str): add_player_name(player_name) add_user_name_to_player(user_name, player_name) - - -def fetch_and_initialize_variants(): - response = requests.get(constants.VARIANTS_JSON_URL) - if not response.status_code == 200: - logger.error("Could not download variants.json file from github (tried url {})".format(constants.VARIANTS_JSON_URL)) - return - variants = json.loads(response.text) - - config = read_config() - - for variant in variants: - variant_id = variant['id'] - name = variant['name'] - clue_starved = variant.get('clueStarved', False) - num_suits = len(variant['suits']) - - if config.min_suit_count <= num_suits <= config.max_suit_count: - if any(var_name in name.lower() for var_name in config.excluded_variants): - continue - cur = conn_manager.get_new_cursor() - cur.execute( - "INSERT INTO variants (id, name, num_suits, clue_starved) VALUES (%s, %s, %s, %s)", - (variant_id, name, num_suits, clue_starved) - ) - conn_manager.get_connection().commit() diff --git a/main.py b/main.py index 5eaf667..e84bbe1 100644 --- a/main.py +++ b/main.py @@ -11,7 +11,7 @@ import log_setup from log_setup import logger -def subcommand_init(force: bool): +def subcommand_init(force: bool, fetch_variants: bool): tables = database.get_existing_tables() if len(tables) > 0 and not force: logger.info( @@ -29,6 +29,8 @@ def subcommand_init(force: bool): return database.init_database() logger.info("Successfully initialized database tables") + if fetch_variants: + database.fetch_and_initialize_variants() def subcommand_generate_config(): @@ -47,6 +49,7 @@ def get_parser() -> argparse.ArgumentParser: init_parser = subparsers.add_parser('init', help='Initialize database.') init_parser.add_argument('--force', '-f', help='Force initialization (Drops existing tables)', action='store_true') + init_parser.add_argument('--fetch-variants', '-v', help='Fetch and initialize variants', action='store_true') subparsers.add_parser('generate-config', help='Generate config file at default location')