-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathconfig_manager.py
More file actions
78 lines (63 loc) · 2.59 KB
/
Copy pathconfig_manager.py
File metadata and controls
78 lines (63 loc) · 2.59 KB
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
import configparser
import os
from pathlib import Path
class ConfigManager:
def __init__(self, app_name: str = "honeycomb"):
self.app_name = app_name
self.config_file = self._get_config_path()
self.config = configparser.ConfigParser()
self._load_config()
def _get_config_path(self) -> Path:
"""Determines the path to the INI file in the user's local folder"""
if os.name == 'nt': # Windows
# %LOCALAPPDATA%\app_name\config.ini
local_app_data = os.environ.get('LOCALAPPDATA', Path.home() / 'AppData' / 'Local')
config_dir = Path(local_app_data) / self.app_name
else: # Linux/macOS
# ~/.local/share/app_name/config.ini
config_dir = Path.home() / '.local' / 'share' / self.app_name
# Create directory if it does not exist
config_dir.mkdir(parents=True, exist_ok=True)
return config_dir / 'config.ini'
def _load_config(self):
"""Loads the configuration from the INI file"""
if self.config_file.exists():
try:
self.config.read(self.config_file, encoding='utf-8')
except Exception as e:
print(f"Error loading config: {e}")
self._create_default_config()
else:
self._create_default_config()
def _create_default_config(self):
"""Creates a default configuration"""
self.config['THEME'] = {
'current_theme': 'nightbee'
}
self._save_config()
def _save_config(self):
"""Saves the configuration to an INI file"""
try:
with open(self.config_file, 'w', encoding='utf-8') as f:
self.config.write(f)
return True
except Exception as e:
print(f"Error saving config: {e}")
return False
def get_theme(self) -> str:
"""Gets the current theme"""
return self.config.get('THEME', 'current_theme', fallback='nightbee')
def set_theme(self, theme: str) -> bool:
"""Saves the selected theme"""
if 'THEME' not in self.config:
self.config.add_section('THEME')
self.config.set('THEME', 'current_theme', theme)
return self._save_config()
# Example usage
if __name__ == "__main__":
config = ConfigManager("honeycomb")
print(f"Config is saved to: {config.config_file}")
print(f"Current theme: {config.get_theme()}")
# Change theme
config.set_theme("cyberhive")
print(f"New theme: {config.get_theme()}")