celerydocs.py 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  1. from docutils import nodes
  2. from sphinx.environment import NoUri
  3. APPATTRS = {
  4. 'amqp': 'celery.app.amqp.AMQP',
  5. 'backend': 'celery.backends.base.BaseBackend',
  6. 'control': 'celery.app.control.Control',
  7. 'events': 'celery.events.Events',
  8. 'loader': 'celery.app.loaders.base.BaseLoader',
  9. 'log': 'celery.app.log.Logging',
  10. 'pool': 'kombu.connection.ConnectionPool',
  11. 'tasks': 'celery.app.registry.Registry',
  12. 'AsyncResult': 'celery.result.AsyncResult',
  13. 'GroupResult': 'celery.result.GroupResult',
  14. 'Worker': 'celery.apps.worker.Worker',
  15. 'WorkController': 'celery.worker.WorkController',
  16. 'Beat': 'celery.apps.beat.Beat',
  17. 'Task': 'celery.app.task.Task',
  18. 'send_task': 'celery.Celery.send_task',
  19. 'connection': 'celery.Celery.connection',
  20. }
  21. ABBRS = {
  22. 'Celery': 'celery.Celery',
  23. }
  24. ABBR_EMPTY = {
  25. 'exc': 'celery.exceptions',
  26. }
  27. DEFAULT_EMPTY = 'celery.Celery'
  28. def typeify(S, type):
  29. if type in ('meth', 'func'):
  30. return S + '()'
  31. return S
  32. def shorten(S, newtarget, src_dict):
  33. if S.startswith('@-'):
  34. return S[2:]
  35. elif S.startswith('@'):
  36. if src_dict is APPATTRS:
  37. return '.'.join([pkg_of(newtarget), S[1:]])
  38. return S[1:]
  39. return S
  40. def get_abbr(pre, rest, type):
  41. if pre:
  42. for d in APPATTRS, ABBRS:
  43. try:
  44. return d[pre], rest, d
  45. except KeyError:
  46. pass
  47. raise KeyError(pre)
  48. else:
  49. for d in APPATTRS, ABBRS:
  50. try:
  51. return d[rest], '', d
  52. except KeyError:
  53. pass
  54. return ABBR_EMPTY.get(type, DEFAULT_EMPTY), rest, ABBR_EMPTY
  55. def resolve(S, type):
  56. if S.startswith('@'):
  57. S = S.lstrip('@-')
  58. try:
  59. pre, rest = S.split('.', 1)
  60. except ValueError:
  61. pre, rest = '', S
  62. target, rest, src = get_abbr(pre, rest, type)
  63. return '.'.join([target, rest]) if rest else target, src
  64. return S, None
  65. def pkg_of(module_fqdn):
  66. return module_fqdn.split('.', 1)[0]
  67. def basename(module_fqdn):
  68. return module_fqdn.lstrip('@').rsplit('.', -1)[-1]
  69. def modify_textnode(T, newtarget, node, src_dict, type):
  70. src = node.children[0].rawsource
  71. return nodes.Text(
  72. (typeify(basename(T), type) if '~' in src
  73. else typeify(shorten(T, newtarget, src_dict), type)),
  74. src,
  75. )
  76. def maybe_resolve_abbreviations(app, env, node, contnode):
  77. domainname = node.get('refdomain')
  78. target = node['reftarget']
  79. type = node['reftype']
  80. if target.startswith('@'):
  81. newtarget, src_dict = resolve(target, type)
  82. node['reftarget'] = newtarget
  83. # shorten text if '~' is not enabled.
  84. if len(contnode) and isinstance(contnode[0], nodes.Text):
  85. contnode[0] = modify_textnode(target, newtarget, node,
  86. src_dict, type)
  87. if domainname:
  88. try:
  89. domain = env.domains[node.get('refdomain')]
  90. except KeyError:
  91. raise NoUri
  92. return domain.resolve_xref(env, node['refdoc'], app.builder,
  93. type, newtarget,
  94. node, contnode)
  95. def setup(app):
  96. app.connect('missing-reference', maybe_resolve_abbreviations)
  97. app.add_crossref_type(
  98. directivename='setting',
  99. rolename='setting',
  100. indextemplate='pair: %s; setting',
  101. )
  102. app.add_crossref_type(
  103. directivename='sig',
  104. rolename='sig',
  105. indextemplate='pair: %s; sig',
  106. )
  107. app.add_crossref_type(
  108. directivename='state',
  109. rolename='state',
  110. indextemplate='pair: %s; state',
  111. )
  112. app.add_crossref_type(
  113. directivename='control',
  114. rolename='control',
  115. indextemplate='pair: %s; control',
  116. )
  117. app.add_crossref_type(
  118. directivename='signal',
  119. rolename='signal',
  120. indextemplate='pair: %s; signal',
  121. )
  122. app.add_crossref_type(
  123. directivename='event',
  124. rolename='event',
  125. indextemplate='pair: %s; event',
  126. )