-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathsettings.py
More file actions
41 lines (35 loc) · 1.37 KB
/
Copy pathsettings.py
File metadata and controls
41 lines (35 loc) · 1.37 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
import json
import os
from pathlib import Path
class SettingsManager:
def __init__(self, name: str, settings_directory: str):
self.name = name
self.settings_directory = settings_directory
self.settings_file = os.path.join(settings_directory, f"{name}.json")
self.settings = {}
def read(self):
"""Read settings from the JSON file"""
try:
if os.path.exists(self.settings_file):
with open(self.settings_file, 'r') as f:
self.settings = json.load(f)
except Exception as e:
print(f"Error reading settings: {e}")
self.settings = {}
return self.settings
def write(self):
"""Write settings to the JSON file"""
try:
# Ensure directory exists
Path(self.settings_directory).mkdir(parents=True, exist_ok=True)
with open(self.settings_file, 'w') as f:
json.dump(self.settings, f, indent=2)
except Exception as e:
print(f"Error writing settings: {e}")
def getSetting(self, key: str, default=None):
"""Get a setting value by key, returning default if not found"""
return self.settings.get(key, default)
def setSetting(self, key: str, value):
"""Set a setting value and save to file"""
self.settings[key] = value
self.write()