-
Notifications
You must be signed in to change notification settings - Fork 3
/
modify_config.py
135 lines (123 loc) · 4.93 KB
/
modify_config.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
import re
import os
import zipfile
from collections import defaultdict
import fa_paths
import translations
import config
from fa_menu import select_option
from mods import mod_manager
CHANGESET_PATH = r"config_changes/.*\.ini"
config_re = re.compile(r"^([\w-]+)=(.*)")
comment_re = re.compile(r"^;")
our_mod_filter = re.compile(fa_paths.MOD_NAME)
def get_changes_from_fp(fp):
changes = defaultdict(dict)
current_comment = ""
current_category = changes[""]
for line in fp:
if line[0] == ";":
current_comment += line[1:]
continue
if line[0] == "[":
current_category = changes[line.strip("[] \t\r\n")]
if match := re.match(config_re, line):
current_category[match[1]] = (match[2], current_comment)
current_comment = ""
return changes
def get_all_changes(after="AA"):
all_changes = {}
with mod_manager:
mod_iter = mod_manager.iter_mod_files(CHANGESET_PATH, True, our_mod_filter)
for cfg_path in mod_iter:
version = cfg_path.name[:2]
if version <= after:
continue
if not re.fullmatch("[A-Z]{2}", version):
raise ValueError(
"configuration change sets must begin with the next two capital letters"
)
with cfg_path.open(encoding="utf8") as fp:
all_changes[version] = get_changes_from_fp(fp)
return all_changes
def do_config_check():
with config.current_conf:
current = ""
try:
for i in ["1", "2"]:
setting_string = f"access-config-version{i}-DO-NOT-EDIT"
current += config.current_conf.get_setting("controls", setting_string)
except config.Config_Missing:
current = "AA"
if current == "Ch":
# to correct for an issue with a config file being badly named.
current = "AE"
change_sets = get_all_changes(after=current)
if not change_sets:
return
p = """There are changes that the Factorio Access mod recommends making to your configuration file that have not yet been applied.
These can be applied interactively or all at once.
How would you like to proceed?"""
ops = [
"Interactively approve changes.",
"Approve all",
"Skip for now (not recommended)",
]
approve_type = select_option(ops, p)
if approve_type == 2:
return
ops = ["Approve change", "Keep Current", "Edit Value", "Approve All"]
for change_set_v, change_set in change_sets.items():
for cat, changes in change_set.items():
for setting, change in changes.items():
try:
current_val = config.current_conf.get_setting(cat, setting)
except config.Config_Missing as e:
current_val = "unknown"
if current_val == change[0]:
continue
if approve_type == 0:
cats_to_try = translations.check_cats[cat]
p = translations.translate(
[
"",
f"{cat}.{setting}\n",
["?"]
+ [
[f"{t_cat}.{setting.removesuffix('-alternative')}"]
for t_cat in cats_to_try
]
+ [""],
"\n",
["?"]
+ [
[f"{t_cat}-description.{setting}"]
for t_cat in cats_to_try
]
+ [""],
"\n",
change[1],
"Current Value:"
+ current_val
+ "\n"
+ "Suggested Value:"
+ change[0],
]
)
action = select_option(ops, p)
if action == 1:
continue
elif action == 2:
change = (input("New Value:"),)
elif action == 3:
approve_type = 1
config.current_conf.set_setting(cat, setting, change[0], force=True)
for i, key in enumerate(change_set_v):
config.current_conf.set_setting(
"controls",
f"access-config-version{i+1}-DO-NOT-EDIT",
key,
force=True,
)
if __name__ == "__main__":
do_config_check()