简体   繁体   English

使用setuptools自动生成Python项目的文档

[英]Autogenerate documentation for Python project using setuptools

I have created a demo project which uses setuptools and has the following structure: 我创建了一个使用setuptools的演示项目,它具有以下结构:

project/
 |- pizza/
 |   |- __init__.py
 |   `- margherita.py
 |
 |- README.rst
 |- setup.cfg
 `- setup.py

I'm trying to autogenerate documentation for this project using Sphinx. 我正在尝试使用Sphinx自动生成此项目的文档。 So far I've tried: 到目前为止,我已经尝试过:

# Generate a sphinx template
sphinx-quickstart
# Use default settings, except for project name, etc.
sphinx-apidoc -o source .
./setup.py build_sphinx

I feel there has to be an easier way to autogenerate this documentation using the README , setup.py and docstrings. 我觉得必须有一种更简单的方法来使用READMEsetup.py和docstrings自动生成此文档。

Ultimately I'd like to autogenerate apidocs for another project where I use the Python C-api as well. 最后,我想为另一个使用Python C-api的项目自动生成apidocs。 I couldn't find anything for this. 我找不到任何东西。

My main question is: Is there an easier way to autogenerate this documentation? 我的主要问题是:是否有更简单的方法来自动生成此文档?

To extend setup.py so it contains an extra command for Sphinx, you could create a custom command. 要扩展setup.py ,使其包含Sphinx的额外命令,您可以创建自定义命令。 I've cooked up a small example that runs Sphinx apidoc and then builds the doc sources. 我已经编写了一个运行Sphinx apidoc的小例子,然后构建了doc源代码。 The project name, author, version and location of the sources defined in the setup.py are used (assuming they are defined). 使用setup.py中定义的源的项目名称,作者,版本和位置(假设它们已定义)。

class Sphinx(Command):
    user_options = []
    description = 'sphinx'

    def initialize_options(self):
        pass

    def finalize_options(self):
        pass

    def run(self):
        # metadata contains information supplied in setup()
        metadata = self.distribution.metadata
        # package_dir may be None, in that case use the current directory.
        src_dir = (self.distribution.package_dir or {'': ''})['']
        src_dir = os.path.join(os.getcwd(),  src_dir)
        # Run sphinx by calling the main method, '--full' also adds a conf.py
        sphinx.apidoc.main(
            ['', '--full', '-H', metadata.name, '-A', metadata.author,
             '-V', metadata.version, '-R', metadata.version,
             '-o', os.path.join('doc', 'source'), src_dir])
        # build the doc sources
        sphinx.main(['', os.path.join('doc', 'source'),
                     os.path.join('doc', 'build')])

Then the command needs to be registered to the entry point group distutils.commands . 然后,该命令需要注册到入口点组distutils.commands Here the command is called sphinx . 这个命令叫做sphinx

from setuptools import setup

setup(
    # ...
    setup_requires = ['sphinx'],
    entry_points = {
        'distutils.commands': [
            'sphinx = example_module:Sphinx'
        ]
    }
)

I don't know how C sources are handled, but this'll get you started. 我不知道如何处理C源,但这会让你开始。

sphinx-apidoc -F -o source .

Will generate a project with sphinx-quickstart and recursively look for python modules 将使用sphinx-quickstart生成一个项目并递归查找python模块

You're about as efficient as you can be at the moment. 你现在的效率和现在一样高效。

=== Just wishful thinking below here === ===这里只是如意的想法===

Wouldn't it be lovely if you could call something like 如果你可以打电话,那会不会很可爱

./setup.py build_sphinx -C

and it would create you index.RST, read any RST files you had knocking about, parse all the docstrings and spit out some html. 它会创建你index.RST,读取你所敲定的任何RST文件,解析所有文档字符串并吐出一些HTML。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM