core: proprely resolve class properties in make_config + add test

This commit is contained in:
Dima Gerasimov 2020-12-13 17:18:49 +00:00 committed by karlicoss
parent dda628e866
commit e81dddddf0
7 changed files with 50 additions and 48 deletions

View file

@ -47,6 +47,7 @@ def from_orgmode() -> Iterator[Result]:
continue
# FIXME use timezone provider
created = config.default_timezone.localize(created)
assert created is not None #??? somehow mypy wasn't happy?
yield Entry(
dt=created,
value=w,

View file

@ -11,23 +11,9 @@ After that, you can set config attributes:
export_path = '/path/to/twitter/exports'
config.twitter = user_config
"""
# TODO do I really need it?
# todo why do we bring this into scope? don't remember..
import my.config as config
from pathlib import Path
from typing import Union
def set_repo(name: str, repo: Union[Path, str]) -> None:
from .core.init import assign_module
from . common import import_from
r = Path(repo)
module = import_from(r.parent, name)
assign_module('my.config.repos', name, module)
# TODO set_repo is still useful, but perhaps move this thing away to core?
# TODO ok, I need to get rid of this, better to rely on regular imports
from .core import __NOT_HPI_MODULE__

View file

@ -63,3 +63,6 @@ class time:
class tz:
pass
class orgmode:
paths: Paths

View file

@ -8,14 +8,19 @@ C = TypeVar('C')
# but short enough to change later
# TODO document why it's necessary?
def make_config(cls: Type[C], migration: Callable[[Attrs], Attrs]=lambda x: x) -> C:
props = dict(vars(cls.__base__))
props = migration(props)
user_config = cls.__base__
old_props = {
# NOTE: deliberately use gettatr to 'force' lcass properties here
k: getattr(user_config, k) for k in vars(user_config)
}
new_props = migration(old_props)
from dataclasses import fields
params = {
k: v
for k, v in props.items()
for k, v in new_props.items()
if k in {f.name for f in fields(cls)}
}
# todo maybe return type here?
return cls(**params) # type: ignore[call-arg]

View file

@ -42,6 +42,7 @@ def _created(n: orgparse.OrgNode) -> Tuple[Optional[datetime], str]:
createds = m.group(0) # could be None
if createds is None:
return (None, heading)
assert isinstance(createds, str)
[odt] = orgparse.date.OrgDate.list_from_str(createds)
dt = odt.start
# todo a bit hacky..