Skip to content

Commit

Permalink
Split show() into show_message() and show_command()
Browse files Browse the repository at this point in the history
  • Loading branch information
AA-Turner committed Oct 2, 2024
1 parent 829ec71 commit 2e4e6f7
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 21 deletions.
8 changes: 4 additions & 4 deletions sphinx_autobuild/__main__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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=()):
Expand Down Expand Up @@ -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):
Expand Down
17 changes: 8 additions & 9 deletions sphinx_autobuild/build.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand All @@ -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}")
Expand All @@ -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(
Expand All @@ -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}")
18 changes: 10 additions & 8 deletions sphinx_autobuild/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)

0 comments on commit 2e4e6f7

Please sign in to comment.