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