celerydocs.py 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  1. from __future__ import absolute_import, unicode_literals
  2. import sys
  3. from docutils import nodes
  4. from sphinx.environment import NoUri
  5. APPATTRS = {
  6. 'amqp': 'celery.app.amqp.AMQP',
  7. 'backend': 'celery.backends.base.BaseBackend',
  8. 'conf': 'celery.app.utils.Settings',
  9. 'control': 'celery.app.control.Control',
  10. 'events': 'celery.events.Events',
  11. 'loader': 'celery.app.loaders.base.BaseLoader',
  12. 'log': 'celery.app.log.Logging',
  13. 'pool': 'kombu.connection.ConnectionPool',
  14. 'tasks': 'celery.app.registry.Registry',
  15. 'AsyncResult': 'celery.result.AsyncResult',
  16. 'ResultSet': 'celery.result.ResultSet',
  17. 'GroupResult': 'celery.result.GroupResult',
  18. 'Worker': 'celery.apps.worker.Worker',
  19. 'WorkController': 'celery.worker.WorkController',
  20. 'Beat': 'celery.apps.beat.Beat',
  21. 'Task': 'celery.app.task.Task',
  22. 'signature': 'celery.canvas.Signature',
  23. }
  24. APPDIRECT = {
  25. 'on_configure', 'on_after_configure', 'on_after_finalize',
  26. 'set_current', 'set_default', 'close', 'on_init', 'start',
  27. 'worker_main', 'task', 'gen_task_name', 'finalize',
  28. 'add_defaults', 'config_from_object', 'config_from_envvar',
  29. 'config_from_cmdline', 'setup_security', 'autodiscover_tasks',
  30. 'send_task', 'connection', 'connection_or_acquire',
  31. 'producer_or_acquire', 'prepare_config', 'now',
  32. 'select_queues', 'either', 'bugreport', 'create_task_cls',
  33. 'subclass_with_self', 'annotations', 'current_task', 'oid',
  34. 'timezone', '__reduce_keys__', 'fixups', 'finalized', 'configured',
  35. 'add_periodic_task',
  36. 'autofinalize', 'steps', 'user_options', 'main', 'clock',
  37. }
  38. APPATTRS.update({x: 'celery.Celery.{0}'.format(x) for x in APPDIRECT})
  39. ABBRS = {
  40. 'Celery': 'celery.Celery',
  41. }
  42. ABBR_EMPTY = {
  43. 'exc': 'celery.exceptions',
  44. }
  45. DEFAULT_EMPTY = 'celery.Celery'
  46. if sys.version_info[0] < 3:
  47. def bytes_if_py2(s):
  48. if isinstance(s, unicode):
  49. return s.encode()
  50. return s
  51. else:
  52. def bytes_if_py2(s): # noqa
  53. return s
  54. def typeify(S, type):
  55. if type in ('meth', 'func'):
  56. return S + '()'
  57. return S
  58. def shorten(S, newtarget, src_dict):
  59. if S.startswith('@-'):
  60. return S[2:]
  61. elif S.startswith('@'):
  62. if src_dict is APPATTRS:
  63. return '.'.join(['app', S[1:]])
  64. return S[1:]
  65. return S
  66. def get_abbr(pre, rest, type, orig=None):
  67. if pre:
  68. for d in APPATTRS, ABBRS:
  69. try:
  70. return d[pre], rest, d
  71. except KeyError:
  72. pass
  73. raise KeyError('Unknown abbreviation: {0} ({1})'.format(
  74. '.'.join([pre, rest]) if orig is None else orig, type,
  75. ))
  76. else:
  77. for d in APPATTRS, ABBRS:
  78. try:
  79. return d[rest], '', d
  80. except KeyError:
  81. pass
  82. return ABBR_EMPTY.get(type, DEFAULT_EMPTY), rest, ABBR_EMPTY
  83. def resolve(S, type):
  84. orig = S
  85. if S.startswith('@'):
  86. S = S.lstrip('@-')
  87. try:
  88. pre, rest = S.split('.', 1)
  89. except ValueError:
  90. pre, rest = '', S
  91. target, rest, src = get_abbr(pre, rest, type, orig)
  92. return '.'.join([target, rest]) if rest else target, src
  93. return S, None
  94. def pkg_of(module_fqdn):
  95. return module_fqdn.split('.', 1)[0]
  96. def basename(module_fqdn):
  97. return module_fqdn.lstrip('@').rsplit('.', -1)[-1]
  98. def modify_textnode(T, newtarget, node, src_dict, type):
  99. src = node.children[0].rawsource
  100. return nodes.Text(
  101. (typeify(basename(T), type) if '~' in src
  102. else typeify(shorten(T, newtarget, src_dict), type)),
  103. src,
  104. )
  105. def maybe_resolve_abbreviations(app, env, node, contnode):
  106. domainname = node.get('refdomain')
  107. target = node['reftarget']
  108. type = node['reftype']
  109. if target.startswith('@'):
  110. newtarget, src_dict = resolve(target, type)
  111. node['reftarget'] = newtarget
  112. # shorten text if '~' is not enabled.
  113. if len(contnode) and isinstance(contnode[0], nodes.Text):
  114. contnode[0] = modify_textnode(target, newtarget, node,
  115. src_dict, type)
  116. if domainname:
  117. try:
  118. domain = env.domains[node.get('refdomain')]
  119. except KeyError:
  120. raise NoUri
  121. return domain.resolve_xref(env, node['refdoc'], app.builder,
  122. type, newtarget,
  123. node, contnode)
  124. def setup(app):
  125. app.connect(
  126. bytes_if_py2('missing-reference'),
  127. maybe_resolve_abbreviations,
  128. )
  129. app.add_crossref_type(
  130. directivename=bytes_if_py2('sig'),
  131. rolename=bytes_if_py2('sig'),
  132. indextemplate=bytes_if_py2('pair: %s; sig'),
  133. )
  134. app.add_crossref_type(
  135. directivename=bytes_if_py2('state'),
  136. rolename=bytes_if_py2('state'),
  137. indextemplate=bytes_if_py2('pair: %s; state'),
  138. )
  139. app.add_crossref_type(
  140. directivename=bytes_if_py2('control'),
  141. rolename=bytes_if_py2('control'),
  142. indextemplate=bytes_if_py2('pair: %s; control'),
  143. )
  144. app.add_crossref_type(
  145. directivename=bytes_if_py2('event'),
  146. rolename=bytes_if_py2('event'),
  147. indextemplate=bytes_if_py2('pair: %s; event'),
  148. )