From 0b2faedc92e078fb3607a3c21b166c006296d97c Mon Sep 17 00:00:00 2001 From: Timo Date: Sun, 22 Feb 2015 18:14:20 +0100 Subject: add authentication and more blocks --- snapdb.py | 47 +++++++++++++++++++++++++++++++++++++---------- 1 file changed, 37 insertions(+), 10 deletions(-) diff --git a/snapdb.py b/snapdb.py index 8bdb2ff..904e188 100755 --- a/snapdb.py +++ b/snapdb.py @@ -1,31 +1,58 @@ #!/usr/bin/env python3 +import sys from mongodict import MongoDict from blockext import * from config import config class Storage: def __init__(self): - self.db = MongoDict( - host=config["host"], port=config["port"], - database=config["database"], collection=config["collection"], - auth=(config["username"], config["password"])) - - @predicate("%s from the database") + self.disconnect() + + @reporter("connect as user %s with password %s to database (host %s , port %n , database %s , collection %s )", defaults=["", "", "localhost", 27017, "snapmesh", "snapmesh"]) + def connect(self, username, password, host, port, database, collection): + try: + self.db = MongoDict(host, port, database, collection, auth=(username, password)) + return "ok" + except: + return "error: " + str(sys.exc_info()[1]) + + @command("disconnect") + def disconnect(self): + self.db = None + + @predicate("connected?") + def is_connected(self): + return self.db is not None + + @reporter("%s from the collection") def get(self, key): + if self.db is None: + return "not connected" + if key in self.db: return self.db[key] else: return "" - @command("%s as %s into the database") + @command("%s as %s into the collection") def put(self, value, key): + if self.db is None: + return "not connected" + self.db[key] = value + @reporter("contents of the collection") + def list(self): + if self.db is None: + return "not connected" + + return "\n".join(self.db.keys()) + descriptor = Descriptor( - name = "SnapDB", - port = config["localport"], - blocks = get_decorated_blocks_from_class(Storage) + name="SnapDB", + port=config["localport"], + blocks=get_decorated_blocks_from_class(Storage) ) extension = Extension(Storage, descriptor) -- cgit v1.3.1