diff options
author | W. Trevor King <wking@drexel.edu> | 2009-07-27 07:18:13 -0400 |
---|---|---|
committer | W. Trevor King <wking@drexel.edu> | 2009-07-27 07:18:13 -0400 |
commit | 159dc9303e10cef81388fb686f79312cc1018c65 (patch) | |
tree | 4924ec024c891631077f37ef1d8c853f17a53c66 /libbe/comment.py | |
parent | 3711c5080619b0decf0ae040a9d244bf3b902c41 (diff) | |
download | bugseverywhere-159dc9303e10cef81388fb686f79312cc1018c65.tar.gz |
Added cmp functions to libbe.comment, and fleshed them out in libbe.bug.
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).
Diffstat (limited to 'libbe/comment.py')
-rw-r--r-- | libbe/comment.py | 58 |
1 files changed, 58 insertions, 0 deletions
diff --git a/libbe/comment.py b/libbe/comment.py index 53519bf..bf14920 100644 --- a/libbe/comment.py +++ b/libbe/comment.py @@ -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() |