sphinx.py 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. # -*- coding: utf-8 -*-
  2. """Sphinx documentation plugin used to document tasks.
  3. Introduction
  4. ============
  5. Usage
  6. -----
  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'd 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, unicode_literals
  22. from inspect import formatargspec
  23. from sphinx.domains.python import PyModulelevel
  24. from sphinx.ext.autodoc import FunctionDocumenter
  25. from celery.app.task import BaseTask
  26. from celery.five import getfullargspec
  27. class TaskDocumenter(FunctionDocumenter):
  28. """Document task definitions."""
  29. objtype = 'task'
  30. member_order = 11
  31. @classmethod
  32. def can_document_member(cls, member, membername, isattr, parent):
  33. return isinstance(member, BaseTask) and getattr(member, '__wrapped__')
  34. def format_args(self):
  35. wrapped = getattr(self.object, '__wrapped__', None)
  36. if wrapped is not None:
  37. argspec = getfullargspec(wrapped)
  38. fmt = formatargspec(*argspec)
  39. fmt = fmt.replace('\\', '\\\\')
  40. return fmt
  41. return ''
  42. def document_members(self, all_members=False):
  43. pass
  44. class TaskDirective(PyModulelevel):
  45. """Sphinx task directive."""
  46. def get_signature_prefix(self, sig):
  47. return self.env.config.celery_task_prefix
  48. def setup(app):
  49. """Setup Sphinx extension."""
  50. app.add_autodocumenter(TaskDocumenter)
  51. app.domains['py'].directives['task'] = TaskDirective
  52. app.add_config_value('celery_task_prefix', '(task)', True)