-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathopen_url.py
44 lines (35 loc) · 964 Bytes
/
open_url.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
"""open_url: Open a URL in the system default web browser
Uses os.system instead of the webbrowser module to work around a
limitation in Sublime Merge.
"""
import os
import re
import sys
urlRegex = re.compile(
# http:// or https://
r"^https?://"
# domain...
r"(?:(?:[A-Z0-9](?:[A-Z0-9-]{0,61}[A-Z0-9])?\.)+(?:[A-Z]{2,6}\.?|[A-Z0-9-]{2,}\.?)|"
# localhost...
r"localhost|"
# ...or ip
r"\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3})"
# optional port
r"(?::\d+)?"
# end of URL
r"(?:/?|[/?][^\"\s]+)$",
re.IGNORECASE,
)
def OpenUrl(url):
"""Validate and (if successfully validated) open a URL
Opens the URL in the user's default browser.
"""
if urlRegex.match(url):
if sys.platform.startswith("win"):
utility = "explorer"
else:
utility = "open"
os.system('{} "{}"'.format(utility, url))
if __name__ == "__main__":
url = sys.argv[1]
OpenUrl(url)