Skip to content

Commit 2c117bb

Browse files
committed
Split out sphinx.ext.apidoc._shared
1 parent 5176a8f commit 2c117bb

File tree

4 files changed

+38
-27
lines changed

4 files changed

+38
-27
lines changed

sphinx/ext/apidoc/__init__.py

Lines changed: 2 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -13,28 +13,9 @@
1313

1414
from typing import TYPE_CHECKING
1515

16-
from sphinx.locale import __
17-
from sphinx.util import logging
16+
from sphinx.ext.apidoc._cli import main
1817

1918
if TYPE_CHECKING:
2019
from collections.abc import Sequence
21-
from pathlib import Path
2220

23-
logger = logging.getLogger(__name__)
24-
25-
26-
def _remove_old_files(
27-
written_files: Sequence[Path], destdir: Path, suffix: str
28-
) -> None:
29-
files_to_keep = frozenset(written_files)
30-
for existing in destdir.rglob(f'*.{suffix}'):
31-
if existing not in files_to_keep:
32-
try:
33-
existing.unlink()
34-
except OSError as exc:
35-
logger.warning(
36-
__('Failed to remove %s: %s'),
37-
existing,
38-
exc.strerror,
39-
type='autodoc',
40-
)
21+
__all__: Sequence[str] = ('main',)

sphinx/ext/apidoc/_cli.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,12 +13,12 @@
1313
import sphinx.locale
1414
from sphinx import __display_version__
1515
from sphinx.cmd.quickstart import EXTENSIONS
16-
from sphinx.ext.apidoc import _remove_old_files, logger
1716
from sphinx.ext.apidoc._generate import (
1817
CliOptions,
1918
create_modules_toc_file,
2019
recurse_tree,
2120
)
21+
from sphinx.ext.apidoc._shared import LOGGER, _remove_old_files
2222
from sphinx.locale import __
2323
from sphinx.util.osutil import ensuredir
2424

@@ -293,7 +293,7 @@ def _parse_args(argv: Sequence[str], /) -> CliOptions:
293293
args.module_path = root_path = Path(args.module_path).resolve()
294294
args.destdir = Path(args.destdir)
295295
if not root_path.is_dir():
296-
logger.error(__('%s is not a directory.'), root_path)
296+
LOGGER.error(__('%s is not a directory.'), root_path)
297297
raise SystemExit(1)
298298

299299
if args.header is None:

sphinx/ext/apidoc/_generate.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
from typing import TYPE_CHECKING
1010

1111
from sphinx import package_dir
12-
from sphinx.ext.apidoc import logger
12+
from sphinx.ext.apidoc._shared import LOGGER
1313
from sphinx.locale import __
1414
from sphinx.util.osutil import FileAvoidWrite
1515
from sphinx.util.template import ReSTRenderer
@@ -64,14 +64,14 @@ def write_file(name: str, text: str, opts: CliOptions) -> Path:
6464
fname = Path(opts.destdir, f'{name}.{opts.suffix}')
6565
if opts.dryrun:
6666
if not opts.quiet:
67-
logger.info(__('Would create file %s.'), fname)
67+
LOGGER.info(__('Would create file %s.'), fname)
6868
return fname
6969
if not opts.force and fname.is_file():
7070
if not opts.quiet:
71-
logger.info(__('File %s already exists, skipping.'), fname)
71+
LOGGER.info(__('File %s already exists, skipping.'), fname)
7272
else:
7373
if not opts.quiet:
74-
logger.info(__('Creating file %s.'), fname)
74+
LOGGER.info(__('Creating file %s.'), fname)
7575
with FileAvoidWrite(fname) as f:
7676
f.write(text)
7777
return fname

sphinx/ext/apidoc/_shared.py

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
from __future__ import annotations
2+
3+
from typing import TYPE_CHECKING
4+
5+
from sphinx.locale import __
6+
from sphinx.util import logging
7+
8+
if TYPE_CHECKING:
9+
from collections.abc import Sequence
10+
from pathlib import Path
11+
from typing import Final
12+
13+
LOGGER: Final[logging.SphinxLoggerAdapter] = logging.getLogger('sphinx.ext.apidoc')
14+
15+
16+
def _remove_old_files(
17+
written_files: Sequence[Path], destdir: Path, suffix: str
18+
) -> None:
19+
files_to_keep = frozenset(written_files)
20+
for existing in destdir.rglob(f'*.{suffix}'):
21+
if existing not in files_to_keep:
22+
try:
23+
existing.unlink()
24+
except OSError as exc:
25+
LOGGER.warning(
26+
__('Failed to remove %s: %s'),
27+
existing,
28+
exc.strerror,
29+
type='autodoc',
30+
)

0 commit comments

Comments
 (0)