Skip to content

Commit a3a6902

Browse files
committed
refactor how config files are handled (closes #46)
1 parent 08ac0ca commit a3a6902

2 files changed

Lines changed: 25 additions & 25 deletions

File tree

src/fantasy_forge/config.toml

Lines changed: 0 additions & 5 deletions
This file was deleted.

src/fantasy_forge/main.py

Lines changed: 25 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
import logging
2-
import toml
32
import shutil
43
from argparse import ArgumentParser
54
from importlib import resources
@@ -8,51 +7,57 @@
87
from threading import current_thread
98
from typing import Any
109

10+
import toml
1111
from xdg_base_dirs import xdg_config_home
1212

1313
from fantasy_forge.area import Area
14+
from fantasy_forge.config import Config
1415
from fantasy_forge.player import Player
1516
from fantasy_forge.world import World
1617

1718

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:]):
1930
parser = ArgumentParser(description="Fantasy Forge: A text-based RPG")
2031
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)
2334
parser.add_argument(
24-
"--description", help="Set player description", default=config["description"]
35+
"--description", help="Set player description", default=config.description
2536
)
2637
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
3039
)
3140
parser.add_argument(
32-
"--loglevel", help="Severity Level for logging", default=config["loglevel"]
41+
"--loglevel", help="Severity Level for logging", default=config.loglevel
3342
)
3443
return parser.parse_args(argv)
3544

3645

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"
4248

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()
4551

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)
4854

4955
return config
5056

51-
usr_config_file = xdg_config_home() / "fantasy_forge.toml"
5257

5358
def main():
5459
# load config and args
55-
config = load_config()
60+
config = read_config_options(user_config())
5661
args = parse_args(config)
5762
description = args.description
5863

0 commit comments

Comments
 (0)