-
Notifications
You must be signed in to change notification settings - Fork 0
/
globale_variablen.py
52 lines (38 loc) · 1.46 KB
/
globale_variablen.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
"""
Hier weden grundelgende Einstellungen / Variablen definiert, die im ganzen Programm verwendet werden
"""
import json
import os
from pathlib import Path
from typing import Dict
setting_path = Path("settings_example.json") if os.environ.get("FLASK_ENV") == "development" else Path("settings.json")
def read_settings(file_path: Path) -> Dict:
with file_path.open(mode="r", encoding="utf8") as file:
settings = json.load(file)
return settings
def save_settings(file_path: Path, values: Dict) -> None:
with file_path.open(mode="w", encoding="utf8") as file:
json.dump(values, file, indent=4, sort_keys=True)
class Config:
def __init__(self):
self.__load_settings()
def __load_settings(self):
settings = read_settings(setting_path)
# Add dynmaic attributes from sttings json
for name, value in settings.items():
setattr(self, name, value)
def update_setting(self, name: str, value: any):
"""Aktualisiert eine Einstellung
:param name: Name der Einstellung
:param value: Wert der Einstellung
"""
settings = read_settings(setting_path)
settings[name] = json.loads(value)
save_settings(setting_path, settings)
self.__load_settings()
def get_settings(self) -> Dict:
"""Gibt die aktuellen Einstellungen zurück
:return: Dict mit den Einstellungen als Inhalt.
"""
return self.__dict__
config = Config()