Add todo file parsing
This commit is contained in:
13
codupoc/slimdownparser.py
Normal file
13
codupoc/slimdownparser.py
Normal file
@@ -0,0 +1,13 @@
|
||||
import re
|
||||
|
||||
from todo import Todo
|
||||
|
||||
TODO_PATTERN = r"^-\[( |X)\] (.+)$"
|
||||
|
||||
def parse_todo(line: str) -> Todo:
|
||||
pattern = re.compile(TODO_PATTERN)
|
||||
match = pattern.match(line)
|
||||
(x, item) = match.group(1, 2)
|
||||
is_checked = x == "X"
|
||||
|
||||
return Todo(item, is_checked)
|
||||
11
codupoc/todo.py
Normal file
11
codupoc/todo.py
Normal file
@@ -0,0 +1,11 @@
|
||||
from base64_random import gen_random_base64
|
||||
|
||||
|
||||
class Todo:
|
||||
def __init__(self, item: str, checked: bool):
|
||||
self.item = item
|
||||
self.checked = checked
|
||||
self.lid = gen_random_base64(10)
|
||||
|
||||
def __str__(self):
|
||||
return f"- [{self.checked}] {self.item}"
|
||||
@@ -1,6 +1,9 @@
|
||||
import sys
|
||||
from os import path
|
||||
|
||||
from todo import Todo
|
||||
from slimdownparser import parse_todo
|
||||
|
||||
class TodoManager:
|
||||
filename = "todo.md"
|
||||
|
||||
@@ -14,4 +17,11 @@ class TodoManager:
|
||||
self.file = open(self.filename, "a")
|
||||
return self.file
|
||||
|
||||
# Parse and save file as needed.
|
||||
def read_todos(self) -> list[Todo]:
|
||||
todos = []
|
||||
f = self.open_todo_file()
|
||||
|
||||
for line in f:
|
||||
todos.append(parse_todo(line))
|
||||
|
||||
return todos
|
||||
Reference in New Issue
Block a user