✨ Update `launch` to not print anything when opening urls (#1035)
Before adoption: this commit came in with the history the project adopted, so no landing records it.
authored by Patrick Arminio · committed by GitHub · parent af49fda · Browse files · 3 files·+120 added−1 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Diff stats
3 files+120 added−1 removed
- tests:+51 added−0 removed
- other:+69 added−1 removed
Provenance
- Commit
ee072d8- Tree
1c8801e- Author
Patrick Arminio <patrick.arminio@gmail.com>- Committer
GitHub <noreply@github.com>- Parent
af49fda
3 files changed+120 added−1 removed
added:tests/test_launch.py+51 added
1+import subprocess2+from unittest.mock import patch3+4+import pytest5+import typer6+7+url = "http://example.com"8+9+10+@pytest.mark.parametrize(11+ "system, command",12+ [13+ ("Darwin", "open"),14+ ("Linux", "xdg-open"),15+ ("FreeBSD", "xdg-open"),16+ ],17+)18+def test_launch_url_unix(system: str, command: str):19+ with patch("platform.system", return_value=system), patch(20+ "shutil.which", return_value=True21+ ), patch("subprocess.Popen") as mock_popen:22+ typer.launch(url)23+24+ mock_popen.assert_called_once_with(25+ [command, url], stdout=subprocess.DEVNULL, stderr=subprocess.STDOUT26+ )27+28+29+def test_launch_url_windows():30+ with patch("platform.system", return_value="Windows"), patch(31+ "webbrowser.open"32+ ) as mock_webbrowser_open:33+ typer.launch(url)34+35+ mock_webbrowser_open.assert_called_once_with(url)36+37+38+def test_launch_url_no_xdg_open():39+ with patch("platform.system", return_value="Linux"), patch(40+ "shutil.which", return_value=None41+ ), patch("webbrowser.open") as mock_webbrowser_open:42+ typer.launch(url)43+44+ mock_webbrowser_open.assert_called_once_with(url)45+46+47+def test_calls_original_launch_when_not_passing_urls():48+ with patch("typer.main.click.launch", return_value=0) as launch_mock:49+ typer.launch("not a url")50+51+ launch_mock.assert_called_once_with("not a url")modified:typer/__init__.py+1 added-1 removed
10 unmodified lines11 from click.termui import confirm as confirm12 from click.termui import echo_via_pager as echo_via_pager13 from click.termui import edit as edit14 from click.termui import getchar as getchar15-from click.termui import launch as launch15 from click.termui import pause as pause16 from click.termui import progressbar as progressbar17 from click.termui import prompt as prompt18 from click.termui import secho as secho7 unmodified lines26 from click.utils import open_file as open_file27 28 from . import colors as colors29 from .main import Typer as Typer30+from .main import launch as launch31 from .main import run as run32 from .models import CallbackParam as CallbackParam33 from .models import Context as Context34 from .models import FileBinaryRead as FileBinaryRead5 unmodified linesmodified:typer/main.py+68 added
1 import inspect2 import os3+import platform4+import shutil5+import subprocess6 import sys7 import traceback8 from datetime import datetime9 from enum import Enum1070 unmodified lines1080 def run(function: Callable[..., Any]) -> None:1081 app = Typer(add_completion=False)1082 app.command()(function)1083 app()1084+1085+1086+def _is_macos() -> bool:1087+ return platform.system() == "Darwin"1088+1089+1090+def _is_linux_or_bsd() -> bool:1091+ if platform.system() == "Linux":1092+ return True1093+1094+ return "BSD" in platform.system()1095+1096+1097+def launch(url: str, wait: bool = False, locate: bool = False) -> int:1098+ """This function launches the given URL (or filename) in the default1099+ viewer application for this file type. If this is an executable, it1100+ might launch the executable in a new session. The return value is1101+ the exit code of the launched application. Usually, ``0`` indicates1102+ success.1103+1104+ This function handles url in different operating systems separately:1105+ - On macOS (Darwin), it uses the 'open' command.1106+ - On Linux and BSD, it uses 'xdg-open' if available.1107+ - On Windows (and other OSes), it uses the standard webbrowser module.1108+1109+ The function avoids, when possible, using the webbrowser module on Linux and macOS1110+ to prevent spammy terminal messages from some browsers (e.g., Chrome).1111+1112+ Examples::1113+1114+ typer.launch("https://typer.tiangolo.com/")1115+ typer.launch("/my/downloaded/file", locate=True)1116+1117+ :param url: URL or filename of the thing to launch.1118+ :param wait: Wait for the program to exit before returning. This1119+ only works if the launched program blocks. In particular,1120+ ``xdg-open`` on Linux does not block.1121+ :param locate: if this is set to `True` then instead of launching the1122+ application associated with the URL it will attempt to1123+ launch a file manager with the file located. This1124+ might have weird effects if the URL does not point to1125+ the filesystem.1126+ """1127+1128+ if url.startswith("http://") or url.startswith("https://"):1129+ if _is_macos():1130+ return subprocess.Popen(1131+ ["open", url], stdout=subprocess.DEVNULL, stderr=subprocess.STDOUT1132+ ).wait()1133+1134+ has_xdg_open = _is_linux_or_bsd() and shutil.which("xdg-open") is not None1135+1136+ if has_xdg_open:1137+ return subprocess.Popen(1138+ ["xdg-open", url], stdout=subprocess.DEVNULL, stderr=subprocess.STDOUT1139+ ).wait()1140+1141+ import webbrowser1142+1143+ webbrowser.open(url)1144+1145+ return 01146+1147+ else:1148+ return click.launch(url)