From 05f0cc396bc24da45fa1a8b0a79c97c79f399465 Mon Sep 17 00:00:00 2001 From: Oleg Broytman Date: Wed, 1 Nov 2023 18:32:01 +0300 Subject: [PATCH] Fix(Py3): Fix import from `urllib` --- Robots/bkmk_robot_base.py | 13 ++++++++----- bkmk_objects.py | 23 +++++++++++++++-------- 2 files changed, 23 insertions(+), 13 deletions(-) diff --git a/Robots/bkmk_robot_base.py b/Robots/bkmk_robot_base.py index 3b5ec27..d2175ac 100644 --- a/Robots/bkmk_robot_base.py +++ b/Robots/bkmk_robot_base.py @@ -15,7 +15,10 @@ from base64 import b64encode import sys import socket import time -import urllib +try: + from urllib.parse import splittype, splithost, splittag +except ImportError: + from urllib import splittype, splithost, splittag from urlparse import urljoin from m_lib.md5wrapper import md5wrapper @@ -62,10 +65,10 @@ class robot_base(Robot): self.start = int(time.time()) bookmark.icon = None - url_type, url_rest = urllib.splittype(bookmark.href) - url_host, url_path = urllib.splithost(url_rest) - url_path, url_tag = urllib.splittag(url_path) # noqa: E221 - # multiple spaces before operator + url_type, url_rest = splittype(bookmark.href) + url_host, url_path = splithost(url_rest) + url_path, url_tag = splittag(url_path) # noqa: E221 + # multiple spaces before operator url = "%s://%s%s" % (url_type, url_host, url_path) error, redirect_code, redirect_to, headers, content = \ diff --git a/bkmk_objects.py b/bkmk_objects.py index 89647aa..4850261 100644 --- a/bkmk_objects.py +++ b/bkmk_objects.py @@ -15,7 +15,14 @@ __all__ = ['Folder', 'Bookmark', 'Ruler', 'Walker', 'Writer', 'Robot', import os -import urllib +try: + from urllib.parse import quote, \ + splittype, splithost, splituser, splitpasswd, \ + splitport +except ImportError: + from urllib import quote, \ + splittype, splithost, splituser, splitpasswd, \ + splitport BKMK_FORMAT = os.environ.get("BKMK_FORMAT", "MOZILLA") @@ -58,14 +65,14 @@ class Bookmark(object): def __init__(self, href, add_date, last_visit=None, last_modified=None, keyword=None, comment='', icon_href=None, icon=None, charset=None, parser_charset=None): - protocol, request = urllib.splittype(href) + protocol, request = splittype(href) 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) if protocol == 'place': @@ -73,9 +80,9 @@ class Bookmark(object): else: href = protocol + "://" if user: - href += urllib.quote(user) + href += quote(user) if password: - href += ':' + urllib.quote(password) + href += ':' + quote(password) href += '@' if host: href += host.decode(parser_charset or 'utf-8').encode('idna') -- 2.39.2