-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmyConfig.py
More file actions
31 lines (25 loc) · 1.01 KB
/
myConfig.py
File metadata and controls
31 lines (25 loc) · 1.01 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
# autor : luka fricker
# date : 19.01.2024
# description : simple class to get and store settings as a python file for values that should persist application restart
import pathlib
import json
import os
class myConfig:
def __init__(self):
parent_path = pathlib.Path(__file__).parent.resolve()
self.__path = os.path.join(parent_path, "config/config.json")
def get_config(self):
try:
with open(self.__path, "r", encoding="utf-8") as file:
config_data = json.load(file)
return config_data
except FileNotFoundError:
print(f"Error: File '{self.__path}' not found.")
return {}
def set_config(self, config):
try:
with open(self.__path, "w", encoding="utf-8") as file:
json.dump(config, file, indent=4)
print(f"Configurations successfully saved to '{self.__path}'.")
except Exception as e:
print(f"Error: Unable to save configurations to '{self.__path}'. {e}")