|
| 1 | +""" |
| 2 | +Sphinx's autodoc module allows for default options to be set, |
| 3 | +and allows for those defaults to be disabled for an auto* directive and different values given instead. |
| 4 | +
|
| 5 | +However, it does not appear to be possible to augment the defaults, |
| 6 | +such as to globally exclude certain members and then exclude additional members of a single class. |
| 7 | +
|
| 8 | +This module monkeypatches in that behaviour. |
| 9 | +""" |
| 10 | + |
| 11 | +# stdlib |
| 12 | +from typing import Any, Dict, Type |
| 13 | + |
| 14 | +# 3rd party |
| 15 | +import sphinx.ext.autodoc.directive |
| 16 | +from docutils.utils import assemble_option_dict |
| 17 | +from sphinx.application import Sphinx |
| 18 | +from sphinx.config import Config |
| 19 | +from sphinx.ext.autodoc import Documenter, Options |
| 20 | + |
| 21 | + |
| 22 | +def process_documenter_options( |
| 23 | + documenter: "Type[Documenter]", |
| 24 | + config: Config, |
| 25 | + options: Dict, |
| 26 | + ) -> Options: |
| 27 | + """ |
| 28 | + Recognize options of Documenter from user input. |
| 29 | +
|
| 30 | + :param documenter: |
| 31 | + :param config: |
| 32 | + :param options: |
| 33 | + :return: |
| 34 | + """ |
| 35 | + |
| 36 | + for name in sphinx.ext.autodoc.directive.AUTODOC_DEFAULT_OPTIONS: |
| 37 | + if name not in documenter.option_spec: |
| 38 | + continue |
| 39 | + else: |
| 40 | + negated = options.pop('no-' + name, True) is None |
| 41 | + if name in config.autodoc_default_options and not negated: |
| 42 | + |
| 43 | + default_value = config.autodoc_default_options[name] |
| 44 | + existing_value = options.get(name, None) |
| 45 | + |
| 46 | + values = list(filter(None, [default_value, existing_value])) |
| 47 | + |
| 48 | + if values: |
| 49 | + options[name] = ",".join(values) |
| 50 | + else: |
| 51 | + options[name] = None |
| 52 | + |
| 53 | + return Options(assemble_option_dict(options.items(), documenter.option_spec)) |
| 54 | + |
| 55 | + |
| 56 | +def setup(app: Sphinx) -> Dict[str, Any]: |
| 57 | + """ |
| 58 | + Setup Sphinx Extension. |
| 59 | +
|
| 60 | + :param app: |
| 61 | +
|
| 62 | + :return: |
| 63 | + """ |
| 64 | + |
| 65 | + sphinx.ext.autodoc.directive.process_documenter_options = process_documenter_options |
| 66 | + |
| 67 | + return {} |
0 commit comments