core: add initial config hacking helper

rescuetime: initial fake data generator
This commit is contained in:
Dima Gerasimov 2020-09-18 20:33:59 +01:00 committed by karlicoss
parent 132db1dc0c
commit ef72ac3386
2 changed files with 46 additions and 13 deletions

View file

@ -16,3 +16,21 @@ def make_config(cls: Type[C], migration: Callable[[Attrs], Attrs]=lambda x: x) -
if k in {f.name for f in fields(cls)}
}
return cls(**params) # type: ignore[call-arg]
F = TypeVar('F')
from contextlib import contextmanager
import inspect
from typing import Iterator
@contextmanager
def override_config(config: F) -> Iterator[F]:
'''
Temporary override for config's parameters, useful for testing/fake data/etc.
'''
orig_properties = {k: v for k, v in vars(config).items() if not k.startswith('__')}
try:
yield config
finally:
# ugh. __dict__ of type objects isn't writable..
for k, v in orig_properties.items():
setattr(config, k, v)

View file

@ -27,13 +27,13 @@ DAL = dal.DAL
Entry = dal.Entry
# todo needs to be cumulative cache
@mcachew
def entries(files=inputs()) -> Iterable[Entry]:
dal = DAL(files)
it = dal.iter_entries()
@mcachew(hashf=lambda: inputs())
def entries() -> Iterable[Entry]:
dal = DAL(inputs())
it = dal.entries()
vit, eit = split_errors(it, ET=Exception)
# todo handle errors, I guess initially I didn't because it's unclear how to easily group?
# todo would be nice if logger unwrapped causes by default??
yield from vit
@ -51,16 +51,31 @@ def stats():
}
# basically, hack config and populate it with fake data? fake data generated by DAL, but the rest is handled by this?
from contextlib import contextmanager
# todo take seed, or what?
@contextmanager
def fake_data(rows=1000):
# todo also disable cachew automatically for such things?
# TODO right, disabled_cachew won't work here because at that point, entries() is already wrapped?
# I guess need to fix this in cachew?
from .core.cachew import disabled_cachew
from .core.cfg import override_config
from tempfile import TemporaryDirectory
with disabled_cachew(), override_config(config) as cfg, TemporaryDirectory() as td:
tdir = Path(td)
cfg.export_path = tdir
f = tdir / 'rescuetime.json'
import json
f.write_text(json.dumps(dal.fake_data_generator(rows=rows)))
yield
# TODO ok, now it's something that actually could run on CI!
# todo not sure if I want to keep these here? vvv
def print_groups():
for gr in groups():
print(f"{gr[0].dt}--{gr[-1].dt}")
# TODO merged db?
# TODO ok, it summarises my sleep intervals pretty well. I guess should adjust it for the fact I don't sleep during the day, and it would be ok!
def fill_influxdb():
from influxdb import InfluxDBClient # type: ignore
client = InfluxDBClient()