sphinx.py 1.9 KB

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