From b3fd31c524f7aea9fba408ff52b50b52e2bc878e Mon Sep 17 00:00:00 2001 From: Lynne Date: Mon, 24 Aug 2020 01:09:08 +1000 Subject: [PATCH] added backup feature, remove save bug --- settings.py | 15 +++++++++++---- 1 file changed, 11 insertions(+), 4 deletions(-) diff --git a/settings.py b/settings.py index 5828100..95451ca 100644 --- a/settings.py +++ b/settings.py @@ -15,7 +15,7 @@ class BuypeebSettings: self.jwatchlist = {} self.location = location - def save(self): + def save(self, backup = False): for id, item in self.watchlist.items(): self.jwatchlist[id] = { "name": item.name, @@ -23,15 +23,20 @@ class BuypeebSettings: "favourite": False } - self.watchlist = {} + # create a shallow copy to avoid deleting the actual watchlist + out_dict = self.__dict__.copy() + out_dict['watchlist'] = {} - print(self.__dict__) - json.dump(self.__dict__, open(self.location + "config.json", 'w')) + filename = "config.json" + if backup: + filename += ".bak" + json.dump(out_dict, open(self.location + filename, 'w')) def load(self): if not path.isfile(self.location + "config.json"): os.makedirs(self.location, 0o755, True) open(self.location + "config.json", 'x') + open(self.location + "config.json.bak", 'x') else: try: j = json.load(open(self.location + "config.json", 'r')) @@ -42,6 +47,8 @@ class BuypeebSettings: self.jwatchlist = {} + self.save(backup = True) + except: raise print("Couldn't load settings - using defaults")