polar: support configuring defensive behaviour, support for highlight tags
This commit is contained in:
parent
844ebf28c1
commit
3d8002c8c9
3 changed files with 38 additions and 14 deletions
|
@ -127,7 +127,9 @@ def wrap(j, throw=True) -> Iterator[Zoomable]:
|
|||
for c in children:
|
||||
if not c.this_consumed(): # TODO hmm. how does it figure out if it's consumed???
|
||||
if throw:
|
||||
raise UnconsumedError(str(c))
|
||||
raise UnconsumedError(f'''
|
||||
Expected {c} to be fully consumed by the parser.
|
||||
'''.lstrip())
|
||||
else:
|
||||
# TODO log?
|
||||
pass
|
||||
|
@ -172,4 +174,14 @@ def test_consume_all():
|
|||
aaa = w['aaa'].zoom()
|
||||
aaa['bbb'].consume_all()
|
||||
|
||||
|
||||
def test_zoom() -> None:
|
||||
import pytest # type: ignore
|
||||
with wrap({'aaa': 'whatever'}) as w:
|
||||
w = cast(Wdict, w)
|
||||
with pytest.raises(KeyError):
|
||||
w['nosuchkey'].zoom()
|
||||
w['aaa'].zoom()
|
||||
|
||||
|
||||
# TODO type check this...
|
||||
|
|
|
@ -27,6 +27,7 @@ class polar(user_config):
|
|||
Polar config is optional, you only need it if you want to specify custom 'polar_dir'
|
||||
'''
|
||||
polar_dir: PathIsh = Path('~/.polar').expanduser()
|
||||
defensive: bool = True # pass False if you want it to fail faster on errors (useful for debugging)
|
||||
|
||||
|
||||
from ..core import make_config
|
||||
|
@ -63,7 +64,7 @@ class Highlight(NamedTuple):
|
|||
created: datetime
|
||||
selection: str
|
||||
comments: Sequence[Comment]
|
||||
|
||||
tags: Sequence[str]
|
||||
|
||||
|
||||
Uid = str
|
||||
|
@ -98,7 +99,7 @@ class Loader:
|
|||
def load_item(self, meta: Zoomable) -> Iterable[Highlight]:
|
||||
meta = cast(Wdict, meta)
|
||||
# TODO this should be destructive zoom?
|
||||
meta['notes'].zoom()
|
||||
meta['notes'].zoom() # TODO ??? is it deliberate?
|
||||
meta['pagemarks'].zoom()
|
||||
if 'notes' in meta:
|
||||
# TODO something nicer?
|
||||
|
@ -153,6 +154,16 @@ class Loader:
|
|||
updated = h['lastUpdated'].zoom().value
|
||||
h['rects'].ignore()
|
||||
|
||||
# TODO make it more generic..
|
||||
htags: List[str] = []
|
||||
if 'tags' in h:
|
||||
ht = h['tags'].zoom()
|
||||
for k, v in list(ht.items()):
|
||||
ctag = v.zoom()
|
||||
ctag['id'].consume()
|
||||
ct = ctag['label'].zoom()
|
||||
htags.append(ct.value)
|
||||
|
||||
h['textSelections'].ignore()
|
||||
h['notes'].consume()
|
||||
h['questions'].consume()
|
||||
|
@ -167,6 +178,7 @@ class Loader:
|
|||
created=isoparse(crt),
|
||||
selection=text,
|
||||
comments=tuple(comments),
|
||||
tags=tuple(htags),
|
||||
)
|
||||
h.consume()
|
||||
|
||||
|
@ -178,7 +190,7 @@ class Loader:
|
|||
|
||||
def load_items(self, metas: Json) -> Iterable[Highlight]:
|
||||
for p, meta in metas.items():
|
||||
with wrap(meta, throw=False) as meta:
|
||||
with wrap(meta, throw=not config.defensive) as meta:
|
||||
yield from self.load_item(meta)
|
||||
|
||||
def load(self) -> Iterable[Result]:
|
||||
|
|
|
@ -27,23 +27,23 @@ def test_orger(prepare: str, tmp_path: Path) -> None:
|
|||
|
||||
|
||||
PARAMS = [
|
||||
# 'data/polar/BojanKV_polar/.polar',
|
||||
'',
|
||||
'data/polar/BojanKV_polar/.polar',
|
||||
'data/polar/TheCedarPrince_KnowledgeRepository',
|
||||
'data/polar/coelias_polardocs',
|
||||
'data/polar/warkdarrior_polar-document-repository'
|
||||
# 'data/polar/TheCedarPrince_KnowledgeRepository',
|
||||
# 'data/polar/coelias_polardocs',
|
||||
# 'data/polar/warkdarrior_polar-document-repository'
|
||||
]
|
||||
|
||||
@pytest.fixture(params=PARAMS)
|
||||
def prepare(request):
|
||||
dotpolar = request.param
|
||||
if dotpolar != '':
|
||||
pdir = Path(ROOT / dotpolar)
|
||||
class user_config:
|
||||
polar_dir = pdir
|
||||
class user_config:
|
||||
if dotpolar != '': # defaul
|
||||
polar_dir = Path(ROOT / dotpolar)
|
||||
defensive = False
|
||||
|
||||
import my.config
|
||||
setattr(my.config, 'polar', user_config)
|
||||
import my.config
|
||||
setattr(my.config, 'polar', user_config)
|
||||
|
||||
import my.reading.polar as polar
|
||||
reload(polar)
|
||||
|
|
Loading…
Add table
Reference in a new issue