From b68c847b954483d5c4fb596af58bafceb4c70840 Mon Sep 17 00:00:00 2001 From: RGBCube <78925721+RGBCube@users.noreply.github.com> Date: Fri, 14 Jan 2022 20:49:46 +0300 Subject: [PATCH] Update db.py --- db.py | 16 +++++++++++++--- 1 file changed, 13 insertions(+), 3 deletions(-) diff --git a/db.py b/db.py index 1cad59d..3cfb37f 100644 --- a/db.py +++ b/db.py @@ -2,10 +2,9 @@ import json import os -class ClutterDB: +class Utils: - def __init__(self, path_to_json: str): - self.path_to_json = path_to_json + def json_check(self, path_to_json): if not os.path.isfile(path_to_json): with open(path_to_json, "w") as json_file: json.dump({}, json_file) @@ -14,7 +13,14 @@ class ClutterDB: with open(path_to_json, "w") as json_file: json.dump({}, json_file) +class ClutterDB: + + def __init__(self, path_to_json: str): + self.path_to_json = path_to_json + self.utils = Utils() + def set(self, key: str, value): + self.utils.json_check(self.path_to_json) with open(self.path_to_json, mode="r") as json_file: json_data = json.load(json_file) with open(self.path_to_json, mode="w") as json_file: @@ -22,15 +28,18 @@ class ClutterDB: json.dump(json_data, json_file, indent=4) def get(self, key: str, *, default=None): + self.utils.json_check(self.path_to_json) with open(self.path_to_json, mode="r") as json_file: json_data = json.load(json_file) return json_data.get(key, default) def all(self): + self.utils.json_check(self.path_to_json) with open(self.path_to_json, mode="r") as json_file: return json.load(json_file) def rem(self, key: str): + self.utils.json_check(self.path_to_json) with open(self.path_to_json, mode="r") as json_file: json_data = json.load(json_file) with open(self.path_to_json, mode="w") as json_file: @@ -38,5 +47,6 @@ class ClutterDB: json.dump(json_data, json_file, indent=4) def nuke(self): + self.utils.json_check(self.path_to_json) with open(self.path_to_json, mode="w") as json_file: json.dump({}, json_file)