-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathconfigure.py
88 lines (75 loc) · 2.88 KB
/
configure.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
import configparser
import os
from init_instances import inst
from win32api import MessageBeep
from win32con import MB_OK
class Configure:
def __init__(self):
self.configDir = os.path.join(os.getenv("APPDATA"), "Troppical")
self.configFile = os.path.join(self.configDir, "config.ini")
self.defaultConfig = {
"Settings": {
"launch_as_admin": "false",
"default_install_path": os.path.join(os.environ["LOCALAPPDATA"]),
}
}
self.config = configparser.ConfigParser()
self.currentConfig = self.load_config()
def load_config(self):
try:
if not os.path.exists(self.configDir):
os.makedirs(self.configDir)
if os.path.exists(self.configFile):
self.config.read(self.configFile)
else:
for section, values in self.defaultConfig.items():
if not self.config.has_section(section):
self.config.add_section(section)
for key, value in values.items():
self.config.set(section, key, str(value))
self.save_config()
return self.config
except Exception as e:
print(f"Error loading config: {e}")
return self.create_default_config()
def create_default_config(self):
self.config = configparser.ConfigParser()
for section, values in self.defaultConfig.items():
self.config.add_section(section)
for key, value in values.items():
self.config.set(section, key, str(value))
return self.config
def save_config(self):
try:
with open(self.configFile, "w") as f:
self.config.write(f)
except Exception as e:
print(f"Error saving config: {e}")
def get_setting(self, key, section="Settings"):
try:
value = self.config.get(section, key)
if value.lower() in ("true", "false"):
return value.lower() == "true"
return value
except:
return self.defaultConfig
def set_setting(self, key, value, section="Settings"):
self.config.set(section, key, str(value))
self.save_config()
def apply_settings(self):
inst.settings.launchAsAdminCheckbox.setChecked(
self.get_setting("launch_as_admin")
)
inst.settings.defaultInstallPath.setText(
self.get_setting("default_install_path")
)
def handle_settings_button_apply(self):
self.set_setting(
"launch_as_admin", inst.settings.launchAsAdminCheckbox.isChecked()
)
self.set_setting(
"default_install_path", inst.settings.defaultInstallPath.text()
)
self.apply_settings()
inst.ui.qt_index_switcher(0)
MessageBeep(MB_OK)