diff --git a/sphinx/ext/apidoc/__init__.py b/sphinx/ext/apidoc/__init__.py index 3d529c0c89c..b574b9bda4a 100644 --- a/sphinx/ext/apidoc/__init__.py +++ b/sphinx/ext/apidoc/__init__.py @@ -13,28 +13,9 @@ from typing import TYPE_CHECKING -from sphinx.locale import __ -from sphinx.util import logging +from sphinx.ext.apidoc._cli import main if TYPE_CHECKING: from collections.abc import Sequence - from pathlib import Path -logger = logging.getLogger(__name__) - - -def _remove_old_files( - written_files: Sequence[Path], destdir: Path, suffix: str -) -> None: - files_to_keep = frozenset(written_files) - for existing in destdir.rglob(f'*.{suffix}'): - if existing not in files_to_keep: - try: - existing.unlink() - except OSError as exc: - logger.warning( - __('Failed to remove %s: %s'), - existing, - exc.strerror, - type='autodoc', - ) +__all__: Sequence[str] = ('main',) diff --git a/sphinx/ext/apidoc/_cli.py b/sphinx/ext/apidoc/_cli.py index 5703c651526..0bf8ee4bb3f 100644 --- a/sphinx/ext/apidoc/_cli.py +++ b/sphinx/ext/apidoc/_cli.py @@ -13,12 +13,12 @@ import sphinx.locale from sphinx import __display_version__ from sphinx.cmd.quickstart import EXTENSIONS -from sphinx.ext.apidoc import _remove_old_files, logger from sphinx.ext.apidoc._generate import ( CliOptions, create_modules_toc_file, recurse_tree, ) +from sphinx.ext.apidoc._shared import LOGGER, _remove_old_files from sphinx.locale import __ from sphinx.util.osutil import ensuredir @@ -293,7 +293,7 @@ def _parse_args(argv: Sequence[str], /) -> CliOptions: args.module_path = root_path = Path(args.module_path).resolve() args.destdir = Path(args.destdir) if not root_path.is_dir(): - logger.error(__('%s is not a directory.'), root_path) + LOGGER.error(__('%s is not a directory.'), root_path) raise SystemExit(1) if args.header is None: diff --git a/sphinx/ext/apidoc/_generate.py b/sphinx/ext/apidoc/_generate.py index 708e7ca35d7..868cd80e50d 100644 --- a/sphinx/ext/apidoc/_generate.py +++ b/sphinx/ext/apidoc/_generate.py @@ -9,7 +9,7 @@ from typing import TYPE_CHECKING from sphinx import package_dir -from sphinx.ext.apidoc import logger +from sphinx.ext.apidoc._shared import LOGGER from sphinx.locale import __ from sphinx.util.osutil import FileAvoidWrite from sphinx.util.template import ReSTRenderer @@ -64,14 +64,14 @@ def write_file(name: str, text: str, opts: CliOptions) -> Path: fname = Path(opts.destdir, f'{name}.{opts.suffix}') if opts.dryrun: if not opts.quiet: - logger.info(__('Would create file %s.'), fname) + LOGGER.info(__('Would create file %s.'), fname) return fname if not opts.force and fname.is_file(): if not opts.quiet: - logger.info(__('File %s already exists, skipping.'), fname) + LOGGER.info(__('File %s already exists, skipping.'), fname) else: if not opts.quiet: - logger.info(__('Creating file %s.'), fname) + LOGGER.info(__('Creating file %s.'), fname) with FileAvoidWrite(fname) as f: f.write(text) return fname diff --git a/sphinx/ext/apidoc/_shared.py b/sphinx/ext/apidoc/_shared.py new file mode 100644 index 00000000000..7489f9d3e6e --- /dev/null +++ b/sphinx/ext/apidoc/_shared.py @@ -0,0 +1,30 @@ +from __future__ import annotations + +from typing import TYPE_CHECKING + +from sphinx.locale import __ +from sphinx.util import logging + +if TYPE_CHECKING: + from collections.abc import Sequence + from pathlib import Path + from typing import Final + +LOGGER: Final[logging.SphinxLoggerAdapter] = logging.getLogger('sphinx.ext.apidoc') + + +def _remove_old_files( + written_files: Sequence[Path], destdir: Path, suffix: str +) -> None: + files_to_keep = frozenset(written_files) + for existing in destdir.rglob(f'*.{suffix}'): + if existing not in files_to_keep: + try: + existing.unlink() + except OSError as exc: + LOGGER.warning( + __('Failed to remove %s: %s'), + existing, + exc.strerror, + type='autodoc', + )