Skip to content

Commit

Permalink
Split out sphinx.ext.apidoc._shared
Browse files Browse the repository at this point in the history
  • Loading branch information
AA-Turner committed Jan 7, 2025
1 parent 5176a8f commit 2c117bb
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 27 deletions.
23 changes: 2 additions & 21 deletions sphinx/ext/apidoc/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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',)
4 changes: 2 additions & 2 deletions sphinx/ext/apidoc/_cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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:
Expand Down
8 changes: 4 additions & 4 deletions sphinx/ext/apidoc/_generate.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
30 changes: 30 additions & 0 deletions sphinx/ext/apidoc/_shared.py
Original file line number Diff line number Diff line change
@@ -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',
)

0 comments on commit 2c117bb

Please sign in to comment.