-
Notifications
You must be signed in to change notification settings - Fork 0
/
data_manager.py
37 lines (28 loc) · 989 Bytes
/
data_manager.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
import json
import logging
from os import path
from typing import Any
from utilities import PathLikeString
class DataManager:
def __init__(self, data_file: PathLikeString) -> None:
self._data_file = path.join(path.dirname(path.abspath(__file__)), data_file)
@property
def data_file(self) -> PathLikeString:
return self._data_file
def load(self) -> Any:
try:
with open(self.data_file, 'r', encoding='utf-8') as file:
return json.load(file)
except (FileNotFoundError, IsADirectoryError):
logging.critical(f'File {self.data_file} doesn\'t exist')
exit()
except PermissionError:
logging.critical(f'Cannot access {self.data_file}')
exit()
def write(self, key: str, value: Any) -> None:
data: Any = self.load()
data[key] = value
with open(self.data_file, 'w', encoding='utf-8') as file:
json.dump(data, file, indent='\t')
data_manager: DataManager = DataManager('data.json')
translation_manager: DataManager = DataManager('translations.json')