示例插件

SDK 附带的示例插件说明。

PyriteSDK 在 examples/ 目录下附带多个示例插件,覆盖所有插件类型和常用场景。每个示例均包含完整的 __main__.pyplugin.toml(manifest v2)。

示例列表

示例类型说明
debug_enhanced_pluginUiPlugin大纲(Outline)与设备变量检查(VariableInspector)
service_pluginServicePlugin文件监听后台服务
theme_pluginDataPluginNord 主题贡献
i18n_pluginDataPlugin英语语言包贡献
stubs_pluginDataPluginMicroPython 类型存根贡献

debug_enhanced_plugin

UI 插件的综合示例,演示大纲视图(native.outline)与设备变量检查器(native.variableInspector)、标签页、命令、菜单和上下文菜单的完整用法。

examples/debug_enhanced_plugin/
├── __main__.py
├── outline.py
├── device_variables.py
├── plugin.toml
└── assets/icons/

关键特性:

  • 声明两个视图:debug-enhanced.outlinenative.outline)与 debug-enhanced.device-variablesnative.variableInspector
  • 使用 self.documents.symbols() 获取文档符号并渲染为 OutlineItem
  • 使用 self.runtime 查询设备作用域与变量,分页加载(start/count
  • 使用 ViewAction 添加 AppBar 操作,ContextMenu/MenuItem 实现右键菜单
  • 使用 self.tabs.create_view() 打开主视图标签页

service_plugin

后台文件监听服务。启动后扫描工作区根目录下的文件列表。

from pyrite_sdk.core.plugin import ServicePlugin


class FileWatcherPlugin(ServicePlugin):
    def on_start(self):
        self._watching = True
        self._scan_files()

    def _scan_files(self):
        self.file.get_root_dir(lambda **cb: self._on_root_dir(cb))

    def _on_root_dir(self, result):
        root = result.get("path", "")
        if root:
            print(f"workspace root = {root}")
            self.file.get_file_list("/", lambda **cb: self._on_file_list(cb))

theme_plugin

Nord 配色主题。DataPlugin 类型,贡献后自动退出。

from pyrite_sdk.core.plugin import DataPlugin


class NordThemePlugin(DataPlugin):
    def on_contribute(self):
        self.theme.contribute(
            "nord",
            {
                "color.primary": "#5E81AC",
                "color.onPrimary": "#ECEFF4",
                "color.surface": "#ECEFF4",
                "color.onSurface": "#2E3440",
                "dark.color.primary": "#88C0D0",
                "dark.color.onPrimary": "#2E3440",
                "dark.color.surface": "#2E3440",
                "dark.color.onSurface": "#ECEFF4",
            },
        )


plugin = NordThemePlugin()
plugin.run_once()

i18n_plugin

英语语言包。使用 I18nKey 枚举确保键名一致。

from pyrite_sdk.api.data import I18nKey
from pyrite_sdk.core.plugin import DataPlugin


class LanguagePackPlugin(DataPlugin):
    def on_contribute(self):
        self.i18n.contribute(
            "en",
            {
                I18nKey.NAV_FILES.key: "Files",
                I18nKey.NAV_DEVICES.key: "Devices",
                I18nKey.NAV_GIT.key: "Git",
                I18nKey.NAV_PLUGINS.key: "Plugins",
                I18nKey.NAV_SETTINGS.key: "Settings",
                I18nKey.SETTINGS_TITLE.key: "Settings",
                I18nKey.GIT_COMMIT.key: "Commit",
            },
        )


plugin = LanguagePackPlugin()
plugin.run_once()

stubs_plugin

MicroPython 类型存根贡献。注册后自动配置 Stubs Layers。

from pyrite_sdk.core.plugin import DataPlugin


class MicroPythonStubsPlugin(DataPlugin):
    def on_contribute(self):
        provider_id = "micropython-stubs-example"
        root = self.path.plugin() / "stubs"
        self.stubs.contribute(
            provider_id=provider_id,
            kind="micropython",
            version="1.0.0",
            profiles=[
                {"id": "generic", "label": "MicroPython Generic", "path": str(root / "generic"), "priority": 10},
                {"id": "esp32", "label": "MicroPython ESP32", "path": str(root / "esp32"), "priority": 20},
            ],
            aliases=["micropython"],
        )


plugin = MicroPythonStubsPlugin()
plugin.run_once()

运行示例

在示例目录中打包并安装到 IDE:

cd examples/theme_plugin
pyrsdk package src --platform Windows

然后从 build/ 目录将 <插件ID>.zip 安装到 PyriteIDE。

创建新插件

使用脚手架命令创建:

pyrsdk create ui my_plugin

生成的 src/ 模板基于对应的插件类型,包含基本的 __main__.pyplugin.toml

On this page