Added cmp functions to libbe.comment, and fleshed them out in libbe.bug.
authorW. Trevor King <wking@drexel.edu>
Mon, 27 Jul 2009 11:18:13 +0000 (07:18 -0400)
committerW. Trevor King <wking@drexel.edu>
Mon, 27 Jul 2009 11:18:13 +0000 (07:18 -0400)
Previous comment comparison had just been the default Tree.__cmp__.

Fleshed out so A == B ensures no meaningful differences between A and B.

Also added first line of comments to new comment output in libbe.diff,
and added a comment/"settings" node and .comment_mod_string() (to
mirror bugdir and bug).

libbe/bug.py
libbe/comment.py
libbe/diff.py

index 3d9cc0883eb3559a24368321a131dc7ff7d1019c..7554318824dc5faaea78a97909defaf2d239690f 100644 (file)
@@ -512,8 +512,12 @@ def cmp_attr(bug_1, bug_2, attr, invert=False):
         return cmp(val_1, val_2)
 
 # alphabetical rankings (a < z)
+cmp_uuid = lambda bug_1, bug_2 : cmp_attr(bug_1, bug_2, "uuid")
 cmp_creator = lambda bug_1, bug_2 : cmp_attr(bug_1, bug_2, "creator")
 cmp_assigned = lambda bug_1, bug_2 : cmp_attr(bug_1, bug_2, "assigned")
+cmp_target = lambda bug_1, bug_2 : cmp_attr(bug_1, bug_2, "target")
+cmp_reporter = lambda bug_1, bug_2 : cmp_attr(bug_1, bug_2, "reporter")
+cmp_summary = lambda bug_1, bug_2 : cmp_attr(bug_1, bug_2, "summary")
 # chronological rankings (newer < older)
 cmp_time = lambda bug_1, bug_2 : cmp_attr(bug_1, bug_2, "time", invert=True)
 
@@ -535,7 +539,8 @@ def cmp_comments(bug_1, bug_2):
     return 0
 
 DEFAULT_CMP_FULL_CMP_LIST = \
-    (cmp_status,cmp_severity,cmp_assigned,cmp_time,cmp_creator,cmp_comments)
+    (cmp_status, cmp_severity, cmp_assigned, cmp_time, cmp_creator,
+     cmp_reporter, cmp_target, cmp_comments, cmp_summary, cmp_uuid)
 
 class BugCompoundComparator (object):
     def __init__(self, cmp_list=DEFAULT_CMP_FULL_CMP_LIST):
index 53519bfd57ae982fad43f4cfa350eceab42ed38a..bf149209305ca0714c0462cc3eae7db9d78152b8 100644 (file)
@@ -271,6 +271,9 @@ class Comment(Tree, settings_object.SavedSettingsObject):
             self.in_reply_to = in_reply_to
             self.body = body
 
+    def __cmp__(self, other):
+        return cmp_full(self, other)
+
     def __str__(self):
         """
         >>> comm = Comment(bug=None, body="Some insightful remarks")
@@ -682,4 +685,59 @@ class Comment(Tree, settings_object.SavedSettingsObject):
                 return comment
         raise KeyError(uuid)
 
+def cmp_attr(comment_1, comment_2, attr, invert=False):
+    """
+    Compare a general attribute between two comments using the conventional
+    comparison rule for that attribute type.  If invert == True, sort
+    *against* that convention.
+    >>> attr="author"
+    >>> commentA = Comment()
+    >>> commentB = Comment()
+    >>> commentA.author = "John Doe"
+    >>> commentB.author = "Jane Doe"
+    >>> cmp_attr(commentA, commentB, attr) < 0
+    True
+    >>> cmp_attr(commentA, commentB, attr, invert=True) > 0
+    True
+    >>> commentB.author = "John Doe"
+    >>> cmp_attr(commentA, commentB, attr) == 0
+    True
+    """
+    if not hasattr(comment_2, attr) :
+        return 1
+    val_1 = getattr(comment_1, attr)
+    val_2 = getattr(comment_2, attr)
+    if val_1 == None: val_1 = None
+    if val_2 == None: val_2 = None
+    
+    if invert == True :
+        return -cmp(val_1, val_2)
+    else :
+        return cmp(val_1, val_2)
+
+# alphabetical rankings (a < z)
+cmp_uuid = lambda comment_1, comment_2 : cmp_attr(comment_1, comment_2, "uuid")
+cmp_author = lambda comment_1, comment_2 : cmp_attr(comment_1, comment_2, "author")
+cmp_in_reply_to = lambda comment_1, comment_2 : cmp_attr(comment_1, comment_2, "in_reply_to")
+cmp_content_type = lambda comment_1, comment_2 : cmp_attr(comment_1, comment_2, "content_type")
+cmp_body = lambda comment_1, comment_2 : cmp_attr(comment_1, comment_2, "body")
+# chronological rankings (newer < older)
+cmp_time = lambda comment_1, comment_2 : cmp_attr(comment_1, comment_2, "time", invert=True)
+
+DEFAULT_CMP_FULL_CMP_LIST = \
+    (cmp_time, cmp_author, cmp_content_type, cmp_body, cmp_in_reply_to,
+     cmp_uuid)
+
+class CommentCompoundComparator (object):
+    def __init__(self, cmp_list=DEFAULT_CMP_FULL_CMP_LIST):
+        self.cmp_list = cmp_list
+    def __call__(self, comment_1, comment_2):
+        for comparison in self.cmp_list :
+            val = comparison(comment_1, comment_2)
+            if val != 0 :
+                return val
+        return 0
+        
+cmp_full = CommentCompoundComparator()
+
 suite = doctest.DocTestSuite()
index decbcf5c92f10149c56be95943513477618fa832..4164e94a5b1e22dec3437b422707114b15f21cc1 100644 (file)
@@ -216,13 +216,13 @@ class Diff (object):
             if not self.new_bugdir.has_bug(uuid):
                 old_bug = self.old_bugdir.bug_from_uuid(uuid)
                 removed.append(old_bug)
-        added.sort(bug.cmp_severity)
-        removed.sort(bug.cmp_severity)
+        added.sort()
+        removed.sort()
         modified.sort(self._bug_modified_cmp)
         self.__changed_bugs = (added, modified, removed)
         return self.__changed_bugs
     def _bug_modified_cmp(self, left, right):
-        return bug.cmp_severity(left[1], right[1])
+        return cmp(left[1], right[1])
     def _changed_comments(self, old, new):
         """
         Search for differences in all loaded comments between the bugs
@@ -348,9 +348,12 @@ class Diff (object):
                 crem.append(c)
             cmod = diff_tree("mod","Modified comments:",requires_children=True)
             for o,n in m:
-                c = diff_tree(n.uuid, self._comment_attribute_changes(o, n),
-                             self.comment_attribute_change_string)
+                c = diff_tree(n.uuid, (o,n), self.comment_mod_string)
                 cmod.append(c)
+                comm_attribute_changes = self._comment_attribute_changes(o, n)
+                if len(comm_attribute_changes) > 0:
+                    cset = diff_tree("settings", comm_attribute_changes,
+                                     self.comment_attribute_change_string)
                 if o.body != n.body:
                     data = (o.body, n.body)
                     cbody = diff_tree("cbody", data,
@@ -391,9 +394,14 @@ class Diff (object):
     def _comment_summary_string(self, comment):
         return "from %s on %s" % (comment.author, time_to_str(comment.time))
     def comment_add_string(self, comment):
-        return self._comment_summary_string(comment)
+        summary = self._comment_summary_string(comment)
+        first_line = comment.body.splitlines()[0]
+        return "%s\n  %s..." % (summary, first_line)
     def comment_rem_string(self, comment):
         return self._comment_summary_string(comment)
+    def comment_mod_string(self, comments):
+        old_comment,new_comment = comments
+        return self._comment_summary_string(new_comment)
     def comment_body_change_string(self, bodies):
         old_body,new_body = bodies
         return difflib.unified_diff(old_body, new_body)