From fd900837fa32c93063992d2cb8a7de3b70a95146 Mon Sep 17 00:00:00 2001 From: Dima Gerasimov Date: Thu, 14 Nov 2019 08:24:11 +0000 Subject: [PATCH] Allow configuration for hypothesis module --- my/hypothesis.py | 49 +++++++++++++++++++++++++++++++++++++++--------- my/instapaper.py | 1 + 2 files changed, 41 insertions(+), 9 deletions(-) diff --git a/my/hypothesis.py b/my/hypothesis.py index 824f8da..91d5909 100644 --- a/my/hypothesis.py +++ b/my/hypothesis.py @@ -1,11 +1,49 @@ +from typing import Dict, List, NamedTuple, Optional, Sequence from pathlib import Path +from datetime import datetime + +from .common import group_by_key, the, cproperty, PathIsh from my_configuration import paths import my_configuration.repos.hypexport.model as hypexport +class Config(NamedTuple): + export_path_: Optional[PathIsh]=None + hypexport_path_: Optional[PathIsh]=None + + @property + def export_path(self) -> Path: + ep = self.export_path_ + if ep is not None: + return Path(ep) + else: + from my_configuration import paths + return Path(paths.hypothesis.export_path) + + @property + def hypexport(self): + hp = self.hypexport_path_ + if hp is not None: + raise RuntimeError("TODO") + else: + import my_configuration.repos.hypexport.model as hypexport + return hypexport + +config = Config() +def configure(*, export_path: Optional[PathIsh]=None, hypexport_path: Optional[PathIsh]=None) -> None: + # TODO kwargs? + global config + config = Config( + export_path_=export_path, + hypexport_path_=hypexport_path, + ) + +# TODO for the purposes of mypy, try importing my_configuration anyway? +# return type for this method as well +# TODO check if it works at runtime.. def get_model() -> hypexport.Model: - export_dir = Path(paths.hypexport.export_dir) - sources = list(sorted(export_dir.glob('*.json'))) + export_path = config.export_path + sources = list(sorted(export_path.glob('*.json'))) model = hypexport.Model(sources) return model @@ -13,13 +51,6 @@ def get_model() -> hypexport.Model: Highlight = hypexport.Highlight -from typing import Dict, List, NamedTuple, Optional, Sequence -from pathlib import Path -from datetime import datetime - -from .common import group_by_key, the, cproperty - - class Page(NamedTuple): """ Represents annotated page along with the highlights diff --git a/my/instapaper.py b/my/instapaper.py index 8747717..3775e77 100644 --- a/my/instapaper.py +++ b/my/instapaper.py @@ -30,6 +30,7 @@ def _get_files(): export_path = _export_path if export_path is None: # fallback to my_configuration + # TODO import my_configuration? from . import paths export_path = paths.instapaper.export_path return list(sorted(Path(export_path).glob('*.json')))