forked from gzinck/des
-
Notifications
You must be signed in to change notification settings - Fork 0
/
global_settings.py
70 lines (58 loc) · 1.56 KB
/
global_settings.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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
import json
from typing import Any, Final
from cli.display.message import show_error
# The settings file for saving
settings_file = "settings.ini"
# The default settings
defaults: Final[dict[str, Any]] = {
"graphviz_file_type": "pdf",
"graphviz_auto_vis": True,
}
settings = None
def initialize() -> None:
"""Initializes the program's settings
Returns
-------
None
"""
global settings
settings = defaults
try:
try:
with open(settings_file, "r") as f:
# Load in the settings
settings = json.load(f)
except FileNotFoundError:
with open(settings_file, "w") as f:
# Write new settings
settings = defaults
json.dump(settings, f)
except Exception:
show_error(
"Error reading/writing settings file.\n" + "Reverting to defaults..."
)
for k, v in defaults.items():
if k not in settings:
settings[k] = v
def update(setting: str, value: Any) -> None:
"""Updates the setting with a new value
Parameters
----------
setting : str
The setting to change
value : any
The new value for the setting
Returns
-------
None
"""
settings[setting] = value
try:
with open(settings_file, "w") as f:
# Write new settings
json.dump(settings, f)
except FileNotFoundError:
show_error(
"Error writing settings file.\n"
+ "Changes are only saved for this session."
)