added backup feature, remove save bug
This commit is contained in:
parent
e519907f9d
commit
b3fd31c524
1 changed files with 11 additions and 4 deletions
15
settings.py
15
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")
|
||||
|
|
Loading…
Reference in a new issue