led-controller-version-3 3.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.
- led_controller_version_3-3.0.2.dist-info/METADATA +172 -0
- led_controller_version_3-3.0.2.dist-info/RECORD +52 -0
- led_controller_version_3-3.0.2.dist-info/WHEEL +4 -0
- led_controller_version_3-3.0.2.dist-info/entry_points.txt +13 -0
- lefx/device/respeaker/__init__.py +35 -0
- lefx/device/respeaker/cli.py +146 -0
- lefx/device/respeaker/contention.py +523 -0
- lefx/device/respeaker/provider.py +158 -0
- lefx/device/respeaker/registration.py +130 -0
- lefx/device/respeaker/sink.py +108 -0
- lefx/device/respeaker/transport.py +344 -0
- lefx/device/respeaker/xvf.py +313 -0
- lefx/engine/__init__.py +110 -0
- lefx/engine/composer.py +203 -0
- lefx/engine/config.py +27 -0
- lefx/engine/errors.py +84 -0
- lefx/engine/inputs.py +196 -0
- lefx/engine/invocation.py +129 -0
- lefx/engine/layers.py +114 -0
- lefx/engine/library.py +181 -0
- lefx/engine/packages/__init__.py +45 -0
- lefx/engine/packages/loader.py +375 -0
- lefx/engine/packages/manifest.py +308 -0
- lefx/engine/registry.py +347 -0
- lefx/engine/renderer.py +63 -0
- lefx/engine/runtime.py +393 -0
- lefx/engine/store.py +125 -0
- lefx/interfaces/__init__.py +40 -0
- lefx/interfaces/api.py +301 -0
- lefx/interfaces/cli.py +380 -0
- lefx/interfaces/client.py +165 -0
- lefx/interfaces/config.py +305 -0
- lefx/interfaces/discovery.py +308 -0
- lefx/interfaces/hosting.py +123 -0
- lefx/interfaces/paths.py +79 -0
- lefx/interfaces/presentation.py +118 -0
- lefx/interfaces/service.py +468 -0
- lefx/sdk/__init__.py +176 -0
- lefx/sdk/calibration.py +202 -0
- lefx/sdk/color.py +108 -0
- lefx/sdk/context.py +72 -0
- lefx/sdk/definitions.py +526 -0
- lefx/sdk/effects.py +63 -0
- lefx/sdk/errors.py +90 -0
- lefx/sdk/parameters.py +469 -0
- lefx/sdk/ports.py +76 -0
- lefx/sdk/validation.py +150 -0
- lefx/sdk/values.py +212 -0
- lefx/sets/core_set/__init__.py +32 -0
- lefx/sets/core_set/core-set.lefxset +0 -0
- lefx/sets/smartspeaker_set/__init__.py +32 -0
- lefx/sets/smartspeaker_set/smartspeaker-set.lefxset +0 -0
|
@@ -0,0 +1,308 @@
|
|
|
1
|
+
"""Finding devices and effect sets without importing them.
|
|
2
|
+
|
|
3
|
+
This is what makes the distribution boundary real rather than conventional. The
|
|
4
|
+
service never imports the hardware package, the simulator or a catalogue; it
|
|
5
|
+
reads the entry point groups ``lefx.frame_sinks``, ``lefx.input_providers`` and
|
|
6
|
+
``lefx.effect_sets`` and uses whatever is installed. Leaving a package out of an
|
|
7
|
+
installation leaves it out of the running system, with no code path to forget.
|
|
8
|
+
|
|
9
|
+
An entry point resolves to a factory. Failing to load one is reported and
|
|
10
|
+
skipped: a broken optional integration must not stop the service from starting.
|
|
11
|
+
|
|
12
|
+
Input providers are named ``<device>.<capability>`` — ``respeaker.doa``,
|
|
13
|
+
``simulator.doa``. The two halves do different jobs. The device half keeps the
|
|
14
|
+
names distinct while both packages are installed, which they routinely are; the
|
|
15
|
+
capability half is the only part a definition ever writes down. Choosing the
|
|
16
|
+
device chooses which providers run, and they are handed to the engine under the
|
|
17
|
+
bare capability, so the same ``provider_id="doa"`` resolves to the hardware or
|
|
18
|
+
to the simulator with nothing in the definition to change.
|
|
19
|
+
|
|
20
|
+
A provider registered without a device prefix belongs to no device and is always
|
|
21
|
+
available — a clock or a system sensor has no cable to be unplugged.
|
|
22
|
+
"""
|
|
23
|
+
|
|
24
|
+
from __future__ import annotations
|
|
25
|
+
|
|
26
|
+
import logging
|
|
27
|
+
from dataclasses import dataclass
|
|
28
|
+
from importlib.metadata import EntryPoint, entry_points
|
|
29
|
+
from pathlib import Path
|
|
30
|
+
from typing import Any, Callable, Mapping
|
|
31
|
+
|
|
32
|
+
from lefx.sdk import FrameSink, InputContext, OutputFrame, SinkStatus
|
|
33
|
+
|
|
34
|
+
from . import config
|
|
35
|
+
|
|
36
|
+
logger = logging.getLogger("lefx.interfaces.discovery")
|
|
37
|
+
|
|
38
|
+
SINK_GROUP = "lefx.frame_sinks"
|
|
39
|
+
PROVIDER_GROUP = "lefx.input_providers"
|
|
40
|
+
EFFECT_SET_GROUP = "lefx.effect_sets"
|
|
41
|
+
|
|
42
|
+
CAPABILITY_SEPARATOR = "."
|
|
43
|
+
"""Splits ``<device>.<capability>`` in an input provider's entry point name."""
|
|
44
|
+
|
|
45
|
+
|
|
46
|
+
def split_provider_name(name: str) -> tuple[str | None, str]:
|
|
47
|
+
"""Separate the device from the capability it provides.
|
|
48
|
+
|
|
49
|
+
``"respeaker.doa"`` becomes ``("respeaker", "doa")``; a bare ``"clock"``
|
|
50
|
+
becomes ``(None, "clock")`` — no device, so no device to select it with.
|
|
51
|
+
Only the first separator counts, so a capability may contain one.
|
|
52
|
+
"""
|
|
53
|
+
device, separator, capability = name.partition(CAPABILITY_SEPARATOR)
|
|
54
|
+
if not separator or not device or not capability:
|
|
55
|
+
return None, name
|
|
56
|
+
return device, capability
|
|
57
|
+
|
|
58
|
+
|
|
59
|
+
class NullSink:
|
|
60
|
+
"""Accepts frames and does nothing with them.
|
|
61
|
+
|
|
62
|
+
Always available, so the engine has somewhere to render even when no output
|
|
63
|
+
package is installed. Keeps the last frame so ``status`` and the API can
|
|
64
|
+
still show what would have been displayed.
|
|
65
|
+
"""
|
|
66
|
+
|
|
67
|
+
name = "null"
|
|
68
|
+
|
|
69
|
+
def __init__(self) -> None:
|
|
70
|
+
self.last_frame: OutputFrame | None = None
|
|
71
|
+
|
|
72
|
+
def apply_frame(self, frame: OutputFrame) -> None:
|
|
73
|
+
self.last_frame = frame
|
|
74
|
+
|
|
75
|
+
def status(self) -> SinkStatus:
|
|
76
|
+
return SinkStatus(available=True, detail="no output device; frames are discarded")
|
|
77
|
+
|
|
78
|
+
def close(self) -> None:
|
|
79
|
+
return None
|
|
80
|
+
|
|
81
|
+
|
|
82
|
+
@dataclass(slots=True, frozen=True)
|
|
83
|
+
class Discovered:
|
|
84
|
+
"""One registered factory, not yet called."""
|
|
85
|
+
|
|
86
|
+
name: str
|
|
87
|
+
factory: Callable[..., Any]
|
|
88
|
+
distribution: str | None = None
|
|
89
|
+
|
|
90
|
+
def create(self, **options: Any) -> Any:
|
|
91
|
+
return self.factory(**options)
|
|
92
|
+
|
|
93
|
+
|
|
94
|
+
def _load(group: str) -> dict[str, Discovered]:
|
|
95
|
+
found: dict[str, Discovered] = {}
|
|
96
|
+
for entry in entry_points(group=group):
|
|
97
|
+
try:
|
|
98
|
+
factory = entry.load()
|
|
99
|
+
except Exception as exc:
|
|
100
|
+
logger.warning("could not load %s entry point %r: %s", group, entry.name, exc)
|
|
101
|
+
continue
|
|
102
|
+
found[entry.name] = Discovered(
|
|
103
|
+
name=entry.name, factory=factory, distribution=_distribution_of(entry)
|
|
104
|
+
)
|
|
105
|
+
return found
|
|
106
|
+
|
|
107
|
+
|
|
108
|
+
def _distribution_of(entry: EntryPoint) -> str | None:
|
|
109
|
+
dist = getattr(entry, "dist", None)
|
|
110
|
+
return None if dist is None else getattr(dist, "name", None)
|
|
111
|
+
|
|
112
|
+
|
|
113
|
+
def available_sinks() -> dict[str, Discovered]:
|
|
114
|
+
return _load(SINK_GROUP)
|
|
115
|
+
|
|
116
|
+
|
|
117
|
+
def available_providers() -> dict[str, Discovered]:
|
|
118
|
+
return _load(PROVIDER_GROUP)
|
|
119
|
+
|
|
120
|
+
|
|
121
|
+
def create_sink(name: str, **options: Any) -> FrameSink:
|
|
122
|
+
"""Build the named sink, or the null sink when the name is ``null``."""
|
|
123
|
+
if name == NullSink.name:
|
|
124
|
+
return NullSink()
|
|
125
|
+
found = available_sinks()
|
|
126
|
+
if name not in found:
|
|
127
|
+
known = ", ".join(sorted([NullSink.name, *found]))
|
|
128
|
+
raise LookupError(
|
|
129
|
+
f"No frame sink named {name!r}. Installed sinks: {known}. "
|
|
130
|
+
"Install the package that provides it, or use 'null'."
|
|
131
|
+
)
|
|
132
|
+
return found[name].create(**options)
|
|
133
|
+
|
|
134
|
+
|
|
135
|
+
def create_providers(*, device: str | None = None, **options: Any) -> dict[str, Any]:
|
|
136
|
+
"""Build the selected device's input providers, keyed by capability.
|
|
137
|
+
|
|
138
|
+
Providers belonging to another device are not built at all — building them
|
|
139
|
+
would open a second device's connection for no reason. Providers with no
|
|
140
|
+
device are always built.
|
|
141
|
+
|
|
142
|
+
``options`` reaches every factory unchanged, which is why factories are
|
|
143
|
+
required to tolerate keywords they do not recognise.
|
|
144
|
+
"""
|
|
145
|
+
built: dict[str, Any] = {}
|
|
146
|
+
claimed: dict[str, str] = {}
|
|
147
|
+
for name, discovered in sorted(available_providers().items()):
|
|
148
|
+
owner, capability = split_provider_name(name)
|
|
149
|
+
if owner is not None and owner != device:
|
|
150
|
+
continue
|
|
151
|
+
if capability in claimed:
|
|
152
|
+
# Two providers offering one capability would otherwise resolve by
|
|
153
|
+
# whichever the metadata happened to list first. Say who won.
|
|
154
|
+
logger.warning(
|
|
155
|
+
"input provider %r offers %r, which %r already provides; skipping it",
|
|
156
|
+
name,
|
|
157
|
+
capability,
|
|
158
|
+
claimed[capability],
|
|
159
|
+
)
|
|
160
|
+
continue
|
|
161
|
+
try:
|
|
162
|
+
built[capability] = discovered.create(**options)
|
|
163
|
+
claimed[capability] = name
|
|
164
|
+
except Exception as exc:
|
|
165
|
+
# A provider that cannot start is a missing data source, not a
|
|
166
|
+
# reason for the service to refuse to run.
|
|
167
|
+
logger.warning("could not create input provider %r: %s", name, exc)
|
|
168
|
+
return built
|
|
169
|
+
|
|
170
|
+
|
|
171
|
+
def provider_callables(providers: Mapping[str, Any]) -> dict[str, Callable[[InputContext], Any]]:
|
|
172
|
+
"""Adapt provider objects to the callable the engine samples."""
|
|
173
|
+
return {name: provider.sample for name, provider in providers.items()}
|
|
174
|
+
|
|
175
|
+
|
|
176
|
+
# -- effect sets ------------------------------------------------------------
|
|
177
|
+
|
|
178
|
+
|
|
179
|
+
def normalize_set_id(name: str) -> str:
|
|
180
|
+
"""The one form two spellings of a set both reduce to.
|
|
181
|
+
|
|
182
|
+
``core``, ``core-set``, ``Core_Set`` and ``core set`` are the same set. The
|
|
183
|
+
id in a ``set.yaml`` carries the ``-set`` suffix and the entry point uses
|
|
184
|
+
that id, but nobody writing ``INCLUDED_LEFXSET`` wants to type it, and
|
|
185
|
+
telling them they must would be a rule with no purpose behind it.
|
|
186
|
+
"""
|
|
187
|
+
reduced = name.strip().casefold().replace("_", "-").replace(" ", "-")
|
|
188
|
+
return reduced[: -len("-set")] if reduced.endswith("-set") else reduced
|
|
189
|
+
|
|
190
|
+
|
|
191
|
+
def available_effect_sets() -> dict[str, Discovered]:
|
|
192
|
+
"""Every installed catalogue, keyed by the set id its entry point declares."""
|
|
193
|
+
return _load(EFFECT_SET_GROUP)
|
|
194
|
+
|
|
195
|
+
|
|
196
|
+
def installed_effect_sets(included: list[str] | None = None) -> dict[str, Path]:
|
|
197
|
+
"""The archives of the installed sets that are switched on, by set id.
|
|
198
|
+
|
|
199
|
+
``included`` is a list of names in any of the forms ``normalize_set_id``
|
|
200
|
+
accepts; ``None`` reads ``included_lefxset`` from the configuration, and an
|
|
201
|
+
empty selection means every installed set. Empty-means-all rather than
|
|
202
|
+
empty-means-none because the selection exists to *narrow* an installation,
|
|
203
|
+
and a fresh install with no config file should play what it installed.
|
|
204
|
+
|
|
205
|
+
A name that matches nothing installed is a warning, not an error: naming a
|
|
206
|
+
set you have not installed yet is a mistake worth seeing, and not one worth
|
|
207
|
+
refusing to start over.
|
|
208
|
+
"""
|
|
209
|
+
if included is None:
|
|
210
|
+
included = config.get("included_lefxset")
|
|
211
|
+
wanted = {normalize_set_id(name) for name in included}
|
|
212
|
+
|
|
213
|
+
found: dict[str, Path] = {}
|
|
214
|
+
matched: set[str] = set()
|
|
215
|
+
for set_id, discovered in sorted(available_effect_sets().items()):
|
|
216
|
+
key = normalize_set_id(set_id)
|
|
217
|
+
if wanted and key not in wanted:
|
|
218
|
+
continue
|
|
219
|
+
matched.add(key)
|
|
220
|
+
try:
|
|
221
|
+
path = Path(discovered.factory())
|
|
222
|
+
except Exception as exc:
|
|
223
|
+
logger.warning("effect set %r could not say where its archive is: %s", set_id, exc)
|
|
224
|
+
continue
|
|
225
|
+
if not path.is_file():
|
|
226
|
+
# The distribution is installed but its archive is not there. In a
|
|
227
|
+
# checkout that means build_effects.py has not run; in an
|
|
228
|
+
# installation it means a broken wheel. Either way, silence here
|
|
229
|
+
# would show up much later as an effect id nobody can resolve.
|
|
230
|
+
logger.warning(
|
|
231
|
+
"effect set %r is installed but %s is missing; it will not be loaded",
|
|
232
|
+
set_id, path,
|
|
233
|
+
)
|
|
234
|
+
continue
|
|
235
|
+
found[set_id] = path
|
|
236
|
+
|
|
237
|
+
for name in sorted(wanted - matched):
|
|
238
|
+
logger.warning(
|
|
239
|
+
"included_lefxset names %r, which is not installed (installed: %s)",
|
|
240
|
+
name, ", ".join(sorted(available_effect_sets())) or "none",
|
|
241
|
+
)
|
|
242
|
+
return found
|
|
243
|
+
|
|
244
|
+
|
|
245
|
+
def effect_set_search_paths(included: list[str] | None = None) -> list[Path]:
|
|
246
|
+
"""The directories the enabled sets live in, for the library to scan.
|
|
247
|
+
|
|
248
|
+
Directories rather than files because that is what the engine's discovery
|
|
249
|
+
takes, and because each catalogue distribution holds exactly one archive in
|
|
250
|
+
a directory of its own — so the two are the same list said differently.
|
|
251
|
+
"""
|
|
252
|
+
seen: dict[Path, None] = {}
|
|
253
|
+
for path in installed_effect_sets(included).values():
|
|
254
|
+
seen.setdefault(path.parent, None)
|
|
255
|
+
return list(seen)
|
|
256
|
+
|
|
257
|
+
|
|
258
|
+
def describe() -> dict[str, Any]:
|
|
259
|
+
"""What is installed, for the status output and the sources listing."""
|
|
260
|
+
enabled = installed_effect_sets()
|
|
261
|
+
return {
|
|
262
|
+
"effect_sets": [
|
|
263
|
+
{
|
|
264
|
+
"name": item.name,
|
|
265
|
+
"distribution": item.distribution,
|
|
266
|
+
"enabled": item.name in enabled,
|
|
267
|
+
"archive": str(enabled[item.name]) if item.name in enabled else None,
|
|
268
|
+
}
|
|
269
|
+
for item in sorted(available_effect_sets().values(), key=lambda item: item.name)
|
|
270
|
+
],
|
|
271
|
+
"sinks": [
|
|
272
|
+
{"name": NullSink.name, "distribution": "led-controller-version-3", "builtin": True},
|
|
273
|
+
*(
|
|
274
|
+
{"name": item.name, "distribution": item.distribution, "builtin": False}
|
|
275
|
+
for item in sorted(available_sinks().values(), key=lambda item: item.name)
|
|
276
|
+
),
|
|
277
|
+
],
|
|
278
|
+
"input_providers": [
|
|
279
|
+
{
|
|
280
|
+
"name": item.name,
|
|
281
|
+
"device": split_provider_name(item.name)[0],
|
|
282
|
+
"capability": split_provider_name(item.name)[1],
|
|
283
|
+
"distribution": item.distribution,
|
|
284
|
+
}
|
|
285
|
+
for item in sorted(available_providers().values(), key=lambda item: item.name)
|
|
286
|
+
],
|
|
287
|
+
}
|
|
288
|
+
|
|
289
|
+
|
|
290
|
+
__all__ = [
|
|
291
|
+
"CAPABILITY_SEPARATOR",
|
|
292
|
+
"EFFECT_SET_GROUP",
|
|
293
|
+
"Discovered",
|
|
294
|
+
"NullSink",
|
|
295
|
+
"PROVIDER_GROUP",
|
|
296
|
+
"SINK_GROUP",
|
|
297
|
+
"available_effect_sets",
|
|
298
|
+
"available_providers",
|
|
299
|
+
"available_sinks",
|
|
300
|
+
"create_providers",
|
|
301
|
+
"create_sink",
|
|
302
|
+
"describe",
|
|
303
|
+
"effect_set_search_paths",
|
|
304
|
+
"installed_effect_sets",
|
|
305
|
+
"normalize_set_id",
|
|
306
|
+
"provider_callables",
|
|
307
|
+
"split_provider_name",
|
|
308
|
+
]
|
|
@@ -0,0 +1,123 @@
|
|
|
1
|
+
"""Running the service as a process: port selection and the instance file.
|
|
2
|
+
|
|
3
|
+
Separate from the service itself, which is perfectly usable embedded in another
|
|
4
|
+
application and should not know about ports or PID files.
|
|
5
|
+
"""
|
|
6
|
+
|
|
7
|
+
from __future__ import annotations
|
|
8
|
+
|
|
9
|
+
import json
|
|
10
|
+
import os
|
|
11
|
+
import socket
|
|
12
|
+
import time
|
|
13
|
+
from dataclasses import asdict, dataclass
|
|
14
|
+
from pathlib import Path
|
|
15
|
+
|
|
16
|
+
DEFAULT_PORT = 8765
|
|
17
|
+
DEFAULT_POOL = tuple(range(8765, 8776))
|
|
18
|
+
|
|
19
|
+
|
|
20
|
+
@dataclass(slots=True, frozen=True)
|
|
21
|
+
class InstanceInfo:
|
|
22
|
+
"""What a running service publishes about itself."""
|
|
23
|
+
|
|
24
|
+
pid: int
|
|
25
|
+
host: str
|
|
26
|
+
port: int
|
|
27
|
+
requested_port: int
|
|
28
|
+
started_at: float
|
|
29
|
+
status: str = "starting"
|
|
30
|
+
|
|
31
|
+
def to_dict(self) -> dict[str, object]:
|
|
32
|
+
return asdict(self)
|
|
33
|
+
|
|
34
|
+
|
|
35
|
+
def port_is_free(host: str, port: int) -> bool:
|
|
36
|
+
with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as probe:
|
|
37
|
+
probe.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
|
|
38
|
+
try:
|
|
39
|
+
probe.bind((host, port))
|
|
40
|
+
except OSError:
|
|
41
|
+
return False
|
|
42
|
+
return True
|
|
43
|
+
|
|
44
|
+
|
|
45
|
+
def select_port(host: str, requested: int, pool: tuple[int, ...] = DEFAULT_POOL) -> int:
|
|
46
|
+
"""The requested port if it is free, otherwise the first free one from the pool."""
|
|
47
|
+
if port_is_free(host, requested):
|
|
48
|
+
return requested
|
|
49
|
+
for candidate in pool:
|
|
50
|
+
if candidate != requested and port_is_free(host, candidate):
|
|
51
|
+
return candidate
|
|
52
|
+
raise OSError(f"No free port for {host}; tried {requested} and {len(pool)} alternatives")
|
|
53
|
+
|
|
54
|
+
|
|
55
|
+
def parse_pool(text: str) -> tuple[int, ...]:
|
|
56
|
+
"""Accept ``8765,8770-8774`` and produce the ports it names."""
|
|
57
|
+
ports: list[int] = []
|
|
58
|
+
for chunk in str(text or "").split(","):
|
|
59
|
+
item = chunk.strip()
|
|
60
|
+
if not item:
|
|
61
|
+
continue
|
|
62
|
+
if "-" in item:
|
|
63
|
+
start, _, end = item.partition("-")
|
|
64
|
+
ports.extend(range(int(start), int(end) + 1))
|
|
65
|
+
else:
|
|
66
|
+
ports.append(int(item))
|
|
67
|
+
return tuple(dict.fromkeys(ports))
|
|
68
|
+
|
|
69
|
+
|
|
70
|
+
def write_instance(path: Path, info: InstanceInfo) -> None:
|
|
71
|
+
path.parent.mkdir(parents=True, exist_ok=True)
|
|
72
|
+
path.write_text(json.dumps(info.to_dict(), indent=2, sort_keys=True), encoding="utf-8")
|
|
73
|
+
|
|
74
|
+
|
|
75
|
+
def read_instance(path: Path) -> InstanceInfo | None:
|
|
76
|
+
try:
|
|
77
|
+
payload = json.loads(path.read_text(encoding="utf-8"))
|
|
78
|
+
except (OSError, json.JSONDecodeError):
|
|
79
|
+
return None
|
|
80
|
+
try:
|
|
81
|
+
return InstanceInfo(**payload)
|
|
82
|
+
except TypeError:
|
|
83
|
+
return None
|
|
84
|
+
|
|
85
|
+
|
|
86
|
+
def update_instance_status(path: Path, status: str) -> None:
|
|
87
|
+
info = read_instance(path)
|
|
88
|
+
if info is None:
|
|
89
|
+
return
|
|
90
|
+
write_instance(path, InstanceInfo(**{**info.to_dict(), "status": status}))
|
|
91
|
+
|
|
92
|
+
|
|
93
|
+
def clear_instance(path: Path, *, pid: int | None = None) -> None:
|
|
94
|
+
"""Remove the instance file, unless another process has claimed it."""
|
|
95
|
+
info = read_instance(path)
|
|
96
|
+
if info is not None and pid is not None and info.pid != pid:
|
|
97
|
+
return
|
|
98
|
+
path.unlink(missing_ok=True)
|
|
99
|
+
|
|
100
|
+
|
|
101
|
+
def create_instance(*, host: str, port: int, requested_port: int) -> InstanceInfo:
|
|
102
|
+
return InstanceInfo(
|
|
103
|
+
pid=os.getpid(),
|
|
104
|
+
host=host,
|
|
105
|
+
port=port,
|
|
106
|
+
requested_port=requested_port,
|
|
107
|
+
started_at=time.time(),
|
|
108
|
+
)
|
|
109
|
+
|
|
110
|
+
|
|
111
|
+
__all__ = [
|
|
112
|
+
"DEFAULT_POOL",
|
|
113
|
+
"DEFAULT_PORT",
|
|
114
|
+
"InstanceInfo",
|
|
115
|
+
"clear_instance",
|
|
116
|
+
"create_instance",
|
|
117
|
+
"parse_pool",
|
|
118
|
+
"port_is_free",
|
|
119
|
+
"read_instance",
|
|
120
|
+
"select_port",
|
|
121
|
+
"update_instance_status",
|
|
122
|
+
"write_instance",
|
|
123
|
+
]
|
lefx/interfaces/paths.py
ADDED
|
@@ -0,0 +1,79 @@
|
|
|
1
|
+
"""Where the service keeps its runtime state, and where it looks for effects.
|
|
2
|
+
|
|
3
|
+
Deliberately the only module that turns settings into locations. The engine
|
|
4
|
+
reads no environment and resolves no paths; that question belongs here, and the
|
|
5
|
+
answers come from :mod:`lefx.interfaces.config` so that a file and an
|
|
6
|
+
environment variable are the same mechanism rather than two.
|
|
7
|
+
|
|
8
|
+
The old names still work, because they are what the settings table produces:
|
|
9
|
+
``state_root`` and ``package_path`` are read from ``LEFX_STATE_ROOT`` and
|
|
10
|
+
``LEFX_PACKAGE_PATH`` exactly as before, and now also from ``config.yaml`` and
|
|
11
|
+
from the unprefixed forms.
|
|
12
|
+
"""
|
|
13
|
+
|
|
14
|
+
from __future__ import annotations
|
|
15
|
+
|
|
16
|
+
import tempfile
|
|
17
|
+
from pathlib import Path
|
|
18
|
+
|
|
19
|
+
from . import config, discovery
|
|
20
|
+
|
|
21
|
+
ENV_STATE_ROOT = "LEFX_STATE_ROOT"
|
|
22
|
+
ENV_PACKAGE_PATH = "LEFX_PACKAGE_PATH"
|
|
23
|
+
|
|
24
|
+
|
|
25
|
+
def state_root() -> Path:
|
|
26
|
+
"""Directory for logs, the instance file and persisted state."""
|
|
27
|
+
configured = config.get("state_root")
|
|
28
|
+
root = Path(configured).expanduser() if configured else Path(tempfile.gettempdir()) / "lefx-runtime"
|
|
29
|
+
root.mkdir(parents=True, exist_ok=True)
|
|
30
|
+
return root
|
|
31
|
+
|
|
32
|
+
|
|
33
|
+
def background_state_file() -> Path:
|
|
34
|
+
return state_root() / "background_state.json"
|
|
35
|
+
|
|
36
|
+
|
|
37
|
+
def instance_file() -> Path:
|
|
38
|
+
return state_root() / "active_service.json"
|
|
39
|
+
|
|
40
|
+
|
|
41
|
+
def log_file() -> Path:
|
|
42
|
+
return state_root() / "service.log"
|
|
43
|
+
|
|
44
|
+
|
|
45
|
+
def package_search_paths() -> list[Path]:
|
|
46
|
+
"""Directories scanned for ``.lefx`` and ``.lefxset`` files, in order.
|
|
47
|
+
|
|
48
|
+
Three sources, each answering a different question:
|
|
49
|
+
|
|
50
|
+
* the installed effect set distributions, filtered by ``included_lefxset``
|
|
51
|
+
— this is where a normal installation gets its catalogue, and where a
|
|
52
|
+
checkout gets it too, because the workspace installs the same packages
|
|
53
|
+
editable;
|
|
54
|
+
* ``package_path``, for archives kept outside any distribution;
|
|
55
|
+
* ``effects/`` beside the working directory, so a file dropped into a
|
|
56
|
+
checkout is picked up without configuring anything.
|
|
57
|
+
|
|
58
|
+
``package_path`` does not replace the installed sets. It used to replace
|
|
59
|
+
everything, which meant that pointing at one extra directory silently
|
|
60
|
+
turned the shipped catalogue off.
|
|
61
|
+
"""
|
|
62
|
+
found: dict[Path, None] = {}
|
|
63
|
+
for path in discovery.effect_set_search_paths():
|
|
64
|
+
found.setdefault(path, None)
|
|
65
|
+
for item in config.get("package_path"):
|
|
66
|
+
found.setdefault(Path(item).expanduser(), None)
|
|
67
|
+
found.setdefault(Path.cwd() / "effects", None)
|
|
68
|
+
return list(found)
|
|
69
|
+
|
|
70
|
+
|
|
71
|
+
__all__ = [
|
|
72
|
+
"ENV_PACKAGE_PATH",
|
|
73
|
+
"ENV_STATE_ROOT",
|
|
74
|
+
"background_state_file",
|
|
75
|
+
"instance_file",
|
|
76
|
+
"log_file",
|
|
77
|
+
"package_search_paths",
|
|
78
|
+
"state_root",
|
|
79
|
+
]
|
|
@@ -0,0 +1,118 @@
|
|
|
1
|
+
"""Turning engine objects into the payloads the API and CLI return.
|
|
2
|
+
|
|
3
|
+
Serialization lives here rather than on the service, which in the predecessor
|
|
4
|
+
had grown a set of private ``_serialize_*`` methods and become a presentation
|
|
5
|
+
layer alongside everything else it did.
|
|
6
|
+
"""
|
|
7
|
+
|
|
8
|
+
from __future__ import annotations
|
|
9
|
+
|
|
10
|
+
from typing import Any
|
|
11
|
+
|
|
12
|
+
from lefx.engine import Preset, RegisteredEffect, ResolvedTarget
|
|
13
|
+
from lefx.sdk import ControlledOverlayDefinition, DefinitionBase, ParamDefinition, StateDefinition
|
|
14
|
+
|
|
15
|
+
|
|
16
|
+
def parameter(param: ParamDefinition) -> dict[str, Any]:
|
|
17
|
+
payload: dict[str, Any] = {
|
|
18
|
+
"type": param.type.value,
|
|
19
|
+
"required": param.required,
|
|
20
|
+
"description": param.description,
|
|
21
|
+
"minimum": param.minimum,
|
|
22
|
+
"maximum": param.maximum,
|
|
23
|
+
"enum_values": list(param.enum_values),
|
|
24
|
+
"unit": param.unit,
|
|
25
|
+
"nullable": param.nullable,
|
|
26
|
+
"aliases": list(param.aliases),
|
|
27
|
+
}
|
|
28
|
+
if param.has_default:
|
|
29
|
+
payload["default"] = param.default
|
|
30
|
+
return payload
|
|
31
|
+
|
|
32
|
+
|
|
33
|
+
def definition(effect: RegisteredEffect) -> dict[str, Any]:
|
|
34
|
+
"""The full contract of one definition."""
|
|
35
|
+
declared = effect.definition
|
|
36
|
+
payload: dict[str, Any] = {
|
|
37
|
+
"id": declared.id,
|
|
38
|
+
"qualified_id": effect.qualified_id,
|
|
39
|
+
"package_id": effect.package_id,
|
|
40
|
+
"source_id": effect.source_id,
|
|
41
|
+
"type": declared.definition_type.value,
|
|
42
|
+
"form": declared.kind.value,
|
|
43
|
+
"overlay_mode": None if declared.overlay_mode is None else declared.overlay_mode.value,
|
|
44
|
+
"title": declared.title,
|
|
45
|
+
"description": declared.description,
|
|
46
|
+
"version": declared.version,
|
|
47
|
+
"tags": list(declared.tags),
|
|
48
|
+
"visual": {
|
|
49
|
+
"color_model": declared.color_model.value,
|
|
50
|
+
"composition": declared.composition.value,
|
|
51
|
+
"animated": declared.animated,
|
|
52
|
+
"directional": declared.directional,
|
|
53
|
+
},
|
|
54
|
+
"config": {name: parameter(param) for name, param in declared.parameter_schema.items()},
|
|
55
|
+
"runtime_inputs": {
|
|
56
|
+
name: parameter(param) for name, param in declared.runtime_input_schema.items()
|
|
57
|
+
},
|
|
58
|
+
"input_sampling": _sampling(declared),
|
|
59
|
+
"placement": _placement(declared),
|
|
60
|
+
}
|
|
61
|
+
return payload
|
|
62
|
+
|
|
63
|
+
|
|
64
|
+
def _sampling(declared: DefinitionBase) -> dict[str, Any] | None:
|
|
65
|
+
policy = declared.input_sampling
|
|
66
|
+
if policy is None or not declared.runtime_input_schema:
|
|
67
|
+
return None
|
|
68
|
+
return {
|
|
69
|
+
"mode": policy.mode.value,
|
|
70
|
+
"provider_id": policy.provider_id,
|
|
71
|
+
"interval_ms": policy.interval_ms,
|
|
72
|
+
"heartbeat_interval_ms": policy.heartbeat_interval_ms,
|
|
73
|
+
"max_missed_heartbeats": policy.max_missed_heartbeats,
|
|
74
|
+
"failure_after_ms": policy.failure_after_ms,
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
|
|
78
|
+
def _placement(declared: DefinitionBase) -> dict[str, Any]:
|
|
79
|
+
"""Where this form runs — derived from the type, never chosen by a caller."""
|
|
80
|
+
if isinstance(declared, StateDefinition):
|
|
81
|
+
return {
|
|
82
|
+
"slots": [slot.value for slot in declared.slots],
|
|
83
|
+
"restorable": declared.restorable,
|
|
84
|
+
}
|
|
85
|
+
if isinstance(declared, ControlledOverlayDefinition):
|
|
86
|
+
return {"requires_channel": True}
|
|
87
|
+
return {
|
|
88
|
+
"duration_field": declared.duration_field.value, # type: ignore[attr-defined]
|
|
89
|
+
"supports_duration_override": declared.supports_duration_override, # type: ignore[attr-defined]
|
|
90
|
+
"default_priority": getattr(declared, "default_priority", None),
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
|
|
94
|
+
def preset(item: Preset) -> dict[str, Any]:
|
|
95
|
+
return item.to_dict()
|
|
96
|
+
|
|
97
|
+
|
|
98
|
+
def resolved(target: str, item: ResolvedTarget) -> dict[str, Any]:
|
|
99
|
+
payload = definition(item.effect)
|
|
100
|
+
payload["resolved_from"] = target
|
|
101
|
+
payload["resolved_kind"] = item.kind
|
|
102
|
+
if item.preset is not None:
|
|
103
|
+
payload["preset"] = item.preset.to_dict()
|
|
104
|
+
return payload
|
|
105
|
+
|
|
106
|
+
|
|
107
|
+
def summary(effect: RegisteredEffect) -> dict[str, Any]:
|
|
108
|
+
"""The short form used by listings without ``--details``."""
|
|
109
|
+
return {
|
|
110
|
+
"id": effect.effect_id,
|
|
111
|
+
"title": effect.definition.title,
|
|
112
|
+
"type": effect.definition.definition_type.value,
|
|
113
|
+
"form": effect.definition.kind.value,
|
|
114
|
+
"source_id": effect.source_id,
|
|
115
|
+
}
|
|
116
|
+
|
|
117
|
+
|
|
118
|
+
__all__ = ["definition", "parameter", "preset", "resolved", "summary"]
|