core: move stuff from tests/demo.py to my/core/tests/test_config.py

also clean all this up a bit
This commit is contained in:
Dima Gerasimov 2024-08-25 16:36:34 +01:00
parent 7331f98efe
commit 2e3bd3d691
4 changed files with 164 additions and 143 deletions

View file

@ -2,19 +2,23 @@
Just a demo module for testing and documentation purposes
'''
from .core import Paths, PathIsh
from typing import Optional
from datetime import tzinfo, timezone
from my.config import demo as user_config
import json
from abc import abstractmethod
from dataclasses import dataclass
from datetime import datetime, timezone, tzinfo
from pathlib import Path
from typing import Iterable, Optional, Protocol, Sequence
from my.core import Json, PathIsh, Paths, get_files
@dataclass
class demo(user_config):
class config(Protocol):
data_path: Paths
# this is to check required attribute handling
username: str
# this is to check optional attribute handling
timezone: tzinfo = timezone.utc
external: Optional[PathIsh] = None
@ -23,47 +27,50 @@ class demo(user_config):
def external_module(self):
rpath = self.external
if rpath is not None:
from .core.utils.imports import import_dir
from my.core.utils.imports import import_dir
return import_dir(rpath)
import my.config.repos.external as m # type: ignore
import my.config.repos.external as m # type: ignore
return m
from .core import make_config
config = make_config(demo)
def make_config() -> config:
from my.config import demo as user_config
# TODO not sure about type checking?
external = config.external_module
class combined_config(user_config, config): ...
return combined_config()
from pathlib import Path
from typing import Sequence, Iterable
from datetime import datetime
from .core import Json, get_files
@dataclass
class Item:
'''
Some completely arbitrary artificial stuff, just for testing
'''
username: str
raw: Json
dt: datetime
def inputs() -> Sequence[Path]:
return get_files(config.data_path)
cfg = make_config()
return get_files(cfg.data_path)
import json
def items() -> Iterable[Item]:
cfg = make_config()
transform = (lambda i: i) if cfg.external is None else cfg.external_module.transform
for f in inputs():
dt = datetime.fromtimestamp(f.stat().st_mtime, tz=config.timezone)
dt = datetime.fromtimestamp(f.stat().st_mtime, tz=cfg.timezone)
j = json.loads(f.read_text())
for raw in j:
yield Item(
username=config.username,
raw=external.identity(raw),
username=cfg.username,
raw=transform(raw),
dt=dt,
)