Skip to content

Commit 6065131

Browse files
committed
Adds Sphinx extension for autodoc documentation of tasks: celery.contrib.sphinx. Closes celery#1833
1 parent 5f48a27 commit 6065131

File tree

4 files changed

+78
-0
lines changed

4 files changed

+78
-0
lines changed

celery/contrib/sphinx.py

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
# -*- coding: utf-8 -*-
2+
"""
3+
celery.contrib.sphinx
4+
=====================
5+
6+
Sphinx documentation plugin
7+
8+
**Usage**
9+
10+
Add the extension to your :file:`docs/conf.py` configuration module:
11+
12+
.. code-block:: python
13+
14+
extensions = (...,
15+
'celery.contrib.sphinx')
16+
17+
If you would like to change the prefix for tasks in reference documentation
18+
then you can change the ``celery_task_prefix`` configuration value:
19+
20+
.. code-block:: python
21+
22+
celery_task_prefix = '(task)' # < default
23+
24+
25+
With the extension installed `autodoc` will automatically find
26+
task decorated objects and generate the correct (as well as add a ``(task)``
27+
prefix), and you can also refer to the tasks using `:task:proj.tasks.add` syntax.
28+
29+
Use ``.. autotask::`` to manually document a task.
30+
31+
"""
32+
from __future__ import absolute_import
33+
34+
from inspect import ArgSpec, formatargspec, getargs, getargspec
35+
36+
from sphinx.domains.python import PyModulelevel
37+
from sphinx.ext.autodoc import FunctionDocumenter
38+
39+
from celery.app.task import BaseTask
40+
41+
42+
class TaskDocumenter(FunctionDocumenter):
43+
objtype = 'task'
44+
member_order = 11
45+
46+
@classmethod
47+
def can_document_member(cls, member, membername, isattr, parent):
48+
return isinstance(member, BaseTask) and getattr(member, '__wrapped__')
49+
50+
def format_args(self):
51+
wrapped = getattr(self.object, '__wrapped__')
52+
if wrapped is not None:
53+
argspec = getargspec(wrapped)
54+
fmt = formatargspec(*argspec)
55+
fmt = fmt.replace('\\', '\\\\')
56+
return fmt
57+
return ''
58+
59+
def document_members(self, all_members=False):
60+
pass
61+
62+
63+
class TaskDirective(PyModulelevel):
64+
65+
def get_signature_prefix(self, sig):
66+
return self.env.config.celery_task_prefix
67+
68+
69+
def setup(app):
70+
app.add_autodocumenter(TaskDocumenter)
71+
app.domains['py'].directives['task'] = TaskDirective
72+
app.add_config_value('celery_task_prefix', '(task)', True)

docs/conf.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
'sphinx.ext.viewcode',
2222
'sphinx.ext.coverage',
2323
'sphinx.ext.intersphinx',
24+
'celery.contrib.sphinx',
2425
'sphinxcontrib.issuetracker',
2526
'celerydocs']
2627

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
.. currentmodule:: celery.contrib.sphinx
2+
3+
.. automodule:: celery.contrib.sphinx
4+
:members:

docs/reference/index.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@
3737
celery.contrib.abortable
3838
celery.contrib.batches
3939
celery.contrib.migrate
40+
celery.contrib.sphinx
4041
celery.contrib.rdb
4142
celery.contrib.methods
4243
celery.events

0 commit comments

Comments
 (0)