better support for pages; hashable, hypothesis link

This commit is contained in:
Dima Gerasimov 2019-03-13 23:38:39 +00:00
parent c5c71cf7f3
commit 63fd835386

View file

@ -1,6 +1,6 @@
from functools import lru_cache from functools import lru_cache
from kython import listdir_abs from kython import listdir_abs
from typing import Dict, List, NamedTuple, Optional from typing import Dict, List, NamedTuple, Optional, Sequence
from pathlib import Path from pathlib import Path
import json import json
from pytz import UTC from pytz import UTC
@ -12,34 +12,36 @@ from kython.misc import the
_PATH = '/L/backups/hypothesis/' _PATH = '/L/backups/hypothesis/'
Url = str
class Entry(NamedTuple): class Entry(NamedTuple):
dt: datetime dt: datetime
summary: str summary: str
content: Optional[str] # might be none if for instance we just marked page with tags. not sure if we want to handle it somehow separately content: Optional[str] # might be none if for instance we just marked page with tags. not sure if we want to handle it somehow separately
link: str link: Url
eid: str eid: str
annotation: Optional[str] annotation: Optional[str]
context: str context: str
tags: List[str] tags: Sequence[str]
hyp_link: str
Url = str
# TODO kython??
cache = lru_cache()
cproperty = lambda f: property(cache(f))
class Page(NamedTuple): class Page(NamedTuple):
highlights: List[Entry] highlights: Sequence[Entry]
@property @cproperty
# @lru_cache() def link(self):
def url(self): return the(h.link for h in self.highlights)
return the(h.url for h in self.highlights)
@property @cproperty
# @lru_cache()
def title(self): def title(self):
return the(h.title for h in self.highlights) return the(h.summary for h in self.highlights)
@property @cproperty
# @lru_cache()
# TODO shit. can't be cached because of self, wtf??? how to get around it??
def dt(self): def dt(self):
return min(h.dt for h in self.highlights) return min(h.dt for h in self.highlights)
@ -68,14 +70,15 @@ def _iter():
annotation = None if len(txt.strip()) == 0 else txt annotation = None if len(txt.strip()) == 0 else txt
context = i['links']['incontext'] context = i['links']['incontext']
yield Entry( yield Entry(
dt, dt=dt,
title, summary=title,
content, content=content,
link, link=link,
eid, eid=eid,
annotation=annotation, annotation=annotation,
context=context, context=context, # TODO FIXME is context used anywhere?
tags=i['tags'], tags=tuple(i['tags']),
hyp_link=context,
) )
@ -83,8 +86,8 @@ def get_pages() -> List[Page]:
grouped = group_by_key(_iter(), key=lambda e: e.link) grouped = group_by_key(_iter(), key=lambda e: e.link)
pages = [] pages = []
for link, group in grouped.items(): for link, group in grouped.items():
group = list(sorted(group, key=lambda e: e.dt)) sgroup = tuple(sorted(group, key=lambda e: e.dt))
pages.append(Page(highlights=group)) pages.append(Page(highlights=sgroup))
pages = list(sorted(pages, key=lambda p: p.dt)) pages = list(sorted(pages, key=lambda p: p.dt))
# TODO fixme page tag?? # TODO fixme page tag??
return pages return pages