Skip to content

Commit

Permalink
Introduce utils.open_browser
Browse files Browse the repository at this point in the history
  • Loading branch information
AA-Turner committed Apr 8, 2024
1 parent 4a3edcb commit 99449ee
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 8 deletions.
13 changes: 5 additions & 8 deletions sphinx_autobuild/__main__.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
from sphinx_autobuild import __version__
from sphinx_autobuild.build import Builder
from sphinx_autobuild.filter import IgnoreFilter
from sphinx_autobuild.utils import find_free_port
from sphinx_autobuild.utils import find_free_port, open_browser


def main():
Expand Down Expand Up @@ -55,12 +55,10 @@ def main():
if not args.no_initial_build:
builder()

if args.openbrowser is True:
server.serve(
port=port_num, host=args.host, root=outdir, open_url_delay=args.delay
)
else:
server.serve(port=port_num, host=args.host, root=outdir)
if args.open_browser:
open_browser(f"{args.host}:{port_num}", args.delay)

server.serve(port=port_num, host=args.host, root=outdir)


def _parse_args(argv):
Expand Down Expand Up @@ -156,7 +154,6 @@ def _add_autobuild_arguments(parser):
)
group.add_argument(
"--open-browser",
dest="openbrowser",
action="store_true",
default=False,
help="open the browser after building documentation",
Expand Down
13 changes: 13 additions & 0 deletions sphinx_autobuild/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@

import shlex
import socket
import threading
import time
import webbrowser

from colorama import Fore, Style

Expand All @@ -17,6 +20,16 @@ def find_free_port():
return s.getsockname()[1]


def open_browser(url_host: str, delay: int) -> None:
def _opener():
time.sleep(delay)
webbrowser.open(f"http://{url_host}")

t = threading.Thread(target=_opener)
t.start()
t.join()


def _log(text, *, colour):
print(f"{Fore.GREEN}[sphinx-autobuild] {colour}{text}{Style.RESET_ALL}")

Expand Down

0 comments on commit 99449ee

Please sign in to comment.