-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathconfig.py
94 lines (71 loc) · 2.24 KB
/
config.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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
import json
import shutil
import os
from SystemInfo import SystemInfo
from typing import Any
from screenshotters import availableTools
sysInfo = SystemInfo()
defaultConfig: dict[str, Any] = {
"ENABLED": True,
"AUTO_DELETE_TYPE": "Disabled",
"DELETE_AFTER_PERIOD": 1,
"SCREENSHOT_FREQUENCY_MS": 3_600_000, # once per hour
"SAVE_LOCATION": sysInfo.getLocation(sysInfo.dbPath),
"DATE_FORMAT": r"%d.%m. %Y %H:%M.%S",
"SCREENSHOT_TOOL": ("Default" if not sysInfo.isWayland else (availableTools[0] if availableTools else "N/A")),
"USE_PASSWORD": False,
"BASEKEY": "",
"SALT": ""
}
config: dict[str, Any] = {}
savePath = f"{sysInfo.dataDir}/settings.json"
def makeConfigIfNotExists() -> None:
file = None
try:
file = open(savePath, "r")
except:
if sysInfo.info:
print("No existing configuration file. (created)")
file = open(savePath, "w")
file.write(json.dumps(defaultConfig))
finally:
if file:
file.close()
def loadConfig():
global config
data = {}
makeConfigIfNotExists()
with open(savePath, "r") as settingsFile:
settings = settingsFile.read()
data = json.loads(settings)
targetKeys = list(defaultConfig.keys())
for i in targetKeys:
if i not in data:
data[i] = defaultConfig[i]
if data["SCREENSHOT_TOOL"] == "Default" and sysInfo.isWayland:
data["SCREENSHOT_TOOL"] = defaultConfig["SCREENSHOT_TOOL"]
config = data
def saveConfig() -> bool:
shutil.copyfile(savePath, f"{savePath}.bak")
fileSaved: bool = _saveConfigFile()
if not fileSaved:
shutil.move(f"{savePath}.bak", savePath)
return False
else:
os.remove(f"{savePath}.bak")
return True
def _saveConfigFile() -> bool:
succ: bool = False
try:
with open(savePath, "w") as f:
f.write(json.dumps(config))
succ = True
except:
succ = False
return succ
def get(setting: str) -> Any:
if setting in config:
return config[setting]
def set(setting: str, value: Any) -> bool:
config[setting] = value
return saveConfig()