-
Notifications
You must be signed in to change notification settings - Fork 0
/
config.py
36 lines (28 loc) · 1.08 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
from pathlib import Path
from subprocess import CalledProcessError
import json
from helpers import get_new_token
from models import Setting
CONFIG = Path('/etc/service-status-indicator/.config.json')
TOKEN = 'insecure'
DEFAULT_REFRESH_INTERVAL = 60
# Initialize the configuration file
if not CONFIG.exists() or CONFIG.stat().st_size == 0:
try:
TOKEN = get_new_token()
except CalledProcessError as err:
print('Error on initial token generation (openssl missing?)')
with open(CONFIG, 'w', encoding='utf8') as file:
json.dump({
'default-refresh-interval': DEFAULT_REFRESH_INTERVAL,
'token': TOKEN
}, file)
def get_setting(setting: Setting):
with open(CONFIG, 'r', encoding='utf8') as config_file:
return dict(json.load(config_file)).get(setting)
def set_setting(setting: Setting, value):
with open(CONFIG, 'r', encoding='utf8') as config_file:
config = dict(json.load(config_file))
config[setting] = value
with open(CONFIG, 'w', encoding='utf8') as config_file:
json.dump(config, config_file)