polar: allow properly specifying polar_dir, with ~ as a default

This commit is contained in:
Dima Gerasimov 2020-05-15 08:14:06 +01:00
parent 8f86d7706b
commit f3d5064ff2
4 changed files with 55 additions and 12 deletions

View file

@ -33,6 +33,7 @@ modules = [
('twint' , 'my.twitter.twint' ), ('twint' , 'my.twitter.twint' ),
('twitter', 'my.twitter.archive' ), ('twitter', 'my.twitter.archive' ),
('lastfm' , 'my.lastfm' ), ('lastfm' , 'my.lastfm' ),
('polar' , 'my.reading.polar' ),
] ]
def indent(s, spaces=4): def indent(s, spaces=4):
@ -117,4 +118,15 @@ for cls, p in modules:
""" """
export_path: Paths export_path: Paths
#+end_src #+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: :end:

View file

@ -1,4 +1,4 @@
# this file only keeps the most common & critical types/utility functions # this file only keeps the most common & critical types/utility functions
from .common import PathIsh, Paths, Json from .common import PathIsh, Paths, Json
from .common import get_files from .common import get_files, LazyLogger
from .cfg import make_config from .cfg import make_config

View file

@ -1,23 +1,51 @@
""" """
[[https://github.com/burtonator/polar-books][Polar]] articles and highlights [[https://github.com/burtonator/polar-books][Polar]] articles and highlights
""" """
from pathlib import Path 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 datetime import datetime
from typing import List, Dict, Iterator, NamedTuple, Sequence, Optional from typing import List, Dict, Iterator, NamedTuple, Sequence, Optional
import json import json
import pytz import pytz
from ..common import LazyLogger, get_files from ..core import get_files, LazyLogger
from ..error import Res, echain, unwrap, sort_res_by from ..error import Res, echain, unwrap, sort_res_by
from ..kython.konsume import wrap, zoom, ignore from ..kython.konsume import wrap, zoom, ignore
_POLAR_DIR = Path('~').expanduser() / '.polar'
logger = LazyLogger(__name__) logger = LazyLogger(__name__)
@ -173,7 +201,7 @@ class Loader:
def iter_entries() -> Iterator[Result]: 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) loader = Loader(d)
try: try:
yield from loader.load() yield from loader.load()

View file

@ -7,18 +7,21 @@ import pytest # type: ignore
# todo maybe search fot info.json recursively? # todo maybe search fot info.json recursively?
@pytest.mark.parametrize('dotpolar', [ @pytest.mark.parametrize('dotpolar', [
'',
'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'
]) ])
def test_hpi(dotpolar: str): def test_hpi(dotpolar: str):
if dotpolar != '':
pdir = Path(ROOT / dotpolar) pdir = Path(ROOT / dotpolar)
class user_config: class user_config:
export_dir = pdir export_dir = pdir
import my.config import my.config
setattr(my.config, 'polar', user_config) setattr(my.config, 'polar', user_config)
import sys import sys
M = 'my.reading.polar' M = 'my.reading.polar'
if M in sys.modules: if M in sys.modules: