-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpreferences.py
52 lines (43 loc) · 1.58 KB
/
preferences.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
import os.path
import json
from util import is_macos, is_darkmode
class Preferences:
pref_file = os.path.join(os.getcwd(), "prefs", "config.json")
defaults = os.path.join(os.getcwd(), "prefs", "defaults.json")
copy_list = [
'report_sheet',
'format_unfinished',
'unfinished_color',
'class_portal',
'protected_set',
'debug'
]
def __init__(self):
with open(Preferences.defaults, "r+") as df:
self.defaults = json.load(df)
if not os.path.isfile(Preferences.pref_file):
with open(Preferences.pref_file, 'w+') as pf:
build_prefs = self.get_default_theme()
for pref in Preferences.copy_list:
build_prefs[pref] = self.defaults[pref]
json.dump(build_prefs, pf)
with open(Preferences.pref_file, "r+") as pf:
self.prefs = json.load(pf)
self.pref_file = Preferences.pref_file
def has_pref(self, key):
return key in self.prefs
def get_pref(self, key):
if self.has_pref(key): return self.prefs[key]
return None
def update_pref(self, key, value):
self.prefs[key] = value
self.save_prefs()
def save_prefs(self):
with open(Preferences.pref_file, "w+") as pf:
json.dump(self.prefs, pf)
def get_default_pref(self, key):
if key in self.defaults: return self.defaults[key]
return None
def get_default_theme(self):
return self.defaults['dark_theme'] if is_darkmode() else self.defaults['light_theme']
prefs = Preferences()