core: update cachew annotations

orgmode: expose method to construct cacheable note
This commit is contained in:
Dima Gerasimov 2020-10-14 22:05:42 +01:00 committed by karlicoss
parent bdfac96352
commit 2a2478bfa9
3 changed files with 33 additions and 12 deletions

View file

@ -197,7 +197,16 @@ if TYPE_CHECKING:
# ok, that's actually a super nice 'pattern' # ok, that's actually a super nice 'pattern'
F = TypeVar('F') F = TypeVar('F')
class McachewType(Protocol): 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 mcachew: McachewType

View file

@ -1,7 +1,7 @@
''' '''
Programmatic access and queries to org-mode files on the filesystem 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 pathlib import Path
from typing import List, Sequence, Iterable, NamedTuple, Optional from typing import List, Sequence, Iterable, NamedTuple, Optional
@ -28,6 +28,26 @@ def _sanitize(p: Path) -> str:
return re.sub(r'\W', '_', str(p)) 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? # todo move to porg?
class Query: class Query:
def __init__(self, files: Sequence[Path]) -> None: def __init__(self, files: Sequence[Path]) -> None:
@ -41,16 +61,7 @@ class Query:
def _iterate(self, f: Path) -> Iterable[OrgNote]: def _iterate(self, f: Path) -> Iterable[OrgNote]:
o = Org.from_file(f) o = Org.from_file(f)
for x in o.iterate(): for x in o.iterate():
try: yield to_note(x)
# 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),
)
def all(self) -> Iterable[OrgNote]: def all(self) -> Iterable[OrgNote]:
for f in self.files: for f in self.files:

View file

@ -40,6 +40,7 @@ class Entry(NamedTuple):
def timestamp(self) -> datetime: def timestamp(self) -> datetime:
ts = self.row['timestamp'] ts = self.row['timestamp']
# already with timezone apparently # already with timezone apparently
# TODO not sure if should stil localize though? it only kept tz offset, not real tz
return fromisoformat(ts) return fromisoformat(ts)
# TODO also has gps info! # TODO also has gps info!