@@ -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