-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathconfig.py
74 lines (59 loc) · 2.15 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
from watchfiles import watch
from threading import Thread
from platformdirs import user_config_dir
import json
from os import path, makedirs
from log import get_logger
logger = get_logger(__name__)
class Config:
config_dir = user_config_dir(appname="randwall")
config_file_path = f'{config_dir}/config.json'
default_config = {
'api_key': '',
'categories': {
'general': True,
'anime': True,
'people': True,
},
'purity': {
'sfw': True,
'sketchy': False,
'nsfw': False,
},
"tags": "-microsoft -logo +trees",
'interval': 30,
'keep': 10,
}
def __init__(self):
self.raw_config = self._load_config()
self._write_config()
self._watch_config_changes()
def __getitem__(self, arg):
return self.raw_config[arg]
def _write_config(self):
if not path.exists(self.config_dir):
makedirs(self.config_dir)
logger.info(f"Writing config to {self.config_file_path}")
with open(self.config_file_path, 'wt') as f:
json.dump(self.raw_config, f, sort_keys=True, indent=2)
def _load_config(self):
if not path.exists(self.config_file_path):
logger.info(f"No config file found at {self.config_file_path}. Loading default config...")
return self.default_config
with open(self.config_file_path, 'rt') as f:
logger.info(f"Reading config from {self.config_file_path}")
try:
loaded_config = json.load(f)
return {**self.default_config, **loaded_config}
except:
logger.error(f"An error occured when reading config from {self.config_file_path}. Loading default config...")
return self.default_config
def _watch_config_changes(self):
def watch_config():
logger.info(f"Watch config changes")
for changes in watch(self.config_file_path):
self.raw_config = self._load_config()
thread = Thread(target=watch_config)
thread.daemon = True
thread.start()
config = Config()