From 33aba4b0845d3c278fb0887d750f1feae5e04d78 Mon Sep 17 00:00:00 2001 From: Chris Wood Date: Thu, 17 Jul 2014 00:59:27 -0400 Subject: [PATCH] Add user_open() function --- i3pystatus/core/util.py | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) diff --git a/i3pystatus/core/util.py b/i3pystatus/core/util.py index aa9b124..a3498da 100644 --- a/i3pystatus/core/util.py +++ b/i3pystatus/core/util.py @@ -380,3 +380,20 @@ def make_bar(percentage): result = result + (10 - len(result)) * ' ' return result + +def user_open(url_or_command): + """Open the specified paramater in the web browser if a URL is detected, + othewrise pass the paramater to the shell as a subprocess. This function + is inteded to bu used in on_leftclick()/on_rightclick() events. + + :param url_or_command: String containing URL or command + """ + from urllib.parse import urlparse + scheme = urlparse(url_or_command).scheme + if scheme == 'http' or scheme == 'https': + import webbrowser + webbrowser.open(url_or_command) + else: + import subprocess + subprocess.Popen(url_or_command, shell=True) +