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

1# SPDX-License-Identifier: Apache-2.0 

2"""Bind reported capabilities to placements. 

3 

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""" 

8 

9from __future__ import annotations 

10 

11from collections.abc import Mapping 

12 

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) 

22 

23 

24class BoundSensor: 

25 """A topology with reported device details and established traits. 

26 

27 Selections, routing and graph compilation read these facts. `bind` validates 

28 capability coverage and trait assertions. Direct construction skips validation. 

29 

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 """ 

34 

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 

45 

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 } 

52 

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. 

56 

57 Trait matching uses the same capability predicates as device 

58 declarations. Unsupported workflow commands are found later, during 

59 compilation. 

60 

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) 

66 

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 ) 

72 

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 ) 

79 

80 if unmet: 

81 raise ValueError("declared traits were not established: " + ", ".join(unmet)) 

82 

83 return sensor 

84 

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) 

90 

91 return found.supported_commands if found else frozenset() 

92 

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) 

98 

99 return found.published_keywords if found else frozenset() 

100 

101 def device_keywords(self, device: str) -> KeywordDict | None: 

102 """Return the supplied device keywords, or `None` if no mapping was supplied. 

103 

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 

109 

110 return self._device_keywords.get(device, KeywordDict()) 

111 

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()) 

117 

118 

119def _unmet(record: Device | Selector, established: frozenset[str]) -> tuple[str, ...]: 

120 """Describe declared traits that binding could not establish. 

121 

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 )