3 from cgi import parse_qsl
4 from getopt import getopt
6 from m_lib.defenc import default_encoding
8 # This must be imported and called before webbrowser
9 # because webbrowser reads BROWSER environment variable at the import time
10 from browser_stack import set_current_browser
16 sys.exit('Usage: %s [-e|--encoding=encoding] [-n|--newwin|-t|--tab] URL' % sys.argv[0])
19 options, arguments = getopt(sys.argv[1:], 'e:nt', ['encoding=', 'newwin', 'tab'])
23 if len(arguments) <> 1:
29 for option, value in options:
30 if option in ('-e', '--encoding'):
32 elif option in ('-n', '--newwin'):
34 elif option in ('-t', '--tab'):
38 encoding = default_encoding
41 protocol, request = urllib.splittype(url)
42 user, password, port = None, None, None
43 host, path = urllib.splithost(request)
45 user, host = urllib.splituser(host)
47 user, password = urllib.splitpasswd(user)
48 host, port = urllib.splitport(host)
49 if port: port = int(port)
50 path, tag = urllib.splittag(path)
51 path, query = urllib.splitquery(path)
52 path = urllib.unquote(path)
53 if tag: tag = urllib.unquote_plus(tag)
57 for name, value in parse_qsl(query):
58 name = unicode(name, default_encoding).encode(encoding)
59 value = unicode(value, default_encoding).encode(encoding)
60 qlist.append((name, value))
62 url = protocol + "://"
64 url += urllib.quote(unicode(user, default_encoding).encode(encoding))
66 url += ':' + urllib.quote(unicode(password, default_encoding).encode(encoding))
69 url += host.decode(encoding).encode('idna')
73 if protocol == "file":
74 url += urllib.quote(path)
76 url += urllib.quote(unicode(path, default_encoding).encode(encoding))
78 url += '?' + urllib.urlencode(qlist)
80 url += '#' + urllib.quote_plus(unicode(tag, default_encoding).encode(encoding))
82 webbrowser.open(url, new)