更新时间:2026-08-05 GMT+08:00
分享

Entry Point插件扩展机制概述

R2C SDK使用Python标准库importlib.metadata.entry_points机制发现和加载第三方插件。第三方开发者只需在pyproject.toml中声明entry_point,无需修改SDK核心代码。

表1 可扩展入口

Entry Point Group

注册内容

用途

r2c_sdk.adapters

工厂函数

接入新的机器人硬件后端。

r2c_sdk.transformers

IValueTransformer子类

自定义数据转换逻辑。

核心概念

Entry Point插件扩展机制有以下三类核心概念:

AdapterFactory

entry_point注册的是工厂函数,不是适配器类本身。

签名

def create_xxx_adapter(
    config: Mapping[str, Any], **extra_kwargs: Any
) -> IRobotHardwareAdapter:
    ...
  • config:YAML中hardware.config的dict。
  • extra_kwargs:运行时注入参数(保留字段)。

AdapterRegistry

r2c_sdk.robots.robot_factory.AdapterRegistry类级缓存:

  • 懒加载:仅在首次get(type_name)时import对应工厂。
  • 错误缓存:加载失败后缓存ValueError,后续直接抛错。
  • 可用类型:AdapterRegistry.available_types()列出所有已注册适配器类型。

TransformerRegistry

r2c_sdk.core.transformers.TransformerRegistry内置优先:

  • 优先级:调用方overrides > 内置builtins > entry_points。
  • 冲突检测:模块导入时自动检测同名冲突,warning日志提示。
  • 内置优先:同名时内置覆盖外部。

Entry Point插件扩展机制向后兼容

  • hardware.type:custom + class_path,继续可用(DeprecationWarning)
  • xxx_config格式:继续可用(DeprecationWarning)
  • class_path 安全检查:load_class()的安全策略不变
  • entry_point 路径:安全性由pip安装环节保证,不走 load_class()

相关文档