From c75747f371a1936a4b66382608558c73de6957b0 Mon Sep 17 00:00:00 2001 From: Dima Gerasimov Date: Sat, 9 May 2020 20:06:07 +0100 Subject: [PATCH] improving config documentation and allowing for fallbacks --- my/reddit.py | 55 ++++++++++++++++++++++++++++++++++++++++++++++------ 1 file changed, 49 insertions(+), 6 deletions(-) diff --git a/my/reddit.py b/my/reddit.py index b5293ed..059579d 100755 --- a/my/reddit.py +++ b/my/reddit.py @@ -2,13 +2,58 @@ Reddit data: saved items/comments/upvotes/etc. """ from pathlib import Path -from typing import List, Sequence, Mapping, Iterator +from typing import List, Sequence, Mapping, Iterator, Optional, TYPE_CHECKING from .kython.kompress import CPath -from .common import mcachew, get_files, LazyLogger, make_dict +from .core.common import mcachew, get_files, LazyLogger, make_dict, PathIsh, classproperty -from my.config import reddit as config -import my.config.repos.rexport.dal as rexport +from my.config import reddit as cfg + +from types import ModuleType + +class config: + ''' + Reddit module uses [[rexport][https://github.com/karlicoss/rexport]] output. + + config reddit: + export_dir: Path | str # path to the exported data + rexport : Optional[Path | str] # path to a local clone of rexport + ''' + # TODO config could load export.. is it too mad? + + + @classproperty + def export_dir(cls) -> PathIsh: + # todo migrate this to export_path? + # TODO here we can defensively extract export_path/export_dir? + return cfg.export_dir + # TODO use export_path + + @classproperty + def rexport(cls) -> ModuleType: + # todo return Type[rexport]?? + # todo ModuleIsh? + rexport: Optional[PathIsh] = getattr(cfg, 'rexport', None) + + if rexport is not None: + from my.cfg import set_repo + set_repo('rexport', rexport) + + import my.config.repos.rexport.dal as m + return m + + +if TYPE_CHECKING: + # TODO not sure what is the right way to handle this.. + import my.config.repos.rexport.dal as rexport + +# interesting... if I move this before TYPE_CHECKING, it fails +# apparently the former wins in case of mypy? +rexport = config.rexport + +### + +logger = LazyLogger(__name__, level='debug') def inputs() -> Sequence[Path]: @@ -19,8 +64,6 @@ def inputs() -> Sequence[Path]: # todo move the assert to get_files? return tuple(res) -logger = LazyLogger(__name__, level='debug') - Sid = rexport.Sid Save = rexport.Save