mirror of
https://github.com/RGBCube/JsonWrapper
synced 2025-07-27 11:47:45 +00:00
Add files via upload
This commit is contained in:
parent
6b46cf3fee
commit
bb1539cced
3 changed files with 137 additions and 22 deletions
43
README.md
43
README.md
|
@ -1,12 +1,11 @@
|
||||||
# 🈷️ JSONx
|
# 🈷️ JsonWrapper
|
||||||
Easy JSON wrapper packed with features.
|
Easy JSON wrapper packed with features.
|
||||||
|
|
||||||
This was made for small discord bots, for big bots you should not use this JSON wrapper.
|
|
||||||
# 📥 Usage
|
# 📥 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
|
# 📄 Docs
|
||||||
|
> Assume that we did `db = JsonWrapper("example.json)`
|
||||||
## `db.set(key: str, value, *, pathmagic="")`
|
## `db.set(key: str, value, *, pathmagic="")`
|
||||||
Sets the key to the value in the JSON.
|
Sets the key to the value in the JSON.
|
||||||
|
|
||||||
|
@ -30,14 +29,14 @@ Deletes everything in the JSON.
|
||||||
|
|
||||||
Use with caution.
|
Use with caution.
|
||||||
# 📘 Examples
|
# 📘 Examples
|
||||||
> Assume that the `db.json` file is empty
|
> Assume that the `example.json` file is empty
|
||||||
## `db.set()`
|
## `db.set()`
|
||||||
### Normal usage
|
### Normal usage
|
||||||
Code
|
Code
|
||||||
```python
|
```python
|
||||||
from db import JSONx
|
from json_wrapper import JsonWrapper
|
||||||
|
|
||||||
db = JSONx("db.json")
|
db = JsonWrapper("example.json")
|
||||||
|
|
||||||
db.set("test", 123)
|
db.set("test", 123)
|
||||||
|
|
||||||
|
@ -52,9 +51,9 @@ Output
|
||||||
### Using with `pathmagic` kwarg
|
### Using with `pathmagic` kwarg
|
||||||
Code
|
Code
|
||||||
```python
|
```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")
|
db.set("test", 123, pathmagic="a+b+c")
|
||||||
|
|
||||||
|
@ -70,9 +69,9 @@ Output
|
||||||
### Normal usage
|
### Normal usage
|
||||||
Code
|
Code
|
||||||
```python
|
```python
|
||||||
from db import JSONx
|
from json_wrapper import JsonWrapper
|
||||||
|
|
||||||
db = JSONx("db.json")
|
db = JsonWrapper("example.json")
|
||||||
|
|
||||||
db.set("test", 123)
|
db.set("test", 123)
|
||||||
|
|
||||||
|
@ -87,9 +86,9 @@ Output
|
||||||
### Using without `default` kwarg
|
### Using without `default` kwarg
|
||||||
Code
|
Code
|
||||||
```python
|
```python
|
||||||
from db import JSONx
|
from json_wrapper import JsonWrapper
|
||||||
|
|
||||||
db = JSONx("db.json")
|
db = JsonWrapper("example.json")
|
||||||
|
|
||||||
data = db.get("test")
|
data = db.get("test")
|
||||||
|
|
||||||
|
@ -102,9 +101,9 @@ None
|
||||||
### Using with `default` kwarg
|
### Using with `default` kwarg
|
||||||
Code
|
Code
|
||||||
```python
|
```python
|
||||||
from db import JSONx
|
from json_wrapper import JsonWrapper
|
||||||
|
|
||||||
db = JSONx("db.json")
|
db = JsonWrapper("example.json")
|
||||||
|
|
||||||
data = db.get("test", default=123)
|
data = db.get("test", default=123)
|
||||||
|
|
||||||
|
@ -117,9 +116,9 @@ Output
|
||||||
### Using with `pathmagic` kwarg
|
### Using with `pathmagic` kwarg
|
||||||
Code
|
Code
|
||||||
```python
|
```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")
|
db.set("test", 123, pathmagic="a+b+c")
|
||||||
|
|
||||||
|
@ -135,9 +134,9 @@ Output
|
||||||
### Normal usage
|
### Normal usage
|
||||||
Code
|
Code
|
||||||
```python
|
```python
|
||||||
from db import JSONx
|
from json_wrapper import JsonWrapper
|
||||||
|
|
||||||
db = JSONx("db.json")
|
db = JsonWrapper("example.json")
|
||||||
|
|
||||||
db.set("test", 123)
|
db.set("test", 123)
|
||||||
|
|
||||||
|
@ -159,9 +158,9 @@ Output
|
||||||
### Using with `pathmagic` kwarg
|
### Using with `pathmagic` kwarg
|
||||||
Code
|
Code
|
||||||
```python
|
```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")
|
db.set("test", 123, pathmagic="a+b+c")
|
||||||
|
|
||||||
|
|
101
json_wrapper.py
Normal file
101
json_wrapper.py
Normal file
|
@ -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)
|
15
setup.py
Normal file
15
setup.py
Normal file
|
@ -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"
|
||||||
|
)
|
Loading…
Add table
Add a link
Reference in a new issue