|
1
|
|
|
from __future__ import annotations |
|
|
|
|
|
|
2
|
|
|
|
|
3
|
|
|
import logging |
|
4
|
|
|
from collections import Mapping |
|
|
|
|
|
|
5
|
|
|
from dataclasses import dataclass |
|
6
|
|
|
from typing import AbstractSet, Any, Callable, Sequence, Union |
|
7
|
|
|
|
|
8
|
|
|
from notifiers import notify |
|
|
|
|
|
|
9
|
|
|
from notifiers.core import Response, get_notifier |
|
|
|
|
|
|
10
|
|
|
|
|
11
|
|
|
from pocketutils.core import PathLike |
|
|
|
|
|
|
12
|
|
|
from pocketutils.core.dot_dict import NestedDotDict |
|
|
|
|
|
|
13
|
|
|
from pocketutils.core.exceptions import XValueError |
|
|
|
|
|
|
14
|
|
|
|
|
15
|
|
|
logger = logging.getLogger("pocketutils") |
|
16
|
|
|
|
|
17
|
|
|
|
|
18
|
|
|
@dataclass(frozen=True) |
|
19
|
|
|
class Notifier: |
|
20
|
|
|
""" |
|
21
|
|
|
A simple config for notifiers from a .json or .toml file. |
|
22
|
|
|
""" |
|
23
|
|
|
|
|
24
|
|
|
__config: Mapping[str, Mapping[str, Union[int, str, bool]]] |
|
25
|
|
|
_warn: Union[bool, Callable[[Response], Any]] = True |
|
26
|
|
|
|
|
27
|
|
|
@property |
|
28
|
|
|
def services(self) -> AbstractSet[str]: |
|
|
|
|
|
|
29
|
|
|
return self.__config.keys() |
|
30
|
|
|
|
|
31
|
|
|
@classmethod |
|
32
|
|
|
def from_json_file( |
|
|
|
|
|
|
33
|
|
|
cls, path: PathLike, *, warn: Union[bool, Callable[[Response], Any]] = True |
|
|
|
|
|
|
34
|
|
|
) -> Notifier: |
|
35
|
|
|
return cls.from_dict(NestedDotDict.read_json(path), warn=warn) |
|
36
|
|
|
|
|
37
|
|
|
@classmethod |
|
38
|
|
|
def from_toml_file( |
|
|
|
|
|
|
39
|
|
|
cls, path: PathLike, *, warn: Union[bool, Callable[[Response], Any]] = True |
|
|
|
|
|
|
40
|
|
|
) -> Notifier: |
|
41
|
|
|
return cls.from_dict(NestedDotDict.read_toml(path), warn=warn) |
|
42
|
|
|
|
|
43
|
|
|
@classmethod |
|
44
|
|
|
def from_dict( |
|
|
|
|
|
|
45
|
|
|
cls, dct: Sequence[Any], *, warn: Union[bool, Callable[[Response], Any]] = True |
|
|
|
|
|
|
46
|
|
|
) -> Notifier: |
|
47
|
|
|
return Notifier({x["services"]: x["defaults"] for x in dct}, _warn=warn) |
|
48
|
|
|
|
|
49
|
|
|
def __post_init__(self): |
|
50
|
|
|
for k, v in self.__config.items(): |
|
|
|
|
|
|
51
|
|
|
if "message" in v: |
|
52
|
|
|
raise XValueError(f"Do not include message in defaults") |
|
|
|
|
|
|
53
|
|
|
get_notifier(provider_name=k, strict=True) # test |
|
54
|
|
|
|
|
55
|
|
|
def notify(self, message: str) -> Mapping[str, Response]: |
|
|
|
|
|
|
56
|
|
|
returns = {} |
|
57
|
|
|
for service, defaults in self.__config.items(): |
|
58
|
|
|
r = notify(service, message=message, **defaults) |
|
|
|
|
|
|
59
|
|
|
returns[service] = r |
|
60
|
|
|
if self._warn and not r.ok and not defaults.get("raise_on_errors", False): |
|
61
|
|
|
if callable(self._warn): |
|
62
|
|
|
self._warn(r) |
|
63
|
|
|
else: |
|
64
|
|
|
logger.error(f"Error ({r.status}) notifying via {service}: {r.errors}") |
|
|
|
|
|
|
65
|
|
|
return returns |
|
66
|
|
|
|
|
67
|
|
|
def __repr__(self): |
|
68
|
|
|
return f"{self.__class__.__name__}({', '.join(self.services)})" |
|
69
|
|
|
|
|
70
|
|
|
def __str__(self): |
|
71
|
|
|
return repr(self) |
|
72
|
|
|
|
|
73
|
|
|
|
|
74
|
|
|
__all__ = ["Notifier"] |
|
75
|
|
|
|