Skip to content

Commit d5ede5c

Browse files
committed
✨ Add autodoc2_packages[autodoc] option
To turn off generating documentation
1 parent 8661219 commit d5ede5c

File tree

3 files changed

+24
-1
lines changed

3 files changed

+24
-1
lines changed

docs/apidocs/autodoc2/autodoc2.config.rst

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -123,6 +123,13 @@ API
123123

124124
.. autodoc2-docstring:: autodoc2.config.PackageConfig.exclude_files
125125

126+
.. py:attribute:: autodoc
127+
:canonical: autodoc2.config.PackageConfig.autodoc
128+
:type: bool
129+
:value: None
130+
131+
.. autodoc2-docstring:: autodoc2.config.PackageConfig.autodoc
132+
126133
.. py:method:: as_triple() -> typing.Iterable[tuple[str, typing.Any, dataclasses.Field]]
127134
:canonical: autodoc2.config.PackageConfig.as_triple
128135

src/autodoc2/config.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,13 @@ class PackageConfig:
5757
},
5858
)
5959

60+
autodoc: bool = dc.field(
61+
default=True,
62+
metadata={
63+
"help": "Whether to generate documentation for the package.",
64+
},
65+
)
66+
6067
def as_triple(self) -> t.Iterable[tuple[str, t.Any, dc.Field]]: # type: ignore[type-arg]
6168
"""Yield triples of (name, value, field)."""
6269
fields = {f.name: f for f in dc.fields(self.__class__)}
@@ -73,6 +80,7 @@ def _coerce_packages(name: str, item: t.Any) -> list[PackageConfig]:
7380
# make sure we don't mutate the original (triggers sphinx total rebuild)
7481
new = [deepcopy(i) for i in item]
7582
for i, package in enumerate(new[:]):
83+
# TODO here we should use the PackageConfig to validate
7684
if isinstance(package, str):
7785
new[i] = package = {"path": package}
7886
if not isinstance(package, dict):
@@ -105,6 +113,8 @@ def _coerce_packages(name: str, item: t.Any) -> list[PackageConfig]:
105113
or not all(isinstance(x, str) for x in package[key])
106114
):
107115
raise ValidationError(f"{name}[{i}][{key!r}] must be a list of strings")
116+
if "autodoc" in package and not isinstance(package["autodoc"], bool):
117+
raise ValidationError(f"{name}[{i}]['autodoc'] must be a boolean")
108118

109119
return [PackageConfig(**p) for p in new]
110120

src/autodoc2/sphinx/extension.py

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -176,6 +176,13 @@ def run_autodoc_package(app: Sphinx, config: Config, pkg_index: int) -> str | No
176176
)
177177
autodoc2_cache[path.as_posix()] = {"hash": hash_str, "db": db}
178178

179+
output = Path(app.srcdir) / PurePosixPath(config.output_dir) / root_module
180+
181+
if not package.autodoc:
182+
if output.exists() and output.is_dir():
183+
shutil.rmtree(output)
184+
return None
185+
179186
# find all the package/module, so we know what files to write
180187
LOGGER.info("[Autodoc2] Determining files to write ...")
181188
to_write: t.List[str] = []
@@ -195,7 +202,6 @@ def _warn_render(msg: str, type_: WarningSubtypes) -> None:
195202
warn_sphinx(msg, type_)
196203

197204
# write the files
198-
output = Path(app.srcdir) / PurePosixPath(config.output_dir) / root_module
199205
output.mkdir(parents=True, exist_ok=True)
200206
paths = []
201207
for mod_name in status_iterator(

0 commit comments

Comments
 (0)