hereyougo 0.0.2__tar.gz
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- hereyougo-0.0.2/PKG-INFO +24 -0
- hereyougo-0.0.2/README.md +15 -0
- hereyougo-0.0.2/pyproject.toml +20 -0
- hereyougo-0.0.2/setup.cfg +4 -0
- hereyougo-0.0.2/setup.py +26 -0
- hereyougo-0.0.2/src/hereyougo/__init__.py +6 -0
- hereyougo-0.0.2/src/hereyougo/_client.py +51 -0
- hereyougo-0.0.2/src/hereyougo.egg-info/PKG-INFO +24 -0
- hereyougo-0.0.2/src/hereyougo.egg-info/SOURCES.txt +10 -0
- hereyougo-0.0.2/src/hereyougo.egg-info/dependency_links.txt +1 -0
- hereyougo-0.0.2/src/hereyougo.egg-info/top_level.txt +1 -0
- hereyougo-0.0.2/tests/test_client.py +66 -0
hereyougo-0.0.2/PKG-INFO
ADDED
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
Metadata-Version: 2.4
|
|
2
|
+
Name: hereyougo
|
|
3
|
+
Version: 0.0.2
|
|
4
|
+
Summary: Send a Telegram notification from your Python code
|
|
5
|
+
Author: KiShmakov
|
|
6
|
+
License: MIT
|
|
7
|
+
Requires-Python: >=3.9
|
|
8
|
+
Description-Content-Type: text/markdown
|
|
9
|
+
|
|
10
|
+
# HereYouGo
|
|
11
|
+
|
|
12
|
+
A tiny, dependency-free client for sending messages directly from your Python
|
|
13
|
+
code:
|
|
14
|
+
|
|
15
|
+
```python
|
|
16
|
+
import hereyougo
|
|
17
|
+
|
|
18
|
+
hereyougo.notify("Training finished", user_id="my-notification-id")
|
|
19
|
+
```
|
|
20
|
+
|
|
21
|
+
Network and HTTP errors are raised to the caller. The default request timeout is
|
|
22
|
+
10 seconds and can be changed with
|
|
23
|
+
`hereyougo.notify(message, user_id="my-id", timeout=30)`. The recipient may
|
|
24
|
+
also be set with the `HEREYOUGO_USER_ID` environment variable.
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
# HereYouGo
|
|
2
|
+
|
|
3
|
+
A tiny, dependency-free client for sending messages directly from your Python
|
|
4
|
+
code:
|
|
5
|
+
|
|
6
|
+
```python
|
|
7
|
+
import hereyougo
|
|
8
|
+
|
|
9
|
+
hereyougo.notify("Training finished", user_id="my-notification-id")
|
|
10
|
+
```
|
|
11
|
+
|
|
12
|
+
Network and HTTP errors are raised to the caller. The default request timeout is
|
|
13
|
+
10 seconds and can be changed with
|
|
14
|
+
`hereyougo.notify(message, user_id="my-id", timeout=30)`. The recipient may
|
|
15
|
+
also be set with the `HEREYOUGO_USER_ID` environment variable.
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
[build-system]
|
|
2
|
+
requires = ["setuptools>=61"]
|
|
3
|
+
build-backend = "setuptools.build_meta"
|
|
4
|
+
|
|
5
|
+
[project]
|
|
6
|
+
name = "hereyougo"
|
|
7
|
+
dynamic = ["version"]
|
|
8
|
+
description = "Send a Telegram notification from your Python code"
|
|
9
|
+
readme = "README.md"
|
|
10
|
+
requires-python = ">=3.9"
|
|
11
|
+
license = { text = "MIT" }
|
|
12
|
+
authors = [
|
|
13
|
+
{ name = "KiShmakov" },
|
|
14
|
+
]
|
|
15
|
+
|
|
16
|
+
[tool.setuptools.dynamic]
|
|
17
|
+
version = { attr = "hereyougo.__version__" }
|
|
18
|
+
|
|
19
|
+
[tool.setuptools.packages.find]
|
|
20
|
+
where = ["src"]
|
hereyougo-0.0.2/setup.py
ADDED
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
"""
|
|
2
|
+
Bake server address into the built package. Should be built on proper machine.
|
|
3
|
+
"""
|
|
4
|
+
|
|
5
|
+
import os
|
|
6
|
+
from pathlib import Path
|
|
7
|
+
|
|
8
|
+
from setuptools import setup
|
|
9
|
+
from setuptools.command.build_py import build_py
|
|
10
|
+
|
|
11
|
+
|
|
12
|
+
class BuildPyWithServer(build_py):
|
|
13
|
+
def run(self):
|
|
14
|
+
super().run()
|
|
15
|
+
host = os.environ.get("HEREYOUGO_VM_IP")
|
|
16
|
+
port = os.environ.get("HEREYOUGO_BOT_PORT")
|
|
17
|
+
if not host or not port:
|
|
18
|
+
raise SystemExit(
|
|
19
|
+
"HEREYOUGO_VM_IP and HEREYOUGO_BOT_PORT must be set to build "
|
|
20
|
+
"hereyougo"
|
|
21
|
+
)
|
|
22
|
+
target = Path(self.build_lib, "hereyougo", "_server.py")
|
|
23
|
+
target.write_text(f"HOST = {host!r}\nPORT = {int(port)}\n")
|
|
24
|
+
|
|
25
|
+
|
|
26
|
+
setup(cmdclass={"build_py": BuildPyWithServer})
|
|
@@ -0,0 +1,51 @@
|
|
|
1
|
+
"""HTTP client for the HereYouGo notification service."""
|
|
2
|
+
|
|
3
|
+
import json
|
|
4
|
+
import os
|
|
5
|
+
from typing import Optional
|
|
6
|
+
import urllib.request
|
|
7
|
+
|
|
8
|
+
try:
|
|
9
|
+
from ._server import HOST, PORT # generated at build time by setup.py
|
|
10
|
+
except ImportError: # source checkout or editable install
|
|
11
|
+
HOST = PORT = None
|
|
12
|
+
|
|
13
|
+
|
|
14
|
+
def notify(message: str, *, user_id: Optional[str] = None, timeout: float = 10) -> None:
|
|
15
|
+
"""Send *message* through the HereYouGo server.
|
|
16
|
+
|
|
17
|
+
The server address is baked in at build time from ``HEREYOUGO_VM_IP`` and
|
|
18
|
+
``HEREYOUGO_BOT_PORT``; the same environment variables override it at run
|
|
19
|
+
time. The recipient ID can be passed explicitly or read from
|
|
20
|
+
``HEREYOUGO_USER_ID``. When ``NOTIFY_TOKEN`` is set, it is sent as a
|
|
21
|
+
bearer token.
|
|
22
|
+
|
|
23
|
+
Network and HTTP errors from :mod:`urllib` are intentionally propagated.
|
|
24
|
+
"""
|
|
25
|
+
host = os.environ.get("HEREYOUGO_VM_IP") or HOST
|
|
26
|
+
port = os.environ.get("HEREYOUGO_BOT_PORT") or PORT
|
|
27
|
+
if not host or not port:
|
|
28
|
+
raise RuntimeError(
|
|
29
|
+
"HereYouGo server is not configured: set HEREYOUGO_VM_IP and "
|
|
30
|
+
"HEREYOUGO_BOT_PORT"
|
|
31
|
+
)
|
|
32
|
+
|
|
33
|
+
user_id = user_id or os.environ.get("HEREYOUGO_USER_ID")
|
|
34
|
+
if not user_id:
|
|
35
|
+
raise RuntimeError(
|
|
36
|
+
"HereYouGo recipient is not configured: pass user_id or set "
|
|
37
|
+
"HEREYOUGO_USER_ID"
|
|
38
|
+
)
|
|
39
|
+
|
|
40
|
+
headers = {"Content-Type": "application/json"}
|
|
41
|
+
token = os.environ.get("NOTIFY_TOKEN")
|
|
42
|
+
if token:
|
|
43
|
+
headers["Authorization"] = f"Bearer {token}"
|
|
44
|
+
|
|
45
|
+
request = urllib.request.Request(
|
|
46
|
+
f"http://{host}:{port}/notify",
|
|
47
|
+
data=json.dumps({"user_id": user_id, "text": message}).encode("utf-8"),
|
|
48
|
+
headers=headers,
|
|
49
|
+
method="POST",
|
|
50
|
+
)
|
|
51
|
+
urllib.request.urlopen(request, timeout=timeout).close()
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
Metadata-Version: 2.4
|
|
2
|
+
Name: hereyougo
|
|
3
|
+
Version: 0.0.2
|
|
4
|
+
Summary: Send a Telegram notification from your Python code
|
|
5
|
+
Author: KiShmakov
|
|
6
|
+
License: MIT
|
|
7
|
+
Requires-Python: >=3.9
|
|
8
|
+
Description-Content-Type: text/markdown
|
|
9
|
+
|
|
10
|
+
# HereYouGo
|
|
11
|
+
|
|
12
|
+
A tiny, dependency-free client for sending messages directly from your Python
|
|
13
|
+
code:
|
|
14
|
+
|
|
15
|
+
```python
|
|
16
|
+
import hereyougo
|
|
17
|
+
|
|
18
|
+
hereyougo.notify("Training finished", user_id="my-notification-id")
|
|
19
|
+
```
|
|
20
|
+
|
|
21
|
+
Network and HTTP errors are raised to the caller. The default request timeout is
|
|
22
|
+
10 seconds and can be changed with
|
|
23
|
+
`hereyougo.notify(message, user_id="my-id", timeout=30)`. The recipient may
|
|
24
|
+
also be set with the `HEREYOUGO_USER_ID` environment variable.
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
README.md
|
|
2
|
+
pyproject.toml
|
|
3
|
+
setup.py
|
|
4
|
+
src/hereyougo/__init__.py
|
|
5
|
+
src/hereyougo/_client.py
|
|
6
|
+
src/hereyougo.egg-info/PKG-INFO
|
|
7
|
+
src/hereyougo.egg-info/SOURCES.txt
|
|
8
|
+
src/hereyougo.egg-info/dependency_links.txt
|
|
9
|
+
src/hereyougo.egg-info/top_level.txt
|
|
10
|
+
tests/test_client.py
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
hereyougo
|
|
@@ -0,0 +1,66 @@
|
|
|
1
|
+
import os
|
|
2
|
+
import unittest
|
|
3
|
+
from unittest.mock import patch
|
|
4
|
+
|
|
5
|
+
import hereyougo
|
|
6
|
+
|
|
7
|
+
|
|
8
|
+
class NotifyTests(unittest.TestCase):
|
|
9
|
+
@patch.dict(os.environ, {}, clear=True)
|
|
10
|
+
@patch("hereyougo._client.PORT", 8080)
|
|
11
|
+
@patch("hereyougo._client.HOST", "192.0.2.1")
|
|
12
|
+
@patch("urllib.request.urlopen")
|
|
13
|
+
def test_notify_posts_json_to_baked_server(self, urlopen):
|
|
14
|
+
result = hereyougo.notify("Val accuracy = 0.95", user_id="alice")
|
|
15
|
+
|
|
16
|
+
self.assertIsNone(result)
|
|
17
|
+
request = urlopen.call_args.args[0]
|
|
18
|
+
self.assertEqual(request.full_url, "http://192.0.2.1:8080/notify")
|
|
19
|
+
self.assertEqual(request.method, "POST")
|
|
20
|
+
self.assertEqual(
|
|
21
|
+
request.data,
|
|
22
|
+
b'{"user_id": "alice", "text": "Val accuracy = 0.95"}',
|
|
23
|
+
)
|
|
24
|
+
self.assertEqual(request.get_header("Content-type"), "application/json")
|
|
25
|
+
self.assertIsNone(request.get_header("Authorization"))
|
|
26
|
+
self.assertEqual(urlopen.call_args.kwargs, {"timeout": 10})
|
|
27
|
+
urlopen.return_value.close.assert_called_once_with()
|
|
28
|
+
|
|
29
|
+
@patch.dict(
|
|
30
|
+
os.environ,
|
|
31
|
+
{
|
|
32
|
+
"HEREYOUGO_VM_IP": "example.test",
|
|
33
|
+
"HEREYOUGO_BOT_PORT": "9000",
|
|
34
|
+
"NOTIFY_TOKEN": "secret",
|
|
35
|
+
"HEREYOUGO_USER_ID": "bob",
|
|
36
|
+
},
|
|
37
|
+
clear=True,
|
|
38
|
+
)
|
|
39
|
+
@patch("hereyougo._client.HOST", "192.0.2.1")
|
|
40
|
+
@patch("urllib.request.urlopen")
|
|
41
|
+
def test_environment_overrides_server_and_sends_token(self, urlopen):
|
|
42
|
+
hereyougo.notify("done", timeout=2.5)
|
|
43
|
+
|
|
44
|
+
request = urlopen.call_args.args[0]
|
|
45
|
+
self.assertEqual(request.full_url, "http://example.test:9000/notify")
|
|
46
|
+
self.assertEqual(request.get_header("Authorization"), "Bearer secret")
|
|
47
|
+
self.assertEqual(request.data, b'{"user_id": "bob", "text": "done"}')
|
|
48
|
+
self.assertEqual(urlopen.call_args.kwargs, {"timeout": 2.5})
|
|
49
|
+
|
|
50
|
+
@patch.dict(os.environ, {}, clear=True)
|
|
51
|
+
@patch("hereyougo._client.HOST", None)
|
|
52
|
+
@patch("hereyougo._client.PORT", None)
|
|
53
|
+
def test_notify_reports_missing_configuration(self):
|
|
54
|
+
with self.assertRaisesRegex(RuntimeError, "HEREYOUGO_VM_IP"):
|
|
55
|
+
hereyougo.notify("done", user_id="alice")
|
|
56
|
+
|
|
57
|
+
@patch.dict(os.environ, {}, clear=True)
|
|
58
|
+
@patch("hereyougo._client.PORT", 8080)
|
|
59
|
+
@patch("hereyougo._client.HOST", "192.0.2.1")
|
|
60
|
+
def test_notify_reports_missing_recipient(self):
|
|
61
|
+
with self.assertRaisesRegex(RuntimeError, "user_id"):
|
|
62
|
+
hereyougo.notify("done")
|
|
63
|
+
|
|
64
|
+
|
|
65
|
+
if __name__ == "__main__":
|
|
66
|
+
unittest.main()
|