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:
|
for c in children:
|
||||||
if not c.this_consumed(): # TODO hmm. how does it figure out if it's consumed???
|
if not c.this_consumed(): # TODO hmm. how does it figure out if it's consumed???
|
||||||
if throw:
|
if throw:
|
||||||
raise UnconsumedError(str(c))
|
raise UnconsumedError(f'''
|
||||||
|
Expected {c} to be fully consumed by the parser.
|
||||||
|
'''.lstrip())
|
||||||
else:
|
else:
|
||||||
# TODO log?
|
# TODO log?
|
||||||
pass
|
pass
|
||||||
|
@ -172,4 +174,14 @@ def test_consume_all():
|
||||||
aaa = w['aaa'].zoom()
|
aaa = w['aaa'].zoom()
|
||||||
aaa['bbb'].consume_all()
|
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...
|
# 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 config is optional, you only need it if you want to specify custom 'polar_dir'
|
||||||
'''
|
'''
|
||||||
polar_dir: PathIsh = Path('~/.polar').expanduser()
|
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
|
from ..core import make_config
|
||||||
|
@ -63,7 +64,7 @@ class Highlight(NamedTuple):
|
||||||
created: datetime
|
created: datetime
|
||||||
selection: str
|
selection: str
|
||||||
comments: Sequence[Comment]
|
comments: Sequence[Comment]
|
||||||
|
tags: Sequence[str]
|
||||||
|
|
||||||
|
|
||||||
Uid = str
|
Uid = str
|
||||||
|
@ -98,7 +99,7 @@ class Loader:
|
||||||
def load_item(self, meta: Zoomable) -> Iterable[Highlight]:
|
def load_item(self, meta: Zoomable) -> Iterable[Highlight]:
|
||||||
meta = cast(Wdict, meta)
|
meta = cast(Wdict, meta)
|
||||||
# TODO this should be destructive zoom?
|
# TODO this should be destructive zoom?
|
||||||
meta['notes'].zoom()
|
meta['notes'].zoom() # TODO ??? is it deliberate?
|
||||||
meta['pagemarks'].zoom()
|
meta['pagemarks'].zoom()
|
||||||
if 'notes' in meta:
|
if 'notes' in meta:
|
||||||
# TODO something nicer?
|
# TODO something nicer?
|
||||||
|
@ -153,6 +154,16 @@ class Loader:
|
||||||
updated = h['lastUpdated'].zoom().value
|
updated = h['lastUpdated'].zoom().value
|
||||||
h['rects'].ignore()
|
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['textSelections'].ignore()
|
||||||
h['notes'].consume()
|
h['notes'].consume()
|
||||||
h['questions'].consume()
|
h['questions'].consume()
|
||||||
|
@ -167,6 +178,7 @@ class Loader:
|
||||||
created=isoparse(crt),
|
created=isoparse(crt),
|
||||||
selection=text,
|
selection=text,
|
||||||
comments=tuple(comments),
|
comments=tuple(comments),
|
||||||
|
tags=tuple(htags),
|
||||||
)
|
)
|
||||||
h.consume()
|
h.consume()
|
||||||
|
|
||||||
|
@ -178,7 +190,7 @@ class Loader:
|
||||||
|
|
||||||
def load_items(self, metas: Json) -> Iterable[Highlight]:
|
def load_items(self, metas: Json) -> Iterable[Highlight]:
|
||||||
for p, meta in metas.items():
|
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)
|
yield from self.load_item(meta)
|
||||||
|
|
||||||
def load(self) -> Iterable[Result]:
|
def load(self) -> Iterable[Result]:
|
||||||
|
|
|
@ -27,23 +27,23 @@ def test_orger(prepare: str, tmp_path: Path) -> None:
|
||||||
|
|
||||||
|
|
||||||
PARAMS = [
|
PARAMS = [
|
||||||
|
# 'data/polar/BojanKV_polar/.polar',
|
||||||
'',
|
'',
|
||||||
'data/polar/BojanKV_polar/.polar',
|
# 'data/polar/TheCedarPrince_KnowledgeRepository',
|
||||||
'data/polar/TheCedarPrince_KnowledgeRepository',
|
# 'data/polar/coelias_polardocs',
|
||||||
'data/polar/coelias_polardocs',
|
# 'data/polar/warkdarrior_polar-document-repository'
|
||||||
'data/polar/warkdarrior_polar-document-repository'
|
|
||||||
]
|
]
|
||||||
|
|
||||||
@pytest.fixture(params=PARAMS)
|
@pytest.fixture(params=PARAMS)
|
||||||
def prepare(request):
|
def prepare(request):
|
||||||
dotpolar = request.param
|
dotpolar = request.param
|
||||||
if dotpolar != '':
|
class user_config:
|
||||||
pdir = Path(ROOT / dotpolar)
|
if dotpolar != '': # defaul
|
||||||
class user_config:
|
polar_dir = Path(ROOT / dotpolar)
|
||||||
polar_dir = pdir
|
defensive = False
|
||||||
|
|
||||||
import my.config
|
import my.config
|
||||||
setattr(my.config, 'polar', user_config)
|
setattr(my.config, 'polar', user_config)
|
||||||
|
|
||||||
import my.reading.polar as polar
|
import my.reading.polar as polar
|
||||||
reload(polar)
|
reload(polar)
|
||||||
|
|
Loading…
Add table
Reference in a new issue