首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >是否可以使用sphinx-apidoc在同一个git存储库中为多个包创建文档?

是否可以使用sphinx-apidoc在同一个git存储库中为多个包创建文档?
EN

Stack Overflow用户
提问于 2021-02-05 06:24:52
回答 1查看 262关注 0票数 1

我有一个git存储库,其中包含多个跟在名称空间后面的包(即PEP 420

我正在尝试使用Sphinx创建一个ReadTheDocs文档。

git存储库如下所示:https://github.com/pypa/sample-namespace-packages

为了在我的本地机器上测试它,我使用了Sphinx的docker镜像sphinxdoc/sphinx

我尝试使用不同的方法为我的所有包生成文档,但每种方法都有不同的问题。

sphinx apidoc

docker run -it -v pwd:/repo --rm rtd bash -c 'make clean && rm -rf /repo/docs/_source/* && sphinx-apidoc -F -o /repo/docs/_source /repo && make html'

这样做的问题是它会生成错误的包,因为sphinx-apidoc使用子文件夹来生成包,这是错误的。这将以pkg_resourcespkg_a.example_pkg.a结束,它不存在,实际上应该是example_pkg.a

autoapi.extension

代码语言:javascript
复制
# conf.py
def install(package):
    subprocess.check_call([sys.executable, "-m", "pip", "install", package, "--no-deps"])


rootfolder=os.path.abspath('../')
add_module_names = False
autoapi_dirs = []
pathlist = Path(rootfolder).glob('repo-*/repo/*/')
for path in pathlist:
     path_in_str = str(path)
     autoapi_dirs.append(path_in_str)
     print(path_in_str)

...
...

extensions = [
    'sphinx.ext.napoleon',
    'sphinx.ext.autosummary', 
    'sphinx.ext.autodoc',
    'sphinx.ext.viewcode',
    'sphinx.ext.coverage',
    'autoapi.extension',
    'sphinx_rtd_theme',
]
autoapi_type = 'python'

autodoc_mock_imports = [
    'tensorflow',
    'flask',
    'numpy',
    'plotly',
    'tqdm',
    'StringIO',
    'lime',
    'vis',
    'efficientnet_pytorch',
    'pycocotools',
    'repo.trainer.self_trainer_model',
    'theano',
    'sklearn',
    'torch',
    'telegram',
    'msvcrt',
    'bs4',
    'livereload',
    'repo.common.config',
    'plotting_server',
    'experiments',
    'cropper',
    "anytree",
    "skimage"
]

我也尝试过这样做,但不幸的是,这并没有在HTML中显示我的包的任何内容,同时还抛出了以下警告:

代码语言:javascript
复制
/repo/docs/autoapi/repo/data/characteristics/detection/kmeanboxes/index.rst: WARNING: document isn't included in any toctree
/repo/docs/autoapi/repo/data/index.rst: WARNING: document isn't included in any toctree
/repo/docs/autoapi/repo/data_structure/index.rst: WARNING: document isn't included in any toctree
/repo/docs/autoapi/repo/detection/index.rst: WARNING: document isn't included in any toctree
/repo/docs/autoapi/repo/generators/index.rst: WARNING: document isn't included in any toctree
/repo/docs/autoapi/repo/inference/index.rst: WARNING: document isn't included in any toctree
/repo/docs/autoapi/repo/mine/repo_eye_naveyaar/index.rst: WARNING: document isn't included in any toctree
/repo/docs/autoapi/repo/mine/index.rst: WARNING: document isn't included in any toctree
/repo/docs/autoapi/repo/mine/miner_vieweryoungweedscropped/index.rst: WARNING: document isn't included in any toctree
/repo/docs/autoapi/repo/trainer/index.rst: WARNING: document isn't included in any toctree
/repo/docs/autoapi/repo/utils/dataset_specific/repoeyeweedsbackgrounds/index.rst: WARNING: document isn't included in any toctree
/repo/docs/autoapi/repo/utils/dataset_specific/repoeyeweedslabdetection/index.rst: WARNING: document isn't included in any toctree
/repo/docs/autoapi/repo/utils/index.rst: WARNING: document isn't included in any toctree
/repo/docs/autoapi/repocommon/repo/common/index.rst: WARNING: document isn't included in any toctree
/repo/docs/autoapi/repodatasets/repo/data_sets/index.rst: WARNING: document isn't included in any toctree
/repo/docs/autoapi/repometrics/repo/metrics/index.rst: WARNING: document isn't included in any toctree
/repo/docs/autoapi/repomodels/repo/models/index.rst: WARNING: document isn't included in any toctree
/repo/docs/autoapi/repooptimizers/repo/optimizers/index.rst: WARNING: document isn't included in any toctree
/repo/docs/autoapi/index.rst: WARNING: document isn't included in any toctree

那么,我的问题是,是否可以使用sphinx-apidoc在同一个git存储库中为多个包创建文档?

EN

回答 1

Stack Overflow用户

发布于 2021-02-13 23:41:08

经过相当多的挖掘后,我发现了一些我在发布问题时不清楚的事情:

  1. sphinx-apidoc仅限于python,并且可能很难使用,因为它实际上导入了python代码,这可能需要使用autodoc_mock_imports.

安装依赖项或模拟依赖项

  1. sphinx-autoapi不需要安装dependencies.

,可以使用its documentation上声明的更多编程语言

但是,为了使用sphinx-apidoc或sphinx-autoapi在同一个git存储库中为多个包创建文档,我必须将以下代码添加到docs/conf.py

代码语言:javascript
复制
from glob import glob
from distutils.dir_util import copy_tree

currentfolder = os.path.abspath('./') # aka docs/
rootfolder = os.path.abspath('../')
tmpfolder = "{}/tmp".format(currentfolder)
globdir = '{}/repo-*/repo'.format(rootfolder)
subprocess.check_call(["mkdir", "-p", tmpfolder]) # make sure temporary folder exists 

all_sub_dir_paths = glob(globdir) # returns list of sub directory paths
for subdir in all_sub_dir_paths:
    print("subdir: {} to tmp folder: {}".format(subdir, tmpfolder))
    copy_tree(subdir, tmpfolder) # copy all folders to docs/tmp/repo/*

autoapi_dirs = [tmpfolder]
sys.path.insert(0, tmpfolder)
autoapi_python_use_implicit_namespaces = True

此解决方案将把repo中的所有包复制到一个临时文件夹,这将允许docstring工具扫描并生成相关的文档。

我最终使用了sphinx-autoapi,但它也适用于sphinx-apidoc,它可能需要您安装这些软件包:

代码语言:javascript
复制
def install(package):
     subprocess.check_call([sys.executable, "-m", "pip", "install", package, "--no-deps"])

def list_files(dir):
    r = []
    for root, dirs, files in os.walk(dir):
        for name in dirs:
            if name.startswith('repo-'):
                l = "{}".format(os.path.join(root, name))
                # print("Added to path: {}".format(l))
                sys.path.insert(0, l)
                install(l) # might no be required 
        break # stop os.walk recurssion
    return r
list_files(currentfolder)

autodoc_mock_imports = [
    'tensorflow',
    'flask',
]

您可以使用docker在本地进行调试:

代码语言:javascript
复制
docker run -it -v `pwd`:/repo --rm rtd bash -c 'python -m sphinx -T -E  -d _build/doctrees-readthedocs -D language=en . _build/html'

如果您使用的是ReadTheDocs业务,您所要做的就是将您的代码推送到相关分支,剩下的工作将自动为您完成。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/66054747

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档