Skip to content

Commit

Permalink
fix #113 (hopefully)
Browse files Browse the repository at this point in the history
  • Loading branch information
s0lst1ce committed May 16, 2020
1 parent 9b2c470 commit 0f97310
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 7 deletions.
8 changes: 5 additions & 3 deletions src/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -79,14 +79,16 @@
#########################################

# Files
EXT_FOLDER = "exts"
EXTENSIONS_FILE = "enabled_exts.json"
SLAPPING_FOLDER = "slapping"

#folders
CONFIG_FOLDER = "servers"
TODO_FOLDER = "todo"
SLAPPING_FOLDER = "slapping"
TIME_FOLDER = "countdowns"
REMINDERS_FOLDER = "remind"
EVENT_FOLDER = "events"
EXT_FOLDER = "exts"
TODO_FOLDER = "todo"
# REMINDERS_FOLDER = os.path.join(TIME_FOLDER, "users")
# EVENT_FOLDER = os.path.join(TIME_FOLDER, "events")
LANG_FOLDER = "lang"
Expand Down
21 changes: 17 additions & 4 deletions src/utilities.py
Original file line number Diff line number Diff line change
Expand Up @@ -239,15 +239,14 @@ def __init__(
self.default = self.data = ConfigFile.folder_mapping[folder]

# currently only supports "json" files
# assert fext=="json", local_logger.error(f'''Can't load file with extension: {fext}''')
# if the extension is correct -> appending the extension name to the filename
self.file += "." + self.fext
# making path
self.path = os.path.join(self.folder, self.file)
# self.last_m_time = os.path.getmtime(self.path)

def __enter__(self):
self.make_file()
if not self.make_file():
return self.default
self.read()
return self

Expand Down Expand Up @@ -275,8 +274,20 @@ def make_file(self):
return False
return True

def is_json(self, file):
try:
out = json.loads(file)
except:
return False
return True

def save(self):
"""makes the file from the dict"""
"""makes the file from the dict
can we really do anything if it fails to write? Save the original contents are write them again?"""
if not self.is_json(self.data):
raise ValueError(f"Incorrect JSON for updated {self.file}")
local_logger.critical(f"Discarding changes to {self.file} because they invalidated the JSON file.")

try:
with open(self.path, "w", encoding="utf-8") as file:
json.dump(self.data, file)
Expand All @@ -298,6 +309,8 @@ def read(self):
local_logger.error(f"An exception occured while reading {self.file}.")
local_logger.exception(e)
raise e
return self.default



class Translator(object):
Expand Down

0 comments on commit 0f97310

Please sign in to comment.