Skip to content

Commit

Permalink
Prefer pathlib
Browse files Browse the repository at this point in the history
  • Loading branch information
AA-Turner committed Sep 3, 2024
1 parent e25b8ec commit 41a7d8c
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 23 deletions.
19 changes: 9 additions & 10 deletions sphinx_autobuild/__main__.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
"""Entrypoint for ``python -m sphinx_autobuild``."""

import argparse
import os
import shlex
import sys
from pathlib import Path

import colorama
import uvicorn
Expand Down Expand Up @@ -33,10 +33,9 @@ def main(argv=()):

args, build_args = _parse_args(list(argv))

src_dir = args.sourcedir
out_dir = args.outdir
if not os.path.exists(out_dir):
os.makedirs(out_dir)
src_dir = Path(args.sourcedir)
out_dir = Path(args.outdir)
out_dir.mkdir(parents=True, exist_ok=True)

host_name = args.host
port_num = args.port or find_free_port()
Expand All @@ -53,7 +52,7 @@ def main(argv=()):
watch_dirs = [src_dir] + args.additional_watched_dirs
ignore_dirs = args.ignore + [out_dir, args.warnings_file, args.doctree_dir]
ignore_handler = IgnoreFilter(
[p for p in ignore_dirs if p],
[Path(p).as_posix() for p in ignore_dirs if p],
args.re_ignore,
)
app = _create_app(watch_dirs, ignore_handler, builder, out_dir, url_host)
Expand Down Expand Up @@ -98,14 +97,14 @@ def _parse_args(argv):
args, build_args = parser.parse_known_args(argv.copy())

# Copy needed settings
args.sourcedir = os.path.realpath(sphinx_args.sourcedir)
args.outdir = os.path.realpath(sphinx_args.outputdir)
args.sourcedir = Path(sphinx_args.sourcedir).resolve(strict=True)
args.outdir = Path(sphinx_args.outputdir).resolve(strict=True)
if sphinx_args.doctreedir:
args.doctree_dir = os.path.realpath(sphinx_args.doctreedir)
args.doctree_dir = Path(sphinx_args.doctreedir).resolve(strict=True)
else:
args.doctree_dir = None
if sphinx_args.warnfile:
args.warnings_file = os.path.realpath(sphinx_args.warnfile)
args.warnings_file = Path(sphinx_args.warnfile).resolve(strict=True)
else:
args.warnings_file = None

Expand Down
4 changes: 2 additions & 2 deletions sphinx_autobuild/filter.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
"""Logic for ignoring paths."""

import fnmatch
import os
import re


Expand All @@ -21,7 +20,8 @@ def __call__(self, path):
"""Determine if 'path' should be ignored."""
# Any regular pattern matches.
for pattern in self.regular_patterns:
if path.startswith((pattern + os.sep, pattern + "/")):
# separators are normalised before creating the IgnoreFilter
if path.startswith(f"{pattern}/"):
return True
if fnmatch.fnmatch(path, pattern):
return True
Expand Down
14 changes: 3 additions & 11 deletions sphinx_autobuild/server.py
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
from __future__ import annotations

import asyncio
import os
import sys
from concurrent.futures import ProcessPoolExecutor
from contextlib import AbstractAsyncContextManager, asynccontextmanager
from pathlib import Path
from typing import TYPE_CHECKING

import watchfiles
from starlette.websockets import WebSocket

if TYPE_CHECKING:
import os
from collections.abc import Callable

from starlette.types import Receive, Scope, Send
Expand All @@ -25,15 +25,7 @@ def __init__(
ignore_filter: IgnoreFilter,
change_callback: Callable[[], None],
) -> None:
if sys.version_info[:2] >= (3, 10):
self.paths = [os.path.realpath(path, strict=True) for path in paths]
else:
self.paths = [os.path.realpath(path) for path in paths]
# Sanity check the paths
for p in self.paths:
if not os.path.exists(p):
raise FileNotFoundError(p)

self.paths = [Path(path).resolve(strict=True) for path in paths]
self.ignore = ignore_filter
self.change_callback = change_callback
self.flag = asyncio.Event()
Expand Down

0 comments on commit 41a7d8c

Please sign in to comment.