sphinx_ext.py 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. # copyright 2003-2011 LOGILAB S.A. (Paris, FRANCE), all rights reserved.
  2. # contact http://www.logilab.fr/ -- mailto:contact@logilab.fr
  3. #
  4. # This file is part of logilab-common.
  5. #
  6. # logilab-common is free software: you can redistribute it and/or modify it under
  7. # the terms of the GNU Lesser General Public License as published by the Free
  8. # Software Foundation, either version 2.1 of the License, or (at your option) any
  9. # later version.
  10. #
  11. # logilab-common is distributed in the hope that it will be useful, but WITHOUT
  12. # ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
  13. # FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
  14. # details.
  15. #
  16. # You should have received a copy of the GNU Lesser General Public License along
  17. # with logilab-common. If not, see <http://www.gnu.org/licenses/>.
  18. from logilab.common.decorators import monkeypatch
  19. from sphinx.ext import autodoc
  20. class DocstringOnlyModuleDocumenter(autodoc.ModuleDocumenter):
  21. objtype = 'docstring'
  22. def format_signature(self):
  23. pass
  24. def add_directive_header(self, sig):
  25. pass
  26. def document_members(self, all_members=False):
  27. pass
  28. def resolve_name(self, modname, parents, path, base):
  29. if modname is not None:
  30. return modname, parents + [base]
  31. return (path or '') + base, []
  32. #autodoc.add_documenter(DocstringOnlyModuleDocumenter)
  33. def setup(app):
  34. app.add_autodocumenter(DocstringOnlyModuleDocumenter)
  35. from sphinx.ext.autodoc import (ViewList, Options, AutodocReporter, nodes,
  36. assemble_option_dict, nested_parse_with_titles)
  37. @monkeypatch(autodoc.AutoDirective)
  38. def run(self):
  39. self.filename_set = set() # a set of dependent filenames
  40. self.reporter = self.state.document.reporter
  41. self.env = self.state.document.settings.env
  42. self.warnings = []
  43. self.result = ViewList()
  44. # find out what documenter to call
  45. objtype = self.name[4:]
  46. doc_class = self._registry[objtype]
  47. # process the options with the selected documenter's option_spec
  48. self.genopt = Options(assemble_option_dict(
  49. self.options.items(), doc_class.option_spec))
  50. # generate the output
  51. documenter = doc_class(self, self.arguments[0])
  52. documenter.generate(more_content=self.content)
  53. if not self.result:
  54. return self.warnings
  55. # record all filenames as dependencies -- this will at least
  56. # partially make automatic invalidation possible
  57. for fn in self.filename_set:
  58. self.env.note_dependency(fn)
  59. # use a custom reporter that correctly assigns lines to source
  60. # filename/description and lineno
  61. old_reporter = self.state.memo.reporter
  62. self.state.memo.reporter = AutodocReporter(self.result,
  63. self.state.memo.reporter)
  64. if self.name in ('automodule', 'autodocstring'):
  65. node = nodes.section()
  66. # necessary so that the child nodes get the right source/line set
  67. node.document = self.state.document
  68. nested_parse_with_titles(self.state, self.result, node)
  69. else:
  70. node = nodes.paragraph()
  71. node.document = self.state.document
  72. self.state.nested_parse(self.result, 0, node)
  73. self.state.memo.reporter = old_reporter
  74. return self.warnings + node.children