feat: update discover() for pattern caps, match_fn, rt_filter gating
Registry changes from dict[str,Capability] to dict[str,list[Capability]]. discover() gains pattern_caps parameter (fallback for unmatched hrefs) and gates each candidate on rt_filter and match_fn before binding. Duplicate hrefs in registry allowed only if all caps set a discriminator.
This commit is contained in:
1 parent
211fcd1324
commit
b10cfad194
7 files changed
+176
-32
No files matched your search
@@ -7,7 +7,7 @@ Unknown hrefs are a coverage gap, logged at debug and skipped.
|
||||
from __future__ import annotations
|
||||
|
||||
from dataclasses import dataclass
|
||||
from typing import Callable, Optional
|
||||
from typing import Callable, Iterable, Optional
|
||||
|
||||
from .capability import Capability
|
||||
from .entities import SamsungEntityDescription
|
||||
@@ -30,20 +30,52 @@ def instance_suffix(href: str) -> str:
|
||||
return ''
|
||||
|
||||
|
||||
def discover(resources: dict[str, dict],
|
||||
registry: dict[str, 'Capability'],
|
||||
log: Optional[Callable[[str], None]] = None) -> list[BoundEntity]:
|
||||
def discover(
|
||||
resources: dict[str, dict],
|
||||
registry: dict[str, list[Capability]],
|
||||
pattern_caps: Iterable[Capability] = (),
|
||||
log: Optional[Callable[[str], None]] = None,
|
||||
) -> list[BoundEntity]:
|
||||
out: list[BoundEntity] = []
|
||||
bound_hrefs: set[str] = set()
|
||||
|
||||
for href, rep in resources.items():
|
||||
if not isinstance(rep, dict):
|
||||
continue
|
||||
cap = registry.get(href)
|
||||
if cap is None:
|
||||
if log is not None:
|
||||
log(f"unknown resource {href}")
|
||||
rts = rep.get('rt') or ()
|
||||
caps = registry.get(href) or []
|
||||
matched = False
|
||||
|
||||
for cap in caps:
|
||||
if cap.rt_filter is not None and cap.rt_filter not in rts:
|
||||
continue
|
||||
if cap.match_fn is not None and not cap.match_fn(rep, resources):
|
||||
continue
|
||||
inst = instance_suffix(href)
|
||||
for desc in cap.entities:
|
||||
out.append(BoundEntity(href=href, capability=cap,
|
||||
desc=desc, instance=inst))
|
||||
matched = True
|
||||
|
||||
if matched:
|
||||
bound_hrefs.add(href)
|
||||
continue
|
||||
inst = instance_suffix(href)
|
||||
for desc in cap.entities:
|
||||
out.append(BoundEntity(href=href, capability=cap,
|
||||
desc=desc, instance=inst))
|
||||
|
||||
# Pattern cap fallback — first matching pattern wins
|
||||
for cap in pattern_caps:
|
||||
if cap.rt_filter is not None and cap.rt_filter not in rts:
|
||||
continue
|
||||
if cap.match_fn is not None and not cap.match_fn(rep, resources):
|
||||
continue
|
||||
inst = instance_suffix(href)
|
||||
key = cap.key_fn(href) if cap.key_fn else None
|
||||
for desc in cap.entities:
|
||||
out.append(BoundEntity(href=href, capability=cap, desc=desc,
|
||||
instance=inst, key_override=key))
|
||||
matched = True
|
||||
break
|
||||
|
||||
if not matched and log is not None:
|
||||
log(f"unknown resource {href}")
|
||||
|
||||
return out
|
||||
@@ -1,27 +1,46 @@
|
||||
"""Global CAPABILITIES registry: href -> Capability.
|
||||
"""Global CAPABILITIES registry: href -> list[Capability].
|
||||
|
||||
Built from the full list of Capability objects in the capabilities package.
|
||||
Consumed by discover() at connection time to bind device resources to entities.
|
||||
|
||||
Raises ValueError at import if any href appears in multiple capabilities.
|
||||
Raises ValueError at import if any href group contains an unfiltered cap
|
||||
alongside other caps (i.e., a cap with neither rt_filter nor match_fn set
|
||||
in a group with multiple caps).
|
||||
"""
|
||||
from .capabilities import ALL
|
||||
from .capability import Capability
|
||||
|
||||
|
||||
def _build() -> dict[str, Capability]:
|
||||
"""Build the registry, raising ValueError if any href is duplicated.
|
||||
def _build() -> dict[str, list[Capability]]:
|
||||
"""Build the registry, raising ValueError for invalid duplicate hrefs.
|
||||
|
||||
Duplicates are allowed only when every cap in the group has at least one
|
||||
of rt_filter or match_fn set. An unfiltered cap sharing an href with
|
||||
another cap would be ambiguous.
|
||||
|
||||
Skips capabilities with href=None (pattern capabilities, handled elsewhere).
|
||||
"""
|
||||
out: dict[str, Capability] = {}
|
||||
out: dict[str, list[Capability]] = {}
|
||||
for cap in ALL:
|
||||
if cap.href is None:
|
||||
continue
|
||||
if cap.href in out:
|
||||
raise ValueError(f"duplicate capability href: {cap.href}")
|
||||
out[cap.href] = cap
|
||||
if cap.href not in out:
|
||||
out[cap.href] = [cap]
|
||||
else:
|
||||
out[cap.href].append(cap)
|
||||
|
||||
# Validate: every group with >1 cap must have all caps filtered
|
||||
for href, caps in out.items():
|
||||
if len(caps) > 1:
|
||||
unfiltered = [c for c in caps
|
||||
if c.rt_filter is None and c.match_fn is None]
|
||||
if unfiltered:
|
||||
raise ValueError(
|
||||
f"duplicate capability href {href!r} has unfiltered cap(s); "
|
||||
f"each cap sharing an href must set rt_filter or match_fn"
|
||||
)
|
||||
|
||||
return out
|
||||
|
||||
|
||||
CAPABILITIES: dict[str, Capability] = _build()
|
||||
CAPABILITIES: dict[str, list[Capability]] = _build()
|
||||
Reference in new issue
Block a user