2021-04-02 23:13:41 +00:00
|
|
|
import logging
|
|
|
|
import os
|
|
|
|
import sys
|
|
|
|
from datetime import datetime
|
|
|
|
from typing import List
|
|
|
|
|
|
|
|
import org_rw
|
2021-09-14 21:02:40 +00:00
|
|
|
from org_rw import OrgDoc, OrgTime
|
2021-04-02 23:13:41 +00:00
|
|
|
|
2021-09-14 22:04:39 +00:00
|
|
|
EXTENSIONS = (".org", ".org.txt")
|
|
|
|
|
2021-04-02 23:13:41 +00:00
|
|
|
|
|
|
|
def is_today(ot: OrgTime):
|
|
|
|
now = datetime.now()
|
|
|
|
return (
|
|
|
|
(ot.time.year == now.year)
|
|
|
|
and (ot.time.month == now.month)
|
|
|
|
and (ot.time.day == now.day)
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
class Agenda:
|
2021-09-14 22:04:39 +00:00
|
|
|
def __init__(
|
|
|
|
self,
|
|
|
|
/,
|
|
|
|
with_hour: List[org_rw.Headline],
|
|
|
|
no_hour: List[org_rw.Headline],
|
|
|
|
):
|
2021-04-02 23:13:41 +00:00
|
|
|
self.with_hour = with_hour
|
|
|
|
self.no_hour = no_hour
|
|
|
|
|
|
|
|
def print(self):
|
|
|
|
for item in self.with_hour:
|
|
|
|
print(item.scheduled.time, item.state, item.title)
|
|
|
|
|
|
|
|
if len(self.with_hour) > 0:
|
|
|
|
print("--------")
|
|
|
|
|
|
|
|
for item in self.no_hour:
|
|
|
|
print(item.scheduled.time, item.state, item.title)
|
|
|
|
|
|
|
|
|
|
|
|
class DocumentManager:
|
2021-04-02 23:47:33 +00:00
|
|
|
docs: list[OrgDoc]
|
|
|
|
|
|
|
|
def __init__(self, base_path: os.PathLike):
|
|
|
|
self.base_path = base_path
|
2021-04-02 23:13:41 +00:00
|
|
|
|
|
|
|
def load(self):
|
2021-04-02 23:47:33 +00:00
|
|
|
top = os.path.abspath(self.base_path)
|
2021-04-02 23:13:41 +00:00
|
|
|
|
|
|
|
docs = []
|
|
|
|
|
|
|
|
for root, dirs, files in os.walk(top):
|
2021-09-14 21:02:40 +00:00
|
|
|
# Prune dirs
|
|
|
|
i = 0
|
|
|
|
while i < len(dirs):
|
2021-09-14 22:04:39 +00:00
|
|
|
if dirs[i].startswith(".git"):
|
2021-09-14 21:02:40 +00:00
|
|
|
del dirs[i]
|
|
|
|
else:
|
|
|
|
i += 1
|
|
|
|
|
|
|
|
# Process files
|
2021-04-02 23:13:41 +00:00
|
|
|
for name in files:
|
2021-04-02 23:47:33 +00:00
|
|
|
if all(map(lambda ext: not name.endswith(ext), EXTENSIONS)):
|
2021-04-02 23:13:41 +00:00
|
|
|
continue
|
|
|
|
|
|
|
|
path = os.path.join(root, name)
|
|
|
|
|
|
|
|
try:
|
|
|
|
doc = org_rw.load(open(path), extra_cautious=True)
|
|
|
|
docs.append(doc)
|
|
|
|
except Exception as err:
|
|
|
|
import traceback
|
|
|
|
|
|
|
|
traceback.print_exc()
|
|
|
|
print(f"== On {path}")
|
|
|
|
sys.exit(1)
|
|
|
|
|
|
|
|
logging.info("Loaded {} files".format(len(docs)))
|
|
|
|
|
|
|
|
self.docs = docs
|
|
|
|
|
|
|
|
def get_agenda(self) -> Agenda:
|
|
|
|
headline_count = 0
|
|
|
|
items_in_agenda = []
|
|
|
|
now = datetime.now()
|
|
|
|
|
|
|
|
for doc in self.docs:
|
|
|
|
for hl in doc.getAllHeadlines():
|
|
|
|
headline_count += 1
|
|
|
|
|
2021-10-24 21:24:00 +00:00
|
|
|
if (
|
|
|
|
hl.scheduled
|
|
|
|
and isinstance(hl.scheduled, OrgTime)
|
|
|
|
and hl.scheduled.time.active
|
|
|
|
):
|
2021-04-02 23:13:41 +00:00
|
|
|
if is_today(hl.scheduled):
|
|
|
|
items_in_agenda.append(hl)
|
|
|
|
elif (hl.scheduled.time.to_datetime() < now) and hl.is_todo:
|
|
|
|
items_in_agenda.append(hl)
|
|
|
|
|
|
|
|
logging.info("Read {} items".format(headline_count))
|
|
|
|
logging.info("{} items in agenda today".format(len(items_in_agenda)))
|
|
|
|
|
|
|
|
items_with_hour = [
|
|
|
|
item
|
|
|
|
for item in items_in_agenda
|
|
|
|
if item.scheduled and is_today(item.scheduled) and item.scheduled.time.hour
|
|
|
|
]
|
|
|
|
other_items = [
|
|
|
|
item
|
|
|
|
for item in items_in_agenda
|
|
|
|
if not (
|
|
|
|
item.scheduled and is_today(item.scheduled) and item.scheduled.time.hour
|
|
|
|
)
|
|
|
|
]
|
|
|
|
|
|
|
|
logging.info("{} items today for a specific hour".format(len(items_with_hour)))
|
|
|
|
|
|
|
|
return Agenda(
|
|
|
|
with_hour=sorted(items_with_hour, key=lambda x: x.scheduled.time),
|
2021-09-14 22:04:39 +00:00
|
|
|
no_hour=other_items,
|
2021-04-02 23:13:41 +00:00
|
|
|
)
|
2021-09-14 22:04:39 +00:00
|
|
|
|
|
|
|
def get_notes(self, query) -> List[org_rw.Headline]:
|
|
|
|
headline_count = 0
|
|
|
|
t0 = datetime.now()
|
|
|
|
notes = []
|
|
|
|
query = [q.lower() for q in query]
|
|
|
|
|
|
|
|
for doc in self.docs:
|
|
|
|
for hl in doc.getAllHeadlines():
|
|
|
|
headline_count += 1
|
|
|
|
|
|
|
|
data = "\n".join(hl.get_contents("raw")).lower()
|
|
|
|
if all([q in data for q in query]):
|
|
|
|
notes.append(hl)
|
|
|
|
|
|
|
|
logging.info(
|
|
|
|
"Filtered {} to {} items in {:.3f}s".format(
|
|
|
|
headline_count, len(notes), (datetime.now() - t0).total_seconds()
|
|
|
|
)
|
|
|
|
)
|
|
|
|
|
|
|
|
return notes
|