rewrite todo.py

This commit is contained in:
jesopo 2020-02-15 00:47:40 +00:00
parent 60c2e1138d
commit 5aaccbbb47

View file

@ -4,80 +4,55 @@ from src import ModuleManager, utils
class Module(ModuleManager.BaseModule): class Module(ModuleManager.BaseModule):
@utils.hook("received.command.todo") @utils.hook("received.command.todo")
@utils.spec("!'list ?<index>int")
@utils.spec("!'add !<description>string")
@utils.spec("!'remove !<index>int")
@utils.spec("!'move !<from>int !<to>int")
def todo(self, event): def todo(self, event):
""" user_todo = event["user"].get_setting("todo", [])
:help: Find out what's in your todo list if event["spec"][0] == "list":
:usage: [item number] if not user_todo:
""" raise utils.EventError("%s: you have no todo items"
todo = event["user"].get_setting("todo", []) % event["user"].nickname)
if event["args"]:
if event["args_split"][0].isdigit() and int(event["args_split"][0]) > 0: if not event["spec"][1] == None:
index = int(event["args_split"][0]) index = event["spec"][1]-1
if len(todo) >= index: if not index in dict(enumerate(user_todo)):
event["stdout"].write("Todo %d: %s" % (index, todo[index-1])) raise utils.EventError("%s: unknown todo index %d"
% (event["user"].nickname, event["spec"][1]))
event["stdout"].write("%s: (%d) %s"
% (event["user"].nickname, event["spec"][1],
user_todo[index]))
else: else:
event["stderr"].write("You do not have that many things in your todo list") outs = ["(%d) %s" % (i+1, s) for i, s in enumerate(user_todo)]
else: event["stdout"].write("%s: %s"
event["stderr"].write("Please provide a number") % (event["user"].nickname, ", ".join(outs)))
else:
todo_count = len(todo)
event["stdout"].write("There are %d items in your todo list" % todo_count)
@utils.hook("received.command.todoadd", min_args=1) elif event["spec"][0] == "add":
def todo_add(self, event): user_todo.append(event["spec"][1])
""" event["user"].set_setting("todo", user_todo)
:help: Add something to your todo list event["stdout"].write("%s: todo item %d added"
:usage: <description> % (event["user"].nickname, len(user_todo)))
"""
arg_lower = event["args"].lower()
todo = event["user"].get_setting("todo", [])
for item in todo:
if item.lower() == arg_lower:
raise utils.EventError("That is already in your todo list")
todo.append(event["args"])
event["user"].set_setting("todo", todo)
event["stdout"].write("Saved")
@utils.hook("received.command.tododel", min_args=1) elif event["spec"][0] == "remove":
def todo_del(self, event): index = event["spec"][1]-1
""" if not index in dict(enumerate(user_todo)):
:help: Remove something from your todo list raise utils.EventError("%s: unknown todo index %d"
:usage: <item number> % event["user"].nickname)
""" description = user_todo.pop(index)
todo = event["user"].get_setting("todo", []) event["user"].set_setting("todo", user_todo)
if event["args_split"][0].isdigit() and int(event["args_split"][0]) > 0: event["stdout"].write("%s: todo item %d removed: %s"
index = int(event["args_split"][0]) % (event["user"].nickname, event["spec"][1], description))
if len(todo) >= index:
todo.pop(index-1)
event["user"].set_setting("todo", todo)
event["stdout"].write("Todo item removed")
else:
event["stderr"].write("You do not have that many things in "
"your todo list")
else:
event["stderr"].write("Please provided a todo item number to remove")
@utils.hook("received.command.todomove", min_args=2) elif event["spec"][0] == "move":
def todo_move(self, event): todo_dict = dict(enumerate(user_todo))
""" start_index, end_index = event["spec"][1]-1, event["spec"][2]-1
:help: Move a todo item to a different index for i, name in [[start_index, "start"], [end_index, "end"]]:
:usage: <from> <to> if not i in todo_dict:
""" raise utils.EventError("%s: unknown todo %s index %d"
_from_str, to_str = event["args_split"][0], event["args_split"][1] % (event["user"].nickname, name, i+1))
if not _from_str.isdigit() or not to_str.isdigit(): description = user_todo.pop(start_index)
event["stdout"].write("Please provide numeric indexes") user_todo.insert(end_index, description)
return event["user"].set_setting("todo", user_todo)
event["stdout"].write("%s: todo item moved to %d: %s"
_from, to = int(_from_str)-1, int(to_str)-1 % (event["user"].nickname, event["spec"][2], description))
if _from < 0 or to < 0:
raise utils.EventError("Both indexes must be above 0")
todo = event["user"].get_setting("todo", [])
if _from > len(todo) or to > len(todo):
raise utils.EventError("Both indexes must be less than the "
"size of your todo list")
todo.insert(to, todo.pop(_from))
event["user"].set_setting("todo", todo)
event["stdout"].write("Moved todo item %s to position %s" % (
_from_str, to_str))