From aeace79cf030859efe70872f0fe5be6d34f7d204 Mon Sep 17 00:00:00 2001 From: Jeremiah England <34973839+Jeremiah-England@users.noreply.github.com> Date: Sun, 12 Nov 2023 17:25:53 -0500 Subject: [PATCH] Handle text browsers explicitly to support unlisted GUI browsers (#724) * Handle text browsers explicitly to support unlisted GUI browsers Ref: https://github.com/plasma-umass/scalene/issues/723 * Handle not finding a browser at all gracefully --- scalene/find_browser.py | 15 +++++++++++++++ scalene/scalene_parseargs.py | 10 +++------- scalene/scalene_profiler.py | 9 ++++----- 3 files changed, 22 insertions(+), 12 deletions(-) create mode 100644 scalene/find_browser.py diff --git a/scalene/find_browser.py b/scalene/find_browser.py new file mode 100644 index 000000000..a1911a8e0 --- /dev/null +++ b/scalene/find_browser.py @@ -0,0 +1,15 @@ +def find_browser(): + """Find the default browser if possible and if compatible.""" + import webbrowser + + # Mostly taken from the standard library webbrowser module. Search "console browsers" in there. + # In general, a browser belongs on this list of the scalene web GUI doesn't work in it. + # See https://github.com/plasma-umass/scalene/issues/723. + incompatible_browsers = {"www-browser", "links", "elinks", "lynx", "w3m", "links2", "links-g"} + + try: + browser = webbrowser.get() + except webbrowser.Error: + return None + + return None if browser.name in incompatible_browsers else browser diff --git a/scalene/scalene_parseargs.py b/scalene/scalene_parseargs.py index 95e0ec51f..ff5efb82a 100644 --- a/scalene/scalene_parseargs.py +++ b/scalene/scalene_parseargs.py @@ -5,6 +5,7 @@ from textwrap import dedent from typing import Any, List, NoReturn, Optional, Tuple +from scalene.find_browser import find_browser from scalene.scalene_arguments import ScaleneArguments from scalene.scalene_version import scalene_version, scalene_date @@ -354,13 +355,8 @@ def parse_args() -> Tuple[argparse.Namespace, List[str]]: # Launch the UI if `--viewer` was selected. if args.viewer: - import webbrowser - - if ( - webbrowser.get() - and type(webbrowser.get()).__name__ != "GenericBrowser" - ): - webbrowser.open(scalene_gui_url) + if browser := find_browser(): + browser.open(scalene_gui_url) else: print(f"Scalene: could not open {scalene_gui_url}.") sys.exit(0) diff --git a/scalene/scalene_profiler.py b/scalene/scalene_profiler.py index aff5008b9..e83d42b2b 100644 --- a/scalene/scalene_profiler.py +++ b/scalene/scalene_profiler.py @@ -41,6 +41,9 @@ # For debugging purposes from rich.console import Console + +from scalene.find_browser import find_browser + console = Console(style="white on blue") def nada(*args): pass @@ -1765,13 +1768,9 @@ def stop() -> None: ): # First, check for a browser. try: - if ( - not webbrowser.get() - or type(webbrowser.get()).__name__ == "GenericBrowser" - ): + if not find_browser(): # Could not open a graphical web browser tab; # act as if --web was not specified - # (GenericBrowser means text-based browsers like Lynx.) Scalene.__args.web = False else: # Force JSON output to profile.json.