get rid of porg dependency, use orgparse directly

This commit is contained in:
Dima Gerasimov 2020-11-06 04:04:05 +00:00 committed by karlicoss
parent 62e1bdc39a
commit a6e5908e6d
8 changed files with 101 additions and 54 deletions

View file

@ -2,8 +2,6 @@
Various helpers for reading org-mode data
"""
from datetime import datetime
def parse_org_datetime(s: str) -> datetime:
s = s.strip('[]')
for fmt, cl in [
@ -19,3 +17,36 @@ def parse_org_datetime(s: str) -> datetime:
continue
else:
raise RuntimeError(f"Bad datetime string {s}")
from orgparse import OrgNode
from typing import Iterable, TypeVar, Callable
V = TypeVar('V')
def collect(n: OrgNode, cfun: Callable[[OrgNode], Iterable[V]]) -> Iterable[V]:
yield from cfun(n)
for c in n.children:
yield from collect(c, cfun)
from more_itertools import one
from orgparse.extra import Table
def one_table(o: OrgNode) -> Table:
return one(collect(o, lambda n: (x for x in n.body_rich if isinstance(x, Table))))
from typing import Iterator, Dict, Any
class TypedTable(Table):
def __new__(cls, orig: Table) -> 'TypedTable':
tt = super().__new__(TypedTable)
tt.__dict__ = orig.__dict__
blocks = list(orig.blocks)
header = blocks[0] # fist block is schema
if len(header) == 2:
# TODO later interpret first line as types
header = header[1:]
tt._blocks = [header, *blocks[1:]]
return tt
@property
def blocks(self):
return getattr(self, '_blocks')