From b50e32c9f6436b6e7d84502586b3bd04bb7dfd6a Mon Sep 17 00:00:00 2001 From: Santo Cariotti Date: Thu, 20 Jul 2017 09:11:00 +0200 Subject: ListCommand and Commands are different classes now --- __init__.py | 1 + __pycache__/commands.cpython-35.pyc | Bin 0 -> 1078 bytes app.py | 63 +++++++++++++++++++----------------- commands.py | 0 4 files changed, 34 insertions(+), 30 deletions(-) create mode 100644 __init__.py create mode 100644 __pycache__/commands.cpython-35.pyc create mode 100644 commands.py diff --git a/__init__.py b/__init__.py new file mode 100644 index 0000000..8b13789 --- /dev/null +++ b/__init__.py @@ -0,0 +1 @@ + diff --git a/__pycache__/commands.cpython-35.pyc b/__pycache__/commands.cpython-35.pyc new file mode 100644 index 0000000..b2272bf Binary files /dev/null and b/__pycache__/commands.cpython-35.pyc differ diff --git a/app.py b/app.py index 923a23a..8bab59d 100644 --- a/app.py +++ b/app.py @@ -26,7 +26,7 @@ class Colors(object): bold = '\033[1m' -class Command(object): +class ListCommand(object): info = { "info" : "this is stout", @@ -51,7 +51,32 @@ class Command(object): pass -class Stout(object): +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) + + +class Stout(Commands): def __init__(self): self.name = 'stout' @@ -80,28 +105,6 @@ class Stout(object): else: return False - 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 Command.commands['set']: - raise IndexError - except IndexError: - Command.err('wrong') - except PersonalError as e: - Command.err('personal', e.value) - def action(self, cmd): if cmd is None: return None @@ -109,20 +112,20 @@ class Stout(object): cmd = cmd.split() count = len(cmd) - if (count == 1 or count == 2) and cmd[0] not in Command.commands: + if (count == 1 or count == 2) and cmd[0] not in ListCommand.commands: try: if cmd[0] == 'info' and count == 1: - print(Command.info['info']) + print(ListCommand.info['info']) else: - print(Command.info[cmd[1]]) + print(ListCommand.info[cmd[1]]) except (KeyError, IndexError): - Command.err('keyword') + ListCommand.err('keyword') else: what = cmd[0] - if what in Command.commands: + if what in ListCommand.commands: self.command(what, cmd) else: - Command.err('keyword') + ListCommand.err('keyword') if __name__ == '__main__': diff --git a/commands.py b/commands.py new file mode 100644 index 0000000..e69de29 -- cgit v1.2.3-18-g5258