extract dataclass-based config helper

This commit is contained in:
Dima Gerasimov 2020-05-10 15:18:45 +01:00
parent 217116dfe9
commit 8cbbafae1d
2 changed files with 29 additions and 19 deletions

18
my/core/cfg.py Normal file
View file

@ -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]

View file

@ -3,7 +3,8 @@ Reddit data: saved items/comments/upvotes/etc.
""" """
from typing import Optional from typing import Optional
from .core.common import PathIsh from .core.common import Paths, PathIsh
from types import ModuleType from types import ModuleType
from my.config import reddit as uconfig from my.config import reddit as uconfig
from dataclasses import dataclass from dataclasses import dataclass
@ -13,24 +14,9 @@ class reddit(uconfig):
''' '''
Reddit module uses [[rexport][https://github.com/karlicoss/rexport]] output 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 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 @property
def rexport_module(self) -> ModuleType: def rexport_module(self) -> ModuleType:
# todo return Type[rexport]?? # todo return Type[rexport]??
@ -43,8 +29,14 @@ class reddit(uconfig):
import my.config.repos.rexport.dal as m import my.config.repos.rexport.dal as m
return 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... # TODO not sure about the laziness...