diff options
author | Olivier Tilloy <olivier@tilloy.net> | 2009-05-04 08:46:31 +0200 |
---|---|---|
committer | Olivier Tilloy <olivier@tilloy.net> | 2009-05-04 08:46:31 +0200 |
commit | 746e780f6ff679616441cf2ee9d8d2cc5e570307 (patch) | |
tree | f6f267a586d55280d4122e375894fd27bc09b37c | |
parent | 9a96447b1c8b41eb0d0efe4cd2a236c637a2868b (diff) | |
download | pyexiv2-746e780f6ff679616441cf2ee9d8d2cc5e570307.tar.gz |
New minimal interface for the notifying list's listeners.
-rw-r--r-- | src/pyexiv2.py | 60 | ||||
-rw-r--r-- | unittest/notifying_list.py | 52 |
2 files changed, 61 insertions, 51 deletions
diff --git a/src/pyexiv2.py b/src/pyexiv2.py index b41afa5..d39edfa 100644 --- a/src/pyexiv2.py +++ b/src/pyexiv2.py @@ -250,29 +250,26 @@ class ListenerInterface(object): # Define an interface that an object that wants to listen to changes on a # notifying list should implement. - def item_changed(self, index, item): - raise NotImplementedError() - - def item_deleted(self, index): - raise NotImplementedError() - - def item_appended(self, item): - raise NotImplementedError() + """ + Changes that can happen on a list: + - items changed + - items deleted + - items inserted + - reordered + """ - def extended(self, items): + def items_changed(self, start_index, items): raise NotImplementedError() - def item_inserted(self, index, item): + def items_deleted(self, start_index, end_index): raise NotImplementedError() - def item_popped(self, index): + def items_inserted(self, start_index, items): raise NotImplementedError() - def item_removed(self, item): + def reordered(self): raise NotImplementedError() - # TODO: define other methods. - class NotifyingList(list): @@ -301,35 +298,56 @@ class NotifyingList(list): def __setitem__(self, index, item): # FIXME: support slice arguments super(NotifyingList, self).__setitem__(index, item) - self._notify_listeners('item_changed', index, item) + self._notify_listeners('items_changed', index, [item]) def __delitem__(self, index): # FIXME: support slice arguments super(NotifyingList, self).__delitem__(index) - self._notify_listeners('item_deleted', index) + self._notify_listeners('items_deleted', index, index + 1) def append(self, item): + index = len(self) super(NotifyingList, self).append(item) - self._notify_listeners('item_appended', item) + self._notify_listeners('items_inserted', index, [item]) def extend(self, items): + index = len(self) super(NotifyingList, self).extend(items) - self._notify_listeners('extended', items) + self._notify_listeners('items_inserted', index, items) def insert(self, index, item): super(NotifyingList, self).insert(index, item) - self._notify_listeners('item_inserted', index, item) + self._notify_listeners('items_inserted', index, [item]) def pop(self, index=None): if index is None: index = len(self) - 1 item = super(NotifyingList, self).pop(index) - self._notify_listeners('item_popped', index) + self._notify_listeners('items_deleted', index, index + 1) return item def remove(self, item): + index = self.index(item) super(NotifyingList, self).remove(item) - self._notify_listeners('item_removed', item) + self._notify_listeners('items_deleted', index, index + 1) + + def reverse(self): + raise NotImplementedError() + + def sort(self, cmp=None, key=None, reverse=False): + raise NotImplementedError() + + def __iadd__(self, other): + raise NotImplementedError() + + def __imul__(self, coefficient): + raise NotImplementedError() + + def __setslice__(self, i, j, sequence): + raise NotImplementedError() + + def __delslice__(self, i, j): + raise NotImplementedError() class MetadataTag(object): diff --git a/unittest/notifying_list.py b/unittest/notifying_list.py index 6a60973..07dec11 100644 --- a/unittest/notifying_list.py +++ b/unittest/notifying_list.py @@ -40,26 +40,17 @@ class SimpleListener(ListenerInterface): self.notifications += 1 self.last = (method_name, args) - def item_changed(self, index, item): - self._notify('item_changed', index, item) + def items_changed(self, start_index, items): + self._notify('items_changed', start_index, items) - def item_deleted(self, index): - self._notify('item_deleted', index) + def items_deleted(self, start_index, end_index): + self._notify('items_deleted', start_index, end_index) - def item_appended(self, item): - self._notify('item_appended', item) + def items_inserted(self, start_index, items): + self._notify('items_inserted', start_index, items) - def extended(self, items): - self._notify('extended', items) - - def item_inserted(self, index, item): - self._notify('item_inserted', index, item) - - def item_popped(self, index): - self._notify('item_popped', index) - - def item_removed(self, item): - self._notify('item_removed', item) + def reordered(self): + self._notify('reordered') class TestNotifyingList(unittest.TestCase): @@ -80,6 +71,7 @@ class TestNotifyingList(unittest.TestCase): self.failUnlessRaises(NotImplementedError, self.values.extend, [11, 22]) self.failUnlessRaises(NotImplementedError, self.values.insert, 4, 24) self.failUnlessRaises(NotImplementedError, self.values.pop) + self.failUnlessRaises(NotImplementedError, self.values.remove, 9) # TODO: test all operations (insertion, slicing, ...) def test_multiple_listeners(self): @@ -92,78 +84,78 @@ class TestNotifyingList(unittest.TestCase): self.failUnlessEqual(self.values, [5, 7, 9, 13, 57, 3, 2]) for listener in listeners: self.failUnlessEqual(listener.notifications, 1) - self.failUnlessEqual(listener.last, ('item_changed', (3, 13))) + self.failUnlessEqual(listener.last, ('items_changed', (3, [13]))) self.failUnlessRaises(IndexError, self.values.__setitem__, 9, 27) self.failUnlessEqual(self.values, [5, 7, 9, 13, 57, 3, 2]) for listener in listeners: self.failUnlessEqual(listener.notifications, 1) - self.failUnlessEqual(listener.last, ('item_changed', (3, 13))) + self.failUnlessEqual(listener.last, ('items_changed', (3, [13]))) del self.values[5] self.failUnlessEqual(self.values, [5, 7, 9, 13, 57, 2]) for listener in listeners: self.failUnlessEqual(listener.notifications, 2) - self.failUnlessEqual(listener.last, ('item_deleted', (5,))) + self.failUnlessEqual(listener.last, ('items_deleted', (5, 6))) self.failUnlessRaises(IndexError, self.values.__delitem__, 9) self.failUnlessEqual(self.values, [5, 7, 9, 13, 57, 2]) for listener in listeners: self.failUnlessEqual(listener.notifications, 2) - self.failUnlessEqual(listener.last, ('item_deleted', (5,))) + self.failUnlessEqual(listener.last, ('items_deleted', (5, 6))) self.values.append(17) self.failUnlessEqual(self.values, [5, 7, 9, 13, 57, 2, 17]) for listener in listeners: self.failUnlessEqual(listener.notifications, 3) - self.failUnlessEqual(listener.last, ('item_appended', (17,))) + self.failUnlessEqual(listener.last, ('items_inserted', (6, [17]))) self.values.extend([11, 22]) self.failUnlessEqual(self.values, [5, 7, 9, 13, 57, 2, 17, 11, 22]) for listener in listeners: self.failUnlessEqual(listener.notifications, 4) - self.failUnlessEqual(listener.last, ('extended', ([11, 22],))) + self.failUnlessEqual(listener.last, ('items_inserted', (7, [11, 22]))) self.failUnlessRaises(TypeError, self.values.extend, 26) self.failUnlessEqual(self.values, [5, 7, 9, 13, 57, 2, 17, 11, 22]) for listener in listeners: self.failUnlessEqual(listener.notifications, 4) - self.failUnlessEqual(listener.last, ('extended', ([11, 22],))) + self.failUnlessEqual(listener.last, ('items_inserted', (7, [11, 22]))) self.values.insert(4, 24) self.failUnlessEqual(self.values, [5, 7, 9, 13, 24, 57, 2, 17, 11, 22]) for listener in listeners: self.failUnlessEqual(listener.notifications, 5) - self.failUnlessEqual(listener.last, ('item_inserted', (4, 24))) + self.failUnlessEqual(listener.last, ('items_inserted', (4, [24]))) self.values.pop() self.failUnlessEqual(self.values, [5, 7, 9, 13, 24, 57, 2, 17, 11]) for listener in listeners: self.failUnlessEqual(listener.notifications, 6) - self.failUnlessEqual(listener.last, ('item_popped', (9,))) + self.failUnlessEqual(listener.last, ('items_deleted', (9, 10))) self.values.pop(4) self.failUnlessEqual(self.values, [5, 7, 9, 13, 57, 2, 17, 11]) for listener in listeners: self.failUnlessEqual(listener.notifications, 7) - self.failUnlessEqual(listener.last, ('item_popped', (4,))) + self.failUnlessEqual(listener.last, ('items_deleted', (4, 5))) self.failUnlessRaises(IndexError, self.values.pop, 33) self.failUnlessEqual(self.values, [5, 7, 9, 13, 57, 2, 17, 11]) for listener in listeners: self.failUnlessEqual(listener.notifications, 7) - self.failUnlessEqual(listener.last, ('item_popped', (4,))) + self.failUnlessEqual(listener.last, ('items_deleted', (4, 5))) self.values.remove(9) self.failUnlessEqual(self.values, [5, 7, 13, 57, 2, 17, 11]) for listener in listeners: self.failUnlessEqual(listener.notifications, 8) - self.failUnlessEqual(listener.last, ('item_removed', (9,))) + self.failUnlessEqual(listener.last, ('items_deleted', (2, 3))) self.failUnlessRaises(ValueError, self.values.remove, 33) self.failUnlessEqual(self.values, [5, 7, 13, 57, 2, 17, 11]) for listener in listeners: self.failUnlessEqual(listener.notifications, 8) - self.failUnlessEqual(listener.last, ('item_removed', (9,))) + self.failUnlessEqual(listener.last, ('items_deleted', (2, 3))) # TODO: test all operations (slicing, ...) |