Commit 79e04513 authored by Jogi Hofmüller's avatar Jogi Hofmüller

Add User, Userlist and getdict export

parent 655680fe
......@@ -29,6 +29,7 @@ import random
import json
import os
import time
import hashlib
class Geruecht():
'''
......@@ -135,14 +136,47 @@ class NameList(list):
def importName(self, data):
self.append(Name(data))
class User(object):
'''
a user with rights inside gdb
'''
def __init__(self, data):
self.key = data['key']
self.name = data['name']
self.passwordhash = data['passwordhash']
self.lastseen = data['lastseen']
def export(self):
return {
'key': self.key,
'name': self.name,
'passwordhash': self.passwordhash,
'lastseen': self.lastseen,
}
class UserList(list):
'''
'''
def authenticate(self, name, password):
passwordhash = hashlib.sha256(password.encode('utf8')).hexdigest()
for user in self:
if user.name == name and user.passwordhash == passwordhash:
return user
return None
def importUser(self, data):
self.append(User(data))
class Gdb:
'''
'''
def __init__(self, jsonfilename):
def __init__(self, jsonfilename, log):
self.jsonfilename = jsonfilename
self.log = log
self.glist = GeruechtList()
self.glist.nlist = NameList()
self.ulist = UserList()
self.modified = False
self.buffer = []
self.buffermin = 64
......@@ -152,26 +186,45 @@ class Gdb:
data = json.load(open(self.jsonfilename, 'r'))
for key in data['geruecht'].keys():
self.glist.importGeruecht(data['geruecht'][key])
print('Loaded {} Geruechte'.format(len(self.glist)))
self.log('Loaded {} Geruechte'.format(len(self.glist)))
for key in data['name'].keys():
self.glist.nlist.importName(data['name'][key])
print('Loaded {} Names'.format(len(self.glist.nlist)))
self.log('Loaded {} Names'.format(len(self.glist.nlist)))
for key in data['user'].keys():
self.ulist.importUser(data['user'][key])
self.log('Loaded {} Users'.format(len(self.ulist)))
def dumpJSON(self):
if not self.modified:
print('Nothing to save')
self.log('Nothing to save')
return
self.backup()
data = {'geruecht': {}, 'name': {},}
data = {'geruecht': {}, 'name': {}, 'user': {}}
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)
for item in self.ulist:
data['user'][item.key] = item.export()
json.dump(data, open(self.jsonfilename, 'w'), indent = 2)
self.modified = False
def getdict(self, type):
if type == 'geruecht':
l = self.glist
elif type == 'name':
l = self.glist.nlist
else:
return None
data = {}
for item in l:
data[item.key] = item.export()
return data
def backup(self):
for number in range(self.backupcopies, 0, -1):
filename = '{}.{}'.format(self.jsonfilename, number)
......@@ -180,12 +233,12 @@ class Gdb:
nextfilename = '{}.{}'.format(self.jsonfilename, number+1)
if number == self.backupcopies:
os.unlink(filename)
print('Deleted {}'.format(filename))
self.log('Deleted {}'.format(filename))
else:
os.rename(filename, nextfilename)
print('Moved {} to {}'.format(filename, nextfilename))
self.log('Moved {} to {}'.format(filename, nextfilename))
os.rename(self.jsonfilename, filename)
print('Moved {} to {}'.format(self.jsonfilename, filename))
self.log('Moved {} to {}'.format(self.jsonfilename, filename))
def fillBuffer(self):
if len(self.buffer) >= self.buffermin:
......@@ -193,18 +246,18 @@ class Gdb:
return
for count in range(self.buffermin):
self.buffer.append(self.glist.getRandomGeruecht())
print("\n\n+++++++++\nFilled Buffer\nBuffer now holds {} items\n+++++++++\n\n".format(len(self.buffer)))
self.log('filled buffer. Buffer now holds {} items'.format(len(self.buffer)))
def getNextGeruecht(self):
self.fillBuffer()
next = self.buffer.pop(0)
return next
def printAll(self):
for g in self.glist:
g.names = self.glist.nlist.getUniqueNames(g.num_names)
print(g.key),
print(g.render())
# def printAll(self):
# for g in self.glist:
# g.names = self.glist.nlist.getUniqueNames(g.num_names)
# print(g.key),
# print(g.render())
class ItemState:
......@@ -218,7 +271,7 @@ itemstate = ItemState()
if __name__ == '__main__':
import sys
gdb = Gdb('/tmp/gdb.json')
gdb = Gdb('/tmp/gdb.json', print)
gdb.loadJSON()
gdb.fillBuffer()
gdb.dumpJSON()
......
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