4 from urllib.parse import parse_qsl, urlencode, \
5 quote, unquote, unquote_plus, \
6 splittype, splithost, splituser, splitpasswd, \
7 splitport, splittag, splitquery
9 from cgi import parse_qsl
10 from urllib import urlencode, quote, unquote, unquote_plus, \
11 splittype, splithost, splituser, splitpasswd, \
12 splitport, splittag, splitquery
13 from getopt import getopt, GetoptError
15 from m_lib.defenc import default_encoding
17 # This must be imported and called before webbrowser
18 # because webbrowser reads BROWSER environment variable at the import time
19 from browser_stack import set_current_browser
25 sys.exit('Usage: %s [-e|--encoding=encoding] [-n|--newwin|-t|--tab] URL' % sys.argv[0])
28 options, arguments = getopt(sys.argv[1:], 'e:nt', ['encoding=', 'newwin', 'tab'])
32 if len(arguments) != 1:
38 for option, value in options:
39 if option in ('-e', '--encoding'):
41 elif option in ('-n', '--newwin'):
43 elif option in ('-t', '--tab'):
47 encoding = default_encoding
50 protocol, request = splittype(url)
51 user, password, port = None, None, None
52 host, path = splithost(request)
54 user, host = splituser(host)
56 user, password = splitpasswd(user)
57 host, port = splitport(host)
58 if port: port = int(port)
59 path, tag = splittag(path)
60 path, query = splitquery(path)
62 if tag: tag = unquote_plus(tag)
66 for name, value in parse_qsl(query):
67 name = unicode(name, default_encoding).encode(encoding)
68 value = unicode(value, default_encoding).encode(encoding)
69 qlist.append((name, value))
71 url = protocol + "://"
73 url += quote(unicode(user, default_encoding).encode(encoding))
75 url += ':' + quote(unicode(password, default_encoding).encode(encoding))
78 url += host.decode(encoding).encode('idna')
82 if protocol == "file":
85 url += quote(unicode(path, default_encoding).encode(encoding))
87 url += '?' + urlencode(qlist)
89 url += '#' + quote_plus(unicode(tag, default_encoding).encode(encoding))
91 webbrowser.open(url, new)