Inherit from the base config

This commit is contained in:
Dima Gerasimov 2020-05-09 22:21:15 +01:00
parent 90b9d1d9c1
commit 4b8c2d4be4

View file

@ -5,6 +5,7 @@ from typing import TYPE_CHECKING
if TYPE_CHECKING: if TYPE_CHECKING:
from typing_extensions import Protocol from typing_extensions import Protocol
# todo extract this for documentation...
class reddit(Protocol): class reddit(Protocol):
''' '''
Reddit module uses [[rexport][https://github.com/karlicoss/rexport]] output Reddit module uses [[rexport][https://github.com/karlicoss/rexport]] output
@ -12,7 +13,17 @@ if TYPE_CHECKING:
export_path: PathIsh # path to the exported data export_path: PathIsh # path to the exported data
rexport : Optional[PathIsh] # path to a local clone of rexport rexport : Optional[PathIsh] # path to a local clone of rexport
# todo extract this in documentation...
# TODO hmm, I need something like an overlay/delegate, which:
# - checks for required attributes (configurable?)
# - fills optional
# - doesn't modify the config user has passed otherwise
# supports existing python code, ideally uses inheritance
#
# I really want loose coupling, so the config wouldn't have to import anything
# this looks promising, but it uses toml/yaml I think.
# https://github.com/karlicoss/HPI/issues/12#issuecomment-610038961
# maybe just use dataclasses or something?
cfg = reddit cfg = reddit
else: else:
@ -22,29 +33,32 @@ else:
# TODO hmm, optional attribute and Optional type are quite different... # TODO hmm, optional attribute and Optional type are quite different...
from typing import Optional, cast
from typing import Optional
from types import ModuleType from types import ModuleType
from .core.common import classproperty, PathIsh from .core.common import classproperty, PathIsh
# todo would be nice to inherit from cfg to get defaults.. but mypy says it's incompatible -- because of classproperty?? # todo would be nice to inherit from cfg to get defaults.. but mypy says it's incompatible -- because of classproperty??
class config: class config(cfg):
@classproperty if not TYPE_CHECKING: # TODO ugh. interferes with typing? not ideal as easy to miss.
def export_path(cls) -> PathIsh: if 'rexport' not in vars(cfg):
legacy: Optional[PathIsh] = getattr(cfg, 'export_dir', None) rexport = None
if legacy is not None: # todo warn?
return legacy # experimenting on
return cfg.export_path if 'export_path' not in vars(cfg):
@classproperty
def export_path(self) -> PathIsh: # type: ignore[override]
legacy_path: Optional[PathIsh] = getattr(cfg, 'export_dir', None)
assert legacy_path is not None # todo warn?
return legacy_path
@classproperty @classproperty
def rexport_module(cls) -> ModuleType: def rexport_module(cls) -> ModuleType:
# todo return Type[rexport]?? # todo return Type[rexport]??
# todo ModuleIsh? # todo ModuleIsh?
rexport: Optional[PathIsh] = getattr(cfg, 'rexport', None) rpath = cls.rexport
if rpath is not None:
if rexport is not None:
from my.cfg import set_repo from my.cfg import set_repo
set_repo('rexport', rexport) set_repo('rexport', rpath)
import my.config.repos.rexport.dal as m import my.config.repos.rexport.dal as m
return m return m