Skip to content

Commit 09bb081

Browse files
authored
First
v1
0 parents  commit 09bb081

File tree

2 files changed

+495
-0
lines changed

2 files changed

+495
-0
lines changed

config.py

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
import json
2+
3+
class Config:
4+
def __init__(self, config=None, path='config.json') -> None:
5+
if config is None:
6+
config = {}
7+
self.config = config
8+
self.path = path
9+
10+
def read(self, config_key=None) -> any:
11+
try:
12+
with open(self.path, "r") as config_file:
13+
self.config = json.load(config_file)
14+
except(FileNotFoundError, json.JSONDecodeError):
15+
self.save(self.config)
16+
17+
if config_key is None:
18+
return self.config
19+
else:
20+
return self.config.get(config_key)
21+
22+
def save(self, config: dict) -> None:
23+
self.config = config
24+
try:
25+
with open(self.path, "w") as config_file:
26+
json.dump(self.config, config_file, indent=4)
27+
except(FileNotFoundError, IOError):
28+
print("Ошибка записи в файл 'config.json'")
29+
30+
31+
def update(self, key: str, value: any) -> None:
32+
self.config = self.read()
33+
self.config[key] = value
34+
self.save(self.config)

0 commit comments

Comments
 (0)