Skip to content

Commit 0f97310

Browse files
committed
fix #113 (hopefully)
1 parent 9b2c470 commit 0f97310

File tree

2 files changed

+22
-7
lines changed

2 files changed

+22
-7
lines changed

src/settings.py

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -79,14 +79,16 @@
7979
#########################################
8080

8181
# Files
82-
EXT_FOLDER = "exts"
8382
EXTENSIONS_FILE = "enabled_exts.json"
84-
SLAPPING_FOLDER = "slapping"
83+
84+
#folders
8585
CONFIG_FOLDER = "servers"
86-
TODO_FOLDER = "todo"
86+
SLAPPING_FOLDER = "slapping"
8787
TIME_FOLDER = "countdowns"
8888
REMINDERS_FOLDER = "remind"
8989
EVENT_FOLDER = "events"
90+
EXT_FOLDER = "exts"
91+
TODO_FOLDER = "todo"
9092
# REMINDERS_FOLDER = os.path.join(TIME_FOLDER, "users")
9193
# EVENT_FOLDER = os.path.join(TIME_FOLDER, "events")
9294
LANG_FOLDER = "lang"

src/utilities.py

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -239,15 +239,14 @@ def __init__(
239239
self.default = self.data = ConfigFile.folder_mapping[folder]
240240

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

249247
def __enter__(self):
250-
self.make_file()
248+
if not self.make_file():
249+
return self.default
251250
self.read()
252251
return self
253252

@@ -275,8 +274,20 @@ def make_file(self):
275274
return False
276275
return True
277276

277+
def is_json(self, file):
278+
try:
279+
out = json.loads(file)
280+
except:
281+
return False
282+
return True
283+
278284
def save(self):
279-
"""makes the file from the dict"""
285+
"""makes the file from the dict
286+
can we really do anything if it fails to write? Save the original contents are write them again?"""
287+
if not self.is_json(self.data):
288+
raise ValueError(f"Incorrect JSON for updated {self.file}")
289+
local_logger.critical(f"Discarding changes to {self.file} because they invalidated the JSON file.")
290+
280291
try:
281292
with open(self.path, "w", encoding="utf-8") as file:
282293
json.dump(self.data, file)
@@ -298,6 +309,8 @@ def read(self):
298309
local_logger.error(f"An exception occured while reading {self.file}.")
299310
local_logger.exception(e)
300311
raise e
312+
return self.default
313+
301314

302315

303316
class Translator(object):

0 commit comments

Comments
 (0)