blob: 10e938f944a9dcc293a97117fdd8987a883cf03b (
plain)
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
|
# -*- coding: utf-8 -*-
import json
import os
class Config(object):
def __init__(self):
self.config = dict()
self.valid_config = bool
# filepath of the config.json in the project directory
self.path = os.path.dirname(__file__)
self.filepath = ('/'.join([self.path, 'config.json']))
# load config
self.load()
def load(self):
try:
# try to read config.json
with open(self.filepath, "r", encoding="utf-8") as f:
self.config = json.load(f)
except FileNotFoundError:
# if file is absent create file
open(self.filepath, "w").close()
except json.decoder.JSONDecodeError:
# config file is present but empty
pass
def get_at(self, attrib: str):
"""
retrieve attribute from config file
:param attrib: keyword corresponding to keyword in config dictionary
:return: value of specified keyword or False if keyword is not present in dictionary
"""
if attrib in self.config:
# return corresponding attrib from config
return self.config[attrib]
else:
# if attrib is not present in config return False
self.config[attrib] = False
def set_at(self, attrib: str, param):
"""
set attribute to parameter inside config file
:param attrib: keyword which should be updated/created in config dictionary
:param param: parameter the keyword should be updated to
"""
self.config[attrib] = param
# save new attrib to file
with open(self.filepath, "w", encoding="utf-8") as f:
f.write(json.dumps(self.config, indent=4))
def unset_at(self, attrib: str):
"""
unset attribute inside config file
:param attrib: attribute which should be unset inside config file
"""
if attrib in self.config:
# only if attrib is actually present unset it
self.config.pop(attrib)
|