-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathconfigmanager.py
38 lines (30 loc) · 961 Bytes
/
configmanager.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
import json
import os
import sys
config_file = 'config.json'
# ConfigManager singleton
class ConfigManager:
_instance = None
def __new__(cls):
if cls._instance is None:
cls._instance = super(ConfigManager, cls).__new__(cls)
return cls._instance
def read_config(self):
try:
with open(config_file, 'r') as f:
return json.load(f)
except FileNotFoundError:
print("Config file {config_file} not found.")
def write_config(self, config):
with open(config_file, 'w') as f:
json.dump(config, f, indent=4)
def update_config(self, key, value):
config = self.read_config()
config[key] = value
self.write_config(config)
def delete_key(self, key):
config = self.read_config()
if key in config:
del config[key]
self.write_config(config)
config = ConfigManager().read_config()