my.zotero: handle colors & extract human readable

This commit is contained in:
Dima Gerasimov 2021-04-11 18:42:51 +01:00 committed by karlicoss
parent 1ef2c5619e
commit 68d3385468
2 changed files with 26 additions and 9 deletions

View file

@ -77,9 +77,9 @@ def _dumps_factory(**kwargs) -> Callable[[Any], str]:
return _orjson_dumps return _orjson_dumps
except ModuleNotFoundError: except ModuleNotFoundError:
import json import json
import warnings from .warnings import high
warnings.warn("You might want to install 'orjson' to support serialization for lots more types!") high("You might want to install 'orjson' to support serialization for lots more types!")
def _stdlib_dumps(obj: Any) -> str: def _stdlib_dumps(obj: Any) -> str:
return json.dumps(obj, **kwargs) return json.dumps(obj, **kwargs)

View file

@ -41,6 +41,12 @@ class Annotation:
text: Optional[str] text: Optional[str]
comment: Optional[str] comment: Optional[str]
tags: Sequence[str] tags: Sequence[str]
color_hex: str
"""Original hex-encoded color in zotero"""
@property
def color_human(self) -> str:
return _hex2human(self.color_hex)
def annotations() -> Iterator[Res[Annotation]]: def annotations() -> Iterator[Res[Annotation]]:
@ -56,9 +62,8 @@ def annotations() -> Iterator[Res[Annotation]]:
# type -- 1 is inline; 2 is note? # type -- 1 is inline; 2 is note?
# todo color? -- for org-mode could map into priority?
_QUERY = ''' _QUERY = '''
SELECT A.itemID, A.parentItemID, text, comment, position, path, dateAdded SELECT A.itemID, A.parentItemID, text, comment, color, position, path, dateAdded
FROM itemAnnotations AS A FROM itemAnnotations AS A
LEFT JOIN itemAttachments AS F ON A.parentItemID = F.ItemID LEFT JOIN itemAttachments AS F ON A.parentItemID = F.ItemID
LEFT JOIN items AS I ON A.itemID = I.itemID LEFT JOIN items AS I ON A.itemID = I.itemID
@ -126,14 +131,25 @@ def _enrich_row(r, conn: sqlite3.Connection):
return r return r
def _hex2human(color_hex: str) -> str:
return {
'#ffd400': 'yellow',
'#a28ae5': 'purple',
'#5fb236': 'green' ,
'#ff6666': 'red' ,
'#2ea8e5': 'blue' ,
}.get(color_hex, color_hex)
def _parse_annotation(r: Dict) -> Annotation: def _parse_annotation(r: Dict) -> Annotation:
text = r['text'] text = r['text']
comment = r['comment'] comment = r['comment']
# todo use json query for this? # todo use json query for this?
page = json.loads(r['position'])['pageIndex'] page = json.loads(r['position'])['pageIndex']
path = r['path'] path = r['path']
addeds = r['dateAdded'] addeds = r['dateAdded']
tags = r['tags'] tags = r['tags']
color_hex= r['color']
added = datetime.strptime(addeds, '%Y-%m-%d %H:%M:%S') added = datetime.strptime(addeds, '%Y-%m-%d %H:%M:%S')
added = added.replace(tzinfo=timezone.utc) added = added.replace(tzinfo=timezone.utc)
@ -151,4 +167,5 @@ def _parse_annotation(r: Dict) -> Annotation:
text=text, text=text,
comment=comment, comment=comment,
tags=tags, tags=tags,
color_hex=color_hex,
) )