saltautodoc.py 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  1. # -*- coding: utf-8 -*-
  2. """
  3. :codeauthor: Pedro Algarvio (pedro@algarvio.me)
  4. saltautodoc.py
  5. ~~~~~~~~~~~~~~
  6. Properly handle ``__func_alias__``
  7. """
  8. # Import Sphinx libs
  9. from sphinx.ext.autodoc import FunctionDocumenter as FunctionDocumenter
  10. class SaltFunctionDocumenter(FunctionDocumenter):
  11. """
  12. Simple override of sphinx.ext.autodoc.FunctionDocumenter to properly render
  13. salt's aliased function names.
  14. """
  15. def format_name(self):
  16. """
  17. Format the function name
  18. """
  19. if not hasattr(self.module, "__func_alias__"):
  20. # Resume normal sphinx.ext.autodoc operation
  21. return super(FunctionDocumenter, self).format_name()
  22. if not self.objpath:
  23. # Resume normal sphinx.ext.autodoc operation
  24. return super(FunctionDocumenter, self).format_name()
  25. if len(self.objpath) > 1:
  26. # Resume normal sphinx.ext.autodoc operation
  27. return super(FunctionDocumenter, self).format_name()
  28. # Use the salt func aliased name instead of the real name
  29. return self.module.__func_alias__.get(self.objpath[0], self.objpath[0])
  30. def setup(app):
  31. def add_documenter(app, env, docnames):
  32. app.add_autodocumenter(SaltFunctionDocumenter)
  33. # add_autodocumenter() must be called after the initial setup and the
  34. # 'builder-inited' event, as sphinx.ext.autosummary will restore the
  35. # original documenter on 'builder-inited'
  36. app.connect("env-before-read-docs", add_documenter)