]> git.phdru.name Git - dotfiles.git/blob - bin/webbrowser-encode-url
unzip.py: Fix encoding
[dotfiles.git] / bin / webbrowser-encode-url
1 #! /usr/bin/env python3
2
3 try:
4     from urllib.parse import parse_qsl, urlencode, \
5         quote, quote_plus, unquote, unquote_plus, \
6         splittype, splithost, splituser, splitpasswd, \
7         splitport, splittag, splitquery
8 except ImportError:
9     from cgi import parse_qsl
10     from urllib import urlencode, quote, quote_plus, unquote, unquote_plus, \
11         splittype, splithost, splituser, splitpasswd, \
12         splitport, splittag, splitquery
13 from getopt import getopt, GetoptError
14 import sys
15 from m_lib.defenc import default_encoding
16
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
20 set_current_browser()
21
22 import webbrowser
23
24 def usage():
25     sys.exit('Usage: %s [-e|--encoding=encoding] [-n|--newwin|-t|--tab] URL' % sys.argv[0])
26
27 try:
28     options, arguments = getopt(sys.argv[1:], 'e:nt', ['encoding=', 'newwin', 'tab'])
29 except GetoptError:
30     usage()
31
32 if len(arguments) != 1:
33     usage()
34
35 encoding = None
36 new = 0
37
38 for option, value in options:
39     if option in ('-e', '--encoding'):
40         encoding = value
41     elif option in ('-n', '--newwin'):
42         new = 1
43     elif option in ('-t', '--tab'):
44         new = 2
45
46 if not encoding:
47     encoding = default_encoding
48
49 url = arguments[0]
50 protocol, request = splittype(url)
51 user, password, port = None, None, None
52 host, path = splithost(request)
53 if host:
54     user, host = splituser(host)
55     if user:
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)
61 path = unquote(path)
62 if tag: tag = unquote_plus(tag)
63
64 if query:
65     qlist = []
66     for name, value in parse_qsl(query):
67         if isinstance(name, bytes):
68             name = name.decode(default_encoding)
69             value = value.decode(default_encoding)
70         name = name.encode(encoding)
71         value = value.encode(encoding)
72         qlist.append((name, value))
73
74 url = protocol + "://"
75 if user:
76     if isinstance(user, bytes):
77         user = user.decode(default_encoding)
78     url += quote(user.encode(encoding))
79     if password:
80         if isinstance(password, bytes):
81             password = password.decode(default_encoding)
82         url += ':' + quote(password.encode(encoding))
83     url += '@'
84 if host:
85     if isinstance(host, bytes):
86         host = host.decode(encoding)
87     url += host.encode('idna').decode('ascii')
88     if port:
89         url += ':%d' % port
90 if path:
91     if protocol == "file":
92         url += quote(path)
93     else:
94         if isinstance(path, bytes):
95             path = path.decode(default_encoding)
96         url += quote(path.encode(encoding))
97 if query:
98     url += '?' + urlencode(qlist)
99 if tag:
100     if isinstance(tag, bytes):
101         tag = tag.decode(default_encoding)
102     url += '#' + quote_plus(tag.encode(encoding))
103
104 webbrowser.open(url, new)