sphinx.py 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  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 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, 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. objtype = 'task'
  29. member_order = 11
  30. @classmethod
  31. def can_document_member(cls, member, membername, isattr, parent):
  32. return isinstance(member, BaseTask) and getattr(member, '__wrapped__')
  33. def format_args(self):
  34. wrapped = getattr(self.object, '__wrapped__', None)
  35. if wrapped is not None:
  36. argspec = getfullargspec(wrapped)
  37. fmt = formatargspec(*argspec)
  38. fmt = fmt.replace('\\', '\\\\')
  39. return fmt
  40. return ''
  41. def document_members(self, all_members=False):
  42. pass
  43. class TaskDirective(PyModulelevel):
  44. def get_signature_prefix(self, sig):
  45. return self.env.config.celery_task_prefix
  46. def setup(app):
  47. app.add_autodocumenter(TaskDocumenter)
  48. app.domains['py'].directives['task'] = TaskDirective
  49. app.add_config_value('celery_task_prefix', '(task)', True)