summaryrefslogtreecommitdiff
path: root/lib
diff options
context:
space:
mode:
Diffstat (limited to 'lib')
-rw-r--r--lib/app.py2
-rw-r--r--lib/classes.py11
-rw-r--r--lib/commands.py61
-rw-r--r--lib/listcommands.py8
4 files changed, 67 insertions, 15 deletions
diff --git a/lib/app.py b/lib/app.py
index 31fd270..169fcea 100644
--- a/lib/app.py
+++ b/lib/app.py
@@ -67,5 +67,3 @@ if __name__ == '__main__':
app.action(cmd)
r.save()
-
- clear()
diff --git a/lib/classes.py b/lib/classes.py
index 1a1c876..50f3e14 100644
--- a/lib/classes.py
+++ b/lib/classes.py
@@ -12,17 +12,6 @@ def clear():
except:
os.system('cls')
-# def userexist(name):
-# """
-# lista is a list for the set called USERSNAME. check if name is in lista
-# """
-# 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):
diff --git a/lib/commands.py b/lib/commands.py
index 61c3eaf..f004d24 100644
--- a/lib/commands.py
+++ b/lib/commands.py
@@ -48,6 +48,21 @@ class Commands(object):
ListCommands.err('keyword')
else:
print(co[cmd[2]])
+ elif cmd[1] == 'todo':
+ todolist = r.zrange('todo', 0, -1)
+
+ if len(todolist) == 0: #if todo is empty
+ print('nessun todo in lista')
+ else:
+ for num, i in enumerate(todolist): #num = index, i = value
+ print('| {} |\t {}'.format(num, i.decode('utf-8')))
+ elif cmd[1] == 'ctodo':
+ count = r.get('idTODO')
+
+ if count is None: #if idTODO is None or 0
+ print('0')
+ else:
+ print(count.decode('utf-8'))
else:
raise KeyError
except IndexError:
@@ -56,3 +71,49 @@ class Commands(object):
ListCommands.err('keyword')
except PersonalError as e:
ListCommands.err('personal', e.value)
+ elif what == 'add':
+ try:
+ if r.get('idTODO') is None: #create idTODO if it's null
+ r.set('idTODO', '0')
+
+ idTODO = r.get('idTODO').decode('utf-8')
+
+ msg = ' '.join(cmd[1:]) #join first word after 'add' to last word
+
+ try:
+ r.zadd('todo', msg, idTODO)
+ r.incr('idTODO')
+ print('Ok')
+ except:
+ raise KeyError
+ except IndexError:
+ ListCommands.err('wrong')
+ except KeyError:
+ ListCommands.err('keyword')
+ elif what == 'del':
+ try:
+ if len(cmd) > ListCommands.info['del'][1]:
+ raise PersonalError(Colors.grey + 'del ' + Colors.red + 'accetta 1 parametro')
+
+ if r.get('idTODO') is None: #idTODO is null, not 0
+ print('nessun todo in lista')
+ elif cmd[1] >= r.get('idTODO').decode('utf-8'):
+ print('nessun todo con questo id')
+ else:
+ try:
+ msg = msgFromId(cmd[1]) #return value of sorted set's rank
+ r.zrem('todo', msg)
+ r.decr('idTODO')
+ print('Ok %s' % msg)
+ except:
+ print('0')
+ except IndexError:
+ ListCommands.err('wrong')
+ except KeyError:
+ ListCommands.err('keyword')
+
+ @staticmethod
+ def msgFromId(value):
+ for i, val in enumerate(r.zrange('todo', 0, -1)):
+ if i == int(value):
+ return val.decode('utf-8')
diff --git a/lib/listcommands.py b/lib/listcommands.py
index 3f4f3ad..d8c7ad9 100644
--- a/lib/listcommands.py
+++ b/lib/listcommands.py
@@ -9,14 +9,18 @@ class ListCommands(object):
info = {
'info' : 'this is stout',
'set' : ['set a value', 3],
- 'get' : ['return a value', 3]
+ 'get' : ['return a value', 3],
+ 'add' : 'new TODO',
+ 'del' : ['del TODO', 2]
}
commands = {
'quit' : None,
'clear' : None,
'set' : ['user'],
- 'get' : ['i', ['user', 'host', 'port', 'unix_socket_path', 'db']]
+ 'get' : ['i', ['user', 'host', 'port', 'unix_socket_path', 'db'], 'todo', 'ctodo'],
+ 'add' : None,
+ 'del' : None,
}
@staticmethod