mirror of
https://github.com/RGBCube/JsonWrapper
synced 2025-07-27 11:47:45 +00:00
Update db.py
This commit is contained in:
parent
961aa9ff57
commit
b68c847b95
1 changed files with 13 additions and 3 deletions
16
db.py
16
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)
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue