03-fault-tolerance #5
1 changed files with 12 additions and 8 deletions
20
todo.py
20
todo.py
|
|
@ -8,11 +8,12 @@ todo_file = os.path.join(base_dir, "todo.md")
|
|||
|
||||
|
||||
def add_task(task: str, parent=None):
|
||||
print(f"Adding task '{task}' to '{parent if parent else 'root'}'")
|
||||
todo = parse_todo()
|
||||
if parent and parent not in todo:
|
||||
print(f"Could not find parent task '{parent}' in root task list")
|
||||
exit(1)
|
||||
|
||||
print(f"Adding task '{task}' to '{parent if parent else 'root'}'")
|
||||
if parent:
|
||||
todo[parent]["sub_tasks"][task] = {"completed": False}
|
||||
else:
|
||||
|
|
@ -21,18 +22,18 @@ def add_task(task: str, parent=None):
|
|||
|
||||
|
||||
def list_tasks(parent=None):
|
||||
print(f"Listing tasks in '{parent if parent else 'root'}'")
|
||||
todo = parse_todo()
|
||||
if parent and parent not in todo:
|
||||
print(f"Could not find parent task '{parent}' in root task list")
|
||||
exit(1)
|
||||
|
||||
print(f"Listing tasks in '{parent if parent else 'root'}'")
|
||||
if parent:
|
||||
todo = {parent: todo[parent]}
|
||||
print(render_todo(todo))
|
||||
|
||||
|
||||
def complete_task(task, parent=None):
|
||||
print(f"Marking task '{task}' in '{parent if parent else 'root'}' as completed")
|
||||
todo = parse_todo()
|
||||
if parent:
|
||||
if parent not in todo:
|
||||
|
|
@ -44,6 +45,8 @@ def complete_task(task, parent=None):
|
|||
if task not in todo:
|
||||
print(f"Could not find task '{task}' in root task list")
|
||||
exit(1)
|
||||
|
||||
print(f"Marking task '{task}' in '{parent if parent else 'root'}' as completed")
|
||||
if parent:
|
||||
task = todo[parent]["sub_tasks"][task]
|
||||
else:
|
||||
|
|
@ -53,7 +56,6 @@ def complete_task(task, parent=None):
|
|||
|
||||
|
||||
def remove_task(task, parent=None):
|
||||
print(f"Removing task '{task}' from '{parent if parent else 'root'}'")
|
||||
todo = parse_todo()
|
||||
if parent:
|
||||
if parent not in todo:
|
||||
|
|
@ -65,6 +67,8 @@ def remove_task(task, parent=None):
|
|||
if task not in todo:
|
||||
print(f"Could not find task '{task}' in root task list")
|
||||
exit(1)
|
||||
|
||||
print(f"Removing task '{task}' from '{parent if parent else 'root'}'")
|
||||
if parent:
|
||||
del todo[parent]["sub_tasks"][task]
|
||||
else:
|
||||
|
|
@ -85,14 +89,14 @@ def parse_todo() -> dict[str, dict]:
|
|||
lines = f.readlines()
|
||||
root_task = None
|
||||
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 = {
|
||||
"completed": matches.group("status") == "x",
|
||||
"sub_tasks": {},
|
||||
}
|
||||
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")
|
||||
root_task["sub_tasks"][leaf_task] = {
|
||||
"completed": matches.group("status") == "x",
|
||||
|
|
@ -125,7 +129,7 @@ def main():
|
|||
print_help()
|
||||
case "list":
|
||||
match sys.argv[2:]:
|
||||
case [parent]:
|
||||
case [parent, *_]:
|
||||
list_tasks(parent)
|
||||
case _:
|
||||
list_tasks()
|
||||
|
|
@ -134,7 +138,7 @@ def main():
|
|||
match sys.argv[2:]:
|
||||
case [task]:
|
||||
globals()[task_func](task)
|
||||
case [task, parent]:
|
||||
case [parent, task, *_]:
|
||||
globals()[task_func](task, parent)
|
||||
case _:
|
||||
print_help()
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue