moat-kv 0.70.23__py3-none-any.whl → 0.71.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.
Files changed (177) hide show
  1. build/lib/docs/source/conf.py +201 -0
  2. build/lib/examples/pathify.py +45 -0
  3. build/lib/moat/kv/__init__.py +19 -0
  4. build/lib/moat/kv/_cfg.yaml +93 -0
  5. build/lib/moat/kv/_main.py +91 -0
  6. build/lib/moat/kv/actor/__init__.py +98 -0
  7. build/lib/moat/kv/actor/deletor.py +139 -0
  8. build/lib/moat/kv/auth/__init__.py +444 -0
  9. build/lib/moat/kv/auth/_test.py +166 -0
  10. build/lib/moat/kv/auth/password.py +234 -0
  11. build/lib/moat/kv/auth/root.py +58 -0
  12. build/lib/moat/kv/backend/__init__.py +67 -0
  13. build/lib/moat/kv/backend/mqtt.py +71 -0
  14. build/lib/moat/kv/client.py +1025 -0
  15. build/lib/moat/kv/code.py +236 -0
  16. build/lib/moat/kv/codec.py +11 -0
  17. build/lib/moat/kv/command/__init__.py +1 -0
  18. build/lib/moat/kv/command/acl.py +180 -0
  19. build/lib/moat/kv/command/auth.py +261 -0
  20. build/lib/moat/kv/command/code.py +293 -0
  21. build/lib/moat/kv/command/codec.py +186 -0
  22. build/lib/moat/kv/command/data.py +265 -0
  23. build/lib/moat/kv/command/dump/__init__.py +143 -0
  24. build/lib/moat/kv/command/error.py +149 -0
  25. build/lib/moat/kv/command/internal.py +248 -0
  26. build/lib/moat/kv/command/job.py +433 -0
  27. build/lib/moat/kv/command/log.py +53 -0
  28. build/lib/moat/kv/command/server.py +114 -0
  29. build/lib/moat/kv/command/type.py +201 -0
  30. build/lib/moat/kv/config.py +46 -0
  31. build/lib/moat/kv/data.py +216 -0
  32. build/lib/moat/kv/errors.py +561 -0
  33. build/lib/moat/kv/exceptions.py +126 -0
  34. build/lib/moat/kv/mock/__init__.py +101 -0
  35. build/lib/moat/kv/mock/mqtt.py +159 -0
  36. build/lib/moat/kv/mock/tracer.py +63 -0
  37. build/lib/moat/kv/model.py +1069 -0
  38. build/lib/moat/kv/obj/__init__.py +646 -0
  39. build/lib/moat/kv/obj/command.py +241 -0
  40. build/lib/moat/kv/runner.py +1347 -0
  41. build/lib/moat/kv/server.py +2809 -0
  42. build/lib/moat/kv/types.py +513 -0
  43. ci/rtd-requirements.txt +4 -0
  44. ci/test-requirements.txt +7 -0
  45. ci/travis.sh +96 -0
  46. debian/.gitignore +7 -0
  47. debian/changelog +1435 -0
  48. debian/control +43 -0
  49. debian/moat-kv/usr/lib/python3/dist-packages/docs/source/conf.py +201 -0
  50. debian/moat-kv/usr/lib/python3/dist-packages/examples/pathify.py +45 -0
  51. debian/moat-kv/usr/lib/python3/dist-packages/moat/kv/__init__.py +19 -0
  52. debian/moat-kv/usr/lib/python3/dist-packages/moat/kv/_cfg.yaml +93 -0
  53. debian/moat-kv/usr/lib/python3/dist-packages/moat/kv/_main.py +91 -0
  54. debian/moat-kv/usr/lib/python3/dist-packages/moat/kv/actor/__init__.py +98 -0
  55. debian/moat-kv/usr/lib/python3/dist-packages/moat/kv/actor/deletor.py +139 -0
  56. debian/moat-kv/usr/lib/python3/dist-packages/moat/kv/auth/__init__.py +444 -0
  57. debian/moat-kv/usr/lib/python3/dist-packages/moat/kv/auth/_test.py +166 -0
  58. debian/moat-kv/usr/lib/python3/dist-packages/moat/kv/auth/password.py +234 -0
  59. debian/moat-kv/usr/lib/python3/dist-packages/moat/kv/auth/root.py +58 -0
  60. debian/moat-kv/usr/lib/python3/dist-packages/moat/kv/backend/__init__.py +67 -0
  61. debian/moat-kv/usr/lib/python3/dist-packages/moat/kv/backend/mqtt.py +71 -0
  62. debian/moat-kv/usr/lib/python3/dist-packages/moat/kv/client.py +1025 -0
  63. debian/moat-kv/usr/lib/python3/dist-packages/moat/kv/code.py +236 -0
  64. debian/moat-kv/usr/lib/python3/dist-packages/moat/kv/codec.py +11 -0
  65. debian/moat-kv/usr/lib/python3/dist-packages/moat/kv/command/__init__.py +1 -0
  66. debian/moat-kv/usr/lib/python3/dist-packages/moat/kv/command/acl.py +180 -0
  67. debian/moat-kv/usr/lib/python3/dist-packages/moat/kv/command/auth.py +261 -0
  68. debian/moat-kv/usr/lib/python3/dist-packages/moat/kv/command/code.py +293 -0
  69. debian/moat-kv/usr/lib/python3/dist-packages/moat/kv/command/codec.py +186 -0
  70. debian/moat-kv/usr/lib/python3/dist-packages/moat/kv/command/data.py +265 -0
  71. debian/moat-kv/usr/lib/python3/dist-packages/moat/kv/command/dump/__init__.py +143 -0
  72. debian/moat-kv/usr/lib/python3/dist-packages/moat/kv/command/error.py +149 -0
  73. debian/moat-kv/usr/lib/python3/dist-packages/moat/kv/command/internal.py +248 -0
  74. debian/moat-kv/usr/lib/python3/dist-packages/moat/kv/command/job.py +433 -0
  75. debian/moat-kv/usr/lib/python3/dist-packages/moat/kv/command/log.py +53 -0
  76. debian/moat-kv/usr/lib/python3/dist-packages/moat/kv/command/server.py +114 -0
  77. debian/moat-kv/usr/lib/python3/dist-packages/moat/kv/command/type.py +201 -0
  78. debian/moat-kv/usr/lib/python3/dist-packages/moat/kv/config.py +46 -0
  79. debian/moat-kv/usr/lib/python3/dist-packages/moat/kv/data.py +216 -0
  80. debian/moat-kv/usr/lib/python3/dist-packages/moat/kv/errors.py +561 -0
  81. debian/moat-kv/usr/lib/python3/dist-packages/moat/kv/exceptions.py +126 -0
  82. debian/moat-kv/usr/lib/python3/dist-packages/moat/kv/mock/__init__.py +101 -0
  83. debian/moat-kv/usr/lib/python3/dist-packages/moat/kv/mock/mqtt.py +159 -0
  84. debian/moat-kv/usr/lib/python3/dist-packages/moat/kv/mock/tracer.py +63 -0
  85. debian/moat-kv/usr/lib/python3/dist-packages/moat/kv/model.py +1069 -0
  86. debian/moat-kv/usr/lib/python3/dist-packages/moat/kv/obj/__init__.py +646 -0
  87. debian/moat-kv/usr/lib/python3/dist-packages/moat/kv/obj/command.py +241 -0
  88. debian/moat-kv/usr/lib/python3/dist-packages/moat/kv/runner.py +1347 -0
  89. debian/moat-kv/usr/lib/python3/dist-packages/moat/kv/server.py +2809 -0
  90. debian/moat-kv/usr/lib/python3/dist-packages/moat/kv/types.py +513 -0
  91. debian/moat-kv.postinst +3 -0
  92. debian/rules +20 -0
  93. debian/source/format +1 -0
  94. debian/watch +4 -0
  95. docs/Makefile +20 -0
  96. docs/make.bat +36 -0
  97. docs/source/TODO.rst +61 -0
  98. docs/source/_static/.gitkeep +0 -0
  99. docs/source/acls.rst +80 -0
  100. docs/source/auth.rst +84 -0
  101. docs/source/client_protocol.rst +456 -0
  102. docs/source/code.rst +341 -0
  103. docs/source/command_line.rst +1187 -0
  104. docs/source/common_protocol.rst +47 -0
  105. docs/source/conf.py +201 -0
  106. docs/source/debugging.rst +70 -0
  107. docs/source/extend.rst +37 -0
  108. docs/source/history.rst +36 -0
  109. docs/source/index.rst +75 -0
  110. docs/source/model.rst +54 -0
  111. docs/source/overview.rst +83 -0
  112. docs/source/related.rst +89 -0
  113. docs/source/server_protocol.rst +450 -0
  114. docs/source/startup.rst +31 -0
  115. docs/source/translator.rst +244 -0
  116. docs/source/tutorial.rst +711 -0
  117. docs/source/v3.rst +168 -0
  118. examples/code/transform.scale.yml +21 -0
  119. examples/code/transform.switch.yml +82 -0
  120. examples/code/transform.timeslot.yml +63 -0
  121. examples/pathify.py +45 -0
  122. moat/kv/_cfg.yaml +2 -6
  123. moat/kv/actor/__init__.py +98 -0
  124. moat/kv/actor/deletor.py +139 -0
  125. moat/kv/auth/__init__.py +444 -0
  126. moat/kv/auth/_test.py +166 -0
  127. moat/kv/auth/password.py +234 -0
  128. moat/kv/auth/root.py +58 -0
  129. moat/kv/backend/__init__.py +67 -0
  130. moat/kv/backend/mqtt.py +71 -0
  131. moat/kv/command/__init__.py +1 -0
  132. moat/kv/command/acl.py +180 -0
  133. moat/kv/command/auth.py +261 -0
  134. moat/kv/command/code.py +293 -0
  135. moat/kv/command/codec.py +186 -0
  136. moat/kv/command/data.py +265 -0
  137. moat/kv/command/dump/__init__.py +143 -0
  138. moat/kv/command/error.py +149 -0
  139. moat/kv/command/internal.py +248 -0
  140. moat/kv/command/job.py +433 -0
  141. moat/kv/command/log.py +53 -0
  142. moat/kv/command/server.py +114 -0
  143. moat/kv/command/type.py +201 -0
  144. moat/kv/mock/__init__.py +101 -0
  145. moat/kv/mock/mqtt.py +159 -0
  146. moat/kv/mock/tracer.py +63 -0
  147. moat/kv/obj/__init__.py +646 -0
  148. moat/kv/obj/command.py +241 -0
  149. {moat_kv-0.70.23.dist-info → moat_kv-0.71.0.dist-info}/METADATA +2 -5
  150. moat_kv-0.71.0.dist-info/RECORD +188 -0
  151. moat_kv-0.71.0.dist-info/top_level.txt +9 -0
  152. scripts/current +15 -0
  153. scripts/env +8 -0
  154. scripts/init +39 -0
  155. scripts/recover +17 -0
  156. scripts/rotate +33 -0
  157. scripts/run +29 -0
  158. scripts/run-all +10 -0
  159. scripts/run-any +10 -0
  160. scripts/run-single +15 -0
  161. scripts/success +4 -0
  162. systemd/moat-kv-recover.service +21 -0
  163. systemd/moat-kv-rotate.service +20 -0
  164. systemd/moat-kv-rotate.timer +10 -0
  165. systemd/moat-kv-run-all.service +26 -0
  166. systemd/moat-kv-run-all@.service +25 -0
  167. systemd/moat-kv-run-any.service +26 -0
  168. systemd/moat-kv-run-any@.service +25 -0
  169. systemd/moat-kv-run-single.service +26 -0
  170. systemd/moat-kv-run-single@.service +25 -0
  171. systemd/moat-kv.service +27 -0
  172. systemd/postinst +7 -0
  173. systemd/sysusers +3 -0
  174. moat_kv-0.70.23.dist-info/RECORD +0 -19
  175. moat_kv-0.70.23.dist-info/top_level.txt +0 -1
  176. {moat_kv-0.70.23.dist-info → moat_kv-0.71.0.dist-info}/WHEEL +0 -0
  177. {moat_kv-0.70.23.dist-info → moat_kv-0.71.0.dist-info}/licenses/LICENSE.txt +0 -0
@@ -0,0 +1,101 @@
1
+ # from asyncclick.testing import CliRunner
2
+ from __future__ import annotations
3
+ import io
4
+ import logging
5
+ import shlex
6
+ import socket
7
+ import sys
8
+ from pathlib import Path
9
+
10
+ import attr
11
+ from asyncscope import main_scope, scope
12
+ from moat.src.test import run # pylint:disable=import-error,no-name-in-module
13
+ from moat.util import ( # pylint:disable=no-name-in-module
14
+ CFG,
15
+ OptCtx,
16
+ attrdict,
17
+ combine_dict,
18
+ ensure_cfg,
19
+ list_ext,
20
+ load_ext,
21
+ wrap_main,
22
+ yload,
23
+ )
24
+
25
+ from moat.kv.client import _scoped_client, client_scope
26
+
27
+ logger = logging.getLogger(__name__)
28
+ try:
29
+ from contextlib import asynccontextmanager
30
+ except ImportError:
31
+ from async_generator import asynccontextmanager
32
+
33
+ ensure_cfg("moat.kv")
34
+
35
+
36
+ @attr.s
37
+ class S:
38
+ tg = attr.ib()
39
+ client_ctx = attr.ib()
40
+ s = attr.ib(factory=list) # servers
41
+ c = attr.ib(factory=list) # clients
42
+ _seq = 1
43
+
44
+ async def ready(self, i=None):
45
+ if i is not None:
46
+ await self.s[i].is_ready
47
+ return self.s[i]
48
+ for s in self.s:
49
+ if s is not None:
50
+ await s.is_ready
51
+ return self.s
52
+
53
+ def __iter__(self):
54
+ return iter(self.s)
55
+
56
+ @asynccontextmanager
57
+ async def client(self, i: int = 0, **kv):
58
+ """Get a (new) client for the i'th server."""
59
+ await self.s[i].is_serving
60
+ self._seq += 1
61
+ for host, port, *_ in self.s[i].ports:
62
+ if host != "::" and host[0] == ":":
63
+ continue
64
+ try:
65
+ cfg = combine_dict(
66
+ dict(conn=dict(host=host, port=port, ssl=self.client_ctx, **kv)),
67
+ CFG["kv"],
68
+ )
69
+
70
+ async def scc(s, **cfg):
71
+ scope.requires(s._scope)
72
+ return await _scoped_client(scope.name, **cfg)
73
+
74
+ async with scope.using_scope():
75
+ c = await scope.service(
76
+ f"moat.kv.client.{i}.{self._seq}",
77
+ scc,
78
+ self.s[i],
79
+ **cfg,
80
+ )
81
+ yield c
82
+ return
83
+ except socket.gaierror:
84
+ pass
85
+ raise RuntimeError("Duh? no connection")
86
+
87
+ async def run(self, *args, do_stdout=True):
88
+ h = p = None
89
+ for s in self.s:
90
+ for h, p, *_ in s.ports:
91
+ if h[0] != ":":
92
+ break
93
+ else:
94
+ continue
95
+ break
96
+ if len(args) == 1:
97
+ args = args[0]
98
+ if isinstance(args, str):
99
+ args = args.split(" ")
100
+ async with scope.using_scope():
101
+ return await run("-VV", "kv", "-h", h, "-p", p, *args, do_stdout=do_stdout)
@@ -0,0 +1,159 @@
1
+ from __future__ import annotations
2
+ import copy
3
+ import logging
4
+ import os
5
+ import time
6
+ from contextlib import AsyncExitStack, asynccontextmanager
7
+ from functools import partial
8
+
9
+ import anyio
10
+ from unittest import mock
11
+ import trio
12
+ from asyncscope import main_scope, scope
13
+ from moat.mqtt.broker import create_broker
14
+ from moat.util import NotGiven, attrdict, combine_dict
15
+
16
+ from moat.kv.mock import S
17
+ from moat.kv.server import Server
18
+
19
+ from . import CFG
20
+
21
+ logger = logging.getLogger(__name__)
22
+
23
+ otm = time.time
24
+
25
+ PORT = 40000 + (os.getpid() + 10) % 10000
26
+
27
+ broker_cfg = {
28
+ "listeners": {"default": {"type": "tcp", "bind": f"127.0.0.1:{PORT}"}},
29
+ "timeout-disconnect-delay": 2,
30
+ "auth": {"allow-anonymous": True, "password-file": None},
31
+ }
32
+
33
+ URI = f"mqtt://127.0.0.1:{PORT}/"
34
+
35
+
36
+ @asynccontextmanager
37
+ async def stdtest(n=1, run=True, ssl=False, tocks=20, **kw):
38
+ C_OUT = CFG.get("_stdout", NotGiven)
39
+ if C_OUT is not NotGiven:
40
+ del CFG["_stdout"]
41
+ TESTCFG = copy.deepcopy(CFG["kv"])
42
+ TESTCFG.server.port = None
43
+ TESTCFG.root = "test"
44
+ if C_OUT is not NotGiven:
45
+ CFG["_stdout"] = C_OUT
46
+ TESTCFG["_stdout"] = C_OUT
47
+
48
+ if ssl:
49
+ import ssl
50
+
51
+ import trustme
52
+
53
+ ca = trustme.CA()
54
+ cert = ca.issue_server_cert("127.0.0.1")
55
+ server_ctx = ssl.create_default_context(purpose=ssl.Purpose.CLIENT_AUTH)
56
+ client_ctx = ssl.create_default_context(purpose=ssl.Purpose.SERVER_AUTH)
57
+ ca.configure_trust(client_ctx)
58
+ cert.configure_cert(server_ctx)
59
+ else:
60
+ server_ctx = client_ctx = False
61
+
62
+ clock = trio.lowlevel.current_clock()
63
+ try:
64
+ clock.autojump_threshold = 0.02 # networking
65
+ except Exception:
66
+ pass # test doesn't have autojump_clock fixture
67
+
68
+ async def mock_get_host_port(st, host):
69
+ i = int(host[host.rindex("_") + 1 :])
70
+ s = st.s[i]
71
+ await s.is_serving
72
+ for host, port, *_ in s.ports:
73
+ if host == "::" or host[0] != ":":
74
+ return host, port
75
+
76
+ def tm():
77
+ try:
78
+ return trio.current_time()
79
+ except RuntimeError:
80
+ return otm()
81
+
82
+ async def mock_set_tock(self, old):
83
+ assert self._tock < tocks, "Test didn't terminate. Limit:" + str(tocks)
84
+ await old()
85
+
86
+ done = False
87
+ async with main_scope("moat.kv.test.mqtt") as scp:
88
+ tg = scp._tg
89
+ st = S(tg, client_ctx)
90
+ async with AsyncExitStack() as ex:
91
+ st.ex = ex # pylint: disable=attribute-defined-outside-init
92
+ ex.enter_context(mock.patch("time.time", new=tm))
93
+ ex.enter_context(mock.patch("time.monotonic", new=tm))
94
+ logging._startTime = tm()
95
+
96
+ async def run_broker(cfg):
97
+ async with create_broker(config=cfg) as srv:
98
+ # NB: some services use "async with await …"
99
+ scope.register(srv)
100
+ await scope.no_more_dependents()
101
+
102
+ async def with_broker(s, *a, **k):
103
+ await scope.service("moat.mqtt.broker", run_broker, broker_cfg)
104
+ s._scope = scope.get()
105
+ return await s._scoped_serve(*a, **k)
106
+
107
+ args_def = kw.get("args", attrdict())
108
+ for i in range(n):
109
+ name = "test_" + str(i)
110
+ args = kw.get(name, args_def)
111
+ args = combine_dict(
112
+ args,
113
+ args_def,
114
+ {
115
+ "cfg": {
116
+ "conn": {"ssl": client_ctx},
117
+ "server": {
118
+ "bind_default": {
119
+ "host": "127.0.0.1",
120
+ "port": i + PORT + 1,
121
+ "ssl": server_ctx,
122
+ },
123
+ "backend": "mqtt",
124
+ "mqtt": {"uri": URI},
125
+ },
126
+ },
127
+ },
128
+ {"cfg": TESTCFG},
129
+ )
130
+ args_def.pop("init", None)
131
+ s = Server(name, **args)
132
+ ex.enter_context(
133
+ mock.patch.object(s, "_set_tock", new=partial(mock_set_tock, s, s._set_tock)),
134
+ )
135
+ ex.enter_context(
136
+ mock.patch.object(s, "_get_host_port", new=partial(mock_get_host_port, st)),
137
+ )
138
+ st.s.append(s)
139
+
140
+ evts = []
141
+ for i in range(n):
142
+ if kw.get(f"run_{i}", run):
143
+ evt = anyio.Event()
144
+ await scp.spawn_service(with_broker, st.s[i], ready_evt=evt)
145
+ evts.append(evt)
146
+ else:
147
+ setattr(st, f"run_{i}", partial(scp.spawn_service, with_broker, st.s[i]))
148
+
149
+ for e in evts:
150
+ await e.wait()
151
+ try:
152
+ done = True
153
+ yield st
154
+ finally:
155
+ with anyio.fail_after(2, shield=True):
156
+ logger.info("Runtime: %s", clock.current_time())
157
+ tg.cancel_scope.cancel()
158
+ if not done:
159
+ yield None
@@ -0,0 +1,63 @@
1
+ from __future__ import annotations
2
+ import traceback
3
+
4
+ import trio
5
+ from outcome import Error
6
+
7
+ import moat
8
+
9
+ moat.kill = False
10
+
11
+ import logging
12
+
13
+ logger = logging.getLogger("TRACE")
14
+ e = logger.error
15
+
16
+
17
+ class Tracer(trio.abc.Instrument):
18
+ def __init__(self):
19
+ super().__init__()
20
+ self.etasks = set()
21
+
22
+ def _print_with_task(self, msg, task, err=None):
23
+ # repr(task) is perhaps more useful than task.name in general,
24
+ # but in context of a tutorial the extra noise is unhelpful.
25
+ if err is not None:
26
+ e("%s: %s %s", msg, task.name, repr(err))
27
+ traceback.print_exception(type(err), err, err.__traceback__)
28
+ else:
29
+ e("%s: %s", msg, task.name)
30
+
31
+ def nursery_end(self, task, exception):
32
+ if isinstance(exception, Exception):
33
+ self.etasks.add(task)
34
+ self._print_with_task("*** task excepted", task, exception)
35
+
36
+ def before_task_step(self, task):
37
+ if isinstance(task._next_send, Error) and isinstance(task._next_send.error, Exception):
38
+ self._print_with_task("*** step resume ERROR", task, task._next_send.error)
39
+ self.etasks.add(task)
40
+ elif moat.kill: # pylint: disable=c-extension-no-member # OH COME ON
41
+ self._print_with_task("*** step resume", task)
42
+
43
+ def task_scheduled(self, task):
44
+ e("SCHED %r", task)
45
+
46
+ def task_exited(self, task):
47
+ self._print_with_task("*** task exited", task)
48
+ self.etasks.discard(task)
49
+
50
+ def before_io_wait(self, timeout):
51
+ if timeout > 10000 and self.etasks:
52
+ e("\n\n\n\n\n\n\n\n\n\n")
53
+ e("*** ERROR: lock-out, killing off error tasks")
54
+ e("\n\n\n\n")
55
+ for t in self.etasks:
56
+ if t._next_send_fn is None:
57
+ self._print_with_task("!!! Killing", t)
58
+ t._runner.reschedule(t, Error(RuntimeError("*** Locked ***")))
59
+ else:
60
+ self._print_with_task("??? already scheduled", t)
61
+
62
+
63
+ # trio.run(main_, instruments=[Tracer()])