-
Notifications
You must be signed in to change notification settings - Fork 1
/
config_manager.py
47 lines (38 loc) · 1.71 KB
/
config_manager.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
import configparser
import os
class ConfigManager:
def __init__(self, config_file="settings.ini", temp_dir_name="YandexMusicRPC"):
self.temp_dir = os.path.join(os.getenv("LOCALAPPDATA"), temp_dir_name)
if not os.path.exists(self.temp_dir):
os.makedirs(self.temp_dir)
self.config_file = os.path.join(self.temp_dir, config_file)
self.config = configparser.ConfigParser()
self._load_config()
def _load_config(self):
if os.path.exists(self.config_file):
self.config.read(self.config_file)
else:
with open(self.config_file, "w") as file:
self.config.write(file)
def get_setting(self, section, option, fallback=None):
if self.config.has_option(section, option):
return self.config.get(section, option)
if fallback is not None:
self.set_setting(section, option, fallback)
return fallback
def set_setting(self, section, option, value):
if not self.config.has_section(section):
self.config.add_section(section)
self.config.set(section, option, value)
self._save_config()
def _save_config(self):
with open(self.config_file, "w") as configfile:
self.config.write(configfile)
def set_enum_setting(self, section, option, enum_value):
self.set_setting(section, option, enum_value.name)
def get_enum_setting(self, section, option, enum_type, fallback=None):
value = self.get_setting(section, option, fallback=fallback.name if fallback else None)
if value in enum_type.__members__:
return enum_type[value]
self.set_enum_setting(section, option, fallback)
return fallback