From bdf83e42d1bca4ffac3a03bfdbb27d06db74dc88 Mon Sep 17 00:00:00 2001 From: Santo Cariotti Date: Thu, 20 Jul 2017 10:30:10 +0200 Subject: now there are more files --- __pycache__/app.cpython-35.pyc | Bin 0 -> 2382 bytes __pycache__/classes.cpython-35.pyc | Bin 0 -> 1480 bytes __pycache__/commands.cpython-35.pyc | Bin 1078 -> 1243 bytes __pycache__/listcommands.cpython-35.pyc | Bin 0 -> 930 bytes app.py | 103 ++++---------------------------- classes.py | 37 ++++++++++++ commands.py | 28 +++++++++ listcommands.py | 27 +++++++++ 8 files changed, 103 insertions(+), 92 deletions(-) create mode 100644 __pycache__/app.cpython-35.pyc create mode 100644 __pycache__/classes.cpython-35.pyc create mode 100644 __pycache__/listcommands.cpython-35.pyc create mode 100644 classes.py create mode 100644 listcommands.py diff --git a/__pycache__/app.cpython-35.pyc b/__pycache__/app.cpython-35.pyc new file mode 100644 index 0000000..8cddba3 Binary files /dev/null and b/__pycache__/app.cpython-35.pyc differ diff --git a/__pycache__/classes.cpython-35.pyc b/__pycache__/classes.cpython-35.pyc new file mode 100644 index 0000000..2ac1e7c Binary files /dev/null and b/__pycache__/classes.cpython-35.pyc differ diff --git a/__pycache__/commands.cpython-35.pyc b/__pycache__/commands.cpython-35.pyc index b2272bf..ab7356e 100644 Binary files a/__pycache__/commands.cpython-35.pyc and b/__pycache__/commands.cpython-35.pyc differ diff --git a/__pycache__/listcommands.cpython-35.pyc b/__pycache__/listcommands.cpython-35.pyc new file mode 100644 index 0000000..48fe8b5 Binary files /dev/null and b/__pycache__/listcommands.cpython-35.pyc differ diff --git a/app.py b/app.py index 8bab59d..be6b0db 100644 --- a/app.py +++ b/app.py @@ -1,80 +1,6 @@ -import redis -import sys -import socket - -r = redis.Redis() - -s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM) -s.connect(("8.8.8.8", 80)) -host = s.getsockname()[0] - -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' - - -class ListCommand(object): - - info = { - "info" : "this is stout", - "set" : "set a value", - } - - commands = { - 'quit' : None, - 'set' : ['user', 'host'], - 'get' : None - } - - @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 - - -class Commands(object): - - def command(self, what, cmd): - if 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 Stout.userexist(cmd[2]) == True: raise PersonalError("questo nome utente esiste già") - - 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 ListCommand.commands['set']: - raise IndexError - except IndexError: - ListCommand.err('wrong') - except PersonalError as e: - ListCommand.err('personal', e.value) - +from classes import PersonalError, Colors, r, s, host, clear +from commands import Commands +from listcommands import ListCommands class Stout(Commands): @@ -96,15 +22,6 @@ class Stout(Commands): else: return '' - @staticmethod - def userexist(name): - lista = r.zrange('usersname', 0, -1) - for i in lista: - if name == i.decode("utf-8"): - return True - else: - return False - def action(self, cmd): if cmd is None: return None @@ -112,23 +29,24 @@ class Stout(Commands): cmd = cmd.split() count = len(cmd) - if (count == 1 or count == 2) and cmd[0] not in ListCommand.commands: + if (count == 1 or count == 2) and cmd[0] not in ListCommands.commands: try: if cmd[0] == 'info' and count == 1: - print(ListCommand.info['info']) + print(ListCommands.info['info']) else: - print(ListCommand.info[cmd[1]]) + print(ListCommands.info[cmd[1]]) except (KeyError, IndexError): - ListCommand.err('keyword') + ListCommands.err('keyword') else: what = cmd[0] - if what in ListCommand.commands: + if what in ListCommands.commands: self.command(what, cmd) else: - ListCommand.err('keyword') + ListCommands.err('keyword') if __name__ == '__main__': + clear() app = Stout() cmd = '' while cmd != 'quit': @@ -137,3 +55,4 @@ if __name__ == '__main__': r.save() s.close() + clear() diff --git a/classes.py b/classes.py new file mode 100644 index 0000000..dc4f38a --- /dev/null +++ b/classes.py @@ -0,0 +1,37 @@ +import redis, socket, os + +r = redis.Redis() +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 index e69de29..2c7b57f 100644 --- a/commands.py +++ b/commands.py @@ -0,0 +1,28 @@ +from classes import userexist, PersonalError, r, host +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 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) diff --git a/listcommands.py b/listcommands.py new file mode 100644 index 0000000..136d214 --- /dev/null +++ b/listcommands.py @@ -0,0 +1,27 @@ +import sys +from classes import Colors + +class ListCommands(object): + + info = { + "info" : "this is stout", + "set" : "set a value", + } + + commands = { + 'quit' : None, + 'clear' : None, + 'set' : ['user', 'host'], + 'get' : None + } + + @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