Skip to content

Commit

Permalink
Handle text browsers explicitly to support unlisted GUI browsers (#724)
Browse files Browse the repository at this point in the history
* Handle text browsers explicitly to support unlisted GUI browsers

Ref: #723

* Handle not finding a browser at all gracefully
  • Loading branch information
Jeremiah-England authored Nov 12, 2023
1 parent 31d3e12 commit aeace79
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 12 deletions.
15 changes: 15 additions & 0 deletions scalene/find_browser.py
Original file line number Diff line number Diff line change
@@ -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
10 changes: 3 additions & 7 deletions scalene/scalene_parseargs.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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)
Expand Down
9 changes: 4 additions & 5 deletions scalene/scalene_profiler.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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.
Expand Down

0 comments on commit aeace79

Please sign in to comment.