]> git.phdru.name Git - bookmarks_db.git/commitdiff
Refactor: Extract the common list of attributes; `copy_bkmk()`
authorOleg Broytman <phd@phdru.name>
Wed, 7 Aug 2024 22:14:28 +0000 (01:14 +0300)
committerOleg Broytman <phd@phdru.name>
Wed, 7 Aug 2024 22:14:28 +0000 (01:14 +0300)
Robots/bkmk_rforking.py
Writers/bkmk_wflad.py
bkmk_objects.py
check_urls_db.py

index ca845b843afaebdda609173496cbf944c3168f1c..ad5b09c656be6596a29a8d6fcfe21c7ea2f76c9c 100644 (file)
@@ -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)
index cae46bb44554c1e79a54a32bd1d1ac72e80ab307..3c3dd9680e855d43f7a579e3515fb167217dd632 100644 (file)
@@ -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):
index bf5904b3287d2872dd6e871398751bc3118c6700..2ebf711aff6a0b7366f852e2ac66968065be7df5 100644 (file)
@@ -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
index 53827f33538a5ba64a8c0cd5bcc642b727c22483..64e1805384eb9c44916b4cf97578c913d99d1721 100755 (executable)
@@ -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)