]> git.phdru.name Git - bookmarks_db.git/blobdiff - sort_db.py
Fix(Py3): Fix `list.join(separator)`
[bookmarks_db.git] / sort_db.py
index 03026e6db3501a05d7f0de92734881b0229f9d5c..143c7d1349c44be69406dc4137f3c1d88c10d23a 100755 (executable)
@@ -1,4 +1,4 @@
-#! /usr/bin/env python
+#! /usr/bin/env python3
 """Sort bookmarks DB according to a rule:
       -a - by AddDate
       -v - by LastVisit
@@ -13,29 +13,22 @@ This file is a part of Bookmarks database and Internet robot.
 """
 
 __author__ = "Oleg Broytman <phd@phdru.name>"
-__copyright__ = "Copyright (C) 2000-2017 PhiloSoft Design"
+__copyright__ = "Copyright (C) 2000-2023 PhiloSoft Design"
 __license__ = "GNU GPL"
 
 import sys
 
 
-class SortBy(object):
+class SortKey(object):
     def __init__(self, sort_by):
         self.sort_by = sort_by
 
-    def __call__(self, o1, o2):
+    def __call__(self, o1):
         try:
-            attr1 = int(getattr(o1, self.sort_by))
+            return int(getattr(o1, self.sort_by))
         except (AttributeError, TypeError, ValueError):
             return 1
 
-        try:
-            attr2 = int(getattr(o2, self.sort_by))
-        except (AttributeError, TypeError, ValueError):
-            return -1
-
-        return cmp(attr1, attr2)
-
 
 def walk_linear(linear, walker):
     for object in linear:
@@ -89,10 +82,10 @@ def run():
     make_linear(root_folder)
 
     linear = root_folder.linear
-    del linear[0] # exclude root folder from sorting
+    del linear[0]  # exclude root folder from sorting
 
-    by = SortBy(sort_by)
-    linear.sort(by)
+    sort_key = SortKey(sort_by)
+    linear.sort(key=sort_key)
 
     from writers import writer
     output_filename = "%s-sorted_by-%s" % (writer.filename, sort_by)