From 6d1aff6894408c89d7a549076293fe011e475eb0 Mon Sep 17 00:00:00 2001 From: Santo Cariotti Date: Thu, 20 Jul 2017 13:24:34 +0200 Subject: added get comand --- .gitignore | 2 +- app.py | 10 +++++----- classes.py | 7 ++++--- commands.py | 36 ++++++++++++++++++++++++++++++++---- listcommands.py | 17 +++++++++-------- 5 files changed, 51 insertions(+), 21 deletions(-) diff --git a/.gitignore b/.gitignore index 13ee1ea..c18dd8d 100644 --- a/.gitignore +++ b/.gitignore @@ -1 +1 @@ -myid.txt +__pycache__/ diff --git a/app.py b/app.py index 2dc8f66..1d9b65b 100644 --- a/app.py +++ b/app.py @@ -9,16 +9,16 @@ class Stout(Commands): self.user = Stout.username() def getName(self): - word = " (" + self.name + ") " + word = ' (' + self.name + ') ' if self.user == '': return word else: - return word + Colors.grey + "(" + self.user + ":" + host + ") " + return word + Colors.grey + '(' + self.user + ':' + host + ') ' @staticmethod def username(): if r.hget('user:'+host, 'name') is not None: - return r.hget('user:'+host, 'name').decode("utf-8") + return r.hget('user:'+host, 'name').decode('utf-8') else: return '' @@ -34,7 +34,7 @@ class Stout(Commands): if cmd[0] == 'info' and count == 1: print(ListCommands.info['info']) elif cmd[0] == 'info' and count == 2: - print(ListCommands.info[cmd[1]]) + print(ListCommands.info[cmd[1]][0]) else: raise KeyError except (KeyError, IndexError): @@ -52,7 +52,7 @@ if __name__ == '__main__': app = Stout() cmd = '' while cmd != 'quit': - cmd = input(">" + Colors.yellow + app.getName() + Colors.black) + cmd = input('>' + Colors.yellow + app.getName() + Colors.black) app.action(cmd) r.save() diff --git a/classes.py b/classes.py index dc4f38a..7f872af 100644 --- a/classes.py +++ b/classes.py @@ -1,8 +1,9 @@ import redis, socket, os +from config import config as co -r = redis.Redis() +r = redis.Redis(host=co['host'], port=co['port'], unix_socket_path=co['unix_socket_path'], db=co['db']) s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM) -s.connect(("8.8.8.8", 80)) +s.connect(('8.8.8.8', 80)) host = s.getsockname()[0] def clear(): @@ -14,7 +15,7 @@ def clear(): def userexist(name): lista = r.zrange('usersname', 0, -1) for i in lista: - if name == i.decode("utf-8"): + if name == i.decode('utf-8'): return True else: return False diff --git a/commands.py b/commands.py index 2c7b57f..fa42b7f 100644 --- a/commands.py +++ b/commands.py @@ -1,4 +1,4 @@ -from classes import userexist, PersonalError, r, host +from classes import userexist, PersonalError, r, host, clear, Colors from listcommands import ListCommands class Commands(object): @@ -9,9 +9,11 @@ class Commands(object): elif what == 'set': try: if cmd[1] == 'user' and cmd[2] is not None: - if len(cmd[2]) > 10: raise PersonalError("lunghezza maggiore del consetito. Max 10") + if len(cmd[2]) > 10: raise PersonalError('lunghezza maggiore del consetito. Max 10') - if userexist(cmd[2]) == True: raise PersonalError("questo nome utente esiste già") + if userexist(cmd[2]) == True: raise PersonalError('questo nome utente esiste già') + + if len(cmd) > ListCommands.info['set'][1]: raise PersonalError(Colors.grey + 'set user' + Colors.red + ' accetta 1 parametri') if self.user != '': r.zrem('usersname', self.user) @@ -19,10 +21,36 @@ class Commands(object): r.hset('user:'+host, 'name', self.user) r.zadd('usersname', self.user, 0) - print("Ok") + print('Ok') elif cmd[1] is not ListCommands.commands['set']: raise IndexError except IndexError: ListCommands.err('wrong') except PersonalError as e: ListCommands.err('personal', e.value) + elif what == 'get': + try: + if len(cmd) > 2 and cmd[1] != 'user?': raise PersonalError(Colors.grey + 'get ' + Colors.red + 'accetta 1 parametro') + + if cmd[1] == 'user': + if self.user != '': + print(r.hget('user:'+host, 'name').decode('utf-8')) + else: + print('nil') + elif cmd[1] == 'host': + print('localhost') + elif cmd[1] == 'port': + print('6379') + elif cmd[1] == 'user?': + if len(cmd) > 3: raise PersonalError(Colors.grey + 'get user?' + Colors.red + ' accetta 1 parametro') + print(userexist(cmd[2])) + elif cmd[1] not in ListCommands.commands: + raise KeyError + else: + raise IndexError + except IndexError: + ListCommands.err('wrong') + except KeyError: + ListCommands.err('keyword') + except PersonalError as e: + ListCommands.err('personal', e.value) diff --git a/listcommands.py b/listcommands.py index 136d214..f93c3d7 100644 --- a/listcommands.py +++ b/listcommands.py @@ -4,24 +4,25 @@ from classes import Colors class ListCommands(object): info = { - "info" : "this is stout", - "set" : "set a value", + 'info' : 'this is stout', + 'set' : ['set a value', 3], } commands = { - 'quit' : None, + 'quit' : None, #quit and exit do same thing + 'exit' : None, 'clear' : None, - 'set' : ['user', 'host'], - 'get' : None + 'set' : ['user'], + 'get' : ['user', 'user?', 'host', 'port'] } @staticmethod def err(err, info = ''): if err == 'keyword': - sys.stderr.write(Colors.red + "keyword inesistente\n" + Colors.black) + sys.stderr.write(Colors.red + 'keyword inesistente\n' + Colors.black) elif err == 'wrong': - sys.stderr.write(Colors.red + "sintassi comando errata\n" + Colors.black) + sys.stderr.write(Colors.red + 'sintassi comando errata\n' + Colors.black) elif err == 'personal': - sys.stderr.write(Colors.red + str(info) + "\n" + Colors.black) + sys.stderr.write(Colors.red + str(info) + '\n' + Colors.black) else: pass -- cgit v1.2.3-18-g5258