Skip to content

Commit 41a7d8c

Browse files
committed
Prefer pathlib
1 parent e25b8ec commit 41a7d8c

3 files changed

Lines changed: 14 additions & 23 deletions

File tree

sphinx_autobuild/__main__.py

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
"""Entrypoint for ``python -m sphinx_autobuild``."""
22

33
import argparse
4-
import os
54
import shlex
65
import sys
6+
from pathlib import Path
77

88
import colorama
99
import uvicorn
@@ -33,10 +33,9 @@ def main(argv=()):
3333

3434
args, build_args = _parse_args(list(argv))
3535

36-
src_dir = args.sourcedir
37-
out_dir = args.outdir
38-
if not os.path.exists(out_dir):
39-
os.makedirs(out_dir)
36+
src_dir = Path(args.sourcedir)
37+
out_dir = Path(args.outdir)
38+
out_dir.mkdir(parents=True, exist_ok=True)
4039

4140
host_name = args.host
4241
port_num = args.port or find_free_port()
@@ -53,7 +52,7 @@ def main(argv=()):
5352
watch_dirs = [src_dir] + args.additional_watched_dirs
5453
ignore_dirs = args.ignore + [out_dir, args.warnings_file, args.doctree_dir]
5554
ignore_handler = IgnoreFilter(
56-
[p for p in ignore_dirs if p],
55+
[Path(p).as_posix() for p in ignore_dirs if p],
5756
args.re_ignore,
5857
)
5958
app = _create_app(watch_dirs, ignore_handler, builder, out_dir, url_host)
@@ -98,14 +97,14 @@ def _parse_args(argv):
9897
args, build_args = parser.parse_known_args(argv.copy())
9998

10099
# Copy needed settings
101-
args.sourcedir = os.path.realpath(sphinx_args.sourcedir)
102-
args.outdir = os.path.realpath(sphinx_args.outputdir)
100+
args.sourcedir = Path(sphinx_args.sourcedir).resolve(strict=True)
101+
args.outdir = Path(sphinx_args.outputdir).resolve(strict=True)
103102
if sphinx_args.doctreedir:
104-
args.doctree_dir = os.path.realpath(sphinx_args.doctreedir)
103+
args.doctree_dir = Path(sphinx_args.doctreedir).resolve(strict=True)
105104
else:
106105
args.doctree_dir = None
107106
if sphinx_args.warnfile:
108-
args.warnings_file = os.path.realpath(sphinx_args.warnfile)
107+
args.warnings_file = Path(sphinx_args.warnfile).resolve(strict=True)
109108
else:
110109
args.warnings_file = None
111110

sphinx_autobuild/filter.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
"""Logic for ignoring paths."""
22

33
import fnmatch
4-
import os
54
import re
65

76

@@ -21,7 +20,8 @@ def __call__(self, path):
2120
"""Determine if 'path' should be ignored."""
2221
# Any regular pattern matches.
2322
for pattern in self.regular_patterns:
24-
if path.startswith((pattern + os.sep, pattern + "/")):
23+
# separators are normalised before creating the IgnoreFilter
24+
if path.startswith(f"{pattern}/"):
2525
return True
2626
if fnmatch.fnmatch(path, pattern):
2727
return True

sphinx_autobuild/server.py

Lines changed: 3 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,16 @@
11
from __future__ import annotations
22

33
import asyncio
4-
import os
5-
import sys
64
from concurrent.futures import ProcessPoolExecutor
75
from contextlib import AbstractAsyncContextManager, asynccontextmanager
6+
from pathlib import Path
87
from typing import TYPE_CHECKING
98

109
import watchfiles
1110
from starlette.websockets import WebSocket
1211

1312
if TYPE_CHECKING:
13+
import os
1414
from collections.abc import Callable
1515

1616
from starlette.types import Receive, Scope, Send
@@ -25,15 +25,7 @@ def __init__(
2525
ignore_filter: IgnoreFilter,
2626
change_callback: Callable[[], None],
2727
) -> None:
28-
if sys.version_info[:2] >= (3, 10):
29-
self.paths = [os.path.realpath(path, strict=True) for path in paths]
30-
else:
31-
self.paths = [os.path.realpath(path) for path in paths]
32-
# Sanity check the paths
33-
for p in self.paths:
34-
if not os.path.exists(p):
35-
raise FileNotFoundError(p)
36-
28+
self.paths = [Path(path).resolve(strict=True) for path in paths]
3729
self.ignore = ignore_filter
3830
self.change_callback = change_callback
3931
self.flag = asyncio.Event()

0 commit comments

Comments
 (0)