From f3d5064ff2ef9eda0b0fe9ac404d631bb7eef75c Mon Sep 17 00:00:00 2001 From: Dima Gerasimov Date: Fri, 15 May 2020 08:14:06 +0100 Subject: [PATCH] polar: allow properly specifying polar_dir, with ~ as a default --- doc/MODULES.org | 12 ++++++++++++ my/core/__init__.py | 2 +- my/reading/polar.py | 40 ++++++++++++++++++++++++++++++++++------ tests/extra/polar.py | 13 ++++++++----- 4 files changed, 55 insertions(+), 12 deletions(-) diff --git a/doc/MODULES.org b/doc/MODULES.org index ddff2bd..0e01188 100644 --- a/doc/MODULES.org +++ b/doc/MODULES.org @@ -33,6 +33,7 @@ modules = [ ('twint' , 'my.twitter.twint' ), ('twitter', 'my.twitter.archive' ), ('lastfm' , 'my.lastfm' ), + ('polar' , 'my.reading.polar' ), ] def indent(s, spaces=4): @@ -117,4 +118,15 @@ for cls, p in modules: """ export_path: Paths #+end_src +- [[file:../my/reading/polar.py][my.reading.polar]] + + [[https://github.com/burtonator/polar-books][Polar]] articles and highlights + + #+begin_src python + class polar: + ''' + Polar config is optional, you only need it if you want to specify custom 'polar_dir' + ''' + polar_dir: Path = Path('~/.polar').expanduser() + #+end_src :end: diff --git a/my/core/__init__.py b/my/core/__init__.py index bc12b60..4515235 100644 --- a/my/core/__init__.py +++ b/my/core/__init__.py @@ -1,4 +1,4 @@ # this file only keeps the most common & critical types/utility functions from .common import PathIsh, Paths, Json -from .common import get_files +from .common import get_files, LazyLogger from .cfg import make_config diff --git a/my/reading/polar.py b/my/reading/polar.py index 7ba4fc2..4f79fcf 100755 --- a/my/reading/polar.py +++ b/my/reading/polar.py @@ -1,23 +1,51 @@ """ [[https://github.com/burtonator/polar-books][Polar]] articles and highlights """ - from pathlib import Path +from typing import Type, Any, cast, TYPE_CHECKING + + +import my.config + +if not TYPE_CHECKING: + user_config = getattr(my.config, 'polar', None) +else: + # mypy can't handle dynamic base classes... https://github.com/python/mypy/issues/2477 + user_config = object + +# by default, Polar doesn't need any config, so perhaps makes sense to make it defensive here +if user_config is None: + class user_config: # type: ignore[no-redef] + pass + + +from dataclasses import dataclass +@dataclass +class polar(user_config): + ''' + Polar config is optional, you only need it if you want to specify custom 'polar_dir' + ''' + polar_dir: Path = Path('~/.polar').expanduser() + + +from ..core import make_config +config = make_config(polar) + +# todo not sure where it keeps stuff on Windows? +# https://github.com/burtonator/polar-bookshelf/issues/296 + from datetime import datetime from typing import List, Dict, Iterator, NamedTuple, Sequence, Optional import json import pytz -from ..common import LazyLogger, get_files +from ..core import get_files, LazyLogger from ..error import Res, echain, unwrap, sort_res_by from ..kython.konsume import wrap, zoom, ignore -_POLAR_DIR = Path('~').expanduser() / '.polar' - - logger = LazyLogger(__name__) @@ -173,7 +201,7 @@ class Loader: def iter_entries() -> Iterator[Result]: - for d in get_files(_POLAR_DIR, glob='*/state.json'): + for d in get_files(config.polar_dir, glob='*/state.json'): loader = Loader(d) try: yield from loader.load() diff --git a/tests/extra/polar.py b/tests/extra/polar.py index e8c1af0..709f44f 100644 --- a/tests/extra/polar.py +++ b/tests/extra/polar.py @@ -7,18 +7,21 @@ import pytest # type: ignore # todo maybe search fot info.json recursively? @pytest.mark.parametrize('dotpolar', [ + '', 'data/polar/BojanKV_polar/.polar', 'data/polar/TheCedarPrince_KnowledgeRepository', 'data/polar/coelias_polardocs', 'data/polar/warkdarrior_polar-document-repository' ]) def test_hpi(dotpolar: str): - pdir = Path(ROOT / dotpolar) - class user_config: - export_dir = pdir + if dotpolar != '': + pdir = Path(ROOT / dotpolar) + class user_config: + export_dir = pdir + + import my.config + setattr(my.config, 'polar', user_config) - import my.config - setattr(my.config, 'polar', user_config) import sys M = 'my.reading.polar' if M in sys.modules: