8

我正在尝试使用 Sphinx 1.4 和扩展为我的 python 类生成sphinx-apidoc文档sphinx.ext.autodoc

我有很多模块,我希望每个模块只显示类名,而不是类中方法的完整列表(在我的代码中都有文档字符串)。

这是我的conf.py文件的片段:

sys.path.insert(0, '/blah/sphinx/src')

extensions = ['sphinx.ext.autodoc']
autodoc_member_order = 'bysource'
autodoc_default_flags = ['no-members']

这是一个玩具模块 ( my_module.py),我用它来了解 Sphinx 的工作原理:

"""
==============
Test my module
==============
"""

def module_function():
    """Here is a module function, let's see if it's in"""
     print 'module level'

class TestClass:
    """
    Test this class

    Here is some more class documentation.
    """
    def __init__(self):
        """Here is init"""
        self.test = True

    def getName(self, inputName):
        """Summary for getName

        more documentation for getName
        """    
        print "hello"
        return inputName

我只显示这个类的代码,以防我需要在我丢失的文档字符串中做一些事情。

我运行 sphinx-apidoc 来生成 rst 文件:

sphinx-apidoc -f -M -e -o docs/ /blah/sphinx/src/

然后构建:

制作html

我可能不清楚autodoc_default_flags应该做什么。我认为当您在设置了这些标志的情况下运行 sphinx-apidoc 时,这些标志将应用于 .rst 文件中的指令。但是,在我运行 sphinx-apidoc 之后,我得到了这个 .rst 文件:

my_module module
=====================

.. automodule:: my_module
    :members:
    :undoc-members:
    :show-inheritance:

由于设置了这些标志,我没想到:members:会被应用,但它确实存在!并且 html 页面具有完整的方法及其文档字符串。

FWIW,autodoc_member_order正在工作;我可以设置它来切换方法出现的顺序。

所以我的问题:

  1. 应该做我autodoc_default_flags所描述的还是我误解了它?
  2. 如果它可以用来自动隐藏构建中的成员,我是否正确使用它?如果是这样,关于为什么我仍然被:members:添加到 .rst 文件中的任何想法?
  3. 如果我误解了它,那么它到底有什么作用?以及如何自动从我的构建中隐藏方法文档字符串?

理想情况下,我想要像 SciPy 这样的东西,例如这里:

http://docs.scipy.org/doc/scipy/reference/cluster.hierarchy.html

为此,我正在玩拿破仑和 sphinx.ext.autosummary 扩展,但似乎只有 apidoc 应该能够隐藏类方法文档。

4

1 回答 1

11

我认为当您在设置了这些标志的情况下运行 sphinx-apidoc 时,这些标志将应用于 .rst 文件中的指令。

sphinx-build确实适用autodoc_default_flags于 conf.py,除非标志在 *.rst 文件中被覆盖。

sphinx-apidoc不使用 conf.py。


可以通过环境变量自定义sphinx-apidoc生成的 *.rst 文件中的标志。SPHINX_APIDOC_OPTIONS例子:

$ export SPHINX_APIDOC_OPTIONS=no-members
$ sphinx-apidoc -o docs/ /blah/sphinx/src/

这将导致automodule如下所示的指令:

.. automodule:: my_module
   :no-members:

如果您需要对生成的输出进行更多控制,您也许可以编写自己的 apidoc.py 模块版本。请参阅为 `sphinx-apidoc` 自定义模板

于 2016-04-05T16:14:15.797 回答