hanabi-league/main.py

101 lines
3 KiB
Python
Raw Normal View History

2023-12-09 13:51:59 +01:00
#! python3
2023-11-22 16:21:30 +01:00
import argparse
2023-12-07 14:31:41 +01:00
import sys
2023-11-22 16:21:30 +01:00
import verboselogs
2023-12-07 14:31:41 +01:00
sys.path.append('src/')
import config
import constants
import database
import log_setup
2023-12-09 13:51:10 +01:00
import fetch_players
import fetch_games
2023-12-07 14:31:41 +01:00
from log_setup import logger
2023-11-22 16:21:30 +01:00
2023-12-08 12:02:46 +01:00
def subcommand_init(force: bool, no_fetch_variants: bool):
2023-11-22 16:21:30 +01:00
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"]:
2023-11-22 16:27:04 +01:00
logger.info("Database initialization aborted.")
2023-11-22 16:21:30 +01:00
return
database.init_database()
logger.info("Successfully initialized database tables")
2023-12-08 12:02:46 +01:00
if not no_fetch_variants:
2023-11-22 18:58:55 +01:00
database.fetch_and_initialize_variants()
2023-11-23 01:18:26 +01:00
database.initialize_variant_base_ratings()
2023-12-07 15:26:42 +01:00
logger.info("Successfully initialized variants and base ratings.")
2023-11-22 16:21:30 +01:00
def subcommand_generate_config():
config.create_db_config()
2023-11-22 17:13:44 +01:00
config.create_config()
2023-11-22 16:21:30 +01:00
2023-12-09 13:51:10 +01:00
def subcommand_fetch(target: str):
if target in ["all", "players"]:
fetch_players.fetch_players_interactive()
if target in ["all", "games"]:
games = fetch_games.fetch_games_for_all_players()
fetch_games.store_new_games(games)
if target in ["all", "game-details"]:
fetch_games.fetch_all_game_details()
2023-11-22 16:21:30 +01:00
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')
2023-12-08 12:02:46 +01:00
init_parser.add_argument('--no-fetch-variants', '-n', help='Do not fetch and initialize variants', action='store_true')
2023-11-22 16:21:30 +01:00
subparsers.add_parser('generate-config', help='Generate config file at default location')
2023-12-09 13:51:10 +01:00
fetch_parser = subparsers.add_parser('fetch', help='Fetch new data.')
fetch_parser.add_argument(dest='target', choices=['all', 'players', 'games', 'game-details'], default='all')
2023-11-22 16:21:30 +01:00
return parser
def main():
parser = get_parser()
args = parser.parse_args()
subcommand_func = {
'init': subcommand_init,
2023-12-09 13:51:10 +01:00
'generate-config': subcommand_generate_config,
'fetch': subcommand_fetch
2023-11-22 16:21:30 +01:00
}[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()