mirror of
https://github.com/RGBCube/JsonWrapper
synced 2025-07-28 04:07:45 +00:00
pathmagic to rem and staticmethods
This commit is contained in:
parent
242a94e86f
commit
55a196c982
1 changed files with 52 additions and 28 deletions
40
db.py
40
db.py
|
@ -4,7 +4,8 @@ import os
|
||||||
|
|
||||||
class Utils:
|
class Utils:
|
||||||
|
|
||||||
def validate_json(self, path_to_json):
|
@staticmethod
|
||||||
|
def validate_json(path_to_json):
|
||||||
if not os.path.isfile(path_to_json):
|
if not os.path.isfile(path_to_json):
|
||||||
with open(path_to_json, "w") as json_file:
|
with open(path_to_json, "w") as json_file:
|
||||||
json.dump({}, json_file)
|
json.dump({}, json_file)
|
||||||
|
@ -13,7 +14,10 @@ class Utils:
|
||||||
with open(path_to_json, "w") as json_file:
|
with open(path_to_json, "w") as json_file:
|
||||||
json.dump({}, json_file)
|
json.dump({}, json_file)
|
||||||
|
|
||||||
def path_magic_set(self, main_dict: dict, path: str, *, key: str, value):
|
class PathMagic:
|
||||||
|
|
||||||
|
@staticmethod
|
||||||
|
def set(main_dict: dict, path: str, *, key: str, value):
|
||||||
def magic(alt_dict: dict, key: str):
|
def magic(alt_dict: dict, key: str):
|
||||||
if key in alt_dict.keys():
|
if key in alt_dict.keys():
|
||||||
return alt_dict
|
return alt_dict
|
||||||
|
@ -28,19 +32,33 @@ class Utils:
|
||||||
main_dict[key] = value
|
main_dict[key] = value
|
||||||
return main_dict_ref
|
return main_dict_ref
|
||||||
|
|
||||||
def path_magic_get(self, main_dict: dict, path: str, *, key, default=None):
|
@staticmethod
|
||||||
|
def get(main_dict: dict, path: str, *, key, default=None):
|
||||||
for dict_name in path.split("+"):
|
for dict_name in path.split("+"):
|
||||||
try:
|
try:
|
||||||
main_dict = main_dict[dict_name]
|
main_dict = main_dict[dict_name]
|
||||||
except (KeyError, AttributeError):
|
except (KeyError, TypeError, AttributeError):
|
||||||
return default
|
return default
|
||||||
return main_dict.get(key, default)
|
return main_dict.get(key, default)
|
||||||
|
|
||||||
|
@staticmethod
|
||||||
|
def rem(main_dict: dict, path: str, *, key):
|
||||||
|
main_dict_ref, i = main_dict, 0
|
||||||
|
for dict_name in path.split("+"):
|
||||||
|
try:
|
||||||
|
i += 1
|
||||||
|
main_dict = main_dict[dict_name]
|
||||||
|
if i == len(path.split("+")):
|
||||||
|
main_dict.pop(key, None)
|
||||||
|
except (KeyError, TypeError, AttributeError):
|
||||||
|
return main_dict_ref
|
||||||
|
return main_dict_ref
|
||||||
|
|
||||||
class JSONx:
|
class JSONx:
|
||||||
|
|
||||||
def __init__(self, path_to_json: str):
|
def __init__(self, path_to_json: str):
|
||||||
self.path_to_json = path_to_json
|
self.path_to_json = path_to_json
|
||||||
self.utils = Utils()
|
self.utils = Utils
|
||||||
self.utils.validate_json(path_to_json)
|
self.utils.validate_json(path_to_json)
|
||||||
|
|
||||||
def set(self, key: str, value, *, pathmagic=""):
|
def set(self, key: str, value, *, pathmagic=""):
|
||||||
|
@ -52,26 +70,32 @@ class JSONx:
|
||||||
json_data[key] = value
|
json_data[key] = value
|
||||||
json.dump(json_data, json_file, indent=4)
|
json.dump(json_data, json_file, indent=4)
|
||||||
else:
|
else:
|
||||||
json.dump(self.utils.path_magic_set(json_data, pathmagic, key=key, value=value), json_file, indent=4)
|
json.dump(self.utils.PathMagic.set(json_data, pathmagic, key=key, value=value), json_file, indent=4)
|
||||||
|
|
||||||
def get(self, key: str, *, default=None, pathmagic=""):
|
def get(self, key: str, *, default=None, pathmagic=""):
|
||||||
self.utils.validate_json(self.path_to_json)
|
self.utils.validate_json(self.path_to_json)
|
||||||
with open(self.path_to_json, mode="r") as json_file:
|
with open(self.path_to_json, mode="r") as json_file:
|
||||||
json_data = json.load(json_file)
|
json_data = json.load(json_file)
|
||||||
return json_data.get(key, default) if pathmagic == "" else return self.utils.path_magic_get(json_data, pathmagic, key=key, default=default)
|
if pathmagic == "":
|
||||||
|
return json_data.get(key, default)
|
||||||
|
else:
|
||||||
|
return self.utils.PathMagic.get(json_data, pathmagic, key=key, default=default)
|
||||||
|
|
||||||
def all(self):
|
def all(self):
|
||||||
self.utils.validate_json(self.path_to_json)
|
self.utils.validate_json(self.path_to_json)
|
||||||
with open(self.path_to_json, mode="r") as json_file:
|
with open(self.path_to_json, mode="r") as json_file:
|
||||||
return json.load(json_file)
|
return json.load(json_file)
|
||||||
|
|
||||||
def rem(self, key: str):
|
def rem(self, key: str, *, pathmagic=""):
|
||||||
self.utils.validate_json(self.path_to_json)
|
self.utils.validate_json(self.path_to_json)
|
||||||
with open(self.path_to_json, mode="r") as json_file:
|
with open(self.path_to_json, mode="r") as json_file:
|
||||||
json_data = json.load(json_file)
|
json_data = json.load(json_file)
|
||||||
with open(self.path_to_json, mode="w") as json_file:
|
with open(self.path_to_json, mode="w") as json_file:
|
||||||
|
if pathmagic == "":
|
||||||
json_data.pop(key, None)
|
json_data.pop(key, None)
|
||||||
json.dump(json_data, json_file, indent=4)
|
json.dump(json_data, json_file, indent=4)
|
||||||
|
else:
|
||||||
|
json.dump(self.utils.PathMagic.rem(json_data, pathmagic, key=key), json_file, indent=4)
|
||||||
|
|
||||||
def nuke(self):
|
def nuke(self):
|
||||||
with open(self.path_to_json, mode="w") as json_file:
|
with open(self.path_to_json, mode="w") as json_file:
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue