From 8cbbafae1dc0c52f4839e0771277ae6dc123060d Mon Sep 17 00:00:00 2001 From: Dima Gerasimov Date: Sun, 10 May 2020 15:18:45 +0100 Subject: [PATCH] extract dataclass-based config helper --- my/core/cfg.py | 18 ++++++++++++++++++ my/reddit.py | 30 +++++++++++------------------- 2 files changed, 29 insertions(+), 19 deletions(-) create mode 100644 my/core/cfg.py diff --git a/my/core/cfg.py b/my/core/cfg.py new file mode 100644 index 0000000..c8fa96e --- /dev/null +++ b/my/core/cfg.py @@ -0,0 +1,18 @@ +from typing import TypeVar, Type, Callable, Dict, Any + +Attrs = Dict[str, Any] + +C = TypeVar('C') + +# todo not sure about it, could be overthinking... +# but short enough to change later +def make_config(cls: Type[C], migration: Callable[[Attrs], Attrs]=lambda x: x) -> C: + props = dict(vars(cls.__base__)) + props = migration(props) + from dataclasses import fields + params = { + k: v + for k, v in props.items() + if k in {f.name for f in fields(cls)} + } + return cls(**params) # type: ignore[call-arg] diff --git a/my/reddit.py b/my/reddit.py index ee65687..29e7242 100755 --- a/my/reddit.py +++ b/my/reddit.py @@ -3,7 +3,8 @@ Reddit data: saved items/comments/upvotes/etc. """ from typing import Optional -from .core.common import PathIsh +from .core.common import Paths, PathIsh + from types import ModuleType from my.config import reddit as uconfig from dataclasses import dataclass @@ -13,24 +14,9 @@ class reddit(uconfig): ''' Reddit module uses [[rexport][https://github.com/karlicoss/rexport]] output ''' - export_path: PathIsh # path to the exported data + export_path: Paths # path[s]/glob to the exported data rexport : Optional[PathIsh] = None # path to a local clone of rexport - @classmethod - def make_config(cls) -> 'reddit': - from dataclasses import fields - - props = dict(vars(cls.__base__)) - if 'export_dir' in props: # legacy name - props['export_path'] = props['export_dir'] - - params = { - k: v - for k, v in props.items() - if k in {f.name for f in fields(cls)} - } - return cls(**params) - @property def rexport_module(self) -> ModuleType: # todo return Type[rexport]?? @@ -43,8 +29,14 @@ class reddit(uconfig): import my.config.repos.rexport.dal as m return m -# TODO generate a generic helper for make config?? -config = reddit.make_config() + +from .core.cfg import make_config, Attrs +# hmm, also nice thing about this is that migration is possible to test without the rest of the config? +def migration(attrs: Attrs) -> Attrs: + if 'export_dir' in attrs: # legacy name + attrs['export_path'] = attrs['export_dir'] + return attrs +config = make_config(reddit, migration=migration) ### # TODO not sure about the laziness...