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
lefx/engine/errors.py
ADDED
|
@@ -0,0 +1,84 @@
|
|
|
1
|
+
"""Engine-level failures, separate from schema and payload errors.
|
|
2
|
+
|
|
3
|
+
Payload problems raise ``lefx.sdk.ParameterValidationError`` and never reach
|
|
4
|
+
here. These are the failures that belong to running the system: an id that
|
|
5
|
+
resolves to nothing or to several things, a command that contradicts the form
|
|
6
|
+
it addresses, a package that does not load, a frame that breaks its contract.
|
|
7
|
+
"""
|
|
8
|
+
|
|
9
|
+
from __future__ import annotations
|
|
10
|
+
|
|
11
|
+
|
|
12
|
+
class EngineError(Exception):
|
|
13
|
+
"""Base class for every engine failure."""
|
|
14
|
+
|
|
15
|
+
|
|
16
|
+
class TargetNotFoundError(EngineError, KeyError):
|
|
17
|
+
"""No definition or preset matches the requested identifier."""
|
|
18
|
+
|
|
19
|
+
def __init__(self, target: str, *, suggestions: tuple[str, ...] = ()) -> None:
|
|
20
|
+
self.target = target
|
|
21
|
+
self.suggestions = suggestions
|
|
22
|
+
message = f"No effect or preset named {target!r}"
|
|
23
|
+
if suggestions:
|
|
24
|
+
message = f"{message}. Did you mean: {', '.join(suggestions)}?"
|
|
25
|
+
super().__init__(message)
|
|
26
|
+
|
|
27
|
+
def __str__(self) -> str: # KeyError would otherwise quote the message
|
|
28
|
+
return self.args[0]
|
|
29
|
+
|
|
30
|
+
|
|
31
|
+
class AmbiguousTargetError(EngineError, ValueError):
|
|
32
|
+
"""Several definitions match; nothing is run on a guess."""
|
|
33
|
+
|
|
34
|
+
def __init__(self, target: str, matches: tuple[str, ...]) -> None:
|
|
35
|
+
self.target = target
|
|
36
|
+
self.matches = matches
|
|
37
|
+
super().__init__(
|
|
38
|
+
f"{target!r} is ambiguous between: {', '.join(matches)}. "
|
|
39
|
+
"Qualify it with its source, for example 'source::id'."
|
|
40
|
+
)
|
|
41
|
+
|
|
42
|
+
|
|
43
|
+
class CommandError(EngineError, ValueError):
|
|
44
|
+
"""A command contradicts the form it addresses."""
|
|
45
|
+
|
|
46
|
+
|
|
47
|
+
class WrongTargetTypeError(CommandError):
|
|
48
|
+
"""The identifier resolves, but to a different form than the verb expects."""
|
|
49
|
+
|
|
50
|
+
|
|
51
|
+
class ChannelNotFoundError(EngineError, KeyError):
|
|
52
|
+
"""No controlled overlay is running on the requested channel."""
|
|
53
|
+
|
|
54
|
+
def __init__(self, channel: str) -> None:
|
|
55
|
+
self.channel = channel
|
|
56
|
+
super().__init__(f"No controlled overlay on channel {channel!r}")
|
|
57
|
+
|
|
58
|
+
def __str__(self) -> str:
|
|
59
|
+
return self.args[0]
|
|
60
|
+
|
|
61
|
+
|
|
62
|
+
class RenderError(EngineError, ValueError):
|
|
63
|
+
"""A definition returned a frame that does not satisfy its own contract."""
|
|
64
|
+
|
|
65
|
+
|
|
66
|
+
class RegistrationError(EngineError, ValueError):
|
|
67
|
+
"""A definition or package cannot be registered."""
|
|
68
|
+
|
|
69
|
+
|
|
70
|
+
class PackageError(EngineError, ValueError):
|
|
71
|
+
"""A package is malformed, altered, or disagrees with the class it carries."""
|
|
72
|
+
|
|
73
|
+
|
|
74
|
+
__all__ = [
|
|
75
|
+
"AmbiguousTargetError",
|
|
76
|
+
"ChannelNotFoundError",
|
|
77
|
+
"CommandError",
|
|
78
|
+
"EngineError",
|
|
79
|
+
"PackageError",
|
|
80
|
+
"RegistrationError",
|
|
81
|
+
"RenderError",
|
|
82
|
+
"TargetNotFoundError",
|
|
83
|
+
"WrongTargetTypeError",
|
|
84
|
+
]
|
lefx/engine/inputs.py
ADDED
|
@@ -0,0 +1,196 @@
|
|
|
1
|
+
"""Runtime input health and the shared polling helper.
|
|
2
|
+
|
|
3
|
+
The engine judges *reception*, not plausibility: whether a value arrived in
|
|
4
|
+
time, never whether it makes sense. A syntactically valid but nonsensical
|
|
5
|
+
reading is the source's problem or the definition's, not the engine's.
|
|
6
|
+
|
|
7
|
+
Nothing here ends an instance. A controlled overlay whose source has gone quiet
|
|
8
|
+
keeps running with null values, and the definition decides what that looks like.
|
|
9
|
+
"""
|
|
10
|
+
|
|
11
|
+
from __future__ import annotations
|
|
12
|
+
|
|
13
|
+
import threading
|
|
14
|
+
from enum import Enum
|
|
15
|
+
from typing import Any, Callable, Mapping
|
|
16
|
+
|
|
17
|
+
from lefx.sdk import DefinitionBase, InputContext
|
|
18
|
+
|
|
19
|
+
from .invocation import Invocation
|
|
20
|
+
|
|
21
|
+
|
|
22
|
+
class InputHealth(str, Enum):
|
|
23
|
+
WAITING = "waiting"
|
|
24
|
+
"""Nothing has arrived yet since activation."""
|
|
25
|
+
|
|
26
|
+
HEALTHY = "healthy"
|
|
27
|
+
"""A value arrived within the grace period."""
|
|
28
|
+
|
|
29
|
+
FAILED = "failed"
|
|
30
|
+
"""The grace period has passed without a value."""
|
|
31
|
+
|
|
32
|
+
|
|
33
|
+
def _anchor(invocation: Invocation) -> float:
|
|
34
|
+
"""The point health is measured from.
|
|
35
|
+
|
|
36
|
+
Activation seeds the clock so a brand new instance is not immediately
|
|
37
|
+
considered failed; every success moves it forward.
|
|
38
|
+
"""
|
|
39
|
+
if invocation.input_last_success_at is not None:
|
|
40
|
+
return invocation.input_last_success_at
|
|
41
|
+
return invocation.activated_at if invocation.activated_at is not None else invocation.created_at
|
|
42
|
+
|
|
43
|
+
|
|
44
|
+
def age_ms(invocation: Invocation, now: float) -> float:
|
|
45
|
+
return max(0.0, (now - _anchor(invocation)) * 1000.0)
|
|
46
|
+
|
|
47
|
+
|
|
48
|
+
def evaluate_health(
|
|
49
|
+
definition: DefinitionBase, invocation: Invocation, now: float
|
|
50
|
+
) -> InputHealth | None:
|
|
51
|
+
"""Reception state, or ``None`` for a form that has no runtime inputs."""
|
|
52
|
+
policy = definition.input_sampling
|
|
53
|
+
if policy is None or not definition.runtime_input_schema:
|
|
54
|
+
return None
|
|
55
|
+
if age_ms(invocation, now) >= policy.failure_after_ms:
|
|
56
|
+
return InputHealth.FAILED
|
|
57
|
+
if invocation.input_last_success_at is None:
|
|
58
|
+
return InputHealth.WAITING
|
|
59
|
+
return InputHealth.HEALTHY
|
|
60
|
+
|
|
61
|
+
|
|
62
|
+
def effective_inputs(
|
|
63
|
+
definition: DefinitionBase, invocation: Invocation, now: float
|
|
64
|
+
) -> dict[str, Any]:
|
|
65
|
+
"""The values a render actually sees.
|
|
66
|
+
|
|
67
|
+
Every declared key is present. During the grace period the last good values
|
|
68
|
+
stay in place; once it lapses every declared field reads null, and the
|
|
69
|
+
definition interprets that — the engine draws no error state of its own.
|
|
70
|
+
"""
|
|
71
|
+
schema = definition.runtime_input_schema
|
|
72
|
+
if not schema:
|
|
73
|
+
return {}
|
|
74
|
+
if evaluate_health(definition, invocation, now) is InputHealth.FAILED:
|
|
75
|
+
return {name: None for name in schema}
|
|
76
|
+
return {name: invocation.inputs.get(name) for name in schema}
|
|
77
|
+
|
|
78
|
+
|
|
79
|
+
def input_status(
|
|
80
|
+
definition: DefinitionBase, invocation: Invocation, now: float
|
|
81
|
+
) -> dict[str, Any] | None:
|
|
82
|
+
"""Machine-readable reception state for the status endpoint."""
|
|
83
|
+
policy = definition.input_sampling
|
|
84
|
+
health = evaluate_health(definition, invocation, now)
|
|
85
|
+
if policy is None or health is None:
|
|
86
|
+
return None
|
|
87
|
+
elapsed = age_ms(invocation, now)
|
|
88
|
+
return {
|
|
89
|
+
"mode": policy.mode.value,
|
|
90
|
+
"provider_id": policy.provider_id,
|
|
91
|
+
"status": health.value,
|
|
92
|
+
"age_ms": int(round(elapsed)),
|
|
93
|
+
"missed_heartbeats": int(elapsed // policy.heartbeat_interval_ms),
|
|
94
|
+
"max_missed_heartbeats": policy.max_missed_heartbeats,
|
|
95
|
+
"failure_after_ms": policy.failure_after_ms,
|
|
96
|
+
"last_error": invocation.input_error,
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
|
|
100
|
+
class PolledInputProvider:
|
|
101
|
+
"""Caches a reading so effect count and device access rate stay decoupled.
|
|
102
|
+
|
|
103
|
+
Shared hardware is read on this provider's own schedule. Ten overlays
|
|
104
|
+
watching the same source therefore produce one read per interval, not ten
|
|
105
|
+
per frame.
|
|
106
|
+
"""
|
|
107
|
+
|
|
108
|
+
def __init__(
|
|
109
|
+
self,
|
|
110
|
+
provider_id: str,
|
|
111
|
+
reader: Callable[[], Mapping[str, Any] | None],
|
|
112
|
+
*,
|
|
113
|
+
max_hz: float,
|
|
114
|
+
) -> None:
|
|
115
|
+
normalized = str(provider_id).strip()
|
|
116
|
+
if not normalized:
|
|
117
|
+
raise ValueError("provider_id must not be empty")
|
|
118
|
+
if max_hz <= 0:
|
|
119
|
+
raise ValueError("max_hz must be greater than zero")
|
|
120
|
+
|
|
121
|
+
self.provider_id = normalized
|
|
122
|
+
self.reader = reader
|
|
123
|
+
self.max_hz = float(max_hz)
|
|
124
|
+
self.interval_s = 1.0 / self.max_hz
|
|
125
|
+
self._lock = threading.RLock()
|
|
126
|
+
self._snapshot: dict[str, Any] | None = None
|
|
127
|
+
self._last_attempt_at: float | None = None
|
|
128
|
+
self._last_success_at: float | None = None
|
|
129
|
+
self._last_error: str | None = None
|
|
130
|
+
self._poll_count = 0
|
|
131
|
+
|
|
132
|
+
@property
|
|
133
|
+
def last_error(self) -> str | None:
|
|
134
|
+
with self._lock:
|
|
135
|
+
return self._last_error
|
|
136
|
+
|
|
137
|
+
def refresh(self, now: float) -> bool:
|
|
138
|
+
"""Read the source if the interval has elapsed. Returns whether it tried."""
|
|
139
|
+
with self._lock:
|
|
140
|
+
if (
|
|
141
|
+
self._last_attempt_at is not None
|
|
142
|
+
and now - self._last_attempt_at < self.interval_s
|
|
143
|
+
):
|
|
144
|
+
return False
|
|
145
|
+
self._last_attempt_at = now
|
|
146
|
+
self._poll_count += 1
|
|
147
|
+
|
|
148
|
+
try:
|
|
149
|
+
sampled = self.reader()
|
|
150
|
+
if sampled is None:
|
|
151
|
+
raise RuntimeError("input source returned no value")
|
|
152
|
+
snapshot = dict(sampled)
|
|
153
|
+
except Exception as exc:
|
|
154
|
+
with self._lock:
|
|
155
|
+
self._last_error = str(exc)
|
|
156
|
+
return True
|
|
157
|
+
|
|
158
|
+
with self._lock:
|
|
159
|
+
self._snapshot = snapshot
|
|
160
|
+
self._last_success_at = now
|
|
161
|
+
self._last_error = None
|
|
162
|
+
return True
|
|
163
|
+
|
|
164
|
+
def sample(self, ctx: InputContext) -> Mapping[str, Any] | None:
|
|
165
|
+
"""Hand out the cached snapshot. Never touches the device itself."""
|
|
166
|
+
del ctx
|
|
167
|
+
with self._lock:
|
|
168
|
+
if self._last_error is not None:
|
|
169
|
+
raise RuntimeError(self._last_error)
|
|
170
|
+
return None if self._snapshot is None else dict(self._snapshot)
|
|
171
|
+
|
|
172
|
+
def status(self, now: float) -> dict[str, Any]:
|
|
173
|
+
with self._lock:
|
|
174
|
+
elapsed = (
|
|
175
|
+
None
|
|
176
|
+
if self._last_success_at is None
|
|
177
|
+
else max(0, int(round((now - self._last_success_at) * 1000.0)))
|
|
178
|
+
)
|
|
179
|
+
return {
|
|
180
|
+
"provider_id": self.provider_id,
|
|
181
|
+
"max_hz": self.max_hz,
|
|
182
|
+
"poll_count": self._poll_count,
|
|
183
|
+
"age_ms": elapsed,
|
|
184
|
+
"last_error": self._last_error,
|
|
185
|
+
"available": self._snapshot is not None and self._last_error is None,
|
|
186
|
+
}
|
|
187
|
+
|
|
188
|
+
|
|
189
|
+
__all__ = [
|
|
190
|
+
"InputHealth",
|
|
191
|
+
"PolledInputProvider",
|
|
192
|
+
"age_ms",
|
|
193
|
+
"effective_inputs",
|
|
194
|
+
"evaluate_health",
|
|
195
|
+
"input_status",
|
|
196
|
+
]
|
|
@@ -0,0 +1,129 @@
|
|
|
1
|
+
"""One activation of a definition.
|
|
2
|
+
|
|
3
|
+
The definition is the immutable contract; the invocation is what is actually
|
|
4
|
+
running. Keeping them apart is what lets the same definition be activated
|
|
5
|
+
several times over without its metadata and its runtime state mixing.
|
|
6
|
+
|
|
7
|
+
Nothing is smuggled through ``params`` here. Earlier generations hid activation
|
|
8
|
+
time, channel and origin in dunder-prefixed keys that then had to be stripped
|
|
9
|
+
before every render; those are ordinary fields now.
|
|
10
|
+
"""
|
|
11
|
+
|
|
12
|
+
from __future__ import annotations
|
|
13
|
+
|
|
14
|
+
from dataclasses import dataclass, field
|
|
15
|
+
from typing import Any
|
|
16
|
+
|
|
17
|
+
from lefx.sdk import DefinitionBase, DefinitionKind, DurationField
|
|
18
|
+
|
|
19
|
+
from .errors import CommandError
|
|
20
|
+
from .layers import LAYER_PRIORITIES, LayerId
|
|
21
|
+
|
|
22
|
+
|
|
23
|
+
@dataclass(slots=True)
|
|
24
|
+
class Invocation:
|
|
25
|
+
"""A definition, its resolved values, and where it stands in its lifecycle."""
|
|
26
|
+
|
|
27
|
+
invocation_id: str
|
|
28
|
+
definition: DefinitionBase
|
|
29
|
+
layer: LayerId
|
|
30
|
+
params: dict[str, Any] = field(default_factory=dict)
|
|
31
|
+
inputs: dict[str, Any] = field(default_factory=dict)
|
|
32
|
+
|
|
33
|
+
created_at: float = 0.0
|
|
34
|
+
"""When the invocation was accepted — for an event, when it entered the queue."""
|
|
35
|
+
|
|
36
|
+
activated_at: float | None = None
|
|
37
|
+
"""When it became visible. A queued event has not been activated yet."""
|
|
38
|
+
|
|
39
|
+
duration_ms: int | None = None
|
|
40
|
+
"""Length for finite forms; ``None`` for states and controlled overlays."""
|
|
41
|
+
|
|
42
|
+
priority: int | None = None
|
|
43
|
+
channel: str | None = None
|
|
44
|
+
preset_id: str | None = None
|
|
45
|
+
source: str | None = None
|
|
46
|
+
|
|
47
|
+
inputs_supplied: bool = False
|
|
48
|
+
"""Whether activation carried runtime values, as opposed to schema defaults."""
|
|
49
|
+
|
|
50
|
+
input_last_attempt_at: float | None = None
|
|
51
|
+
input_last_success_at: float | None = None
|
|
52
|
+
input_error: str | None = None
|
|
53
|
+
|
|
54
|
+
@property
|
|
55
|
+
def effect_id(self) -> str:
|
|
56
|
+
return self.definition.id
|
|
57
|
+
|
|
58
|
+
@property
|
|
59
|
+
def is_active(self) -> bool:
|
|
60
|
+
return self.activated_at is not None
|
|
61
|
+
|
|
62
|
+
def effective_priority(self) -> int:
|
|
63
|
+
"""Queue order for events; falls back to the definition, then the layer."""
|
|
64
|
+
if self.priority is not None:
|
|
65
|
+
return int(self.priority)
|
|
66
|
+
declared = getattr(self.definition, "default_priority", None)
|
|
67
|
+
if declared is not None:
|
|
68
|
+
return int(declared)
|
|
69
|
+
return LAYER_PRIORITIES[self.layer]
|
|
70
|
+
|
|
71
|
+
def activate(self, now: float) -> None:
|
|
72
|
+
"""Start the clock.
|
|
73
|
+
|
|
74
|
+
A queued event only begins to age once it is on screen, so its declared
|
|
75
|
+
duration is the time it is actually visible rather than the time since
|
|
76
|
+
it was requested.
|
|
77
|
+
"""
|
|
78
|
+
self.activated_at = now
|
|
79
|
+
if self.input_last_success_at is None and self.inputs_supplied:
|
|
80
|
+
# Values handed over at activation count as a reception, so an
|
|
81
|
+
# instance that was given data up front does not start out waiting.
|
|
82
|
+
# A schema default does not count: nothing has arrived from the
|
|
83
|
+
# source, and reporting otherwise would hide a source that never
|
|
84
|
+
# speaks at all.
|
|
85
|
+
self.input_last_success_at = now
|
|
86
|
+
|
|
87
|
+
def expires_at(self) -> float | None:
|
|
88
|
+
if self.duration_ms is None or self.activated_at is None:
|
|
89
|
+
return None
|
|
90
|
+
return self.activated_at + (self.duration_ms / 1000.0)
|
|
91
|
+
|
|
92
|
+
def is_expired(self, now: float) -> bool:
|
|
93
|
+
deadline = self.expires_at()
|
|
94
|
+
return deadline is not None and now >= deadline
|
|
95
|
+
|
|
96
|
+
def remaining_ms(self, now: float) -> int | None:
|
|
97
|
+
deadline = self.expires_at()
|
|
98
|
+
if deadline is None:
|
|
99
|
+
return None
|
|
100
|
+
return max(0, int(round((deadline - now) * 1000.0)))
|
|
101
|
+
|
|
102
|
+
|
|
103
|
+
def duration_from_config(
|
|
104
|
+
definition: DefinitionBase,
|
|
105
|
+
params: dict[str, Any],
|
|
106
|
+
*,
|
|
107
|
+
override_ms: int | None = None,
|
|
108
|
+
) -> int | None:
|
|
109
|
+
"""The length a finite form will run for.
|
|
110
|
+
|
|
111
|
+
States and controlled overlays are indefinite and return ``None``. An
|
|
112
|
+
override is only honoured when the definition allows one, so a caller cannot
|
|
113
|
+
stretch a signal that was designed to be brief.
|
|
114
|
+
"""
|
|
115
|
+
if definition.kind in {DefinitionKind.STATE, DefinitionKind.CONTROLLED_OVERLAY}:
|
|
116
|
+
return None
|
|
117
|
+
field_name: DurationField = definition.duration_field # type: ignore[attr-defined]
|
|
118
|
+
configured = params[field_name.value]
|
|
119
|
+
if override_ms is None:
|
|
120
|
+
return int(configured)
|
|
121
|
+
if not definition.supports_duration_override: # type: ignore[attr-defined]
|
|
122
|
+
raise CommandError(
|
|
123
|
+
f"{definition.id!r} does not support a duration override; "
|
|
124
|
+
f"set config.{field_name.value} instead"
|
|
125
|
+
)
|
|
126
|
+
return int(override_ms)
|
|
127
|
+
|
|
128
|
+
|
|
129
|
+
__all__ = ["Invocation", "duration_from_config"]
|
lefx/engine/layers.py
ADDED
|
@@ -0,0 +1,114 @@
|
|
|
1
|
+
"""The layer stack and the fixed mapping from definition form to placement.
|
|
2
|
+
|
|
3
|
+
Callers never choose a layer. A definition's form decides it, which is what
|
|
4
|
+
prevents an event from becoming permanent or a state from floating above an
|
|
5
|
+
overlay. The only choice an author has is which of the two state slots a state
|
|
6
|
+
is designed for, and even that is declared rather than passed per activation.
|
|
7
|
+
"""
|
|
8
|
+
|
|
9
|
+
from __future__ import annotations
|
|
10
|
+
|
|
11
|
+
from enum import Enum
|
|
12
|
+
from types import MappingProxyType
|
|
13
|
+
from typing import Mapping
|
|
14
|
+
|
|
15
|
+
from lefx.sdk import DefinitionBase, DefinitionKind, StateSlot
|
|
16
|
+
|
|
17
|
+
from .errors import CommandError
|
|
18
|
+
|
|
19
|
+
|
|
20
|
+
class LayerId(str, Enum):
|
|
21
|
+
"""Composition stages, listed bottom to top."""
|
|
22
|
+
|
|
23
|
+
BACKGROUND_STATE = "background_state"
|
|
24
|
+
PRIMARY_STATE = "primary_state"
|
|
25
|
+
TIMED_OVERLAY = "timed_overlay"
|
|
26
|
+
CONTROLLED_OVERLAY = "controlled_overlay"
|
|
27
|
+
EVENT = "event"
|
|
28
|
+
|
|
29
|
+
|
|
30
|
+
LAYER_PRIORITIES: Mapping[LayerId, int] = MappingProxyType(
|
|
31
|
+
{
|
|
32
|
+
LayerId.BACKGROUND_STATE: 100,
|
|
33
|
+
LayerId.PRIMARY_STATE: 200,
|
|
34
|
+
LayerId.TIMED_OVERLAY: 400,
|
|
35
|
+
LayerId.CONTROLLED_OVERLAY: 500,
|
|
36
|
+
LayerId.EVENT: 600,
|
|
37
|
+
}
|
|
38
|
+
)
|
|
39
|
+
|
|
40
|
+
# Controlled overlays sit above timed ones on purpose: a running functional
|
|
41
|
+
# readout should stay visible, and anything that genuinely has to cover
|
|
42
|
+
# everything for a moment is an event.
|
|
43
|
+
COMPOSITION_ORDER: tuple[LayerId, ...] = tuple(
|
|
44
|
+
sorted(LayerId, key=lambda layer: LAYER_PRIORITIES[layer])
|
|
45
|
+
)
|
|
46
|
+
|
|
47
|
+
_SLOT_LAYERS: Mapping[StateSlot, LayerId] = MappingProxyType(
|
|
48
|
+
{
|
|
49
|
+
StateSlot.BACKGROUND: LayerId.BACKGROUND_STATE,
|
|
50
|
+
StateSlot.PRIMARY: LayerId.PRIMARY_STATE,
|
|
51
|
+
}
|
|
52
|
+
)
|
|
53
|
+
|
|
54
|
+
_KIND_LAYERS: Mapping[DefinitionKind, LayerId] = MappingProxyType(
|
|
55
|
+
{
|
|
56
|
+
DefinitionKind.CONTROLLED_OVERLAY: LayerId.CONTROLLED_OVERLAY,
|
|
57
|
+
DefinitionKind.TIMED_OVERLAY: LayerId.TIMED_OVERLAY,
|
|
58
|
+
DefinitionKind.EVENT: LayerId.EVENT,
|
|
59
|
+
}
|
|
60
|
+
)
|
|
61
|
+
|
|
62
|
+
|
|
63
|
+
def layer_for(definition: DefinitionBase, *, slot: StateSlot | None = None) -> LayerId:
|
|
64
|
+
"""The one layer this definition may occupy.
|
|
65
|
+
|
|
66
|
+
``slot`` applies to states only, defaults to the definition's first declared
|
|
67
|
+
slot, and must be one the definition allows.
|
|
68
|
+
"""
|
|
69
|
+
if definition.kind is not DefinitionKind.STATE:
|
|
70
|
+
if slot is not None:
|
|
71
|
+
raise CommandError(
|
|
72
|
+
f"{definition.id!r} is a {definition.definition_type.value}; "
|
|
73
|
+
"only states have a slot"
|
|
74
|
+
)
|
|
75
|
+
return _KIND_LAYERS[definition.kind]
|
|
76
|
+
|
|
77
|
+
allowed = definition.slots # type: ignore[attr-defined]
|
|
78
|
+
chosen = allowed[0] if slot is None else slot
|
|
79
|
+
if chosen not in allowed:
|
|
80
|
+
permitted = ", ".join(item.value for item in allowed)
|
|
81
|
+
raise CommandError(
|
|
82
|
+
f"State {definition.id!r} does not allow the {chosen.value} slot; "
|
|
83
|
+
f"it declares: {permitted}"
|
|
84
|
+
)
|
|
85
|
+
return _SLOT_LAYERS[chosen]
|
|
86
|
+
|
|
87
|
+
|
|
88
|
+
def slot_for(layer: LayerId) -> StateSlot | None:
|
|
89
|
+
"""The state slot a layer represents, or ``None`` for non-state layers."""
|
|
90
|
+
for slot, mapped in _SLOT_LAYERS.items():
|
|
91
|
+
if mapped is layer:
|
|
92
|
+
return slot
|
|
93
|
+
return None
|
|
94
|
+
|
|
95
|
+
|
|
96
|
+
def parse_state_slot(value: str | StateSlot) -> StateSlot:
|
|
97
|
+
if isinstance(value, StateSlot):
|
|
98
|
+
return value
|
|
99
|
+
normalized = str(value or "").strip().lower()
|
|
100
|
+
for slot in StateSlot:
|
|
101
|
+
if slot.value == normalized:
|
|
102
|
+
return slot
|
|
103
|
+
permitted = ", ".join(slot.value for slot in StateSlot)
|
|
104
|
+
raise CommandError(f"Unknown state slot {value!r}. Expected one of: {permitted}")
|
|
105
|
+
|
|
106
|
+
|
|
107
|
+
__all__ = [
|
|
108
|
+
"COMPOSITION_ORDER",
|
|
109
|
+
"LAYER_PRIORITIES",
|
|
110
|
+
"LayerId",
|
|
111
|
+
"layer_for",
|
|
112
|
+
"parse_state_slot",
|
|
113
|
+
"slot_for",
|
|
114
|
+
]
|