Skip to content

Commit 6c6b8a5

Browse files
April-Yjjespressif-bot
authored andcommitted
docs: Add CN translation to idf-py.rst
1 parent 96ff174 commit 6c6b8a5

File tree

2 files changed

+110
-5
lines changed

2 files changed

+110
-5
lines changed

docs/en/api-guides/tools/idf-py.rst

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -369,14 +369,14 @@ The ``action_extensions`` function takes arguments ``base_actions`` (all current
369369
- ``global_options``: A list of options available globally for all commands. Each option is a dictionary with fields such as ``names``, ``help``, ``type``, ``is_flag``, ``scope``, etc.
370370
- ``global_action_callbacks``: A list of functions called once before any task execution. Each global action callback function accepts three arguments:
371371

372-
- ``ctx`` — the `click context`_,
373-
- ``global_args`` — all available global arguments,
374-
- ``tasks`` — the list of tasks to be executed. Task refer to the action / sub-command used with `idf.py`
372+
- ``ctx`` — The `click context`_
373+
- ``global_args`` — All available global arguments
374+
- ``tasks`` — The list of tasks to be executed. Task refer to the action / sub-command used with `idf.py`
375375

376376
- ``actions``: A dictionary of new subcommands. Each action has a ``callback`` function and may also include ``options``, ``arguments``, ``dependencies``, etc. Each action callback function accepts three to four arguments:
377377

378-
- ``subcommand_name`` — the name of the command (useful if multiple commands share the same callback),
379-
- ``ctx`` — the `click context`_,
378+
- ``subcommand_name`` — the name of the command (useful if multiple commands share the same callback)
379+
- ``ctx`` — the `click context`_
380380
- ``global_args`` — all available global arguments,
381381
- ``**action_args`` — (optional) arguments passed to the action
382382

docs/zh_CN/api-guides/tools/idf-py.rst

Lines changed: 105 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -301,7 +301,112 @@ uf2 二进制文件也可以通过 :ref:`idf.py uf2 <generate-uf2-binary>` 生
301301

302302
关于参数文件的更多示例,如通过 @filename 创建配置文件概要,请参阅 :example_file:`多个构建配置示例 <build_system/cmake/multi_config/README.md>`。
303303

304+
扩展 ``idf.py``
305+
====================
306+
307+
``idf.py`` 支持扩展功能。通过项目中的扩展文件以及参与构建的组件中的扩展文件,可以增加额外的子命令、全局选项和回调函数;通过暴露入口点的外部 Python 包,可以提供新的扩展功能。
308+
309+
- **参与构建的组件**:在项目根目录,或注册在项目 ``CMakeLists.txt`` 中的组件根目录,放置名为 ``idf_ext.py`` 的文件,该文件会在项目配置完成后得到识别。运行 ``idf.py build`` 或 ``idf.py reconfigure``,新添加的命令即可生效。
310+
- **Python 入口点**:对于任何已安装的 Python 包,在 ``idf_extension`` 组中定义入口点后,就可以提供扩展功能。只要安装了 Python 包就可以使用扩展功能,无需重新构建项目。
311+
312+
.. important::
313+
314+
扩展不能定义与 ``idf.py`` 命令同名的子命令或选项。系统会检查自定义的动作和选项名称是否存在冲突,不允许覆盖默认命令,如有冲突会打印警告。对于 Python 入口点,必须使用唯一标识符,否则会忽略重复的入口点名称并发出警告。
315+
316+
扩展文件示例
317+
----------------------
318+
319+
扩展文件需要定义一个 ``action_extensions`` 函数,用于返回扩展的动作或选项。组件扩展 ``idf_ext.py`` 和基于包的扩展(例如 ``<package_name>_ext.py``)使用相同的结构,如下所示:
320+
321+
.. code-block:: python
322+
323+
from typing import Any
324+
import click
325+
326+
def action_extensions(base_actions: dict, project_path: str) -> dict:
327+
def hello_test(subcommand_name: str, ctx: click.Context, global_args: dict, **action_args: Any) -> None:
328+
message = action_args.get('message')
329+
print(f"Running action: {subcommand_name}. Message: {message}")
330+
331+
def global_callback_detail(ctx: click.Context, global_args: dict, tasks: list) -> None:
332+
if getattr(global_args, 'detail', False):
333+
print(f"About to execute {len(tasks)} task(s): {[t.name for t in tasks]}")
334+
335+
return {
336+
"version": "1",
337+
"global_options": [
338+
{
339+
"names": ["--detail", "-d"],
340+
"is_flag": True,
341+
"help": "Enable detailed output",
342+
}
343+
],
344+
"global_action_callbacks": [global_callback_detail],
345+
"actions": {
346+
"hello": {
347+
"callback": hello_test,
348+
"short_help": "Hello from component",
349+
"help": "Test command from component extension",
350+
"options": [
351+
{
352+
"names": ["--message", "-m"],
353+
"help": "Custom message to display",
354+
"default": "Hi there!",
355+
"type": str,
356+
}
357+
]
358+
},
359+
},
360+
}
361+
362+
363+
扩展 API 参考
364+
-----------------------
365+
366+
``action_extensions`` 函数接收两个参数: ``base_actions`` 表示当前已注册的所有命令, ``project_path`` 表示项目的绝对路径。该函数返回一个包含最多四个键的字典:
367+
368+
- ``version``:表示扩展接口版本。当前 API 版本为 ``1``。此键为必填项。
369+
- ``global_options``:一组全局选项,适用于所有命令。每个选项都是一个字典,包含 ``names``、 ``help``、 ``type``、 ``is_flag``、 ``scope`` 等字段。
370+
- ``global_action_callbacks``:表示一组全局回调函数,在执行任何任务之前都会调用一次。每个全局回调函数接受三个参数:
371+
372+
- ``ctx``:即 `click context`_
373+
- ``global_args``:所有可用的全局参数
374+
- ``tasks``:将要执行的任务列表。任务指的是运行 ``idf.py`` 时所调用的具体动作或子命令
375+
376+
- ``actions``:子命令字典,用于定义新的子命令。每个子命令都有一个 ``callback`` 函数,并且可以包含 ``options``、 ``arguments``、 ``dependencies`` 等。每个回调函数接受三到四个参数:
377+
378+
- ``subcommand_name``:命令的名称(在多个命令共享同一回调时很有用)
379+
- ``ctx``:即 `click context`_
380+
- ``global_args``:所有可用的全局参数
381+
- ``**action_args``:传递给该子命令的具体参数,可选
382+
383+
基本用法示例
384+
--------------------
385+
386+
1) **通过项目组件提供扩展**
387+
388+
在项目根目录或某个已注册的组件目录下创建 ``idf_ext.py`` (例如 ``components/my_component/idf_ext.py`` )。实现内容可参考上面的扩展文件示例。
389+
390+
运行 ``idf.py build`` 或 ``idf.py reconfigure`` 加载新命令,然后执行 ``idf.py --help`` 即可看到新扩展。
391+
392+
2) **通过 Python 包入口点提供扩展**
393+
394+
使用上述扩展文件示例,在名为 ``<package_name>_ext.py`` 的模块中实现扩展,并通过 ``idf_extension`` 入口点组暴露 ``action_extensions`` 函数。例如,在 ``pyproject.toml`` 中配置:
395+
396+
.. code-block:: TOML
397+
398+
[project]
399+
name = "my_comp"
400+
version = "0.1.0"
401+
402+
[project.entry-points.idf_extension]
403+
my_pkg_ext = "my_component.my_ext:action_extensions"
404+
405+
406+
将该包安装到与 ``idf.py`` 相同的 Python 环境中(例如在包目录下执行 ``pip install -e .``)。建议使用唯一的模块名(例如 ``<package_name>_ext.py``)避免命名冲突。安装成功后,运行 ``idf.py --help`` 就可以看到新扩展命令。
407+
304408
.. _cmake: https://cmake.org
305409
.. _ninja: https://ninja-build.org
306410
.. _esptool.py: https://github.com/espressif/esptool/#readme
307411
.. _CCache: https://ccache.dev/
412+
.. _click context: https://click.palletsprojects.com/en/stable/api/#context

0 commit comments

Comments
 (0)