todo/todo/Todo.py

148 lines
5.0 KiB
Python
Raw Permalink Normal View History

2021-09-25 19:47:26 +00:00
#!/bin/python3
import re
import logging
from pathlib import Path
from datetime import datetime
from typing import Optional, Type
2021-09-25 19:47:26 +00:00
from .TodoObject import TodoObject
from .Category import Category
from .Note import Note
from .Task import Task
logger = logging.getLogger("Todo")
logger.addHandler(logging.StreamHandler())
class Todo(TodoObject):
2021-09-26 00:16:08 +00:00
def __init__(self, data_file : Path = Path.home().joinpath("todo.md"), tab_spacing = 2):
2021-09-25 19:47:26 +00:00
"""Todo class, reads and parses a todo.md from a file. Default file is 'todo.md' in the home directory.
Args:
data_file (Path, optional): Location of the markdown file to load from. Defaults to ~/todo.md.
2021-09-26 00:16:08 +00:00
tab_spacing (int, optional): How many levels a tab character should represent. Defaults to 2.
2021-09-25 19:47:26 +00:00
"""
self.data_file = data_file
2021-09-26 00:16:08 +00:00
self.tab_spacing = tab_spacing
2021-09-25 19:47:26 +00:00
super().__init__(-1, "File")
self._load_data()
def __str__(self) -> str:
return f"File: {self.data_file.name}"
2021-09-25 19:47:26 +00:00
def _get_level(self, line: str) -> int:
"""Calculates the level at which this element lies by parsing the given markdown line.
2021-09-25 19:47:26 +00:00
Args:
line (str): The string to parse.
Returns:
int: The level, starting at 0.
"""
count = 0
if line[0] == "-" or line[0] == " ":
for char in line:
if char == " ":
count += 1
else:
break
return count // 2
elif line[0] == "#":
for char in line:
if char == "#":
count += 1
else:
break
return count - 1
else: # catch-all for unlisted notes
return 0
2021-09-28 05:36:12 +00:00
def _get_parent(self, level: int, obj: TodoObject, obj_type: Optional[Type[TodoObject]] = None):
"""Gets the parent for an object at level X.
If an object with a lower level cannot be found within the obj, it will return the obj.
2021-09-25 19:47:26 +00:00
Args:
level (int): The level to find the parent for.
obj (TodoObject): Object to grab parents from.
2021-09-28 05:36:12 +00:00
obj_type (Type[TodoObject], optional): The type of the object the parent should be. Default is None.
2021-09-25 19:47:26 +00:00
Returns:
TodoObject: The object found.
"""
if level > 0:
for child in obj.children[::-1]:
if obj_type is not None and isinstance(child, obj_type) and child.level < level:
2021-09-25 19:47:26 +00:00
return child
if obj_type is None and child.level < level:
return child
return obj
2021-09-25 19:47:26 +00:00
else:
return obj
2021-09-25 19:47:26 +00:00
def _parse_note(self, line: str, parent: TodoObject):
"""Parses a markdown line representing a note.
Args:
line (str): The line to parse.
category (Category): The category this note belongs to.
"""
level = self._get_level(line)
parent = self._get_parent(level, parent)
if re.match(" * -", line):
Note(level, line[level*2+2:], parent)
else:
Note(level, line[level*2:], parent, False)
def _parse_task(self, line: str, parent: Category):
"""Parses the markdown line representing a task.
Args:
line (str): The line to parse.
category (Category): The category this task belongs to.
"""
level = self._get_level(line)
parent = self._get_parent(level, parent)
if "[ ]" in line:
complete = False
elif "[x]" in line:
complete = True
line = line[level*2+6:]
if "|" in line:
line = line.replace(" |", "|")
name = line.split("|")[0]
date = datetime.strptime(line.split("|")[1], '%b %d %Y')
else:
name = line
date = None
Task(name, date, complete, level, parent)
def _parse_category(self, line: str):
"""Parses the markdown line representing a category of todos.
Args:
line (str): The line to parse.
"""
level = self._get_level(line)
parent = self._get_parent(level, self, obj_type=Category)
2021-09-25 19:47:26 +00:00
line = line[level+2:]
Category(line, level, parent)
def _load_data(self):
"""Load categories and tasks from self.data_file."""
with self.data_file.open("r") as f:
for line in f:
2021-09-26 00:16:08 +00:00
line = line.replace("\t", self.tab_spacing*" ")
2021-09-25 19:47:26 +00:00
line = line.rstrip()
if len(line.strip()) == 0: # skip empty lines
continue
elif line[0] == "#":
self._parse_category(line)
elif re.match(" *- \\[[x ]\\]", line):
self._parse_task(line, self.get_children(obj_type=Category)[-1])
2021-09-25 19:47:26 +00:00
else:
self._parse_note(line, self.get_children(obj_type=Category)[-1])
2021-09-25 19:47:26 +00:00
def write_data(self):
"""Write the Todo data to the datafile."""
self.data_file.write_text(self.get_md())