-
-
Notifications
You must be signed in to change notification settings - Fork 3
/
notify.py
73 lines (64 loc) · 2.37 KB
/
notify.py
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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
import os
import sys
from functools import partial
from pathlib import Path
from typing import Any
import yaml
from loguru import logger
from notifiers import get_notifier
class Notifier:
def __init__(self, config_path: str = "~/.dotfiles/.notifiers.yaml"):
"""__init__.
Parameters
----------
config_path : str
The path to config file with yaml type. The config will be used buy [notifiers](https://github.com/liiight/notifiers)
For example
```yaml
provider: telegram
kwargs:
chat_id: <Your Chat ID>
token: <Your token>
```
If you need proxy for your provider, please use the config below.
The env will be updated when running `ntf` method
[This solution](https://github.com/liiight/notifiers/issues/236) is proposed by notifiers
```
env:
HTTP_PROXY: 'http://IP:PORT'
HTTPS_PROXY: 'http://IP:PORT'
```
"""
# TODO: DEBUG mode
path = Path(config_path).expanduser()
if not path.exists():
msg = f"Can't find the config file for notifiers: {path}"
logger.warning(msg)
raise FileExistsError(msg)
else:
with path.open() as f:
self.config = yaml.load(f, Loader=yaml.FullLoader)
logger.remove()
log_level = self.config.get("log_level", "INFO")
logger.add(sys.stderr, level=log_level)
logger.debug(f"log level: {log_level}")
self._provider = get_notifier(self.config["provider"])
kwargs = self.config["kwargs"]
self._ntf = partial(self._provider.notify, **kwargs)
self.env = self.config.get("env", {})
def ntf(self, *messages):
message = " ".join(str(m)for m in messages)
logger.debug("Sending message: {}".format(message))
if len(message) == 0:
logger.warning("Blank message.")
# set proxy if needed
env_back = os.environ.copy()
os.environ.update(self.env)
self._ntf(message=message)
for k, v in self.env.items():
if k not in env_back:
del os.environ[k]
else:
os.environ[k] = env_back[k]
def __call__(self, *args: Any, **kwds: Any) -> Any:
return self.ntf(*args, **kwds)