celerydocs.py 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  1. import sys
  2. import typing
  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. def typeify(S, type):
  47. if type in ('meth', 'func'):
  48. return S + '()'
  49. return S
  50. def shorten(S, newtarget, src_dict):
  51. if S.startswith('@-'):
  52. return S[2:]
  53. elif S.startswith('@'):
  54. if src_dict is APPATTRS:
  55. return '.'.join(['app', S[1:]])
  56. return S[1:]
  57. return S
  58. def get_abbr(pre, rest, type, orig=None):
  59. if pre:
  60. for d in APPATTRS, ABBRS:
  61. try:
  62. return d[pre], rest, d
  63. except KeyError:
  64. pass
  65. raise KeyError('Unknown abbreviation: {0} ({1})'.format(
  66. '.'.join([pre, rest]) if orig is None else orig, type,
  67. ))
  68. else:
  69. for d in APPATTRS, ABBRS:
  70. try:
  71. return d[rest], '', d
  72. except KeyError:
  73. pass
  74. return ABBR_EMPTY.get(type, DEFAULT_EMPTY), rest, ABBR_EMPTY
  75. def resolve(S, type):
  76. if '.' not in S:
  77. try:
  78. getattr(typing, S)
  79. except AttributeError:
  80. pass
  81. else:
  82. return 'typing.{0}'.format(S), None
  83. orig = S
  84. if S.startswith('@'):
  85. S = S.lstrip('@-')
  86. try:
  87. pre, rest = S.split('.', 1)
  88. except ValueError:
  89. pre, rest = '', S
  90. target, rest, src = get_abbr(pre, rest, type, orig)
  91. return '.'.join([target, rest]) if rest else target, src
  92. return S, None
  93. def pkg_of(module_fqdn):
  94. return module_fqdn.split('.', 1)[0]
  95. def basename(module_fqdn):
  96. return module_fqdn.lstrip('@').rsplit('.', -1)[-1]
  97. def modify_textnode(T, newtarget, node, src_dict, type):
  98. src = node.children[0].rawsource
  99. return nodes.Text(
  100. (typeify(basename(T), type) if '~' in src
  101. else typeify(shorten(T, newtarget, src_dict), type)),
  102. src,
  103. )
  104. def maybe_resolve_abbreviations(app, env, node, contnode):
  105. domainname = node.get('refdomain')
  106. target = node['reftarget']
  107. type = node['reftype']
  108. if target.startswith('@'):
  109. newtarget, src_dict = resolve(target, type)
  110. node['reftarget'] = newtarget
  111. # shorten text if '~' is not enabled.
  112. if len(contnode) and isinstance(contnode[0], nodes.Text):
  113. contnode[0] = modify_textnode(target, newtarget, node,
  114. src_dict, type)
  115. if domainname:
  116. try:
  117. domain = env.domains[node.get('refdomain')]
  118. except KeyError:
  119. raise NoUri
  120. return domain.resolve_xref(env, node['refdoc'], app.builder,
  121. type, newtarget,
  122. node, contnode)
  123. def setup(app):
  124. app.connect('missing-reference', maybe_resolve_abbreviations)
  125. app.add_crossref_type(
  126. directivename='sig',
  127. rolename='sig',
  128. indextemplate='pair: %s; sig',
  129. )
  130. app.add_crossref_type(
  131. directivename='state',
  132. rolename='state',
  133. indextemplate='pair: %s; state',
  134. )
  135. app.add_crossref_type(
  136. directivename='control',
  137. rolename='control',
  138. indextemplate='pair: %s; control',
  139. )
  140. app.add_crossref_type(
  141. directivename='event',
  142. rolename='event',
  143. indextemplate='pair: %s; event',
  144. )