-
Notifications
You must be signed in to change notification settings - Fork 31
/
config.py
40 lines (35 loc) · 1.24 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
import json
import os
class Config:
file_path = None
dir_path = None
data = None
def __init__(self, file_path, dir_path):
self.file_path = file_path
self.dir_path = dir_path
self.data = {}
def load(self):
try:
with open(self.file_path, 'r', encoding='utf-8') as f:
self.data = json.load(f) or {}
except FileNotFoundError:
try:
if not os.path.isdir(self.dir_path):
os.makedirs(self.dir_path)
with open(self.file_path, 'w'):
pass
except Exception as e:
print('error: ', e)
print('error: while creating config file: ' + self.file_path)
except Exception as e:
print('error: ', e)
print('error: while reading config file: ' + self.file_path)
def save(self):
try:
if not os.path.isdir(self.dir_path):
os.makedirs(self.dir_path)
with open(self.file_path, 'w') as f:
json.dump(self.data, f, sort_keys=True, indent=4)
except Exception as e:
print('error: ', e)
print('error: while saving config file: ' + self.file_path)