From b34e6c27aa640e4bd4bde91d684e1e59095ba8b1 Mon Sep 17 00:00:00 2001 From: Santo Cariotti Date: Thu, 20 Jul 2017 14:24:08 +0200 Subject: Moved files Moved all files of lib into a lib dir --- app.py | 64 ----------------------------------------------------- classes.py | 38 ------------------------------- commands.py | 56 ---------------------------------------------- config.py | 6 ----- lib/__init__.py | 0 lib/app.py | 64 +++++++++++++++++++++++++++++++++++++++++++++++++++++ lib/classes.py | 38 +++++++++++++++++++++++++++++++ lib/commands.py | 56 ++++++++++++++++++++++++++++++++++++++++++++++ lib/config.py | 6 +++++ lib/listcommands.py | 27 ++++++++++++++++++++++ listcommands.py | 27 ---------------------- 11 files changed, 191 insertions(+), 191 deletions(-) delete mode 100644 app.py delete mode 100644 classes.py delete mode 100644 commands.py delete mode 100644 config.py create mode 100644 lib/__init__.py create mode 100644 lib/app.py create mode 100644 lib/classes.py create mode 100644 lib/commands.py create mode 100644 lib/config.py create mode 100644 lib/listcommands.py delete mode 100644 listcommands.py diff --git a/app.py b/app.py deleted file mode 100644 index ab2f90e..0000000 --- a/app.py +++ /dev/null @@ -1,64 +0,0 @@ -from classes import PersonalError, Colors, r, s, host, clear -from commands import Commands -from listcommands import ListCommands - -class Stout(Commands): - - def __init__(self): - self.name = u'\U0001F37A' - self.user = Stout.username() - - def getName(self): - word = ' (' + self.name + ') ' - if self.user == '': - return word - else: - 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') - else: - return '' - - def action(self, cmd): - if cmd is None: - return None - else: - cmd = cmd.split() - count = len(cmd) - - if (count == 1 or count == 2) and cmd[0] not in ListCommands.commands: - try: - if cmd[0] == 'info' and count == 1: - print(ListCommands.info['info']) - elif cmd[0] == 'info' and count == 2: - print(ListCommands.info[cmd[1]][0]) - else: - raise KeyError - except (KeyError, IndexError): - ListCommands.err('keyword') - else: - what = cmd[0] - if what in ListCommands.commands: - self.command(what, cmd) - else: - ListCommands.err('keyword') - - -if __name__ == '__main__': - clear() - app = Stout() - cmd = '' - while cmd != 'quit': - try: - cmd = input('>' + Colors.yellow + app.getName() + Colors.black) - except EOFError: - break - - app.action(cmd) - r.save() - - s.close() - clear() diff --git a/classes.py b/classes.py deleted file mode 100644 index 7f872af..0000000 --- a/classes.py +++ /dev/null @@ -1,38 +0,0 @@ -import redis, socket, os -from config import config as co - -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)) -host = s.getsockname()[0] - -def clear(): - try: - os.system('clear') - except: - os.system('cls') - -def userexist(name): - lista = r.zrange('usersname', 0, -1) - for i in lista: - if name == i.decode('utf-8'): - return True - else: - return False - -class PersonalError(Exception): - - def __init__(self, value): - self.value = value - - def __str__(self): - return repr(self.value) - - -class Colors(object): - - red = '\033[91m' - yellow = '\033[93m' - grey = '\033[90m' - black = '\033[0m' - bold = '\033[1m' diff --git a/commands.py b/commands.py deleted file mode 100644 index c9525db..0000000 --- a/commands.py +++ /dev/null @@ -1,56 +0,0 @@ -from classes import userexist, PersonalError, r, host, clear, Colors -from listcommands import ListCommands - -class Commands(object): - - def command(self, what, cmd): - if what == 'clear': - clear() - 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 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 parametro') - - if self.user != '': r.zrem('usersname', self.user) - - self.user = cmd[2] - r.hset('user:'+host, 'name', self.user) - r.zadd('usersname', self.user, 0) - - 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/config.py b/config.py deleted file mode 100644 index 3ccd82d..0000000 --- a/config.py +++ /dev/null @@ -1,6 +0,0 @@ -config = { - 'host' : 'localhost', - 'port' : 6379, - 'db' : 0, - 'unix_socket_path' : None, -} diff --git a/lib/__init__.py b/lib/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/lib/app.py b/lib/app.py new file mode 100644 index 0000000..ab2f90e --- /dev/null +++ b/lib/app.py @@ -0,0 +1,64 @@ +from classes import PersonalError, Colors, r, s, host, clear +from commands import Commands +from listcommands import ListCommands + +class Stout(Commands): + + def __init__(self): + self.name = u'\U0001F37A' + self.user = Stout.username() + + def getName(self): + word = ' (' + self.name + ') ' + if self.user == '': + return word + else: + 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') + else: + return '' + + def action(self, cmd): + if cmd is None: + return None + else: + cmd = cmd.split() + count = len(cmd) + + if (count == 1 or count == 2) and cmd[0] not in ListCommands.commands: + try: + if cmd[0] == 'info' and count == 1: + print(ListCommands.info['info']) + elif cmd[0] == 'info' and count == 2: + print(ListCommands.info[cmd[1]][0]) + else: + raise KeyError + except (KeyError, IndexError): + ListCommands.err('keyword') + else: + what = cmd[0] + if what in ListCommands.commands: + self.command(what, cmd) + else: + ListCommands.err('keyword') + + +if __name__ == '__main__': + clear() + app = Stout() + cmd = '' + while cmd != 'quit': + try: + cmd = input('>' + Colors.yellow + app.getName() + Colors.black) + except EOFError: + break + + app.action(cmd) + r.save() + + s.close() + clear() diff --git a/lib/classes.py b/lib/classes.py new file mode 100644 index 0000000..7f872af --- /dev/null +++ b/lib/classes.py @@ -0,0 +1,38 @@ +import redis, socket, os +from config import config as co + +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)) +host = s.getsockname()[0] + +def clear(): + try: + os.system('clear') + except: + os.system('cls') + +def userexist(name): + lista = r.zrange('usersname', 0, -1) + for i in lista: + if name == i.decode('utf-8'): + return True + else: + return False + +class PersonalError(Exception): + + def __init__(self, value): + self.value = value + + def __str__(self): + return repr(self.value) + + +class Colors(object): + + red = '\033[91m' + yellow = '\033[93m' + grey = '\033[90m' + black = '\033[0m' + bold = '\033[1m' diff --git a/lib/commands.py b/lib/commands.py new file mode 100644 index 0000000..c9525db --- /dev/null +++ b/lib/commands.py @@ -0,0 +1,56 @@ +from classes import userexist, PersonalError, r, host, clear, Colors +from listcommands import ListCommands + +class Commands(object): + + def command(self, what, cmd): + if what == 'clear': + clear() + 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 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 parametro') + + if self.user != '': r.zrem('usersname', self.user) + + self.user = cmd[2] + r.hset('user:'+host, 'name', self.user) + r.zadd('usersname', self.user, 0) + + 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/lib/config.py b/lib/config.py new file mode 100644 index 0000000..3ccd82d --- /dev/null +++ b/lib/config.py @@ -0,0 +1,6 @@ +config = { + 'host' : 'localhost', + 'port' : 6379, + 'db' : 0, + 'unix_socket_path' : None, +} diff --git a/lib/listcommands.py b/lib/listcommands.py new file mode 100644 index 0000000..8313d26 --- /dev/null +++ b/lib/listcommands.py @@ -0,0 +1,27 @@ +import sys +from classes import Colors + +class ListCommands(object): + + info = { + 'info' : 'this is stout', + 'set' : ['set a value', 3], + } + + commands = { + 'quit' : None, + 'clear' : 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) + elif err == 'wrong': + sys.stderr.write(Colors.red + 'sintassi comando errata\n' + Colors.black) + elif err == 'personal': + sys.stderr.write(Colors.red + str(info) + '\n' + Colors.black) + else: + pass diff --git a/listcommands.py b/listcommands.py deleted file mode 100644 index 8313d26..0000000 --- a/listcommands.py +++ /dev/null @@ -1,27 +0,0 @@ -import sys -from classes import Colors - -class ListCommands(object): - - info = { - 'info' : 'this is stout', - 'set' : ['set a value', 3], - } - - commands = { - 'quit' : None, - 'clear' : 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) - elif err == 'wrong': - sys.stderr.write(Colors.red + 'sintassi comando errata\n' + Colors.black) - elif err == 'personal': - sys.stderr.write(Colors.red + str(info) + '\n' + Colors.black) - else: - pass -- cgit v1.2.3-18-g5258