From: Oleg Broytman Date: Wed, 7 Aug 2024 22:14:28 +0000 (+0300) Subject: Refactor: Extract the common list of attributes; `copy_bkmk()` X-Git-Tag: 5.6.0~4 X-Git-Url: https://git.phdru.name/?a=commitdiff_plain;h=26b9a86afac8a1fbfb1a0bab1ca8d5fafec48aa6;p=bookmarks_db.git Refactor: Extract the common list of attributes; `copy_bkmk()` --- diff --git a/Robots/bkmk_rforking.py b/Robots/bkmk_rforking.py index ca845b8..ad5b09c 100644 --- a/Robots/bkmk_rforking.py +++ b/Robots/bkmk_rforking.py @@ -19,7 +19,7 @@ except ImportError: import pickle from subproc import Subprocess, RecordFile -from bkmk_objects import Robot +from bkmk_objects import Robot, copy_bkmk # This is to catch 'close failed: [Errno 9] Bad file descriptor' message @@ -82,14 +82,7 @@ class robot_forking(Robot): if check_subp.waitForPendingChar(60): # wait a minute new_b = pickle.loads(subp_pipe.read_record()) - for attr in ( - "error", "no_error", - "moved", "size", "md5", "real_title", - "last_tested", "last_modified", "test_time", - "icon", "icon_href", "charset", - ): - if hasattr(new_b, attr): - setattr(bookmark, attr, getattr(new_b, attr)) + copy_bkmk(new_b, bookmark) else: bookmark.error = "Subprocess connection timed out" restart_subp(self.log) diff --git a/Writers/bkmk_wflad.py b/Writers/bkmk_wflad.py index cae46bb..3c3dd96 100644 --- a/Writers/bkmk_wflad.py +++ b/Writers/bkmk_wflad.py @@ -11,7 +11,7 @@ __all__ = ['writer_flad'] import time -from bkmk_objects import Writer +from bkmk_objects import Writer, bkmk_attrs from compat import unicode @@ -56,12 +56,7 @@ Comment: %s""" % ( b.keyword, b.comment) ) - for attr_name, attr_out in ( - ("error", "Error"), ("no_error", "NoError"), - ("moved", "Moved"), ("size", "Size"), ("md5", "Md5"), - ("real_title", "RealTitle"), ("test_time", "TestTime"), - ("icon_href", "IconURI"), ("icon", "Icon"), ("charset", "Charset"), - ): + for attr_name, attr_out in bkmk_attrs.items(): if hasattr(b, attr_name): value = getattr(b, attr_name) #if isinstance(value, unicode): diff --git a/bkmk_objects.py b/bkmk_objects.py index bf5904b..2ebf711 100644 --- a/bkmk_objects.py +++ b/bkmk_objects.py @@ -69,6 +69,30 @@ class Bookmark(object): self.charset = charset +bkmk_attrs = { + "charset": "Charset", + "error": "Error", + "icon": "Icon", + "icon_href": "IconURI", + "md5": "Md5", + "moved": "Moved", + "no_error": "NoError", + "real_title": "RealTitle", + "size": "Size", + "test_time": "TestTime", +} + + +bkmk_attrs_sentinel = object() + + +def copy_bkmk(old_bkmk, new_bkmk): + for attr in bkmk_attrs.keys(): + value = getattr(old_bkmk, attr, bkmk_attrs_sentinel) + if value is not bkmk_attrs_sentinel: + setattr(new_bkmk, attr, value) + + class Ruler(object): isFolder = 0 isBookmark = 0 diff --git a/check_urls_db.py b/check_urls_db.py index 53827f3..64e1805 100755 --- a/check_urls_db.py +++ b/check_urls_db.py @@ -13,6 +13,9 @@ __copyright__ = "Copyright (C) 2000-2024 PhiloSoft Design" __license__ = "GNU GPL" +from bkmk_objects import copy_bkmk + + def run(): from getopt import getopt optlist, args = getopt(sys.argv[1:], "ise") @@ -123,14 +126,7 @@ def run(): if href in checked: log("Already checked %s" % href) old_object = root_folder.linear[checked[href]] - for attr_name in ( - "last_visit", "last_modified", - "error", "no_error", "moved", "size", "md5", "real_title", - "last_tested", "test_time", "icon", "charset", - ): - if hasattr(old_object, attr_name): - setattr(object, attr_name, - getattr(old_object, attr_name)) + copy_bkmk(old_object, object) else: log("Checking %s" % href) robot.check_bookmark(object)