PageRenderTime 52ms CodeModel.GetById 20ms RepoModel.GetById 0ms app.codeStats 0ms

/documentation/doctools/trunk/sphinx/directives/desc.py

https://github.com/creasyw/IMTAphy
Python | 549 lines | 506 code | 21 blank | 22 comment | 72 complexity | 276a1af00e3191a1bc93ff968560da54 MD5 | raw file
Possible License(s): BSD-3-Clause, GPL-2.0, LGPL-2.1
  1. # -*- coding: utf-8 -*-
  2. """
  3. sphinx.directives.desc
  4. ~~~~~~~~~~~~~~~~~~~~~~
  5. :copyright: 2007-2008 by Georg Brandl.
  6. :license: BSD.
  7. """
  8. import re
  9. import string
  10. from docutils import nodes
  11. from docutils.parsers.rst import directives
  12. from sphinx import addnodes
  13. ws_re = re.compile(r'\s+')
  14. # ------ information units ---------------------------------------------------------
  15. def desc_index_text(desctype, module, name):
  16. if desctype == 'function':
  17. if not module:
  18. return _('%s() (built-in function)') % name
  19. return _('%s() (in module %s)') % (name, module)
  20. elif desctype == 'data':
  21. if not module:
  22. return _('%s (built-in variable)') % name
  23. return _('%s (in module %s)') % (name, module)
  24. elif desctype == 'class':
  25. return _('%s (class in %s)') % (name, module)
  26. elif desctype == 'exception':
  27. return name
  28. elif desctype == 'method':
  29. try:
  30. clsname, methname = name.rsplit('.', 1)
  31. except ValueError:
  32. if module:
  33. return _('%s() (in module %s)') % (name, module)
  34. else:
  35. return '%s()' % name
  36. if module:
  37. return _('%s() (%s.%s method)') % (methname, module, clsname)
  38. else:
  39. return _('%s() (%s method)') % (methname, clsname)
  40. elif desctype == 'staticmethod':
  41. try:
  42. clsname, methname = name.rsplit('.', 1)
  43. except ValueError:
  44. if module:
  45. return _('%s() (in module %s)') % (name, module)
  46. else:
  47. return '%s()' % name
  48. if module:
  49. return _('%s() (%s.%s static method)') % (methname, module, clsname)
  50. else:
  51. return _('%s() (%s static method)') % (methname, clsname)
  52. elif desctype == 'attribute':
  53. try:
  54. clsname, attrname = name.rsplit('.', 1)
  55. except ValueError:
  56. if module:
  57. return _('%s (in module %s)') % (name, module)
  58. else:
  59. return name
  60. if module:
  61. return _('%s (%s.%s attribute)') % (attrname, module, clsname)
  62. else:
  63. return _('%s (%s attribute)') % (attrname, clsname)
  64. elif desctype == 'cfunction':
  65. return _('%s (C function)') % name
  66. elif desctype == 'cmember':
  67. return _('%s (C member)') % name
  68. elif desctype == 'cmacro':
  69. return _('%s (C macro)') % name
  70. elif desctype == 'ctype':
  71. return _('%s (C type)') % name
  72. elif desctype == 'cvar':
  73. return _('%s (C variable)') % name
  74. else:
  75. raise ValueError('unhandled descenv: %s' % desctype)
  76. # ------ make field lists (like :param foo:) in desc bodies prettier
  77. _ = lambda x: x # make gettext extraction in constants possible
  78. doc_fields_with_arg = {
  79. 'param': '%param',
  80. 'parameter': '%param',
  81. 'arg': '%param',
  82. 'argument': '%param',
  83. 'keyword': '%param',
  84. 'kwarg': '%param',
  85. 'kwparam': '%param',
  86. 'type': '%type',
  87. 'raises': _('Raises'),
  88. 'raise': 'Raises',
  89. 'exception': 'Raises',
  90. 'except': 'Raises',
  91. 'var': _('Variable'),
  92. 'ivar': 'Variable',
  93. 'cvar': 'Variable',
  94. 'returns': _('Returns'),
  95. 'return': 'Returns',
  96. }
  97. doc_fields_without_arg = {
  98. 'returns': 'Returns',
  99. 'return': 'Returns',
  100. 'rtype': _('Return type'),
  101. }
  102. del _
  103. def handle_doc_fields(node):
  104. # don't traverse, only handle field lists that are immediate children
  105. for child in node.children:
  106. if not isinstance(child, nodes.field_list):
  107. continue
  108. params = None
  109. param_nodes = {}
  110. param_types = {}
  111. new_list = nodes.field_list()
  112. for field in child:
  113. fname, fbody = field
  114. try:
  115. typ, obj = fname.astext().split(None, 1)
  116. typ = _(doc_fields_with_arg[typ])
  117. if len(fbody.children) == 1 and \
  118. isinstance(fbody.children[0], nodes.paragraph):
  119. children = fbody.children[0].children
  120. else:
  121. children = fbody.children
  122. if typ == '%param':
  123. if not params:
  124. pfield = nodes.field()
  125. pfield += nodes.field_name('', _('Parameters'))
  126. pfield += nodes.field_body()
  127. params = nodes.bullet_list()
  128. pfield[1] += params
  129. new_list += pfield
  130. dlitem = nodes.list_item()
  131. dlpar = nodes.paragraph()
  132. dlpar += nodes.emphasis(obj, obj)
  133. dlpar += nodes.Text('', ' -- ')
  134. dlpar += children
  135. param_nodes[obj] = dlpar
  136. dlitem += dlpar
  137. params += dlitem
  138. elif typ == '%type':
  139. param_types[obj] = fbody.astext()
  140. else:
  141. fieldname = typ + ' ' + obj
  142. nfield = nodes.field()
  143. nfield += nodes.field_name(fieldname, fieldname)
  144. nfield += nodes.field_body()
  145. nfield[1] += fbody.children
  146. new_list += nfield
  147. except (KeyError, ValueError):
  148. fnametext = fname.astext()
  149. try:
  150. typ = _(doc_fields_without_arg[fnametext])
  151. except KeyError:
  152. # at least capitalize the field name
  153. typ = fnametext.capitalize()
  154. fname[0] = nodes.Text(typ)
  155. new_list += field
  156. for param, type in param_types.iteritems():
  157. if param in param_nodes:
  158. param_nodes[param].insert(1, nodes.Text(' (%s)' % type))
  159. child.replace_self(new_list)
  160. # ------ functions to parse a Python or C signature and create desc_* nodes.
  161. py_sig_re = re.compile(
  162. r'''^ ([\w.]*\.)? # class name(s)
  163. (\w+) \s* # thing name
  164. (?: \((.*)\) # optional arguments
  165. (\s* -> \s* .*)? )? $ # optional return annotation
  166. ''', re.VERBOSE)
  167. py_paramlist_re = re.compile(r'([\[\],])') # split at '[', ']' and ','
  168. def parse_py_signature(signode, sig, desctype, module, env):
  169. """
  170. Transform a python signature into RST nodes.
  171. Return (fully qualified name of the thing, classname if any).
  172. If inside a class, the current class name is handled intelligently:
  173. * it is stripped from the displayed name if present
  174. * it is added to the full name (return value) if not present
  175. """
  176. m = py_sig_re.match(sig)
  177. if m is None:
  178. raise ValueError
  179. classname, name, arglist, retann = m.groups()
  180. if env.currclass:
  181. add_module = False
  182. if classname and classname.startswith(env.currclass):
  183. fullname = classname + name
  184. # class name is given again in the signature
  185. classname = classname[len(env.currclass):].lstrip('.')
  186. elif classname:
  187. # class name is given in the signature, but different
  188. # (shouldn't happen)
  189. fullname = env.currclass + '.' + classname + name
  190. else:
  191. # class name is not given in the signature
  192. fullname = env.currclass + '.' + name
  193. else:
  194. add_module = True
  195. fullname = classname and classname + name or name
  196. if desctype == 'staticmethod':
  197. signode += addnodes.desc_annotation('static ', 'static ')
  198. if classname:
  199. signode += addnodes.desc_addname(classname, classname)
  200. # exceptions are a special case, since they are documented in the
  201. # 'exceptions' module.
  202. elif add_module and env.config.add_module_names and \
  203. module and module != 'exceptions':
  204. nodetext = module + '.'
  205. signode += addnodes.desc_addname(nodetext, nodetext)
  206. signode += addnodes.desc_name(name, name)
  207. if not arglist:
  208. if desctype in ('function', 'method', 'staticmethod'):
  209. # for callables, add an empty parameter list
  210. signode += addnodes.desc_parameterlist()
  211. return fullname, classname
  212. signode += addnodes.desc_parameterlist()
  213. stack = [signode[-1]]
  214. for token in py_paramlist_re.split(arglist):
  215. if token == '[':
  216. opt = addnodes.desc_optional()
  217. stack[-1] += opt
  218. stack.append(opt)
  219. elif token == ']':
  220. try:
  221. stack.pop()
  222. except IndexError:
  223. raise ValueError
  224. elif not token or token == ',' or token.isspace():
  225. pass
  226. else:
  227. token = token.strip()
  228. stack[-1] += addnodes.desc_parameter(token, token)
  229. if len(stack) != 1:
  230. raise ValueError
  231. if retann:
  232. retann = u' \N{RIGHTWARDS ARROW} ' + retann.strip()[2:]
  233. signode += addnodes.desc_type(retann, retann)
  234. return fullname, classname
  235. c_sig_re = re.compile(
  236. r'''^([^(]*?) # return type
  237. ([\w:]+) \s* # thing name (colon allowed for C++ class names)
  238. (?: \((.*)\) )? # optionally arguments
  239. (\s+const)? $ # const specifier
  240. ''', re.VERBOSE)
  241. c_funcptr_sig_re = re.compile(
  242. r'''^([^(]+?) # return type
  243. (\( [^()]+ \)) \s* # name in parentheses
  244. \( (.*) \) # arguments
  245. (\s+const)? $ # const specifier
  246. ''', re.VERBOSE)
  247. c_funcptr_name_re = re.compile(r'^\(\s*\*\s*(.*?)\s*\)$')
  248. # RE to split at word boundaries
  249. wsplit_re = re.compile(r'(\W+)')
  250. # These C types aren't described in the reference, so don't try to create
  251. # a cross-reference to them
  252. stopwords = set(('const', 'void', 'char', 'int', 'long', 'FILE', 'struct'))
  253. def parse_c_type(node, ctype):
  254. # add cross-ref nodes for all words
  255. for part in filter(None, wsplit_re.split(ctype)):
  256. tnode = nodes.Text(part, part)
  257. if part[0] in string.letters+'_' and part not in stopwords:
  258. pnode = addnodes.pending_xref(
  259. '', reftype='ctype', reftarget=part, modname=None, classname=None)
  260. pnode += tnode
  261. node += pnode
  262. else:
  263. node += tnode
  264. def parse_c_signature(signode, sig, desctype):
  265. """Transform a C (or C++) signature into RST nodes."""
  266. # first try the function pointer signature regex, it's more specific
  267. m = c_funcptr_sig_re.match(sig)
  268. if m is None:
  269. m = c_sig_re.match(sig)
  270. if m is None:
  271. raise ValueError('no match')
  272. rettype, name, arglist, const = m.groups()
  273. signode += addnodes.desc_type('', '')
  274. parse_c_type(signode[-1], rettype)
  275. try:
  276. classname, funcname = name.split('::', 1)
  277. classname += '::'
  278. signode += addnodes.desc_addname(classname, classname)
  279. signode += addnodes.desc_name(funcname, funcname)
  280. # name (the full name) is still both parts
  281. except ValueError:
  282. signode += addnodes.desc_name(name, name)
  283. # clean up parentheses from canonical name
  284. m = c_funcptr_name_re.match(name)
  285. if m:
  286. name = m.group(1)
  287. if not arglist:
  288. if desctype == 'cfunction':
  289. # for functions, add an empty parameter list
  290. signode += addnodes.desc_parameterlist()
  291. return name
  292. paramlist = addnodes.desc_parameterlist()
  293. arglist = arglist.replace('`', '').replace('\\ ', '') # remove markup
  294. # this messes up function pointer types, but not too badly ;)
  295. args = arglist.split(',')
  296. for arg in args:
  297. arg = arg.strip()
  298. param = addnodes.desc_parameter('', '', noemph=True)
  299. try:
  300. ctype, argname = arg.rsplit(' ', 1)
  301. except ValueError:
  302. # no argument name given, only the type
  303. parse_c_type(param, arg)
  304. else:
  305. parse_c_type(param, ctype)
  306. param += nodes.emphasis(' '+argname, ' '+argname)
  307. paramlist += param
  308. signode += paramlist
  309. if const:
  310. signode += addnodes.desc_addname(const, const)
  311. return name
  312. option_desc_re = re.compile(
  313. r'(/|-|--)([-_a-zA-Z0-9]+)(\s*.*?)(?=,\s+(?:/|-|--)|$)')
  314. def parse_option_desc(signode, sig):
  315. """Transform an option description into RST nodes."""
  316. count = 0
  317. firstname = ''
  318. for m in option_desc_re.finditer(sig):
  319. prefix, optname, args = m.groups()
  320. if count:
  321. signode += addnodes.desc_addname(', ', ', ')
  322. signode += addnodes.desc_name(prefix+optname, prefix+optname)
  323. signode += addnodes.desc_addname(args, args)
  324. if not count:
  325. firstname = optname
  326. count += 1
  327. if not firstname:
  328. raise ValueError
  329. return firstname
  330. def desc_directive(desctype, arguments, options, content, lineno,
  331. content_offset, block_text, state, state_machine):
  332. env = state.document.settings.env
  333. inode = addnodes.index(entries=[])
  334. node = addnodes.desc()
  335. node['desctype'] = desctype
  336. noindex = ('noindex' in options)
  337. node['noindex'] = noindex
  338. # remove backslashes to support (dummy) escapes; helps Vim's highlighting
  339. signatures = map(lambda s: s.strip().replace('\\', ''), arguments[0].split('\n'))
  340. names = []
  341. clsname = None
  342. module = options.get('module', env.currmodule)
  343. for i, sig in enumerate(signatures):
  344. # add a signature node for each signature in the current unit
  345. # and add a reference target for it
  346. sig = sig.strip()
  347. signode = addnodes.desc_signature(sig, '')
  348. signode['first'] = False
  349. node.append(signode)
  350. try:
  351. if desctype in ('function', 'data', 'class', 'exception',
  352. 'method', 'staticmethod', 'attribute'):
  353. name, clsname = parse_py_signature(signode, sig, desctype, module, env)
  354. elif desctype in ('cfunction', 'cmember', 'cmacro', 'ctype', 'cvar'):
  355. name = parse_c_signature(signode, sig, desctype)
  356. elif desctype == 'cmdoption':
  357. optname = parse_option_desc(signode, sig)
  358. if not noindex:
  359. targetname = 'cmdoption-' + optname
  360. signode['ids'].append(targetname)
  361. state.document.note_explicit_target(signode)
  362. inode['entries'].append(('pair', _('command line option; %s') % sig,
  363. targetname, targetname))
  364. env.note_reftarget('option', optname, targetname)
  365. continue
  366. elif desctype == 'describe':
  367. signode.clear()
  368. signode += addnodes.desc_name(sig, sig)
  369. continue
  370. else:
  371. # another registered generic x-ref directive
  372. rolename, indextemplate, parse_node = additional_xref_types[desctype]
  373. if parse_node:
  374. fullname = parse_node(env, sig, signode)
  375. else:
  376. signode.clear()
  377. signode += addnodes.desc_name(sig, sig)
  378. # normalize whitespace like xfileref_role does
  379. fullname = ws_re.sub('', sig)
  380. if not noindex:
  381. targetname = '%s-%s' % (rolename, fullname)
  382. signode['ids'].append(targetname)
  383. state.document.note_explicit_target(signode)
  384. if indextemplate:
  385. indexentry = _(indextemplate) % (fullname,)
  386. indextype = 'single'
  387. colon = indexentry.find(':')
  388. if colon != -1:
  389. indextype = indexentry[:colon].strip()
  390. indexentry = indexentry[colon+1:].strip()
  391. inode['entries'].append((indextype, indexentry,
  392. targetname, targetname))
  393. env.note_reftarget(rolename, fullname, targetname)
  394. # don't use object indexing below
  395. continue
  396. except ValueError, err:
  397. # signature parsing failed
  398. signode.clear()
  399. signode += addnodes.desc_name(sig, sig)
  400. continue # we don't want an index entry here
  401. # only add target and index entry if this is the first description of the
  402. # function name in this desc block
  403. if not noindex and name not in names:
  404. fullname = (module and module + '.' or '') + name
  405. # note target
  406. if fullname not in state.document.ids:
  407. signode['names'].append(fullname)
  408. signode['ids'].append(fullname)
  409. signode['first'] = (not names)
  410. state.document.note_explicit_target(signode)
  411. env.note_descref(fullname, desctype, lineno)
  412. names.append(name)
  413. indextext = desc_index_text(desctype, module, name)
  414. inode['entries'].append(('single', indextext, fullname, fullname))
  415. subnode = addnodes.desc_content()
  416. # needed for automatic qualification of members
  417. clsname_set = False
  418. if desctype in ('class', 'exception') and names:
  419. env.currclass = names[0]
  420. clsname_set = True
  421. elif desctype in ('method', 'staticmethod', 'attribute') and \
  422. clsname and not env.currclass:
  423. env.currclass = clsname.strip('.')
  424. clsname_set = True
  425. # needed for association of version{added,changed} directives
  426. if names:
  427. env.currdesc = names[0]
  428. state.nested_parse(content, content_offset, subnode)
  429. handle_doc_fields(subnode)
  430. if clsname_set:
  431. env.currclass = None
  432. env.currdesc = None
  433. node.append(subnode)
  434. return [inode, node]
  435. desc_directive.content = 1
  436. desc_directive.arguments = (1, 0, 1)
  437. desc_directive.options = {'noindex': directives.flag,
  438. 'module': directives.unchanged}
  439. desctypes = [
  440. # the Python ones
  441. 'function',
  442. 'data',
  443. 'class',
  444. 'method',
  445. 'staticmethod',
  446. 'attribute',
  447. 'exception',
  448. # the C ones
  449. 'cfunction',
  450. 'cmember',
  451. 'cmacro',
  452. 'ctype',
  453. 'cvar',
  454. # for command line options
  455. 'cmdoption',
  456. # the generic one
  457. 'describe',
  458. 'envvar',
  459. ]
  460. for _name in desctypes:
  461. directives.register_directive(_name, desc_directive)
  462. _ = lambda x: x
  463. # Generic cross-reference types; they can be registered in the application;
  464. # the directives are either desc_directive or target_directive
  465. additional_xref_types = {
  466. # directive name: (role name, index text, function to parse the desc node)
  467. 'envvar': ('envvar', _('environment variable; %s'), None),
  468. }
  469. del _
  470. # ------ target --------------------------------------------------------------------
  471. def target_directive(targettype, arguments, options, content, lineno,
  472. content_offset, block_text, state, state_machine):
  473. """Generic target for user-defined cross-reference types."""
  474. env = state.document.settings.env
  475. rolename, indextemplate, foo = additional_xref_types[targettype]
  476. # normalize whitespace in fullname like xfileref_role does
  477. fullname = ws_re.sub('', arguments[0].strip())
  478. targetname = '%s-%s' % (rolename, fullname)
  479. node = nodes.target('', '', ids=[targetname])
  480. state.document.note_explicit_target(node)
  481. ret = [node]
  482. if indextemplate:
  483. indexentry = indextemplate % (fullname,)
  484. indextype = 'single'
  485. colon = indexentry.find(':')
  486. if colon != -1:
  487. indextype = indexentry[:colon].strip()
  488. indexentry = indexentry[colon+1:].strip()
  489. inode = addnodes.index(entries=[(indextype, indexentry, targetname, targetname)])
  490. ret.insert(0, inode)
  491. env.note_reftarget(rolename, fullname, targetname)
  492. return ret
  493. target_directive.content = 0
  494. target_directive.arguments = (1, 0, 1)
  495. # note, the target directive is not registered here, it is used by the application
  496. # when registering additional xref types