|
1 | 1 | import logging |
2 | | -import toml |
3 | 2 | import shutil |
4 | 3 | from argparse import ArgumentParser |
5 | 4 | from importlib import resources |
|
8 | 7 | from threading import current_thread |
9 | 8 | from typing import Any |
10 | 9 |
|
| 10 | +import toml |
11 | 11 | from xdg_base_dirs import xdg_config_home |
12 | 12 |
|
13 | 13 | from fantasy_forge.area import Area |
| 14 | +from fantasy_forge.config import Config |
14 | 15 | from fantasy_forge.player import Player |
15 | 16 | from fantasy_forge.world import World |
16 | 17 |
|
17 | 18 |
|
18 | | -def parse_args(config: dict[str, Any], argv=argv[1:]): |
| 19 | +def read_config_options(user_config: dict[str, Any]) -> Config: |
| 20 | + config = Config() |
| 21 | + config_options = dir(Config) |
| 22 | + for option in config_options: |
| 23 | + if option in user_config: |
| 24 | + setattr(config, option, user_config[option]) |
| 25 | + |
| 26 | + return config |
| 27 | + |
| 28 | + |
| 29 | +def parse_args(config: Config, argv=argv[1:]): |
19 | 30 | parser = ArgumentParser(description="Fantasy Forge: A text-based RPG") |
20 | 31 | parser.add_argument("--version", action="version", version="%(prog)s 0.1.0") |
21 | | - parser.add_argument("--world", help="The world to play in", default=config["world"]) |
22 | | - parser.add_argument("--name", help="Set player name", default=config["name"]) |
| 32 | + parser.add_argument("--world", help="The world to play in", default=config.world) |
| 33 | + parser.add_argument("--name", help="Set player name", default=config.name) |
23 | 34 | parser.add_argument( |
24 | | - "--description", help="Set player description", default=config["description"] |
| 35 | + "--description", help="Set player description", default=config.description |
25 | 36 | ) |
26 | 37 | parser.add_argument( |
27 | | - "--logfile", |
28 | | - help="Enables logging for debug purposes", |
29 | | - default=config["logfile"], |
| 38 | + "--logfile", help="Enables logging for debug purposes", default=config.logfile |
30 | 39 | ) |
31 | 40 | parser.add_argument( |
32 | | - "--loglevel", help="Severity Level for logging", default=config["loglevel"] |
| 41 | + "--loglevel", help="Severity Level for logging", default=config.loglevel |
33 | 42 | ) |
34 | 43 | return parser.parse_args(argv) |
35 | 44 |
|
36 | 45 |
|
37 | | -def load_config() -> dict[str, Any]: |
38 | | - with resources.as_file(resources.files()) as resource_path: |
39 | | - default_config_file = resource_path / "config.toml" |
40 | | - |
41 | | - usr_config_file = xdg_config_home() / "fantasy_forge.toml" |
| 46 | +def user_config() -> dict[str, Any]: |
| 47 | + config_file = xdg_config_home() / "fantasy_forge.toml" |
42 | 48 |
|
43 | | - if not usr_config_file.exists(): |
44 | | - shutil.copyfile(default_config_file, usr_config_file) |
| 49 | + if not config_file.exists(): |
| 50 | + config_file.touch() |
45 | 51 |
|
46 | | - with usr_config_file.open() as config_file: |
47 | | - config = toml.load(config_file) |
| 52 | + with config_file.open() as file: |
| 53 | + config = toml.load(file) |
48 | 54 |
|
49 | 55 | return config |
50 | 56 |
|
51 | | -usr_config_file = xdg_config_home() / "fantasy_forge.toml" |
52 | 57 |
|
53 | 58 | def main(): |
54 | 59 | # load config and args |
55 | | - config = load_config() |
| 60 | + config = read_config_options(user_config()) |
56 | 61 | args = parse_args(config) |
57 | 62 | description = args.description |
58 | 63 |
|
|
0 commit comments