Skip to content

Commit ea7ab41

Browse files
committed
save config on change
1 parent bd9499d commit ea7ab41

File tree

2 files changed

+39
-27
lines changed

2 files changed

+39
-27
lines changed

src/fantasy_forge/config.py

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,10 @@
1+
from __future__ import annotations
2+
13
from dataclasses import dataclass
4+
from typing import Any, Self
5+
6+
import toml
7+
from xdg_base_dirs import xdg_config_home
28

39

410
@dataclass
@@ -8,3 +14,32 @@ class Config:
814
description = "the heroic player"
915
logfile = "fantasy_forge.log"
1016
loglevel = "INFO"
17+
18+
def user_config() -> dict[str, Any]:
19+
config_file = xdg_config_home() / "fantasy_forge.toml"
20+
21+
if not config_file.exists():
22+
config_file.touch()
23+
24+
with config_file.open() as file:
25+
config = toml.load(file)
26+
27+
return config
28+
29+
def read() -> Config:
30+
user_config = Config.user_config()
31+
config = Config()
32+
config_options = dir(Config)
33+
for option in config_options:
34+
if option in user_config:
35+
setattr(config, option, user_config[option])
36+
37+
return config
38+
39+
def save(self: Self):
40+
config_file = xdg_config_home() / "fantasy_forge.toml"
41+
42+
with config_file.open("w") as file:
43+
toml.dump(self.__dict__, file)
44+
45+
return

src/fantasy_forge/main.py

Lines changed: 4 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -7,25 +7,12 @@
77
from threading import current_thread
88
from typing import Any
99

10-
import toml
11-
from xdg_base_dirs import xdg_config_home
12-
1310
from fantasy_forge.area import Area
1411
from fantasy_forge.config import Config
1512
from fantasy_forge.player import Player
1613
from fantasy_forge.world import World
1714

1815

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-
2916
def parse_args(config: Config, argv=argv[1:]):
3017
parser = ArgumentParser(description="Fantasy Forge: A text-based RPG")
3118
parser.add_argument("--version", action="version", version="%(prog)s 0.1.0")
@@ -43,21 +30,9 @@ def parse_args(config: Config, argv=argv[1:]):
4330
return parser.parse_args(argv)
4431

4532

46-
def user_config() -> dict[str, Any]:
47-
config_file = xdg_config_home() / "fantasy_forge.toml"
48-
49-
if not config_file.exists():
50-
config_file.touch()
51-
52-
with config_file.open() as file:
53-
config = toml.load(file)
54-
55-
return config
56-
57-
5833
def main():
5934
# load config and args
60-
config = read_config_options(user_config())
35+
config = Config.read()
6136
args = parse_args(config)
6237
description = args.description
6338

@@ -84,7 +59,7 @@ def main():
8459
):
8560
if name_input:
8661
player_name = name_input
87-
setattr(config, 'name', name_input)
62+
setattr(config, "name", name_input)
8863
print(
8964
world.l10n.format_value(
9065
"character-name-change-successful", {"chosen_name": name_input}
@@ -111,6 +86,8 @@ def main():
11186
)
11287
continue
11388

89+
config.save()
90+
11491
print()
11592
player = Player(world, player_name, description, current_thread())
11693

0 commit comments

Comments
 (0)