48 lines
1.0 KiB
Python
48 lines
1.0 KiB
Python
import sys
|
|
from os import path
|
|
|
|
from codupoc.todo import Todo
|
|
from codupoc.slimdownparser import parse_todo
|
|
|
|
class TodoManager:
|
|
filename = "todo.md"
|
|
|
|
def __init__(self):
|
|
self.file = None
|
|
|
|
def does_todo_file_exist(self):
|
|
return path.exists(self.filename)
|
|
|
|
def open_todo_file(self):
|
|
self.file = open(self.filename, "r+")
|
|
return self.file
|
|
|
|
def read_todos(self) -> list[Todo]:
|
|
todos = []
|
|
f = self.open_todo_file()
|
|
|
|
for line in f:
|
|
todos.append(parse_todo(line))
|
|
|
|
self.file.seek(0)
|
|
|
|
return todos
|
|
|
|
def close_todo_file(self):
|
|
self.file.close()
|
|
|
|
def get_todos(self) -> dict[str, Todo]:
|
|
if not self.does_todo_file_exist():
|
|
self.open_todo_file()
|
|
return {}
|
|
|
|
todos = self.read_todos()
|
|
out = {}
|
|
for t in todos:
|
|
out[t.lid] = t
|
|
|
|
return out
|
|
|
|
def write_todos(self, todos: list[Todo]):
|
|
for t in todos:
|
|
self.file.write(str(t) + "\n") |