diff options
author | Matěj Cepl <mcepl@cepl.eu> | 2024-01-19 03:07:54 +0100 |
---|---|---|
committer | Matěj Cepl <mcepl@cepl.eu> | 2024-01-21 21:57:15 +0100 |
commit | b638466e6a6ada7478758cf740c89650d0f70f59 (patch) | |
tree | f34979881a12818ba46a4b74a7a7096e737e441a /libbe/diff.py | |
parent | b11b63156666589ec9749fa318fe7ecd9d1f136d (diff) | |
download | bugseverywhere-b638466e6a6ada7478758cf740c89650d0f70f59.tar.gz |
WIP plenty of clean-ups and porting to Python 3.
Diffstat (limited to 'libbe/diff.py')
-rw-r--r-- | libbe/diff.py | 66 |
1 files changed, 42 insertions, 24 deletions
diff --git a/libbe/diff.py b/libbe/diff.py index fe83881..595d512 100644 --- a/libbe/diff.py +++ b/libbe/diff.py @@ -22,6 +22,7 @@ """ import difflib +import functools import types import libbe @@ -32,12 +33,7 @@ from libbe.storage.util.settings_object import setting_name_to_attr_name from libbe.util.utility import time_to_str -# https://stackoverflow.com/a/56719588/164233 -def cmp(a, b): - return (int(a) > int(b)) - (int(a) < int(b)) - - - +@functools.total_ordering class SubscriptionType (libbe.util.tree.Tree): """Trees of subscription types to allow users to select exactly what notifications they want to subscribe to. @@ -47,8 +43,13 @@ class SubscriptionType (libbe.util.tree.Tree): self.type = type_name def __str__(self): return self.type - def __cmp__(self, other): - return cmp(self.type, other.type) + + def __eq__(self, other): + return self.type == other.type + + def __lt__(self, other): + return self.type < other.type + def __repr__(self): return '<SubscriptionType: %s>' % str(self) def string_tree(self, indent=0): @@ -87,6 +88,7 @@ def type_from_name(name, type_root, default=None, default_ok=False): return default raise InvalidType(name, type_root) +@functools.total_ordering class Subscription (object): """A user subscription. @@ -96,7 +98,7 @@ class Subscription (object): >>> subscriptions = [Subscription('XYZ', 'all'), ... Subscription('DIR', 'new'), ... Subscription('ABC', BUG_TYPE_ALL),] - >>> print sorted(subscriptions) + >>> print(sorted(subscriptions)) [<Subscription: DIR (new)>, <Subscription: ABC (all)>, <Subscription: XYZ (all)>] """ def __init__(self, id, subscription_type, **kwargs): @@ -109,17 +111,26 @@ class Subscription (object): subscription_type = type_from_name(subscription_type, **kwargs) self.id = id self.type = subscription_type - def __cmp__(self, other): + + def __eq__(self, other): + return isinstance(other, Subscription) and \ + (self.id == other.id) and \ + (self.type == self.type) + + def __lt__(self, other): for attr in 'id', 'type': - value = cmp(getattr(self, attr), getattr(other, attr)) - if value != 0: + left = getattr(self, attr) + right = getattr(other, attr) + if left != right: if self.id == BUGDIR_ID: - return -1 + return True elif other.id == BUGDIR_ID: - return 1 - return value + return False + return left < right + def __str__(self): return str(self.type) + def __repr__(self): return '<Subscription: %s (%s)>' % (self.id, self.type) @@ -165,12 +176,12 @@ class DiffTree (libbe.util.tree.Tree): >>> bugs.append(new) >>> rem = DiffTree('rem', 'removed bugs: RST, UVW') >>> bugs.append(rem) - >>> print bugdir.report_string() + >>> print(bugdir.report_string()) target: None -> 1.0 bug-count: 5 -> 6 new bugs: ABC, DEF removed bugs: RST, UVW - >>> print '\\n'.join(bugdir.paths()) + >>> print('\\n'.join(bugdir.paths())) bugdir bugdir/settings bugdir/bugs @@ -189,7 +200,7 @@ class DiffTree (libbe.util.tree.Tree): >>> bugdir.child_by_path('bugdir/bugs') == bugs True >>> bugdir.child_by_path('/bugs').masked = True - >>> print bugdir.report_string() + >>> print(bugdir.report_string()) target: None -> 1.0 """ def __init__(self, name, data=None, data_part_fn=str, @@ -290,7 +301,7 @@ class Diff (object): >>> c = bd_new.new_bug('Bug C', _uuid='c') >>> d = Diff(bd, bd_new) >>> r = d.report_tree() - >>> print '\\n'.join(r.paths()) + >>> print('\\n'.join(r.paths())) bugdir bugdir/settings bugdir/bugs @@ -306,7 +317,7 @@ class Diff (object): bugdir/bugs/mod/a/comments/new/acom bugdir/bugs/mod/a/comments/rem bugdir/bugs/mod/a/comments/mod - >>> print r.report_string() + >>> print(r.report_string()) Changed bug directory settings: target: None -> 1.0 New bugs: @@ -327,7 +338,7 @@ class Diff (object): >>> subscriptions = [Subscription('DIR', BUGDIR_TYPE_NEW), ... Subscription('b', BUG_TYPE_ALL)] >>> r = d.report_tree(subscriptions) - >>> print r.report_string() + >>> print(r.report_string()) New bugs: abc/c:om: Bug C Removed bugs: @@ -340,10 +351,10 @@ class Diff (object): first, and then use report_tree() to avoid redundant comparisons. >>> d.full_report() - >>> print d.report_tree([subscriptions[0]]).report_string() + >>> print(d.report_tree([subscriptions[0]]).report_string()) New bugs: abc/c:om: Bug C - >>> print d.report_tree([subscriptions[1]]).report_string() + >>> print(d.report_tree([subscriptions[1]]).report_string()) Removed bugs: abc/b:cm: Bug B @@ -455,8 +466,15 @@ class Diff (object): removed.sort() modified.sort(self._bug_modified_cmp) return (added, modified, removed) + def _bug_modified_cmp(self, left, right): - return cmp(left[1], right[1]) + l = left[1] + r = right[1] + + if l == r: return 0 + if l > r: return 1 + if l < r: return -1 + def _changed_comments(self, old, new): """ Search for differences in all loaded comments between the bugs |