HPI/my/taplog.py
Dima Gerasimov 2a2478bfa9 core: update cachew annotations
orgmode: expose method to construct cacheable note
2020-10-21 01:29:29 +02:00

66 lines
1.6 KiB
Python

'''
[[https://play.google.com/store/apps/details?id=com.waterbear.taglog][Taplog]] app data
'''
from datetime import datetime
from typing import NamedTuple, Dict, Optional, Iterable
from .core import get_files
from .core.common import fromisoformat
from my.config import taplog as user_config
class Entry(NamedTuple):
row: Dict
@property
def id(self) -> str:
return str(self.row['_id'])
@property
def number(self) -> Optional[float]:
ns = self.row['number']
# TODO ??
if isinstance(ns, str):
ns = ns.strip()
return None if len(ns) == 0 else float(ns)
else:
return ns
@property
def note(self) -> str:
return self.row['note']
@property
def button(self) -> str:
return self.row['cat1']
@property
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!
def entries() -> Iterable[Entry]:
last = max(get_files(user_config.export_path))
from .core.dataset import connect_readonly
db = connect_readonly(last)
# todo is it sorted by timestamp?
for row in db['Log'].all():
yield Entry(row)
# I guess worth having as top level considering it would be quite common?
def by_button(button: str) -> Iterable[Entry]:
for e in entries():
if e.button == button:
yield e
from .core import stat, Stats
def stats() -> Stats:
return stat(entries)