X-Git-Url: https://git.phdru.name/?a=blobdiff_plain;ds=sidebyside;f=bkmk_objects.py;h=d672bcfc7f2850e0e7c98d5fec7adf7148d50b16;hb=refs%2Ftags%2F5.2.2;hp=140cb981856bad7d3b3f9111ea68b5f4521a65b3;hpb=31ea58de8250b674586058e970d439f20094a7ae;p=bookmarks_db.git diff --git a/bkmk_objects.py b/bkmk_objects.py index 140cb98..d672bcf 100644 --- a/bkmk_objects.py +++ b/bkmk_objects.py @@ -5,19 +5,21 @@ This file is a part of Bookmarks database and Internet robot. """ __author__ = "Oleg Broytman " -__copyright__ = "Copyright (C) 2000-2023 PhiloSoft Design" +__copyright__ = "Copyright (C) 2000-2024 PhiloSoft Design" __license__ = "GNU GPL" __all__ = ['Folder', 'Bookmark', 'Ruler', 'Walker', 'Writer', 'Robot', - 'InverseLinker', 'Linear', 'make_linear', 'make_tree', 'break_tree', - 'quote_title', 'unquote_title', 'parse_params', 'set_params', - ] + 'InverseLinker', 'Linear', 'make_linear', 'make_tree', 'break_tree', + 'quote_title', 'unquote_title', 'parse_params', 'set_params', + ] -import os, urllib +from urllib.parse import urlsplit, quote, unquote +import os BKMK_FORMAT = os.environ.get("BKMK_FORMAT", "MOZILLA") + class Folder(list): isFolder = 1 isBookmark = 0 @@ -29,7 +31,7 @@ class Folder(list): self.last_modified = last_modified def walk_depth(self, walker, level=0): - if hasattr(self, "header"): # root folder + if hasattr(self, "header"): # root folder prune = 0 walker.root_folder(self) else: @@ -54,29 +56,26 @@ class Bookmark(object): isBookmark = 1 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) - user, password, port = None, None, None - host, path = urllib.splithost(request) - if host: - user, host = urllib.splituser(host) - if user: - user, password = urllib.splitpasswd(user) - host, port = urllib.splitport(host) - if port: port = int(port) + keyword=None, comment='', icon_href=None, icon=None, + charset=None, parser_charset=None): + split_results = urlsplit(href) + protocol, netloc, path, query, tag = split_results + user = split_results.username + password = split_results.password + host = split_results.hostname + port = split_results.port if protocol == 'place': href = protocol + ":" 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') + href += host.encode('idna').decode('ascii') if port: href += ':%d' % port if path: @@ -140,7 +139,7 @@ class Robot(object): self.log = log def stop(self): - pass # Nothing to do on cleanup + pass # Nothing to do on cleanup # Helper class to make inverese links (nodes linked to their parent) @@ -150,7 +149,8 @@ class InverseLinker(Walker): def start_folder(self, f, level): f.parent = self.parent_stack[-1] - self.parent_stack.append(f) # Push the folder onto the stack of parents + # Push the folder onto the stack of parents + self.parent_stack.append(f) def end_folder(self, f, level): del self.parent_stack[-1] # Pop off the stack @@ -181,7 +181,8 @@ class Linear(Walker): self.add_object(r) -# Helper - make linked linear represenatation of the tree, suitable to be stored in sequential storage +# Helper - make linked linear represenatation of the tree, +# suitable to be stored in sequential storage. def make_linear(root_folder): linker = InverseLinker() root_folder.walk_depth(linker) @@ -190,7 +191,8 @@ def make_linear(root_folder): root_folder.walk_depth(linear) -# Helper, opposite of make_linear - make a tree from the linked linear representation +# Helper, opposite of make_linear - +# make a tree from the linked linear representation. def make_tree(linear): root_folder = linear[0] del linear[0] @@ -200,6 +202,7 @@ def make_tree(linear): return root_folder + def break_tree(linear): del linear[0] @@ -212,11 +215,18 @@ def quote_title(title): title = title.replace("'", "'") return title + def unquote_title(title): if BKMK_FORMAT == "MOZILLA": - from HTMLParser import HTMLParser - title = HTMLParser().unescape(title.replace("&", '&').decode('utf-8')) - title = title.encode('utf-8').replace("'", "'") + try: + from HTMLParser import HTMLParser + except ImportError: + from html import unescape + else: + unescape = HTMLParser().unescape + title = unescape( + title.replace("&", '&')) + title = title.replace("'", "'") return title @@ -226,9 +236,10 @@ def parse_params(param_str): param_list = {} for param in params: key, value = param.split('=', 1) - param_list[key] = value + param_list[key] = unquote(value) return main_param, param_list + def set_params(obj, params): if hasattr(params, "items"): params = params.items()