added backup feature, remove save bug

This commit is contained in:
Lynne Megido 2020-08-24 01:09:08 +10:00
parent e519907f9d
commit b3fd31c524
Signed by: lynnesbian
GPG Key ID: F0A184B5213D9F90

View File

@ -15,7 +15,7 @@ class BuypeebSettings:
self.jwatchlist = {} self.jwatchlist = {}
self.location = location self.location = location
def save(self): def save(self, backup = False):
for id, item in self.watchlist.items(): for id, item in self.watchlist.items():
self.jwatchlist[id] = { self.jwatchlist[id] = {
"name": item.name, "name": item.name,
@ -23,15 +23,20 @@ class BuypeebSettings:
"favourite": False "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__) filename = "config.json"
json.dump(self.__dict__, open(self.location + "config.json", 'w')) if backup:
filename += ".bak"
json.dump(out_dict, open(self.location + filename, 'w'))
def load(self): def load(self):
if not path.isfile(self.location + "config.json"): if not path.isfile(self.location + "config.json"):
os.makedirs(self.location, 0o755, True) os.makedirs(self.location, 0o755, True)
open(self.location + "config.json", 'x') open(self.location + "config.json", 'x')
open(self.location + "config.json.bak", 'x')
else: else:
try: try:
j = json.load(open(self.location + "config.json", 'r')) j = json.load(open(self.location + "config.json", 'r'))
@ -42,6 +47,8 @@ class BuypeebSettings:
self.jwatchlist = {} self.jwatchlist = {}
self.save(backup = True)
except: except:
raise raise
print("Couldn't load settings - using defaults") print("Couldn't load settings - using defaults")