""".. tab:: 中文 Sphinx 文档插件用于为任务生成文档。.. tab:: 英文 Sphinx documentation plugin used to document tasks.简介============Introduction用法-----Usage.. tab:: 中文 Celery 的 Sphinx 扩展要求 Sphinx 版本为 2.0 或更高。 在你的 :file:`docs/conf.py` 配置模块中添加该扩展: .. code-block:: python extensions = (..., 'celery.contrib.sphinx') 如果你希望在参考文档中更改任务的前缀,可以设置 ``celery_task_prefix`` 配置项: .. code-block:: python celery_task_prefix = '(task)' # < 默认值 安装该扩展后,`autodoc` 会自动识别使用装饰器标记的任务对象(例如在使用 automodule 指令时), 并正确生成文档(同时添加 ``(task)`` 前缀)。你也可以使用 `:task:proj.tasks.add` 语法引用任务。 你也可以使用 ``.. autotask::`` 来手动为某个任务编写文档。.. tab:: 英文 The Celery extension for Sphinx requires Sphinx 2.0 or later. Add the extension to your :file:`docs/conf.py` configuration module: .. code-block:: python extensions = (..., 'celery.contrib.sphinx') If you'd like to change the prefix for tasks in reference documentation then you can change the ``celery_task_prefix`` configuration value: .. code-block:: python celery_task_prefix = '(task)' # < default With the extension installed `autodoc` will automatically find task decorated objects (e.g. when using the automodule directive) and generate the correct (as well as add a ``(task)`` prefix), and you can also refer to the tasks using `:task:proj.tasks.add` syntax. Use ``.. autotask::`` to alternatively manually document a task."""frominspectimportsignaturefromdocutilsimportnodesfromsphinx.domains.pythonimportPyFunctionfromsphinx.ext.autodocimportFunctionDocumenterfromcelery.app.taskimportBaseTask
[文档]defcheck_module(self):# Normally checks if *self.object* is really defined in the module# given by *self.modname*. But since functions decorated with the @task# decorator are instances living in the celery.local, we have to check# the wrapped function instead.wrapped=getattr(self.object,"__wrapped__",None)ifwrappedandgetattr(wrapped,"__module__")==self.modname:returnTruereturnsuper().check_module()
[文档]defautodoc_skip_member_handler(app,what,name,obj,skip,options):"""Handler for autodoc-skip-member event."""# Celery tasks created with the @task decorator have the property# that *obj.__doc__* and *obj.__class__.__doc__* are equal, which# trips up the logic in sphinx.ext.autodoc that is supposed to# suppress repetition of class documentation in an instance of the# class. This overrides that behavior.ifisinstance(obj,BaseTask)andgetattr(obj,"__wrapped__"):ifskip:returnFalsereturnNone