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.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")