aboutsummaryrefslogtreecommitdiffstats
path: root/app/utils.py
blob: 626ee243c0faa4201c97c9f7c8cde21d1dccc531 (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
from datetime import datetime
from typing import TypeAlias, Union
from app.Item import Item
from dataclasses import asdict

from app.PluginInterface import Params

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


def get_param(key: str, params: Params) -> str:
    if key not in params:
        raise Exception(f"no {key} field in config entry: " + str(params))

    return params[key]


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