pernox 0.1.0__py3-none-any.whl

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.
pernox/__init__.py ADDED
@@ -0,0 +1,96 @@
1
+ """Send yourself a notification from a script running in nox.
2
+
3
+ from pernox import notify
4
+
5
+ notify("3 new flats under £1,500", title="Flat search")
6
+
7
+ Inside nox, each run is given a token (NOX_TOKEN) and nox's address (NOX_API_URL), so
8
+ there's nothing to set up. Anywhere else, notify prints the message instead of sending it,
9
+ so the same script runs on your own machine unchanged.
10
+ """
11
+
12
+ from __future__ import annotations
13
+
14
+ import json
15
+ import os
16
+ import sys
17
+ import urllib.error
18
+ import urllib.request
19
+ from importlib.metadata import PackageNotFoundError, version
20
+ from typing import Optional
21
+
22
+ __all__ = ["notify", "NotifyError"]
23
+ try:
24
+ __version__ = version("pernox")
25
+ except PackageNotFoundError: # run from the source tree without installing
26
+ __version__ = "0.0.0"
27
+
28
+ _DEFAULT_API_URL = "https://pernox.dev"
29
+
30
+
31
+ class NotifyError(Exception):
32
+ """nox couldn't send the notification. status is the HTTP status, if nox answered."""
33
+
34
+ def __init__(self, message: str, status: Optional[int] = None) -> None:
35
+ super().__init__(message)
36
+ self.status = status
37
+
38
+
39
+ def notify(
40
+ message: str,
41
+ title: Optional[str] = None,
42
+ *,
43
+ url: Optional[str] = None,
44
+ timeout: float = 60,
45
+ ) -> bool:
46
+ """Email the script's owner, at their nox account's address.
47
+
48
+ message: the text, up to 1,024 characters.
49
+ title: the subject line, up to 250 characters. Without one, it's the script's name.
50
+ url: a link to include.
51
+
52
+ Returns True once sent, or False if it was printed instead because the script isn't
53
+ running in nox. Raises NotifyError if nox couldn't send it.
54
+ """
55
+ token = os.environ.get("NOX_TOKEN")
56
+ if not token:
57
+ heading = f"{title}: " if title else ""
58
+ print(f"[pernox] {heading}{message} (not sent: runs outside nox)", file=sys.stderr)
59
+ return False
60
+
61
+ body = {"channel": "email", "message": message}
62
+ if title is not None:
63
+ body["title"] = title
64
+ if url is not None:
65
+ body["url"] = url
66
+ api = os.environ.get("NOX_API_URL", _DEFAULT_API_URL).rstrip("/")
67
+ request = urllib.request.Request(
68
+ api + "/api/v1/notify",
69
+ data=json.dumps(body).encode(),
70
+ method="POST",
71
+ headers={
72
+ "Authorization": f"Bearer {token}",
73
+ "Content-Type": "application/json",
74
+ "User-Agent": f"pernox/{__version__}",
75
+ },
76
+ )
77
+ try:
78
+ with urllib.request.urlopen(request, timeout=timeout) as response:
79
+ response.read()
80
+ except urllib.error.HTTPError as e:
81
+ raise NotifyError(_error_message(e), e.code) from None
82
+ except (urllib.error.URLError, TimeoutError) as e:
83
+ reason = getattr(e, "reason", e)
84
+ raise NotifyError(f"Couldn't reach nox at {api}: {reason}") from None
85
+ return True
86
+
87
+
88
+ def _error_message(e: urllib.error.HTTPError) -> str:
89
+ """nox explains what went wrong in the body's "error"; fall back to the status."""
90
+ try:
91
+ message = json.loads(e.read())["error"]
92
+ if isinstance(message, str) and message:
93
+ return message
94
+ except (ValueError, KeyError, TypeError, OSError):
95
+ pass
96
+ return f"nox answered {e.code} {e.reason}"
pernox/py.typed ADDED
File without changes
@@ -0,0 +1,53 @@
1
+ Metadata-Version: 2.5
2
+ Name: pernox
3
+ Version: 0.1.0
4
+ Summary: Email yourself from a script running in nox.
5
+ Project-URL: Homepage, https://pernox.dev
6
+ Project-URL: Documentation, https://pernox.dev/docs#notify
7
+ License-Expression: MIT
8
+ License-File: LICENSE
9
+ Keywords: cron,email,notifications,nox,scheduler
10
+ Classifier: Operating System :: OS Independent
11
+ Classifier: Programming Language :: Python :: 3
12
+ Classifier: Topic :: Communications
13
+ Classifier: Typing :: Typed
14
+ Requires-Python: >=3.10
15
+ Description-Content-Type: text/markdown
16
+
17
+ # pernox
18
+
19
+ Email yourself from a script running in [nox](https://pernox.dev).
20
+
21
+ ```
22
+ pip install pernox
23
+ ```
24
+
25
+ ```python
26
+ from pernox import notify
27
+
28
+ notify("3 new flats under £1,500", title="Flat search")
29
+ ```
30
+
31
+ In nox, add `pernox` to the script's requirements. There's nothing else to set up: each run
32
+ is given its own token, which stops working when the run ends, and the email goes to your
33
+ nox account's address.
34
+
35
+ Notifications come with nox Pro, and go only to a verified address: verify yours in
36
+ [Settings](https://pernox.dev/settings#profile).
37
+
38
+ Run the script anywhere else and `notify` prints the message instead of sending it, so the
39
+ same script works on your own machine.
40
+
41
+ `notify` returns `True` once the email is sent, and `False` when it was only printed. If nox
42
+ can't send it, it raises `pernox.NotifyError`, which fails the run like any other exception.
43
+ Catch it if a notification failing shouldn't.
44
+
45
+ ## Arguments
46
+
47
+ | | |
48
+ |---|---|
49
+ | `message` | The text, up to 1,024 characters. |
50
+ | `title` | The subject line, up to 250 characters. Without one, it's the script's name. |
51
+ | `url` | A link to include. |
52
+
53
+ No dependencies beyond the standard library. Python 3.10 or later. MIT licensed.
@@ -0,0 +1,6 @@
1
+ pernox/__init__.py,sha256=ab6XSWPpbZ_LniPcgH4gdYggTe3KbvvkfpFEXTbeyDg,3164
2
+ pernox/py.typed,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
3
+ pernox-0.1.0.dist-info/METADATA,sha256=KZhlouRLi6CeN4B_M7Mt1cZKBEKqC6OAtXYBGRlZo5w,1717
4
+ pernox-0.1.0.dist-info/WHEEL,sha256=2iZrJkDIkeDh0k9SikKMqFuRP-ua6mb4Ir1AsER4BEw,87
5
+ pernox-0.1.0.dist-info/licenses/LICENSE,sha256=eCpx3f9Nysno3SsZ6BrvWggy72c7hDCfuIDLOtBdwu8,1080
6
+ pernox-0.1.0.dist-info/RECORD,,
@@ -0,0 +1,4 @@
1
+ Wheel-Version: 1.0
2
+ Generator: hatchling 1.33.0
3
+ Root-Is-Purelib: true
4
+ Tag: py3-none-any
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2026 Sebastien Williams-Wynn
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.