diff options
author | Santo Cariotti <sancn@live.com> | 2017-07-20 10:30:10 +0200 |
---|---|---|
committer | Santo Cariotti <sancn@live.com> | 2017-07-20 10:30:10 +0200 |
commit | bdf83e42d1bca4ffac3a03bfdbb27d06db74dc88 (patch) | |
tree | e408c50422e4159a0008c5e529a8be6308a0f445 /app.py | |
parent | b50e32c9f6436b6e7d84502586b3bd04bb7dfd6a (diff) |
now there are more files
Diffstat (limited to 'app.py')
-rw-r--r-- | app.py | 103 |
1 files changed, 11 insertions, 92 deletions
@@ -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() |