|
| 1 | +from pathlib import Path |
| 2 | +from typing import Any |
| 3 | + |
| 4 | +import click |
| 5 | +import yaml |
| 6 | + |
| 7 | +from . import consts |
| 8 | + |
| 9 | + |
| 10 | +@click.command(name="config") |
| 11 | +@click.option( |
| 12 | + "--root", |
| 13 | + "root_directory", |
| 14 | + type=consts.ROOT_DIRECTORY_TYPE, |
| 15 | + prompt=True, |
| 16 | + prompt_required=False, |
| 17 | + help="root directory of Advent of Code puzzles project", |
| 18 | +) |
| 19 | +@click.option( |
| 20 | + "--session-id", |
| 21 | + prompt=True, |
| 22 | + prompt_required=False, |
| 23 | + hide_input=True, |
| 24 | + help="session ID to access puzzles input", |
| 25 | +) |
| 26 | +def command(root_directory: str, session_id: str): |
| 27 | + """Set options.""" |
| 28 | + app_data_directory = click.get_app_dir(consts.APP_DATA_DIRECTORY) |
| 29 | + configuration_file = Path(app_data_directory, consts.CONFIGURATION_FILE_NAME) |
| 30 | + try: |
| 31 | + # Use an empty dictionary if no actual configuration is in the configuration file. |
| 32 | + configuration = yaml.safe_load(configuration_file.read_text()) or {} |
| 33 | + except FileNotFoundError: |
| 34 | + Path(app_data_directory).mkdir(exist_ok=True) |
| 35 | + configuration_file.touch() |
| 36 | + configuration = {} |
| 37 | + initial_configuration = configuration.copy() |
| 38 | + |
| 39 | + root_directory = _configure_root_directory(configuration, root_directory) |
| 40 | + root_directory = Path(root_directory) |
| 41 | + root_directory.mkdir(exist_ok=True) |
| 42 | + |
| 43 | + _configure_session_id(configuration, session_id) |
| 44 | + |
| 45 | + if configuration != initial_configuration: |
| 46 | + configuration_file.write_text(yaml.dump(configuration)) |
| 47 | + |
| 48 | + |
| 49 | +def _configure_root_directory( |
| 50 | + configuration: dict[str, Any], root_directory: str | None |
| 51 | +) -> str: |
| 52 | + """ |
| 53 | + Edit the root directory configuration if needed. |
| 54 | + :param configuration: current configuration |
| 55 | + :param root_directory: root directory passed by the user, `None` if wasn't passed |
| 56 | + :return: root directory after configuration if needed |
| 57 | + """ |
| 58 | + if root_directory is not None: |
| 59 | + configuration[consts.ROOT_DIRECTORY] = root_directory |
| 60 | + elif consts.ROOT_DIRECTORY not in configuration: |
| 61 | + configuration[consts.ROOT_DIRECTORY] = click.prompt( |
| 62 | + "Enter path for Advent of Code project root directory", |
| 63 | + type=consts.ROOT_DIRECTORY_TYPE, |
| 64 | + ) |
| 65 | + return configuration[consts.ROOT_DIRECTORY] |
| 66 | + |
| 67 | + |
| 68 | +def _configure_session_id(configuration: dict[str, Any], session_id: str | None): |
| 69 | + """ |
| 70 | + Configure the session ID if needed. |
| 71 | + :param configuration: current configuration |
| 72 | + :param session_id: session ID passed by the user, `None` if wasn't passed |
| 73 | + """ |
| 74 | + if session_id is not None: |
| 75 | + configuration[consts.SESSION_ID] = session_id |
| 76 | + elif consts.SESSION_ID not in configuration: |
| 77 | + configuration[consts.SESSION_ID] = click.prompt( |
| 78 | + "Enter session ID to download input files (available in AoC website cookies)", |
| 79 | + hide_input=True, |
| 80 | + ) |
0 commit comments