Pyrophone 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.
- pyrophone/__init__.py +62 -0
- pyrophone/_channels.py +125 -0
- pyrophone/_compile.py +118 -0
- pyrophone/_control.py +229 -0
- pyrophone/_driver.py +91 -0
- pyrophone/_entry.py +53 -0
- pyrophone/_exceptions.py +42 -0
- pyrophone/_graph.py +283 -0
- pyrophone/_graph_display.py +117 -0
- pyrophone/_inspection.py +50 -0
- pyrophone/_node.py +216 -0
- pyrophone/_ports.py +105 -0
- pyrophone/_properties.py +64 -0
- pyrophone/_runner.py +83 -0
- pyrophone/_runtime.py +140 -0
- pyrophone/_schema.py +197 -0
- pyrophone/_shell.py +130 -0
- pyrophone/_terminal.py +73 -0
- pyrophone/_trace.py +93 -0
- pyrophone/cabe.py +222 -0
- pyrophone/jq.py +65 -0
- pyrophone/mqtt.py +122 -0
- pyrophone/nodes.py +110 -0
- pyrophone/py.typed +0 -0
- pyrophone/script.py +74 -0
- pyrophone-0.1.0.dist-info/METADATA +38 -0
- pyrophone-0.1.0.dist-info/RECORD +29 -0
- pyrophone-0.1.0.dist-info/WHEEL +4 -0
- pyrophone-0.1.0.dist-info/licenses/doc/LICENSE +202 -0
pyrophone/cabe.py
ADDED
|
@@ -0,0 +1,222 @@
|
|
|
1
|
+
"""Byte-stream CABE transforms backed by the optional ``cabe`` extra.
|
|
2
|
+
|
|
3
|
+
Encryption nodes each own one PyCABE client and its lease cache. Service settings
|
|
4
|
+
are captured at startup; encapsulation attributes are read for each message.
|
|
5
|
+
Printer inspects envelope headers locally, without a client or decryption.
|
|
6
|
+
Importing this module or declaring graphs does not import PyCABE or perform I/O.
|
|
7
|
+
"""
|
|
8
|
+
|
|
9
|
+
import json
|
|
10
|
+
import os
|
|
11
|
+
from collections.abc import AsyncIterator, Mapping
|
|
12
|
+
from contextlib import AbstractAsyncContextManager, asynccontextmanager
|
|
13
|
+
from dataclasses import dataclass
|
|
14
|
+
from importlib import import_module
|
|
15
|
+
from itertools import chain
|
|
16
|
+
from typing import Protocol, cast
|
|
17
|
+
|
|
18
|
+
from ._exceptions import ConfigurationError
|
|
19
|
+
from ._node import node_type
|
|
20
|
+
from ._properties import Property
|
|
21
|
+
from .nodes import Transform
|
|
22
|
+
|
|
23
|
+
__all__ = ["Decapsulator", "Encapsulator", "Printer"]
|
|
24
|
+
|
|
25
|
+
|
|
26
|
+
class _Message(Protocol):
|
|
27
|
+
@property
|
|
28
|
+
def payload(self) -> bytes: ...
|
|
29
|
+
|
|
30
|
+
|
|
31
|
+
class _Headers(Protocol):
|
|
32
|
+
@property
|
|
33
|
+
def protected(self) -> Mapping[int | str, object]: ...
|
|
34
|
+
|
|
35
|
+
@property
|
|
36
|
+
def unprotected(self) -> Mapping[int | str, object]: ...
|
|
37
|
+
|
|
38
|
+
|
|
39
|
+
class _Envelope(Protocol):
|
|
40
|
+
@property
|
|
41
|
+
def lease_ref(self) -> bytes: ...
|
|
42
|
+
|
|
43
|
+
@property
|
|
44
|
+
def headers(self) -> _Headers: ...
|
|
45
|
+
|
|
46
|
+
@property
|
|
47
|
+
def attributes(self) -> Mapping[str, object]: ...
|
|
48
|
+
|
|
49
|
+
def encode(self) -> bytes: ...
|
|
50
|
+
|
|
51
|
+
|
|
52
|
+
class _EnvelopeFactory(Protocol):
|
|
53
|
+
def decode(self, encoded: bytes, *, wire_limit: int | None = None) -> _Envelope: ...
|
|
54
|
+
|
|
55
|
+
|
|
56
|
+
class _Client(Protocol):
|
|
57
|
+
async def encapsulate(self, message: _Message) -> _Envelope: ...
|
|
58
|
+
|
|
59
|
+
async def decapsulate(self, envelope: _Envelope) -> _Message: ...
|
|
60
|
+
|
|
61
|
+
|
|
62
|
+
class _API(Protocol):
|
|
63
|
+
def Client(
|
|
64
|
+
self, base_url: str, **options: object
|
|
65
|
+
) -> AbstractAsyncContextManager[_Client]: ...
|
|
66
|
+
|
|
67
|
+
def Message(self, payload: bytes, attributes: dict[str, object]) -> _Message: ...
|
|
68
|
+
|
|
69
|
+
@property
|
|
70
|
+
def Envelope(self) -> _EnvelopeFactory: ...
|
|
71
|
+
|
|
72
|
+
|
|
73
|
+
@dataclass
|
|
74
|
+
class _Codec:
|
|
75
|
+
api: _API
|
|
76
|
+
client: _Client
|
|
77
|
+
wire_limit: int | None = None
|
|
78
|
+
|
|
79
|
+
async def encapsulate(self, payload: bytes, attrs: dict[str, object]) -> bytes:
|
|
80
|
+
message = self.api.Message(payload, attrs)
|
|
81
|
+
envelope = await self.client.encapsulate(message)
|
|
82
|
+
return envelope.encode()
|
|
83
|
+
|
|
84
|
+
async def decapsulate(self, payload: bytes) -> bytes:
|
|
85
|
+
envelope = self.api.Envelope.decode(payload, wire_limit=self.wire_limit)
|
|
86
|
+
message = await self.client.decapsulate(envelope)
|
|
87
|
+
return message.payload
|
|
88
|
+
|
|
89
|
+
|
|
90
|
+
def _load_api() -> _API:
|
|
91
|
+
try:
|
|
92
|
+
return cast(_API, import_module("cabe"))
|
|
93
|
+
except ModuleNotFoundError as error:
|
|
94
|
+
if error.name != "cabe":
|
|
95
|
+
raise
|
|
96
|
+
raise ImportError(
|
|
97
|
+
"CABE nodes require optional dependencies: pip install 'Pyrophone[cabe]'"
|
|
98
|
+
) from error
|
|
99
|
+
|
|
100
|
+
|
|
101
|
+
@asynccontextmanager
|
|
102
|
+
async def _service(service: dict[str, object]) -> AsyncIterator[_Codec]:
|
|
103
|
+
api = _load_api()
|
|
104
|
+
tls_module = import_module("cabe.tls")
|
|
105
|
+
options = dict(service)
|
|
106
|
+
url = options.pop("url", None)
|
|
107
|
+
tls = options.pop("tls", {})
|
|
108
|
+
if not isinstance(url, str) or not url:
|
|
109
|
+
raise ConfigurationError("service.url must be a nonempty CABE service URL")
|
|
110
|
+
if not isinstance(tls, dict):
|
|
111
|
+
raise ConfigurationError("service.tls must be a dictionary")
|
|
112
|
+
tls = dict(tls)
|
|
113
|
+
for field, variable in (
|
|
114
|
+
("cert_file", "CABE_CERTIFICATE_FILE"),
|
|
115
|
+
("key_file", "CABE_KEY_FILE"),
|
|
116
|
+
("ca_file", "CABE_CA_FILE"),
|
|
117
|
+
):
|
|
118
|
+
if path := os.getenv(variable):
|
|
119
|
+
tls.setdefault(field, path)
|
|
120
|
+
if not tls.get("cert_file") or not tls.get("key_file"):
|
|
121
|
+
raise ConfigurationError(
|
|
122
|
+
"service.tls requires cert_file and key_file, or set "
|
|
123
|
+
"CABE_CERTIFICATE_FILE and CABE_KEY_FILE"
|
|
124
|
+
)
|
|
125
|
+
async with api.Client(url, tls=tls_module.FileTLS(**tls), **options) as client:
|
|
126
|
+
yield _Codec(api, client, cast(int | None, options.get("wire_limit")))
|
|
127
|
+
|
|
128
|
+
|
|
129
|
+
class _CABETransform(Transform[bytes, bytes]):
|
|
130
|
+
service = Property[dict[str, object]](
|
|
131
|
+
title="Key service",
|
|
132
|
+
description=(
|
|
133
|
+
"CABE service URL and optional PyCABE client settings. Missing TLS "
|
|
134
|
+
"paths default from CABE_CERTIFICATE_FILE, CABE_KEY_FILE and "
|
|
135
|
+
"CABE_CA_FILE when the node starts."
|
|
136
|
+
),
|
|
137
|
+
)
|
|
138
|
+
_codec: _Codec
|
|
139
|
+
|
|
140
|
+
async def run(self) -> None:
|
|
141
|
+
async with _service(self.service) as self._codec:
|
|
142
|
+
await super().run()
|
|
143
|
+
|
|
144
|
+
|
|
145
|
+
@node_type
|
|
146
|
+
class Encapsulator(_CABETransform):
|
|
147
|
+
"""Encrypt each plaintext byte payload into one encoded CABE envelope."""
|
|
148
|
+
|
|
149
|
+
attrs = Property[dict[str, object]](
|
|
150
|
+
default={},
|
|
151
|
+
title="Attributes",
|
|
152
|
+
description="CABE attributes for each outgoing message, such as sensitivity.",
|
|
153
|
+
)
|
|
154
|
+
|
|
155
|
+
async def transform(self, value: bytes) -> bytes:
|
|
156
|
+
return await self._codec.encapsulate(value, self.attrs)
|
|
157
|
+
|
|
158
|
+
|
|
159
|
+
@node_type
|
|
160
|
+
class Decapsulator(_CABETransform):
|
|
161
|
+
"""Authenticate and decrypt each CABE envelope, emitting only its payload.
|
|
162
|
+
|
|
163
|
+
Envelope attributes and content type are not forwarded on the bytes port.
|
|
164
|
+
Decode, authorization, and authentication errors fail the node; unauthenticated
|
|
165
|
+
plaintext is never emitted.
|
|
166
|
+
"""
|
|
167
|
+
|
|
168
|
+
async def transform(self, value: bytes) -> bytes:
|
|
169
|
+
return await self._codec.decapsulate(value)
|
|
170
|
+
|
|
171
|
+
|
|
172
|
+
_HEADER_NAMES = {
|
|
173
|
+
2: "critical",
|
|
174
|
+
3: "content_type",
|
|
175
|
+
4: "key_id",
|
|
176
|
+
5: "iv",
|
|
177
|
+
7: "counter_signature",
|
|
178
|
+
}
|
|
179
|
+
|
|
180
|
+
|
|
181
|
+
def _format_label(label: int | str) -> str:
|
|
182
|
+
if isinstance(label, int):
|
|
183
|
+
return _HEADER_NAMES.get(label, str(label))
|
|
184
|
+
return label if label.isascii() and label.isidentifier() else json.dumps(label)
|
|
185
|
+
|
|
186
|
+
|
|
187
|
+
def _format_value(value: object) -> str:
|
|
188
|
+
if isinstance(value, str):
|
|
189
|
+
return json.dumps(value)
|
|
190
|
+
if isinstance(value, bytes):
|
|
191
|
+
return value.hex()
|
|
192
|
+
return ascii(value)
|
|
193
|
+
|
|
194
|
+
|
|
195
|
+
def _format_envelope(envelope: _Envelope) -> str:
|
|
196
|
+
attributes = ", ".join(
|
|
197
|
+
f"{_format_label(name)}={_format_value(value)}"
|
|
198
|
+
for name, value in sorted(envelope.attributes.items())
|
|
199
|
+
)
|
|
200
|
+
reference = envelope.lease_ref.hex()
|
|
201
|
+
fields = [f"Attributes=({attributes})", f"LeaseRef={reference}"]
|
|
202
|
+
headers = envelope.headers
|
|
203
|
+
for label, value in chain(headers.protected.items(), headers.unprotected.items()):
|
|
204
|
+
if label not in (1, 6, "CABE_AttributeSet", "CABE_LeaseRef"):
|
|
205
|
+
fields.append(f"{_format_label(label)}={_format_value(value)}")
|
|
206
|
+
return "CABE Envelope: " + ", ".join(fields)
|
|
207
|
+
|
|
208
|
+
|
|
209
|
+
@node_type
|
|
210
|
+
class Printer(Transform[bytes, bytes]):
|
|
211
|
+
"""Print CABE headers on one stdout line and forward the original bytes.
|
|
212
|
+
|
|
213
|
+
Inspection decodes and validates the envelope format without contacting a
|
|
214
|
+
key service. Headers remain unverified; this node does not authenticate or
|
|
215
|
+
decrypt messages. Attributes appear first; lease references use full hex.
|
|
216
|
+
Algorithm and partial IV are omitted; other headers follow.
|
|
217
|
+
"""
|
|
218
|
+
|
|
219
|
+
async def transform(self, value: bytes) -> bytes:
|
|
220
|
+
envelope = _load_api().Envelope.decode(value)
|
|
221
|
+
print(_format_envelope(envelope), flush=True)
|
|
222
|
+
return value
|
pyrophone/jq.py
ADDED
|
@@ -0,0 +1,65 @@
|
|
|
1
|
+
"""JSON byte-stream filtering with the optional PyPI ``jq`` binding."""
|
|
2
|
+
|
|
3
|
+
import json
|
|
4
|
+
from collections.abc import Iterable
|
|
5
|
+
from importlib import import_module
|
|
6
|
+
from typing import Protocol, cast
|
|
7
|
+
|
|
8
|
+
from ._node import Node, node_type
|
|
9
|
+
from ._ports import Input, Output
|
|
10
|
+
from ._properties import Property
|
|
11
|
+
from .nodes import JSONValue
|
|
12
|
+
|
|
13
|
+
__all__ = ["JQ"]
|
|
14
|
+
|
|
15
|
+
|
|
16
|
+
class _Program(Protocol):
|
|
17
|
+
def input_value(self, value: JSONValue) -> Iterable[JSONValue]: ...
|
|
18
|
+
|
|
19
|
+
|
|
20
|
+
def _compile(program: str) -> _Program:
|
|
21
|
+
try:
|
|
22
|
+
jq = import_module("jq")
|
|
23
|
+
except ModuleNotFoundError as error:
|
|
24
|
+
if error.name != "jq":
|
|
25
|
+
raise
|
|
26
|
+
raise ImportError(
|
|
27
|
+
"JQ requires optional dependencies: pip install 'Pyrophone[jq]'"
|
|
28
|
+
) from error
|
|
29
|
+
return cast(_Program, jq.compile(program))
|
|
30
|
+
|
|
31
|
+
|
|
32
|
+
@node_type
|
|
33
|
+
class JQ(Node):
|
|
34
|
+
"""Apply a jq program to each JSON payload, emitting zero or more results.
|
|
35
|
+
|
|
36
|
+
Each input is one complete JSON value encoded as bytes. Results are emitted
|
|
37
|
+
individually as compact UTF-8 JSON, preserving jq order and backpressure.
|
|
38
|
+
Unlike the one-to-one Transform base, jq can filter or expand messages.
|
|
39
|
+
Compilation and evaluation use synchronous bindings on the runtime loop.
|
|
40
|
+
"""
|
|
41
|
+
|
|
42
|
+
expr = Property[str](
|
|
43
|
+
title="jq expression",
|
|
44
|
+
description=(
|
|
45
|
+
"jq expression applied to each JSON message, for example .count += 1. "
|
|
46
|
+
"Live changes take effect on the next input message."
|
|
47
|
+
),
|
|
48
|
+
)
|
|
49
|
+
in_port = Input[bytes](description="One complete JSON value per payload.")
|
|
50
|
+
out_port = Output[bytes](description="One JSON-encoded payload per jq result.")
|
|
51
|
+
|
|
52
|
+
async def run(self) -> None:
|
|
53
|
+
source = self.expr
|
|
54
|
+
compiled = _compile(source)
|
|
55
|
+
async for payload in self.in_port:
|
|
56
|
+
current = self.expr
|
|
57
|
+
if current != source:
|
|
58
|
+
compiled = _compile(current)
|
|
59
|
+
source = current
|
|
60
|
+
value: JSONValue = json.loads(payload)
|
|
61
|
+
for result in compiled.input_value(value):
|
|
62
|
+
encoded = json.dumps(
|
|
63
|
+
result, separators=(",", ":"), ensure_ascii=False, allow_nan=False
|
|
64
|
+
).encode("utf-8")
|
|
65
|
+
await self.out_port.send(encoded)
|
pyrophone/mqtt.py
ADDED
|
@@ -0,0 +1,122 @@
|
|
|
1
|
+
"""MQTT payload sources and sinks, backed by the optional ``mqtt`` extra.
|
|
2
|
+
|
|
3
|
+
Importing this module and declaring graphs needs no MQTT client installation.
|
|
4
|
+
Install ``Pyrophone[mqtt]`` to execute these nodes. Payloads are bytes; decoding
|
|
5
|
+
and encoding belong in application transforms.
|
|
6
|
+
"""
|
|
7
|
+
|
|
8
|
+
import logging
|
|
9
|
+
import os
|
|
10
|
+
from collections.abc import AsyncIterator
|
|
11
|
+
from contextlib import AbstractAsyncContextManager
|
|
12
|
+
from importlib import import_module
|
|
13
|
+
from typing import Protocol, cast
|
|
14
|
+
|
|
15
|
+
from ._node import Node, node_type
|
|
16
|
+
from ._ports import Input, Output
|
|
17
|
+
from ._properties import Property
|
|
18
|
+
|
|
19
|
+
__all__ = ["Sink", "Source"]
|
|
20
|
+
|
|
21
|
+
_logger = logging.getLogger(__name__)
|
|
22
|
+
|
|
23
|
+
|
|
24
|
+
class _Message(Protocol):
|
|
25
|
+
@property
|
|
26
|
+
def payload(self) -> bytes: ...
|
|
27
|
+
|
|
28
|
+
|
|
29
|
+
class _Client(Protocol):
|
|
30
|
+
"""The small client interface used without importing optional dependencies."""
|
|
31
|
+
|
|
32
|
+
@property
|
|
33
|
+
def messages(self) -> AsyncIterator[_Message]: ...
|
|
34
|
+
|
|
35
|
+
async def subscribe(self, topic: str, *, qos: int) -> object: ...
|
|
36
|
+
|
|
37
|
+
async def publish(self, topic: str, payload: bytes, *, qos: int) -> None: ...
|
|
38
|
+
|
|
39
|
+
|
|
40
|
+
def _client(
|
|
41
|
+
host: str, port: int, username: str | None = None, password: str | None = None
|
|
42
|
+
) -> AbstractAsyncContextManager[_Client]:
|
|
43
|
+
try:
|
|
44
|
+
aiomqtt = import_module("aiomqtt")
|
|
45
|
+
except ModuleNotFoundError as error:
|
|
46
|
+
if error.name != "aiomqtt":
|
|
47
|
+
raise
|
|
48
|
+
raise ImportError(
|
|
49
|
+
"MQTT nodes require optional dependencies: pip install 'Pyrophone[mqtt]'"
|
|
50
|
+
) from error
|
|
51
|
+
return cast(
|
|
52
|
+
AbstractAsyncContextManager[_Client],
|
|
53
|
+
aiomqtt.Client(
|
|
54
|
+
host,
|
|
55
|
+
port,
|
|
56
|
+
username=username if username is not None else os.getenv("MQTT_USERNAME"),
|
|
57
|
+
password=password if password is not None else os.getenv("MQTT_PASSWORD"),
|
|
58
|
+
),
|
|
59
|
+
)
|
|
60
|
+
|
|
61
|
+
|
|
62
|
+
class _MQTTNode(Node):
|
|
63
|
+
host = Property[str](
|
|
64
|
+
default="127.0.0.1",
|
|
65
|
+
title="Broker host",
|
|
66
|
+
description="Hostname or address of the MQTT broker.",
|
|
67
|
+
)
|
|
68
|
+
port = Property[int](
|
|
69
|
+
default=1883,
|
|
70
|
+
title="Broker port",
|
|
71
|
+
description="TCP port on which the MQTT broker is listening.",
|
|
72
|
+
minimum=1,
|
|
73
|
+
maximum=65535,
|
|
74
|
+
)
|
|
75
|
+
username = Property[str | None](
|
|
76
|
+
default=None, description="MQTT username; None uses MQTT_USERNAME at startup."
|
|
77
|
+
)
|
|
78
|
+
password = Property[str | None](
|
|
79
|
+
default=None, description="MQTT password; None uses MQTT_PASSWORD at startup."
|
|
80
|
+
)
|
|
81
|
+
topic = Property[str](description="MQTT topic to subscribe to or publish on.")
|
|
82
|
+
qos = Property[int](
|
|
83
|
+
default=1,
|
|
84
|
+
title="Quality of service",
|
|
85
|
+
description="MQTT QoS level for this broker connection's messages.",
|
|
86
|
+
minimum=0,
|
|
87
|
+
maximum=2,
|
|
88
|
+
)
|
|
89
|
+
|
|
90
|
+
|
|
91
|
+
@node_type
|
|
92
|
+
class Source(_MQTTNode):
|
|
93
|
+
"""Subscribe to an MQTT topic and emit raw message payloads.
|
|
94
|
+
|
|
95
|
+
Broker disconnection fails the node; reconnection and application-level
|
|
96
|
+
acknowledgement policies are not supplied by this node.
|
|
97
|
+
"""
|
|
98
|
+
|
|
99
|
+
out_port = Output[bytes](description="MQTT message payloads, without decoding.")
|
|
100
|
+
|
|
101
|
+
async def run(self) -> None:
|
|
102
|
+
async with _client(
|
|
103
|
+
self.host, self.port, self.username, self.password
|
|
104
|
+
) as client:
|
|
105
|
+
await client.subscribe(self.topic, qos=self.qos)
|
|
106
|
+
_logger.info("Subscribed to %s", self.topic)
|
|
107
|
+
async for message in client.messages:
|
|
108
|
+
await self.out_port.send(message.payload)
|
|
109
|
+
|
|
110
|
+
|
|
111
|
+
@node_type
|
|
112
|
+
class Sink(_MQTTNode):
|
|
113
|
+
"""Publish raw payloads to an MQTT topic, without retaining messages."""
|
|
114
|
+
|
|
115
|
+
in_port = Input[bytes](description="Raw MQTT payloads to publish.")
|
|
116
|
+
|
|
117
|
+
async def run(self) -> None:
|
|
118
|
+
async with _client(
|
|
119
|
+
self.host, self.port, self.username, self.password
|
|
120
|
+
) as client:
|
|
121
|
+
async for payload in self.in_port:
|
|
122
|
+
await client.publish(self.topic, payload, qos=self.qos)
|
pyrophone/nodes.py
ADDED
|
@@ -0,0 +1,110 @@
|
|
|
1
|
+
"""Reusable node implementations built on the public component model."""
|
|
2
|
+
|
|
3
|
+
import json
|
|
4
|
+
from typing import ClassVar, overload
|
|
5
|
+
|
|
6
|
+
from ._node import Node, node_type
|
|
7
|
+
from ._ports import Input, Output
|
|
8
|
+
|
|
9
|
+
type JSONValue = (
|
|
10
|
+
bool | int | float | str | list[JSONValue] | dict[str, JSONValue] | None
|
|
11
|
+
)
|
|
12
|
+
|
|
13
|
+
|
|
14
|
+
class Transform[InputT, OutputT](Node):
|
|
15
|
+
"""Transform each input value into one output value, in stream order.
|
|
16
|
+
|
|
17
|
+
Override transform() and register a concrete subclass with node_type.
|
|
18
|
+
"""
|
|
19
|
+
|
|
20
|
+
in_port = Input[InputT](description="Values to transform.")
|
|
21
|
+
out_port = Output[OutputT](description="Transformed values.")
|
|
22
|
+
|
|
23
|
+
async def run(self) -> None:
|
|
24
|
+
async for value in self.in_port:
|
|
25
|
+
result = await self.transform(value)
|
|
26
|
+
await self.out_port.send(result)
|
|
27
|
+
|
|
28
|
+
async def transform(self, value: InputT) -> OutputT:
|
|
29
|
+
raise NotImplementedError("Transform subclasses must implement transform()")
|
|
30
|
+
|
|
31
|
+
|
|
32
|
+
class Identity[T](Transform[T, T]):
|
|
33
|
+
"""Forward each value unchanged, preserving its type and object identity.
|
|
34
|
+
|
|
35
|
+
Register a concrete subclass, such as Identity[int], with node_type.
|
|
36
|
+
"""
|
|
37
|
+
|
|
38
|
+
async def transform(self, value: T) -> T:
|
|
39
|
+
return value
|
|
40
|
+
|
|
41
|
+
|
|
42
|
+
@node_type
|
|
43
|
+
class IdentityBytes(Identity[bytes]):
|
|
44
|
+
"""Pass byte payloads through unchanged."""
|
|
45
|
+
|
|
46
|
+
|
|
47
|
+
class Print[T](Transform[T, T]):
|
|
48
|
+
"""Print each value to stdout and forward it unchanged.
|
|
49
|
+
|
|
50
|
+
Uses Python's normal print formatting and flushes each line immediately.
|
|
51
|
+
Register a concrete subclass such as Print[int] with node_type.
|
|
52
|
+
"""
|
|
53
|
+
|
|
54
|
+
async def transform(self, value: T) -> T:
|
|
55
|
+
print(value, flush=True)
|
|
56
|
+
return value
|
|
57
|
+
|
|
58
|
+
|
|
59
|
+
@node_type
|
|
60
|
+
class PrintBytes(Print[bytes]):
|
|
61
|
+
"""Print byte payloads (including the b prefix) without decoding them."""
|
|
62
|
+
|
|
63
|
+
|
|
64
|
+
@node_type
|
|
65
|
+
class PrintText(Transform[str | bytes, str | bytes]):
|
|
66
|
+
"""Print text or UTF-8 bytes and forward the original value unchanged.
|
|
67
|
+
|
|
68
|
+
Invalid UTF-8 prints as a bytes repr. Each print flushes stdout immediately.
|
|
69
|
+
The graph infers the output type from the connected upstream port.
|
|
70
|
+
"""
|
|
71
|
+
|
|
72
|
+
_output_type_sources: ClassVar[dict[str, str]] = {"out_port": "in_port"}
|
|
73
|
+
|
|
74
|
+
@overload
|
|
75
|
+
async def transform(self, value: str) -> str: ...
|
|
76
|
+
|
|
77
|
+
@overload
|
|
78
|
+
async def transform(self, value: bytes) -> bytes: ...
|
|
79
|
+
|
|
80
|
+
async def transform(self, value: str | bytes) -> str | bytes:
|
|
81
|
+
text = value
|
|
82
|
+
if isinstance(value, bytes):
|
|
83
|
+
try:
|
|
84
|
+
text = value.decode("utf-8")
|
|
85
|
+
except UnicodeDecodeError:
|
|
86
|
+
text = repr(value)
|
|
87
|
+
print(text, flush=True)
|
|
88
|
+
return value
|
|
89
|
+
|
|
90
|
+
|
|
91
|
+
class JSONTransform(Transform[bytes, bytes]):
|
|
92
|
+
"""Decode JSON, transform its value, and encode the result as UTF-8 bytes.
|
|
93
|
+
|
|
94
|
+
Override transform_json(); ports remain byte-oriented for direct connection
|
|
95
|
+
to transports such as MQTT. Encoding uses compact JSON with Unicode text
|
|
96
|
+
preserved and rejects non-finite numbers. Codec and hook errors propagate
|
|
97
|
+
through the normal node failure contract.
|
|
98
|
+
"""
|
|
99
|
+
|
|
100
|
+
async def transform(self, payload: bytes) -> bytes:
|
|
101
|
+
value: JSONValue = json.loads(payload)
|
|
102
|
+
result = await self.transform_json(value)
|
|
103
|
+
return json.dumps(
|
|
104
|
+
result, separators=(",", ":"), ensure_ascii=False, allow_nan=False
|
|
105
|
+
).encode("utf-8")
|
|
106
|
+
|
|
107
|
+
async def transform_json(self, value: JSONValue) -> JSONValue:
|
|
108
|
+
raise NotImplementedError(
|
|
109
|
+
"JSONTransform subclasses must implement transform_json()"
|
|
110
|
+
)
|
pyrophone/py.typed
ADDED
|
File without changes
|
pyrophone/script.py
ADDED
|
@@ -0,0 +1,74 @@
|
|
|
1
|
+
"""Convenience vocabulary for ``from pyrophone.script import *``.
|
|
2
|
+
|
|
3
|
+
These are ordinary, statically discoverable Python exports. Importing this
|
|
4
|
+
module starts no execution and needs no optional transport dependencies.
|
|
5
|
+
"""
|
|
6
|
+
|
|
7
|
+
import pyrophone
|
|
8
|
+
|
|
9
|
+
from . import (
|
|
10
|
+
ChannelClosedError,
|
|
11
|
+
EndOfStream,
|
|
12
|
+
Graph,
|
|
13
|
+
Input,
|
|
14
|
+
InputPort,
|
|
15
|
+
Node,
|
|
16
|
+
NodeDeclaration,
|
|
17
|
+
NodeHandle,
|
|
18
|
+
Output,
|
|
19
|
+
OutputPort,
|
|
20
|
+
PortHandle,
|
|
21
|
+
Property,
|
|
22
|
+
Runtime,
|
|
23
|
+
Trace,
|
|
24
|
+
cabe,
|
|
25
|
+
main,
|
|
26
|
+
mqtt,
|
|
27
|
+
node_type,
|
|
28
|
+
run_async,
|
|
29
|
+
run_sync,
|
|
30
|
+
)
|
|
31
|
+
from .jq import JQ
|
|
32
|
+
from .nodes import (
|
|
33
|
+
Identity,
|
|
34
|
+
IdentityBytes,
|
|
35
|
+
JSONTransform,
|
|
36
|
+
JSONValue,
|
|
37
|
+
Print,
|
|
38
|
+
PrintBytes,
|
|
39
|
+
PrintText,
|
|
40
|
+
Transform,
|
|
41
|
+
)
|
|
42
|
+
|
|
43
|
+
__all__ = [
|
|
44
|
+
"JQ",
|
|
45
|
+
"ChannelClosedError",
|
|
46
|
+
"EndOfStream",
|
|
47
|
+
"Graph",
|
|
48
|
+
"Identity",
|
|
49
|
+
"IdentityBytes",
|
|
50
|
+
"Input",
|
|
51
|
+
"InputPort",
|
|
52
|
+
"JSONTransform",
|
|
53
|
+
"JSONValue",
|
|
54
|
+
"Node",
|
|
55
|
+
"NodeDeclaration",
|
|
56
|
+
"NodeHandle",
|
|
57
|
+
"Output",
|
|
58
|
+
"OutputPort",
|
|
59
|
+
"PortHandle",
|
|
60
|
+
"Print",
|
|
61
|
+
"PrintBytes",
|
|
62
|
+
"PrintText",
|
|
63
|
+
"Property",
|
|
64
|
+
"Runtime",
|
|
65
|
+
"Trace",
|
|
66
|
+
"Transform",
|
|
67
|
+
"cabe",
|
|
68
|
+
"main",
|
|
69
|
+
"mqtt",
|
|
70
|
+
"node_type",
|
|
71
|
+
"pyrophone",
|
|
72
|
+
"run_async",
|
|
73
|
+
"run_sync",
|
|
74
|
+
]
|
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
Metadata-Version: 2.5
|
|
2
|
+
Name: Pyrophone
|
|
3
|
+
Version: 0.1.0
|
|
4
|
+
Summary: Python message routing DSL environment
|
|
5
|
+
Project-URL: Repository, https://github.com/messier-42/pyrophone
|
|
6
|
+
Project-URL: Issues, https://github.com/messier-42/pyrophone/issues
|
|
7
|
+
License-Expression: Apache-2.0
|
|
8
|
+
License-File: doc/LICENSE
|
|
9
|
+
Classifier: Development Status :: 1 - Planning
|
|
10
|
+
Classifier: Intended Audience :: Developers
|
|
11
|
+
Classifier: Programming Language :: Python :: 3 :: Only
|
|
12
|
+
Classifier: Programming Language :: Python :: 3.12
|
|
13
|
+
Classifier: Programming Language :: Python :: 3.13
|
|
14
|
+
Classifier: Programming Language :: Python :: 3.14
|
|
15
|
+
Classifier: Programming Language :: Python :: 3.15
|
|
16
|
+
Classifier: Typing :: Typed
|
|
17
|
+
Requires-Python: >=3.12
|
|
18
|
+
Requires-Dist: aioconsole<1,>=0.9
|
|
19
|
+
Requires-Dist: prompt-toolkit<4,>=3.0.52
|
|
20
|
+
Requires-Dist: pyyaml<7,>=6
|
|
21
|
+
Provides-Extra: amqp
|
|
22
|
+
Provides-Extra: cabe
|
|
23
|
+
Requires-Dist: pycabe>=0.1.0.dev0; extra == 'cabe'
|
|
24
|
+
Provides-Extra: irc
|
|
25
|
+
Provides-Extra: jq
|
|
26
|
+
Requires-Dist: jq<2,>=1.13; extra == 'jq'
|
|
27
|
+
Provides-Extra: mqtt
|
|
28
|
+
Requires-Dist: aiomqtt<3,>=2.4; extra == 'mqtt'
|
|
29
|
+
Provides-Extra: redis
|
|
30
|
+
Provides-Extra: xmpp
|
|
31
|
+
Description-Content-Type: text/markdown
|
|
32
|
+
|
|
33
|
+
# Pyrophone
|
|
34
|
+
|
|
35
|
+
Pyrophone is a Python-based message router programming environment. It
|
|
36
|
+
functions as a DSL constructed within the Python programming language to enable
|
|
37
|
+
people to readily construct message routing services using a minimal number of
|
|
38
|
+
lines of code.
|
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
pyrophone/__init__.py,sha256=eKZPK-YLqHOPPbGveqLBPoSyhFPav8fjamiqDLvoZ7A,1470
|
|
2
|
+
pyrophone/_channels.py,sha256=FrAWzqqJWLUTwZRKg_KsEagq9YKjLeAOQ6QNKTxYrB4,4305
|
|
3
|
+
pyrophone/_compile.py,sha256=LO74fEtK7NzvK9nY1hvg16jbNkCbhS8xDQq7ire-v-U,3810
|
|
4
|
+
pyrophone/_control.py,sha256=LcjscIdKlJaOmcCXPea3N3_WPTK0kNF-dtg64vCt100,7893
|
|
5
|
+
pyrophone/_driver.py,sha256=ic34e8H9SbBlRRNMggFnQVL6CSKjIsAEEApLkx6Fnio,2944
|
|
6
|
+
pyrophone/_entry.py,sha256=AQqdSqUI7alToF7nb2NVIKP3SfQTdw5CVvyaeltpnA0,1655
|
|
7
|
+
pyrophone/_exceptions.py,sha256=ilviTw242GXBoz2N-tScsLyC1bdrcWLukJvzc31bV5k,1222
|
|
8
|
+
pyrophone/_graph.py,sha256=ql8FGWZB4yFIAFvC-B2D9Q5PJLAfoQouV5ORFvcjuNw,10710
|
|
9
|
+
pyrophone/_graph_display.py,sha256=yZBD_fafSrj-2_Ncyiy6ncXGAdV5Y60h60nmyF1X_Cs,4401
|
|
10
|
+
pyrophone/_inspection.py,sha256=52EKbBma_3BsfeezrdVnhaS7qIoMYClxZHphQbfyAKg,1689
|
|
11
|
+
pyrophone/_node.py,sha256=CexX9Ys_wA0hy4pEibjZme8NxZ33xrhi2dEYlgcXx_U,8282
|
|
12
|
+
pyrophone/_ports.py,sha256=Rjb6R-xOZ3Ws1SMpX2TU5VRIDtADaJixd_KgsEsqAsU,3329
|
|
13
|
+
pyrophone/_properties.py,sha256=HvOOO1_3J6gLyhYzDOXM_RTu7ZgJhBH44a-tkT155tY,2014
|
|
14
|
+
pyrophone/_runner.py,sha256=2OkORWRWDR7BvbFioMO8YqLjzP3mxS2wLSOL7Cr0g6s,2640
|
|
15
|
+
pyrophone/_runtime.py,sha256=yAMEUIdE3muatYDD5ElDleMxcrRz2rHPhqisj82WYtY,5435
|
|
16
|
+
pyrophone/_schema.py,sha256=d0SNIs3mp9ugVKihu2o_riDu-dB8l8p78zlvAh2yNDA,7221
|
|
17
|
+
pyrophone/_shell.py,sha256=Gv9l52tkRPIQf2QK-MPah2dcJa9PAnsnWxHqiZh9tpw,4594
|
|
18
|
+
pyrophone/_terminal.py,sha256=JTVHT6GeZIrLYw1rybDpGNQeM62-7XRfUm9q8dw-3HU,2572
|
|
19
|
+
pyrophone/_trace.py,sha256=29YrGH7sAEXDv7yIHjpAG9dSMJjCnJHeEateK530cuE,2991
|
|
20
|
+
pyrophone/cabe.py,sha256=eyKuS5-x1405VKFU2OaZSK5_TAaZADisreFirbBmrVo,7084
|
|
21
|
+
pyrophone/jq.py,sha256=0J-MtzY-jcij6tAHYZw6N59sqIGkhtBgCt1vn2vT1Z8,2226
|
|
22
|
+
pyrophone/mqtt.py,sha256=Z0-kzPIdt1ZSYO9gqTxCCsN_TN43SRg6A1Gz2CGP_FA,3870
|
|
23
|
+
pyrophone/nodes.py,sha256=7xsI9CgjTOwnTNCTJnacigvU2EAlc-n0wLHedraBDhY,3497
|
|
24
|
+
pyrophone/py.typed,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
|
|
25
|
+
pyrophone/script.py,sha256=M65JkWZvkxcYgqyDVSuJbpdh5JrERSIKLAm7kbcpKXM,1206
|
|
26
|
+
pyrophone-0.1.0.dist-info/METADATA,sha256=8gEi2OKdM0niR_HGtALZauGXpcw4NiWGPIed7P0krec,1382
|
|
27
|
+
pyrophone-0.1.0.dist-info/WHEEL,sha256=2iZrJkDIkeDh0k9SikKMqFuRP-ua6mb4Ir1AsER4BEw,87
|
|
28
|
+
pyrophone-0.1.0.dist-info/licenses/doc/LICENSE,sha256=z8d0m5b2O9McPEK1xHG_dWgUBT6EfBDz6wA0F7xSPTA,11358
|
|
29
|
+
pyrophone-0.1.0.dist-info/RECORD,,
|