X-Git-Url: https://git.phdru.name/?a=blobdiff_plain;f=bin%2Fwebbrowser-encode-url;h=cc8f8279e8bffc085c0fcb6ad4933a2e16feec8f;hb=HEAD;hp=e82c48c1234763722b266b5447a396996d36fe05;hpb=ca6727b0c6d3bc59ddc14b4b90d2eaa205cd92f6;p=dotfiles.git diff --git a/bin/webbrowser-encode-url b/bin/webbrowser-encode-url index e82c48c..e30b6ff 100755 --- a/bin/webbrowser-encode-url +++ b/bin/webbrowser-encode-url @@ -1,17 +1,9 @@ -#! /usr/bin/env python +#! /usr/bin/env python3 -try: - from urllib.parse import parse_qsl, urlencode, \ - quote, unquote, unquote_plus, \ - splittype, splithost, splituser, splitpasswd, \ - splitport, splittag, splitquery -except ImportError: - from cgi import parse_qsl - from urllib import urlencode, quote, unquote, unquote_plus, \ - splittype, splithost, splituser, splitpasswd, \ - splitport, splittag, splitquery from getopt import getopt, GetoptError +from urllib.parse import urlsplit, parse_qsl, quote, quote_plus, urlencode import sys + from m_lib.defenc import default_encoding # This must be imported and called before webbrowser @@ -19,13 +11,17 @@ from m_lib.defenc import default_encoding from browser_stack import set_current_browser set_current_browser() -import webbrowser +import webbrowser # noqa: E402 module level import not at top of file + def usage(): - sys.exit('Usage: %s [-e|--encoding=encoding] [-n|--newwin|-t|--tab] URL' % sys.argv[0]) + sys.exit('Usage: %s [-e|--encoding=encoding] [-n|--newwin|-t|--tab] URL' + % sys.argv[0]) + try: - options, arguments = getopt(sys.argv[1:], 'e:nt', ['encoding=', 'newwin', 'tab']) + options, arguments = getopt( + sys.argv[1:], 'e:nt', ['encoding=', 'newwin', 'tab']) except GetoptError: usage() @@ -47,45 +43,52 @@ if not encoding: encoding = default_encoding url = arguments[0] -protocol, request = splittype(url) -user, password, port = None, None, None -host, path = splithost(request) -if host: - user, host = splituser(host) - if user: - user, password = splitpasswd(user) - host, port = splitport(host) - if port: port = int(port) -path, tag = splittag(path) -path, query = splitquery(path) -path = unquote(path) -if tag: tag = unquote_plus(tag) + +split_results = urlsplit(url) +protocol, netloc, path, query, tag = split_results +user = split_results.username +password = split_results.password +host = split_results.hostname +port = split_results.port if query: - qlist = [] - for name, value in parse_qsl(query): - name = unicode(name, default_encoding).encode(encoding) - value = unicode(value, default_encoding).encode(encoding) - qlist.append((name, value)) + qlist = [] + for name, value in parse_qsl(query): + if isinstance(name, bytes): + name = name.decode(default_encoding) + value = value.decode(default_encoding) + name = name.encode(encoding) + value = value.encode(encoding) + qlist.append((name, value)) url = protocol + "://" if user: - url += quote(unicode(user, default_encoding).encode(encoding)) - if password: - url += ':' + quote(unicode(password, default_encoding).encode(encoding)) - url += '@' + if isinstance(user, bytes): + user = user.decode(default_encoding) + url += quote(user.encode(encoding)) + if password: + if isinstance(password, bytes): + password = password.decode(default_encoding) + url += ':' + quote(password.encode(encoding)) + url += '@' if host: - url += host.decode(encoding).encode('idna') - if port: - url += ':%d' % port + if isinstance(host, bytes): + host = host.decode(encoding) + url += host.encode('idna').decode('ascii') + if port: + url += ':%d' % port if path: - if protocol == "file": - url += quote(path) - else: - url += quote(unicode(path, default_encoding).encode(encoding)) + if protocol == "file": + url += quote(path) + else: + if isinstance(path, bytes): + path = path.decode(default_encoding) + url += quote(path.encode(encoding)) if query: - url += '?' + urlencode(qlist) + url += '?' + urlencode(qlist) if tag: - url += '#' + quote_plus(unicode(tag, default_encoding).encode(encoding)) + if isinstance(tag, bytes): + tag = tag.decode(default_encoding) + url += '#' + quote_plus(tag.encode(encoding)) webbrowser.open(url, new)