From 2e4e6f7ca44201fca7dfa8ebe69a8488b70a6323 Mon Sep 17 00:00:00 2001 From: Adam Turner <9087854+aa-turner@users.noreply.github.com> Date: Thu, 26 Sep 2024 16:04:58 +0100 Subject: [PATCH] Split ``show()`` into ``show_message()`` and ``show_command()`` --- sphinx_autobuild/__main__.py | 8 ++++---- sphinx_autobuild/build.py | 17 ++++++++--------- sphinx_autobuild/utils.py | 18 ++++++++++-------- 3 files changed, 22 insertions(+), 21 deletions(-) diff --git a/sphinx_autobuild/__main__.py b/sphinx_autobuild/__main__.py index c502d44..519259c 100644 --- a/sphinx_autobuild/__main__.py +++ b/sphinx_autobuild/__main__.py @@ -20,7 +20,7 @@ from sphinx_autobuild.filter import IgnoreFilter from sphinx_autobuild.middleware import JavascriptInjectorMiddleware from sphinx_autobuild.server import RebuildServer -from sphinx_autobuild.utils import find_free_port, open_browser, show +from sphinx_autobuild.utils import find_free_port, open_browser, show_message def main(argv=()): @@ -80,17 +80,17 @@ def main(argv=()): app = _create_app(watch_dirs, ignore_handler, builder, serve_dir, url_host) if not args.no_initial_build: - show(context="Starting initial build") + show_message("Starting initial build") builder(rebuild=False) if args.open_browser: open_browser(url_host, args.delay) - show(context="Waiting to detect changes...") + show_message("Waiting to detect changes...") try: uvicorn.run(app, host=host_name, port=port_num, log_level="warning") except KeyboardInterrupt: - show(context="Server ceasing operations. Cheerio!") + show_message("Server ceasing operations. Cheerio!") def _create_app(watch_dirs, ignore_handler, builder, out_dir, url_host): diff --git a/sphinx_autobuild/build.py b/sphinx_autobuild/build.py index b54d4d4..f4c40b1 100644 --- a/sphinx_autobuild/build.py +++ b/sphinx_autobuild/build.py @@ -3,7 +3,7 @@ import subprocess import sys -from sphinx_autobuild.utils import show +from sphinx_autobuild.utils import show_command, show_message class Builder: @@ -15,11 +15,12 @@ def __init__(self, sphinx_args, *, url_host, pre_build_commands): def __call__(self, *, rebuild: bool = True): """Generate the documentation using ``sphinx``.""" if rebuild: - show(context="Detected change. Rebuilding...") + show_message("Detected change. Rebuilding...") try: for command in self.pre_build_commands: - show(context="pre-build", command=command) + show_message("pre-build") + show_command(command) subprocess.run(command, check=True) except subprocess.CalledProcessError as e: print(f"Pre-build command exited with exit code: {e.returncode}") @@ -29,12 +30,10 @@ def __call__(self, *, rebuild: bool = True): ) raise - show(command=["python", "-m", "sphinx"] + self.sphinx_args) + sphinx_build_args = ["-m", "sphinx"] + self.sphinx_args + show_command(["python"] + sphinx_build_args) try: - subprocess.run( - [sys.executable, "-m", "sphinx"] + self.sphinx_args, - check=True, - ) + subprocess.run([sys.executable] + sphinx_build_args, check=True) except subprocess.CalledProcessError as e: print(f"Sphinx exited with exit code: {e.returncode}") print( @@ -44,4 +43,4 @@ def __call__(self, *, rebuild: bool = True): "server." ) # Remind the user of the server URL for convenience. - show(context=f"Serving on {self.uri}") + show_message(f"Serving on {self.uri}") diff --git a/sphinx_autobuild/utils.py b/sphinx_autobuild/utils.py index 62b9673..2693be3 100644 --- a/sphinx_autobuild/utils.py +++ b/sphinx_autobuild/utils.py @@ -34,11 +34,13 @@ def _log(text, *, colour): print(f"{Fore.GREEN}[sphinx-autobuild] {colour}{text}{Style.RESET_ALL}") -def show(*, context=None, command=None): - """Show context and command-to-be-executed, with nice formatting and colours.""" - if context is not None: - _log(context, colour=Fore.CYAN) - if command is not None: - assert isinstance(command, (list, tuple)) - msg = f"> {shlex.join(command)}" - _log(msg, colour=Fore.BLUE) +def show_message(context: str, /) -> None: + """Show context, with nice formatting and colours.""" + _log(context, colour=Fore.CYAN) + + +def show_command(command: list[str] | tuple[str, ...], /) -> None: + """Show command-to-be-executed, with nice formatting and colours.""" + assert isinstance(command, (list, tuple)) + msg = f"> {shlex.join(command)}" + _log(msg, colour=Fore.BLUE)