1
0
Fork 0
mirror of synced 2024-07-16 03:35:55 +12:00
bulk-downloader-for-reddit/bulkredditdownloader/json_helper.py

58 lines
1.7 KiB
Python
Raw Normal View History

import json
2021-02-07 01:29:13 +13:00
import os
2021-02-07 14:05:18 +13:00
from bulkredditdownloader.errors import InvalidJSONFile
class JsonFile:
""" Write and read JSON files
Use add(self,toBeAdded) to add to files
Use delete(self,*deletedKeys) to delete keys
"""
file_dir = ""
2021-02-07 01:29:13 +13:00
def __init__(self, file_dir: str):
self.file_dir = file_dir
2021-02-07 01:29:13 +13:00
if not os.path.exists(self.file_dir):
self.__writeToFile({}, create=True)
2021-02-07 01:29:13 +13:00
def read(self) -> dict:
try:
with open(self.file_dir, 'r') as f:
return json.load(f)
except json.decoder.JSONDecodeError:
raise InvalidJSONFile(f"{self.file_dir} cannot be read")
2021-02-07 01:29:13 +13:00
def add(self, to_be_added: dict, sub=None) -> dict:
"""Takes a dictionary and merges it with json file.
It uses new key's value if a key already exists.
Returns the new content as a dictionary.
"""
data = self.read()
if sub:
data[sub] = {**data[sub], **to_be_added}
else:
data = {**data, **to_be_added}
self.__writeToFile(data)
return self.read()
2021-02-07 01:29:13 +13:00
def delete(self, *delete_keys: str):
"""Delete given keys from JSON file.
Returns the new content as a dictionary.
"""
data = self.read()
for deleteKey in delete_keys:
if deleteKey in data:
del data[deleteKey]
found = True
if not found:
return False
self.__writeToFile(data)
2021-02-07 01:29:13 +13:00
def __writeToFile(self, content: (dict, list, tuple), create: bool = False):
if not create:
2021-02-07 01:29:13 +13:00
os.remove(self.file_dir)
with open(self.file_dir, 'w') as f:
json.dump(content, f, indent=4)