From 2a2478bfa936e54f4c2a797c24d4bc14156b8e35 Mon Sep 17 00:00:00 2001 From: Dima Gerasimov Date: Wed, 14 Oct 2020 22:05:42 +0100 Subject: [PATCH] core: update cachew annotations orgmode: expose method to construct cacheable note --- my/core/common.py | 11 ++++++++++- my/orgmode.py | 33 ++++++++++++++++++++++----------- my/taplog.py | 1 + 3 files changed, 33 insertions(+), 12 deletions(-) diff --git a/my/core/common.py b/my/core/common.py index 9cabef0..d57f63a 100644 --- a/my/core/common.py +++ b/my/core/common.py @@ -197,7 +197,16 @@ if TYPE_CHECKING: # ok, that's actually a super nice 'pattern' F = TypeVar('F') class McachewType(Protocol): - def __call__(self, cache_path: Any=None, *, hashf: Any=None, chunk_by: int=0, logger: Any=None) -> Callable[[F], F]: + def __call__( + self, + cache_path: Any=None, + *, + hashf: Any=None, # todo deprecate + depends_on: Any=None, + force_file: bool=False, + chunk_by: int=0, + logger: Any=None, + ) -> Callable[[F], F]: ... mcachew: McachewType diff --git a/my/orgmode.py b/my/orgmode.py index bf47606..a59dce3 100644 --- a/my/orgmode.py +++ b/my/orgmode.py @@ -1,7 +1,7 @@ ''' Programmatic access and queries to org-mode files on the filesystem ''' -from datetime import datetime +from datetime import datetime, date from pathlib import Path from typing import List, Sequence, Iterable, NamedTuple, Optional @@ -28,6 +28,26 @@ def _sanitize(p: Path) -> str: return re.sub(r'\W', '_', str(p)) +def to_note(x: Org) -> OrgNote: + # ugh. hack to merely make it cacheable + created: Optional[datetime] + try: + # TODO(porg) not sure if created should ever throw... maybe warning/log? + c = x.created + if c is not None and isinstance(c, date): + # meh. not sure if should return date... + created = None + else: + created = c + except Exception as e: + created = None + return OrgNote( + created=created, + heading=x.heading, # todo include the rest? + tags=list(x.tags), + ) + + # todo move to porg? class Query: def __init__(self, files: Sequence[Path]) -> None: @@ -41,16 +61,7 @@ class Query: def _iterate(self, f: Path) -> Iterable[OrgNote]: o = Org.from_file(f) for x in o.iterate(): - try: - # TODO(porg) not sure if created should ever throw... maybe warning/log? - created = x.created - except Exception as e: - created = None - yield OrgNote( - created=created, - heading=x.heading, # todo include the rest? - tags=list(x.tags), - ) + yield to_note(x) def all(self) -> Iterable[OrgNote]: for f in self.files: diff --git a/my/taplog.py b/my/taplog.py index 0b11508..adc84c8 100644 --- a/my/taplog.py +++ b/my/taplog.py @@ -40,6 +40,7 @@ class Entry(NamedTuple): def timestamp(self) -> datetime: ts = self.row['timestamp'] # already with timezone apparently + # TODO not sure if should stil localize though? it only kept tz offset, not real tz return fromisoformat(ts) # TODO also has gps info!