aboutsummaryrefslogtreecommitdiffstats
path: root/app/utils.py
blob: 5401c5ceb98276ad7f9359792a13ee27f607d0ba (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
import os
from datetime import datetime
from typing import Any, TypeAlias, Union
from app.Item import Item
from dataclasses import asdict

ItemDictValue: TypeAlias = Union[str, dict[str, "ItemDictValue"], list["ItemDictValue"]]
ItemDict: TypeAlias = dict[str, ItemDictValue]


def evaluate_env_ref(v: Any) -> str:
    if type(v) == str and v.startswith("${") and v.endswith("}"):
        return os.environ[v[2:-1]]
    else:
        return v


def get_config(config: dict[str, Any], key: str) -> Any:
    v: Any
    try:
        v = config[key]
    except KeyError:
        raise Exception(f"no {key} field in config: " + str(config))

    if type(v) == list:
        mapped = map(evaluate_env_ref, v)
        return list(mapped)
    else:
        return evaluate_env_ref(v)


def get_config_or_default(
    config: dict[str, Any], key: str, default: Any | None = None
) -> Any:
    v: Any = config.get(key, default)

    if type(v) == list:
        mapped = map(evaluate_env_ref, v)
        return list(mapped)
    else:
        return evaluate_env_ref(v)


def item_to_dict(item: Item) -> ItemDict:
    d = asdict(item)
    d["pub_date"] = datetime.isoformat(d["pub_date"]) if d["pub_date"] else None
    return d


def dict_to_item(d: ItemDict) -> Item:
    pub_date, rest = (lambda pub_date, **rest: (pub_date, rest))(**d)  # type: ignore
    return Item(pub_date=datetime.fromisoformat(pub_date), **rest)  # type: ignore