From: Oleg Broytman Date: Wed, 7 Jun 2023 08:06:46 +0000 (+0300) Subject: Feat(bin/webbrowser-encode-url): Port to Python 2+3 X-Git-Url: https://git.phdru.name/?a=commitdiff_plain;h=ca6727b0c6d3bc59ddc14b4b90d2eaa205cd92f6;p=dotfiles.git Feat(bin/webbrowser-encode-url): Port to Python 2+3 --- diff --git a/bin/webbrowser-encode-url b/bin/webbrowser-encode-url index 2dc183f..e82c48c 100755 --- a/bin/webbrowser-encode-url +++ b/bin/webbrowser-encode-url @@ -1,8 +1,17 @@ #! /usr/bin/env python -from cgi import parse_qsl +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 -import sys, urllib +import sys from m_lib.defenc import default_encoding # This must be imported and called before webbrowser @@ -20,7 +29,7 @@ try: except GetoptError: usage() -if len(arguments) <> 1: +if len(arguments) != 1: usage() encoding = None @@ -38,19 +47,19 @@ if not encoding: encoding = default_encoding url = arguments[0] -protocol, request = urllib.splittype(url) +protocol, request = splittype(url) user, password, port = None, None, None -host, path = urllib.splithost(request) +host, path = splithost(request) if host: - user, host = urllib.splituser(host) + user, host = splituser(host) if user: - user, password = urllib.splitpasswd(user) - host, port = urllib.splitport(host) + user, password = splitpasswd(user) + host, port = splitport(host) if port: port = int(port) -path, tag = urllib.splittag(path) -path, query = urllib.splitquery(path) -path = urllib.unquote(path) -if tag: tag = urllib.unquote_plus(tag) +path, tag = splittag(path) +path, query = splitquery(path) +path = unquote(path) +if tag: tag = unquote_plus(tag) if query: qlist = [] @@ -61,9 +70,9 @@ if query: url = protocol + "://" if user: - url += urllib.quote(unicode(user, default_encoding).encode(encoding)) + url += quote(unicode(user, default_encoding).encode(encoding)) if password: - url += ':' + urllib.quote(unicode(password, default_encoding).encode(encoding)) + url += ':' + quote(unicode(password, default_encoding).encode(encoding)) url += '@' if host: url += host.decode(encoding).encode('idna') @@ -71,12 +80,12 @@ if host: url += ':%d' % port if path: if protocol == "file": - url += urllib.quote(path) + url += quote(path) else: - url += urllib.quote(unicode(path, default_encoding).encode(encoding)) + url += quote(unicode(path, default_encoding).encode(encoding)) if query: - url += '?' + urllib.urlencode(qlist) + url += '?' + urlencode(qlist) if tag: - url += '#' + urllib.quote_plus(unicode(tag, default_encoding).encode(encoding)) + url += '#' + quote_plus(unicode(tag, default_encoding).encode(encoding)) webbrowser.open(url, new)