|
26 | 26 |
|
27 | 27 | from sphinx import __display_version__
|
28 | 28 | from sphinx.quickstart import EXTENSIONS
|
29 |
| -from sphinx.util import rst |
| 29 | +from sphinx.util import logging, rst |
30 | 30 | from sphinx.util.osutil import FileAvoidWrite, walk
|
31 | 31 |
|
32 | 32 | if False:
|
33 | 33 | # For type annotation
|
34 |
| - from typing import Any, List, Tuple # NOQA |
| 34 | + from typing import Any, Dict, List, Tuple # NOQA |
| 35 | + from sphinx.application import Sphinx # NOQA |
| 36 | + |
| 37 | +logger = logging.getLogger(__name__) |
35 | 38 |
|
36 | 39 | # automodule options
|
37 | 40 | if 'SPHINX_APIDOC_OPTIONS' in os.environ:
|
@@ -382,7 +385,9 @@ def main(argv=sys.argv[1:]):
|
382 | 385 | os.makedirs(opts.destdir)
|
383 | 386 | rootpath = path.abspath(rootpath)
|
384 | 387 | excludes = normalize_excludes(rootpath, excludes)
|
| 388 | + |
385 | 389 | modules = recurse_tree(rootpath, excludes, opts)
|
| 390 | + |
386 | 391 | if opts.full:
|
387 | 392 | from sphinx import quickstart as qs
|
388 | 393 | modules.sort()
|
@@ -435,6 +440,45 @@ def main(argv=sys.argv[1:]):
|
435 | 440 | return 0
|
436 | 441 |
|
437 | 442 |
|
438 |
| -# So program can be started with "python -m sphinx.apidoc ..." |
| 443 | +def builder_inited(app): |
| 444 | + # type: (Sphinx) -> None |
| 445 | + module_dir = app.config.apidoc_module_dir |
| 446 | + output_dir = path.join(app.srcdir, app.config.apidoc_output_dir) |
| 447 | + excludes = app.config.apidoc_excluded_modules |
| 448 | + |
| 449 | + if not module_dir: |
| 450 | + logger.warning("No 'apidoc_module_dir' specified; skipping API doc " |
| 451 | + "generation") |
| 452 | + return |
| 453 | + |
| 454 | + # if the path is relative, make it relative to the 'conf.py' directory |
| 455 | + if not path.isabs(module_dir): |
| 456 | + module_dir = path.abspath(path.join(app.srcdir, module_dir)) |
| 457 | + |
| 458 | + excludes = [path.abspath(path.join(module_dir, exc)) for exc in excludes] |
| 459 | + |
| 460 | + # refactor this module so that we can call 'recurse_tree' like a sane |
| 461 | + # person - at present there is way too much passing around of the |
| 462 | + # 'optparse.Value' instance returned by 'optparse.parse_args' |
| 463 | + cmd = [module_dir, '--force', '-o', output_dir] |
| 464 | + if excludes: |
| 465 | + cmd += excludes |
| 466 | + |
| 467 | + main(cmd) |
| 468 | + |
| 469 | + |
| 470 | +def setup(app): |
| 471 | + # type: (Sphinx) -> Dict[unicode, Any] |
| 472 | + app.setup_extension('sphinx.ext.autodoc') # We need autodoc to function |
| 473 | + |
| 474 | + app.connect('builder-inited', builder_inited) |
| 475 | + app.add_config_value('apidoc_module_dir', None, 'env', [str]) |
| 476 | + app.add_config_value('apidoc_output_dir', 'api', 'env', [str]) |
| 477 | + app.add_config_value('apidoc_excluded_modules', ['setup.py'], 'env', |
| 478 | + [[str]]) |
| 479 | + |
| 480 | + return {'version': __display_version__, 'parallel_read_safe': True} |
| 481 | + |
| 482 | + |
439 | 483 | if __name__ == "__main__":
|
440 | 484 | main()
|
0 commit comments