Pep8 format (#184)

* Format file to be PEP8 compliant

* Remove unused imports

* Format file to PEP8

* Format file to PEP8

* Remove unused imports

* Format file to PEP8

* Format file to PEP8

* Format file to PEP8

* Format file to PEP8

* Remove unused imports

* Format file to PEP8

* Remove unused imports

* Format file to PEP8

* Remove unused imports

* Format file to PEP8

* Format file to PEP8

* Remove unused imports

* Format file to PEP8

* Remove unused imports

* Format file to PEP8

* Remove unused imports

* Format file to PEP8

* Remove unused imports

* Format file to PEP8

* Remove unused imports

* Format file to PEP8

* Remove unused imports

* Format file to PEP8

* Remove unused imports

* Format file to PEP8

* Remove unused imports

* Format file to PEP8

* Remove unused imports

* Format file to PEP8

* Format file to PEP8

* Remove unused imports

* Format file to PEP8

* Remove unused imports

* Condense spacing
This commit is contained in:
Serene
2021-02-06 18:35:50 +10:00
committed by Ali Parlakci
parent f034c1a87c
commit 4143b86467
22 changed files with 836 additions and 1074 deletions

View File

@@ -6,47 +6,43 @@ from src.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
"""
FILEDIR = ""
file_dir = ""
def __init__(self, FILEDIR):
self.FILEDIR = FILEDIR
if not path.exists(self.FILEDIR):
def __init__(self, file_dir):
self.file_dir = file_dir
if not path.exists(self.file_dir):
self.__writeToFile({}, create=True)
def read(self):
try:
with open(self.FILEDIR, 'r') as f:
with open(self.file_dir, 'r') as f:
return json.load(f)
except json.decoder.JSONDecodeError:
raise InvalidJSONFile(f"{self.FILEDIR} cannot be read")
raise InvalidJSONFile(f"{self.file_dir} cannot be read")
def add(self, toBeAdded, sub=None):
def add(self, to_be_added, sub=None):
"""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], **toBeAdded}
data[sub] = {**data[sub], **to_be_added}
else:
data = {**data, **toBeAdded}
data = {**data, **to_be_added}
self.__writeToFile(data)
return self.read()
def delete(self, *deleteKeys):
def delete(self, *delete_keys):
"""Delete given keys from JSON file.
Returns the new content as a dictionary.
"""
data = self.read()
for deleteKey in deleteKeys:
for deleteKey in delete_keys:
if deleteKey in data:
del data[deleteKey]
found = True
@@ -56,6 +52,6 @@ class JsonFile:
def __writeToFile(self, content, create=False):
if not create:
remove(self.FILEDIR)
with open(self.FILEDIR, 'w') as f:
remove(self.file_dir)
with open(self.file_dir, 'w') as f:
json.dump(content, f, indent=4)