Split out `sphinx.ext.apidoc._shared`

This commit is contained in:
Adam Turner 2025-01-07 10:41:40 +00:00
parent 5176a8f33a
commit 2c117bbbf0
4 changed files with 38 additions and 27 deletions

View File

@ -13,28 +13,9 @@ from __future__ import annotations
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',)

View File

@ -13,12 +13,12 @@ from typing import TYPE_CHECKING, Any
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:

View File

@ -9,7 +9,7 @@ from pathlib import Path
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

View File

@ -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',
)