03-fault-tolerance #5

Open
uda wants to merge 2 commits from 03-fault-tolerance into 02-todo-cli
3 changed files with 19 additions and 12 deletions

View file

@ -5,4 +5,7 @@ python3 todo.py add Test
python3 todo.py list Test python3 todo.py list Test
python3 todo.py list Test Test python3 todo.py list Test Test
python3 todo.py add test1 Test Test python3 todo.py add Test test1 test2
python3 todo.py add Test test1 test3
python3 todo.py complete Test test1 test3
python3 todo.py remove Test test1 test3

View file

@ -12,17 +12,17 @@ python3 todo.py list
python3 todo.py add Test python3 todo.py add Test
python3 todo.py list python3 todo.py list
python3 todo.py list Test python3 todo.py list Test
python3 todo.py add test Test python3 todo.py add Test test
python3 todo.py list Test python3 todo.py list Test
python3 todo.py list python3 todo.py list
python3 todo.py complete test Test python3 todo.py complete Test test
python3 todo.py list Test python3 todo.py list Test
python3 todo.py list python3 todo.py list
python3 todo.py complete Test python3 todo.py complete Test
python3 todo.py list python3 todo.py list
python3 todo.py remove test Test python3 todo.py remove Test test
python3 todo.py list Test python3 todo.py list Test
python3 todo.py list python3 todo.py list
python3 todo.py remove Test python3 todo.py remove Test

20
todo.py
View file

@ -8,11 +8,12 @@ todo_file = os.path.join(base_dir, "todo.md")
def add_task(task: str, parent=None): def add_task(task: str, parent=None):
print(f"Adding task '{task}' to '{parent if parent else 'root'}'")
todo = parse_todo() todo = parse_todo()
if parent and parent not in todo: if parent and parent not in todo:
print(f"Could not find parent task '{parent}' in root task list") print(f"Could not find parent task '{parent}' in root task list")
exit(1) exit(1)
print(f"Adding task '{task}' to '{parent if parent else 'root'}'")
if parent: if parent:
todo[parent]["sub_tasks"][task] = {"completed": False} todo[parent]["sub_tasks"][task] = {"completed": False}
else: else:
@ -21,18 +22,18 @@ def add_task(task: str, parent=None):
def list_tasks(parent=None): def list_tasks(parent=None):
print(f"Listing tasks in '{parent if parent else 'root'}'")
todo = parse_todo() todo = parse_todo()
if parent and parent not in todo: if parent and parent not in todo:
print(f"Could not find parent task '{parent}' in root task list") print(f"Could not find parent task '{parent}' in root task list")
exit(1) exit(1)
print(f"Listing tasks in '{parent if parent else 'root'}'")
if parent: if parent:
todo = {parent: todo[parent]} todo = {parent: todo[parent]}
print(render_todo(todo)) print(render_todo(todo))
def complete_task(task, parent=None): def complete_task(task, parent=None):
print(f"Marking task '{task}' in '{parent if parent else 'root'}' as completed")
todo = parse_todo() todo = parse_todo()
if parent: if parent:
if parent not in todo: if parent not in todo:
@ -44,6 +45,8 @@ def complete_task(task, parent=None):
if task not in todo: if task not in todo:
print(f"Could not find task '{task}' in root task list") print(f"Could not find task '{task}' in root task list")
exit(1) exit(1)
print(f"Marking task '{task}' in '{parent if parent else 'root'}' as completed")
if parent: if parent:
task = todo[parent]["sub_tasks"][task] task = todo[parent]["sub_tasks"][task]
else: else:
@ -53,7 +56,6 @@ def complete_task(task, parent=None):
def remove_task(task, parent=None): def remove_task(task, parent=None):
print(f"Removing task '{task}' from '{parent if parent else 'root'}'")
todo = parse_todo() todo = parse_todo()
if parent: if parent:
if parent not in todo: if parent not in todo:
@ -65,6 +67,8 @@ def remove_task(task, parent=None):
if task not in todo: if task not in todo:
print(f"Could not find task '{task}' in root task list") print(f"Could not find task '{task}' in root task list")
exit(1) exit(1)
print(f"Removing task '{task}' from '{parent if parent else 'root'}'")
if parent: if parent:
del todo[parent]["sub_tasks"][task] del todo[parent]["sub_tasks"][task]
else: else:
@ -85,14 +89,14 @@ def parse_todo() -> dict[str, dict]:
lines = f.readlines() lines = f.readlines()
root_task = None root_task = None
for line in lines: for line in lines:
if matches := re.match(r"^-\s+(\[(?P<status>[x ])]\s+)?(?P<task>.*)$", line.rstrip()): if matches := re.match(r"^-\s+(\[(?P<status>[x ])]\s+)?(?P<task>.+)$", line.rstrip()):
root_task_name = matches.group("task") root_task_name = matches.group("task")
root_task = { root_task = {
"completed": matches.group("status") == "x", "completed": matches.group("status") == "x",
"sub_tasks": {}, "sub_tasks": {},
} }
todo[root_task_name] = root_task todo[root_task_name] = root_task
elif matches := re.match(r"^\s+-\s+(\[(?P<status>[x ])]\s+)?(?P<task>.*)$", line.rstrip()): elif matches := re.match(r"^\s+-\s+(\[(?P<status>[x ])]\s+)?(?P<task>.+)$", line.rstrip()):
leaf_task = matches.group("task") leaf_task = matches.group("task")
root_task["sub_tasks"][leaf_task] = { root_task["sub_tasks"][leaf_task] = {
"completed": matches.group("status") == "x", "completed": matches.group("status") == "x",
@ -125,7 +129,7 @@ def main():
print_help() print_help()
case "list": case "list":
match sys.argv[2:]: match sys.argv[2:]:
case [parent]: case [parent, *_]:
list_tasks(parent) list_tasks(parent)
case _: case _:
list_tasks() list_tasks()
@ -134,7 +138,7 @@ def main():
match sys.argv[2:]: match sys.argv[2:]:
case [task]: case [task]:
globals()[task_func](task) globals()[task_func](task)
case [task, parent]: case [parent, task, *_]:
globals()[task_func](task, parent) globals()[task_func](task, parent)
case _: case _:
print_help() print_help()