Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 21 additions & 5 deletions dimos/hardware/whole_body/registry.py
Original file line number Diff line number Diff line change
Expand Up @@ -71,15 +71,19 @@ def available(self) -> list[str]:
return sorted(self._adapters.keys())

def discover(self) -> None:
"""Discover and register whole-body hardware adapters.

Walks the hardware whole-body package recursively looking for
``adapter.py`` modules that provide a ``register(registry)`` function.
"""
"""Discover and register whole-body adapters from the hardware and sim roots."""
import dimos.hardware.whole_body as hw_pkg

self._discover_in("dimos.hardware.whole_body", hw_pkg.__path__[0], max_depth=2)

try:
import dimos.simulation.adapters.whole_body as sim_pkg
except ImportError as e:
logger.warning(f"Skipping sim whole-body adapters: {e}")
return
for sim_root in sim_pkg.__path__:
self._discover_flat("dimos.simulation.adapters.whole_body", sim_root)

def _discover_in(self, pkg_path: str, dir_path: str, *, max_depth: int) -> None:
for entry in sorted(os.listdir(dir_path)):
entry_path = os.path.join(dir_path, entry)
Expand All @@ -99,6 +103,18 @@ def _discover_in(self, pkg_path: str, dir_path: str, *, max_depth: int) -> None:
if hasattr(mod, "register"):
mod.register(self)

def _discover_flat(self, pkg_path: str, dir_path: str) -> None:
for entry in sorted(os.listdir(dir_path)):
if not entry.endswith(".py") or entry.startswith(("_", ".")):
continue
try:
mod = importlib.import_module(f"{pkg_path}.{entry[:-3]}")
except ImportError as e:
logger.warning(f"Skipping whole-body adapter {entry}: {e}")
continue
if hasattr(mod, "register"):
mod.register(self)
Comment on lines +106 to +116

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2 The per-module except ImportError in _discover_flat will not catch Exception subclasses raised by a broken mod.register(self) call. If any sim adapter's register() raises (e.g., a constructor call with a bad default), the exception propagates out of _discover_flat, out of discover(), and ultimately fails the module-level import of registry.py — crashing the whole application. Consider a guarded call here, consistent with the "graceful skip" intent of the rest of the method.

Suggested change
def _discover_flat(self, pkg_path: str, dir_path: str) -> None:
for entry in sorted(os.listdir(dir_path)):
if not entry.endswith(".py") or entry.startswith(("_", ".")):
continue
try:
mod = importlib.import_module(f"{pkg_path}.{entry[:-3]}")
except ImportError as e:
logger.warning(f"Skipping whole-body adapter {entry}: {e}")
continue
if hasattr(mod, "register"):
mod.register(self)
if hasattr(mod, "register"):
try:
mod.register(self)
except Exception as e: # noqa: BLE001
logger.warning(f"Skipping whole-body adapter {entry} (register() failed): {e}")



whole_body_adapter_registry = WholeBodyAdapterRegistry()
whole_body_adapter_registry.discover()
Loading