Skip to content

Commit bda901a

Browse files
committed
Refactor get_builder() into a Builder callable
1 parent 0ffc86f commit bda901a

File tree

2 files changed

+28
-24
lines changed

2 files changed

+28
-24
lines changed

sphinx_autobuild/__main__.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
from livereload import Server
1111

1212
from sphinx_autobuild import __version__
13-
from sphinx_autobuild.build import SPHINX_BUILD_OPTIONS, get_builder
13+
from sphinx_autobuild.build import SPHINX_BUILD_OPTIONS, Builder
1414
from sphinx_autobuild.ignore import get_ignore
1515
from sphinx_autobuild.utils import find_free_port
1616

@@ -176,7 +176,7 @@ def main():
176176
server = Server()
177177

178178
build_args, pre_build_commands = _get_build_args(args)
179-
builder = get_builder(
179+
builder = Builder(
180180
server.watcher,
181181
build_args,
182182
host=args.host,

sphinx_autobuild/build.py

Lines changed: 26 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -51,39 +51,43 @@ def show(*, context=None, command=None):
5151
_log(msg, colour=Fore.BLUE)
5252

5353

54-
def get_builder(watcher, sphinx_args, *, host, port, pre_build_commands):
55-
"""Prepare the function that calls sphinx."""
56-
sphinx_command = [sys.executable, "-m", "sphinx"] + sphinx_args
54+
class Builder:
55+
def __init__(self, watcher, sphinx_args, *, host, port, pre_build_commands):
56+
self.watcher = watcher
57+
self.sphinx_args = sphinx_args
58+
self.pre_build_commands = pre_build_commands
59+
self.uri = f"http://{host}:{port}"
5760

58-
def build():
61+
def __call__(self):
5962
"""Generate the documentation using ``sphinx``."""
60-
if watcher.filepath:
61-
show(context=f"Detected change: {watcher.filepath}")
63+
64+
sphinx_command = [sys.executable, "-m", "sphinx"] + self.sphinx_args
65+
66+
if self.watcher.filepath:
67+
show(context=f"Detected change: {self.watcher.filepath}")
6268

6369
try:
64-
for command in pre_build_commands:
70+
for command in self.pre_build_commands:
6571
show(context="pre-build", command=command)
6672
subprocess.run(command, check=True)
6773

68-
show(command=["sphinx-build"] + sphinx_args)
74+
show(command=["sphinx-build"] + self.sphinx_args)
6975
subprocess.run(sphinx_command, check=True)
7076
except subprocess.CalledProcessError as e:
71-
cmd_exit(e.returncode)
77+
self.cmd_exit(e.returncode)
7278
finally:
7379
# We present this information, so that the user does not need to keep track
7480
# of the port being used. It is presented by livereload when starting the
7581
# server, so don't present it in the initial build.
76-
if watcher.filepath:
77-
show(context=f"Serving on http://{host}:{port}")
78-
79-
return build
80-
82+
if self.watcher.filepath:
83+
show(context=f"Serving on {self.uri}")
8184

82-
def cmd_exit(return_code):
83-
print(f"Command exited with exit code: {return_code}")
84-
print(
85-
"The server will continue serving the build folder, but the contents "
86-
"being served are no longer in sync with the documentation sources. "
87-
"Please fix the cause of the error above or press Ctrl+C to stop the "
88-
"server."
89-
)
85+
@staticmethod
86+
def cmd_exit(return_code):
87+
print(f"Command exited with exit code: {return_code}")
88+
print(
89+
"The server will continue serving the build folder, but the contents "
90+
"being served are no longer in sync with the documentation sources. "
91+
"Please fix the cause of the error above or press Ctrl+C to stop the "
92+
"server."
93+
)

0 commit comments

Comments
 (0)