From bb1539ccedf6e654db257615ccb459f05cd6591a Mon Sep 17 00:00:00 2001 From: RGBCube <78925721+RGBCube@users.noreply.github.com> Date: Wed, 26 Jan 2022 22:14:06 +0300 Subject: [PATCH] Add files via upload --- README.md | 43 ++++++++++----------- json_wrapper.py | 101 ++++++++++++++++++++++++++++++++++++++++++++++++ setup.py | 15 +++++++ 3 files changed, 137 insertions(+), 22 deletions(-) create mode 100644 json_wrapper.py create mode 100644 setup.py diff --git a/README.md b/README.md index e178572..0a73fca 100644 --- a/README.md +++ b/README.md @@ -1,12 +1,11 @@ -# 🈷️ JSONx +# 🈷️ JsonWrapper Easy JSON wrapper packed with features. - -This was made for small discord bots, for big bots you should not use this JSON wrapper. # 📥 Usage -Clone [this](https://github.com/RGBCube/JSONx/blob/main/db.py) file into your project folder. +Execute `pip install json_wrapper`. -Add `from db import JSONx` to the top of your project. +Add `from json_wrapper import JsonWrapper` to the top of your project. # 📄 Docs +> Assume that we did `db = JsonWrapper("example.json)` ## `db.set(key: str, value, *, pathmagic="")` Sets the key to the value in the JSON. @@ -30,14 +29,14 @@ Deletes everything in the JSON. Use with caution. # 📘 Examples -> Assume that the `db.json` file is empty +> Assume that the `example.json` file is empty ## `db.set()` ### Normal usage Code ```python -from db import JSONx +from json_wrapper import JsonWrapper -db = JSONx("db.json") +db = JsonWrapper("example.json") db.set("test", 123) @@ -52,9 +51,9 @@ Output ### Using with `pathmagic` kwarg Code ```python -from db import JSONx +from json_wrapper import JsonWrapper -db = JSONx("db.json") +db = JsonWrapper("example.json") db.set("test", 123, pathmagic="a+b+c") @@ -70,9 +69,9 @@ Output ### Normal usage Code ```python -from db import JSONx +from json_wrapper import JsonWrapper -db = JSONx("db.json") +db = JsonWrapper("example.json") db.set("test", 123) @@ -87,9 +86,9 @@ Output ### Using without `default` kwarg Code ```python -from db import JSONx +from json_wrapper import JsonWrapper -db = JSONx("db.json") +db = JsonWrapper("example.json") data = db.get("test") @@ -102,9 +101,9 @@ None ### Using with `default` kwarg Code ```python -from db import JSONx +from json_wrapper import JsonWrapper -db = JSONx("db.json") +db = JsonWrapper("example.json") data = db.get("test", default=123) @@ -117,9 +116,9 @@ Output ### Using with `pathmagic` kwarg Code ```python -from db import JSONx +from json_wrapper import JsonWrapper -db = JSONx("db.json") +db = JsonWrapper("example.json") db.set("test", 123, pathmagic="a+b+c") @@ -135,9 +134,9 @@ Output ### Normal usage Code ```python -from db import JSONx +from json_wrapper import JsonWrapper -db = JSONx("db.json") +db = JsonWrapper("example.json") db.set("test", 123) @@ -159,9 +158,9 @@ Output ### Using with `pathmagic` kwarg Code ```python -from db import JSONx +from json_wrapper import JsonWrapper -db = JSONx("db.json") +db = JsonWrapper("example.json") db.set("test", 123, pathmagic="a+b+c") diff --git a/json_wrapper.py b/json_wrapper.py new file mode 100644 index 0000000..ce98540 --- /dev/null +++ b/json_wrapper.py @@ -0,0 +1,101 @@ +import json +import os + + +class Utils: + + @staticmethod + def validate_json(path_to_json): + if not os.path.isfile(path_to_json): + with open(path_to_json, "w") as json_file: + json.dump({}, json_file) + else: + if os.path.getsize(path_to_json) == 0: + with open(path_to_json, "w") as json_file: + json.dump({}, json_file) + + class PathMagic: + + @staticmethod + def set(main_dict: dict, path: str, *, key: str, value): + def magic(alt_dict: dict, key: str): + if key in alt_dict.keys() and isinstance(alt_dict[key], dict): + return alt_dict + alt_dict[key] = {} + return alt_dict + main_dict_ref, i = main_dict, 0 + for dict_name in path.split("+"): + i += 1 + main_dict = magic(main_dict, dict_name)[dict_name] + if i == len(path.split("+")): + main_dict[key] = value + return main_dict_ref + + @staticmethod + def get(main_dict: dict, path: str, *, key, default=None): + for dict_name in path.split("+"): + try: + main_dict = main_dict[dict_name] + except (KeyError, TypeError, AttributeError): + return 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 JsonWrapper: + + def __init__(self, path_to_json: str): + self.path_to_json = path_to_json + self.utils = Utils + self.utils.validate_json(path_to_json) + + def set(self, key: str, value, *, pathmagic=""): + self.utils.validate_json(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: + if pathmagic == "": + json_data[key] = value + json.dump(json_data, json_file, indent=4) + else: + 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=""): + self.utils.validate_json(self.path_to_json) + with open(self.path_to_json, mode="r") as json_file: + json_data = json.load(json_file) + if pathmagic == "": + return json_data.get(key, default) + else: + return self.utils.PathMagic.get(json_data, pathmagic, key=key, default=default) + + def all(self): + self.utils.validate_json(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, *, pathmagic=""): + self.utils.validate_json(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: + if pathmagic == "": + json_data.pop(key, None) + 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): + with open(self.path_to_json, mode="w") as json_file: + json.dump({}, json_file) diff --git a/setup.py b/setup.py new file mode 100644 index 0000000..be0fd2d --- /dev/null +++ b/setup.py @@ -0,0 +1,15 @@ +from setuptools import setup + +readme = "" +with open("README.md") as file: + readme = file.read() + +setup( + name="json_wrapper", + description="Easy JSON wrapper packed with features", + url="https://github.com/RGBCube/json_wrapper", + version="1.0", + author="RGBCube", + py_modules=["json_wrapper"], + license="CC0 1.0 Universal" +) \ No newline at end of file