notices 0.0.2__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.
notices/__init__.py ADDED
@@ -0,0 +1,94 @@
1
+ """notices: one small interface for notifications and reminders over many channels.
2
+
3
+ >>> from notices import Notice, Notifier
4
+ >>> from notices.channels.recording import RecordingChannel
5
+ >>> nf = Notifier({"default": ["rec"]}, ledger={}, channels={"rec": RecordingChannel("rec")})
6
+ >>> [r.status for r in nf.send(Notice.reminder("call-bank", "Call the bank"))]
7
+ ['sent']
8
+ >>> [r.status for r in nf.send(Notice.reminder("call-bank", "Call the bank"))] # same: no-op
9
+ ['sent']
10
+ >>> [r.status for r in nf.retract("call-bank")]
11
+ ['retracted']
12
+
13
+ A :class:`Notice` is what to tell someone and when; a :class:`Notifier` routes it to
14
+ channels (plugins: ntfy, Apple Reminders and Calendar, macOS, Slack, Google Calendar),
15
+ keeps a ledger of the copies so a re-send is idempotent and a retract reaches every copy,
16
+ and reads back what the person did (Done, Snooze) as :class:`Event` objects. The module
17
+ functions (:func:`send`, :func:`retract`, :func:`poll`, :func:`tick`) use
18
+ :func:`default_notifier`, configured from ``~/.local/share/notices/config.toml``.
19
+ """
20
+
21
+ from __future__ import annotations
22
+
23
+ from functools import lru_cache
24
+
25
+ from notices.base import Notifier, UnknownChannel
26
+ from notices.channels import (
27
+ Channel,
28
+ Unsupported,
29
+ make_channel,
30
+ plugins,
31
+ register_channel,
32
+ )
33
+ from notices.model import (
34
+ ACTION_KINDS,
35
+ EVENT_TYPES,
36
+ FEATURES,
37
+ URGENCIES,
38
+ Action,
39
+ Capabilities,
40
+ Event,
41
+ Notice,
42
+ Receipt,
43
+ )
44
+
45
+
46
+ @lru_cache(maxsize=1)
47
+ def default_notifier() -> Notifier:
48
+ """The notifier the module functions use (settings file, JSON ledger, inbox if set)."""
49
+ return Notifier()
50
+
51
+
52
+ def send(notice: Notice, **kwargs):
53
+ """Send ``notice`` with :func:`default_notifier` (see :meth:`Notifier.send`)."""
54
+ return default_notifier().send(notice, **kwargs)
55
+
56
+
57
+ def retract(notice_id: str, **kwargs):
58
+ """Withdraw every copy of a notice (see :meth:`Notifier.retract`)."""
59
+ return default_notifier().retract(notice_id, **kwargs)
60
+
61
+
62
+ def poll(**kwargs):
63
+ """New events: Done, Snooze, ... (see :meth:`Notifier.poll`)."""
64
+ return default_notifier().poll(**kwargs)
65
+
66
+
67
+ def tick():
68
+ """Send what is due (see :meth:`Notifier.tick`)."""
69
+ return default_notifier().tick()
70
+
71
+
72
+ __all__ = [
73
+ "ACTION_KINDS",
74
+ "Action",
75
+ "Capabilities",
76
+ "Channel",
77
+ "EVENT_TYPES",
78
+ "Event",
79
+ "FEATURES",
80
+ "Notice",
81
+ "Notifier",
82
+ "Receipt",
83
+ "URGENCIES",
84
+ "UnknownChannel",
85
+ "Unsupported",
86
+ "default_notifier",
87
+ "make_channel",
88
+ "plugins",
89
+ "poll",
90
+ "register_channel",
91
+ "retract",
92
+ "send",
93
+ "tick",
94
+ ]
notices/__main__.py ADDED
@@ -0,0 +1,45 @@
1
+ """The command line: ``notices <verb>``, generated from :mod:`notices.tools` by ``cw``.
2
+
3
+ Results print as JSON, since the main callers are agents. An expected failure (an unknown
4
+ channel, a bad time, a missing secret) is one line on stderr and exit status 1.
5
+ """
6
+
7
+ from __future__ import annotations
8
+
9
+ import functools
10
+ import json
11
+ import sys
12
+
13
+ from notices.tools import _dispatch_funcs
14
+
15
+
16
+ def _cli_func(func):
17
+ """Print the result as JSON; an expected failure is one line on stderr, exit 1."""
18
+
19
+ @functools.wraps(func)
20
+ def wrapper(*args, **kwargs):
21
+ try:
22
+ result = func(*args, **kwargs)
23
+ except (LookupError, ValueError) as e:
24
+ print(f"notices: {e}", file=sys.stderr)
25
+ raise SystemExit(1) from None
26
+ print(json.dumps(result, indent=2, default=str))
27
+
28
+ return wrapper
29
+
30
+
31
+ def main(argv=None):
32
+ import cw
33
+
34
+ # cw returns the exit status rather than exiting, so raise it (else errors exit 0).
35
+ raise SystemExit(
36
+ cw.dispatch(
37
+ [_cli_func(f) for f in _dispatch_funcs],
38
+ argv,
39
+ egress=lambda result, **kwargs: None,
40
+ )
41
+ )
42
+
43
+
44
+ if __name__ == "__main__":
45
+ main()