Commit c2e31e67 authored by Jogi Hofmüller's avatar Jogi Hofmüller

Add saving of modifications and backup of gdb.json

parent 6e0e90f1
......@@ -146,17 +146,46 @@ class Gdb:
self.modified = False
self.buffer = []
self.buffermin = 64
self.backupcopies = 8
def loadJSON(self):
data = json.load(open(self.jsonfilename, 'r'))
for key in data['geruecht'].keys():
self.loadGeruecht(data['geruecht'][key])
self.glist.importGeruecht(data['geruecht'][key])
print('Loaded {} Geruechte'.format(len(self.glist)))
for key in data['name'].keys():
self.loadName(data['name'][key])
self.glist.nlist.importName(data['name'][key])
print('Loaded {} Names'.format(len(self.glist.nlist)))
def dumpJSON(self):
if not self.modified:
return
self.backup()
data = {'geruecht': {}, 'name': {},}
for item in self.glist:
data['geruecht'][item.key] = item.export()
for item in self.glist.nlist:
data['name'][item.key] = item.export()
json.dump(data, open(self.jsonfilename, 'w'), indent = 4)
self.modified = False
def backup(self):
for number in range(self.backupcopies, 0, -1):
filename = '{}.{}'.format(self.jsonfilename, number)
if not os.path.isfile(filename):
continue
nextfilename = '{}.{}'.format(self.jsonfilename, number+1)
if number == self.backupcopies:
os.unlink(filename)
print('Deleted {}'.format(filename))
else:
os.rename(filename, nextfilename)
print('Moved {} to {}'.format(filename, nextfilename))
os.rename(self.jsonfilename, filename)
print('Moved {} to {}'.format(self.jsonfilename, filename))
def fillBuffer(self):
if len(self.buffer) >= self.buffermin:
# print("buffersize={}".format(len(self.buffer)))
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment