Coverage for core / src / sensorkit / sensor / binding.py: 94%
36 statements
« prev ^ index » next coverage.py v7.13.5, created at 2026-11-24 01:04 +0000
« prev ^ index » next coverage.py v7.13.5, created at 2026-11-24 01:04 +0000
1# SPDX-License-Identifier: Apache-2.0
2"""Bind reported capabilities to placements.
4Binding requires capability details for every configured device and checks
5its declared traits. Established traits come from reported commands and keywords;
6assertions do not restrict which capabilities compilation may use.
7"""
9from __future__ import annotations
11from collections.abc import Mapping
13from sensorkit.common.keyword import KeywordDict
14from sensorkit.core.entity import DeviceDetails
15from sensorkit.core.trait import get_trait, match_traits
16from sensorkit.sensor.topology import (
17 Device,
18 Placement,
19 Selector,
20 Topology,
21)
24class BoundSensor:
25 """A topology with reported device details and established traits.
27 Selections, routing and graph compilation read these facts. `bind` validates
28 capability coverage and trait assertions. Direct construction skips validation.
30 `device_keywords` supplies device keyword values for planning. `None`
31 means no mapping was supplied. The values may describe capabilities or
32 current state and may become stale before execution.
33 """
35 def __init__(
36 self,
37 topology: Topology,
38 details: Mapping[str, DeviceDetails],
39 *,
40 device_keywords: Mapping[str, KeywordDict] | None = None,
41 ):
42 self.topology = topology
43 self.details = dict(details)
44 self._device_keywords = device_keywords
46 # Keep every matching trait, including multiple archetypes.
47 self._traits = {
48 placement: frozenset(t.name for t in match_traits(found))
49 for placement in topology.placements()
50 if (found := self.details.get(placement.device)) is not None
51 }
53 @classmethod
54 def bind(cls, topology: Topology, details: Mapping[str, DeviceDetails]) -> BoundSensor:
55 """Check device capability details and trait assertions, then return a sensor.
57 Trait matching uses the same capability predicates as device
58 declarations. Unsupported workflow commands are found later, during
59 compilation.
61 Raises:
62 ValueError: Capability details are missing, or a declared trait is
63 unregistered or not satisfied by its device.
64 """
65 silent = tuple(p for p in topology.placements() if p.device not in details)
67 if silent:
68 raise ValueError(
69 "the structure names devices that reported nothing: "
70 + ", ".join(f"'{p.device}' at '{p.path_str}'" for p in silent)
71 )
73 sensor = cls(topology, details)
74 unmet = tuple(
75 f"'{p.device}' at '{p.path_str}' {reason}"
76 for p in topology.placements()
77 for reason in _unmet(topology.record(p), sensor.traits(p))
78 )
80 if unmet:
81 raise ValueError("declared traits were not established: " + ", ".join(unmet))
83 return sensor
85 def supported_commands(self, device: str) -> frozenset[str]:
86 """Return reported command identifiers, or an empty set for an
87 unknown device.
88 """
89 found = self.details.get(device)
91 return found.supported_commands if found else frozenset()
93 def published_keywords(self, device: str) -> frozenset[str]:
94 """Return reported keyword identifiers, or an empty set for an
95 unknown device.
96 """
97 found = self.details.get(device)
99 return found.published_keywords if found else frozenset()
101 def device_keywords(self, device: str) -> KeywordDict | None:
102 """Return the supplied device keywords, or `None` if no mapping was supplied.
104 A device missing from a supplied mapping has an empty keyword set.
105 Supplied keyword values are returned without copying.
106 """
107 if self._device_keywords is None:
108 return None
110 return self._device_keywords.get(device, KeywordDict())
112 def traits(self, placement: Placement) -> frozenset[str]:
113 """Return established traits, or an empty set for an unknown
114 placement.
115 """
116 return self._traits.get(placement, frozenset())
119def _unmet(record: Device | Selector, established: frozenset[str]) -> tuple[str, ...]:
120 """Describe declared traits that binding could not establish.
122 Distinguish unregistered names from registered traits the device lacks.
123 """
124 return tuple(
125 f"declares trait '{name}', which is not registered"
126 if get_trait(name) is None
127 else f"declares trait '{name}', which its device does not satisfy"
128 for name in record.traits
129 if name not in established
130 )