def test_move(self): a = {'key': 'value'} b = {'key2': 'value2'} c = 4 d = 23 lista = ObservableList() lista.append(a) lista.append(b) lista.append(c) lista.append(d) lista.observer = MagicMock() new_index = 2 old_index = lista.index(a) lista.move(a, new_index) self.assertEqual(lista.index(a), new_index) self.assertListEqual([b, c, a, d], list(lista)) expected = [ call(UpdateType.DELETED, a, old_index), call(UpdateType.CREATED, a, new_index) ] self.assertListEqual(expected, lista.observer.call_args_list)
def test_append(self): element = {'test': 5, 'a':[1,2,3,5]} lista = ObservableList() lista.observer = MagicMock() lista.append(element) lista.observer.assert_called_once_with(UpdateType.CREATED, element, 0)
def test_insert(self): lista = ObservableList() lista.append(0) lista.append(2) lista.observer = MagicMock() lista.insert(1, 'a') lista.observer.assert_called_once_with(UpdateType.CREATED, 'a', 1)
def test__setitem_equal__(self): lista = ObservableList() lista.append(1) lista.append(2) lista.append(3) lista.observer = MagicMock() lista[1] = 2 lista.observer.assert_not_called()
def test_contains(self): lista = ObservableList() lista.append(123) lista.append(456) lista.append(789) self.assertTrue(123 in lista) self.assertTrue(456 in lista) self.assertTrue(789 in lista) self.assertFalse(987 in lista) self.assertTrue(987 not in lista)
def test__delitem__(self): lista = ObservableList() lista.append(123) lista.append(456) lista.append(789) lista.observer = MagicMock() index = 1 del lista[index] lista.observer.assert_called_once_with(UpdateType.DELETED, 456, index)
def test__setitem__(self): lista = ObservableList() lista.append(1) lista.append(2) lista.append(3) lista.observer = MagicMock() index = 1 old_value = lista[index] new_value = 4 lista[index] = new_value lista.observer.assert_called_once_with(UpdateType.UPDATED, new_value, index, old=old_value)
def test_pop_empty_parameter(self): lista = ObservableList() a = 'a' b = 'b' c = 'c' d = 'd' lista.append(a) lista.append(b) lista.append(c) lista.append(d) lista.observer = MagicMock() self.assertEqual(d, lista.pop()) self.assertEqual(3, len(lista)) lista.observer.assert_called_once_with(UpdateType.DELETED, d, len(lista))
def test_move_same_index(self): a = {'key': 'value'} b = {'key2': 'value2'} c = 4 d = 23 lista = ObservableList() lista.append(a) lista.append(b) lista.append(c) lista.append(d) lista.observer = MagicMock() same_index = lista.index(a) lista.move(a, same_index) lista.observer.assert_not_called()
def test_swap_2(self): a = {'key': 'value'} b = {'key2': 'value2'} lista = ObservableList() lista.append(a) listb = ObservableList() listb.append(b) lista.observer = MagicMock() listb.observer = MagicMock() lista[0], listb[0] = listb[0], lista[0] self.assertEqual(b, lista[0]) self.assertEqual(a, listb[0]) lista.observer.assert_called_once_with(UpdateType.UPDATED, lista[0], 0, old=listb[0]) listb.observer.assert_called_once_with(UpdateType.UPDATED, listb[0], 0, old=lista[0])
def test_swap(self): a = {'key': 'value'} b = {'key2': 'value2'} lista = ObservableList() lista.append(a) lista.append(b) lista.observer = MagicMock() lista[0], lista[1] = lista[1], lista[0] self.assertEqual(b, lista[0]) self.assertEqual(a, lista[1]) expected = [ call(UpdateType.UPDATED, lista[0], 0, old=lista[1]), call(UpdateType.UPDATED, lista[1], 1, old=lista[0]) ] self.assertListEqual(expected, lista.observer.call_args_list)
def test_remove(self): lista = ObservableList() lista.append(1) lista.append('2') lista.append(3) lista.observer = MagicMock() lista.remove('2') lista.observer.assert_called_once_with(UpdateType.DELETED, '2', 1)
class RestrictionList(metaclass=ABCMeta): """ List with validation when add a element """ def __init__(self): self._items = set() self.real_list = ObservableList() @property def observer(self): return self.real_list.observer @observer.setter def observer(self, observer): self.real_list.observer = observer def check_insertion(self, item): pass def __str__(self): """ See :meth:`~pluginsmanager.observer.observable_list.ObservableList.__repr__()` method """ return repr(self.real_list) def __repr__(self): """ See :meth:`~pluginsmanager.observer.observable_list.ObservableList.__repr__()` method """ return repr(self.real_list) def append(self, item): """ See :meth:`~pluginsmanager.observer.observable_list.ObservableList.append()` method """ self.check_insertion(item) self.real_list.append(item) self._items |= {item} def remove(self, item): """ See :meth:`~pluginsmanager.observer.observable_list.ObservableList.remove()` method """ self.real_list.remove(item) self._items.remove(item) def remove_silently(self, item): """ Remove item and not notify """ self.real_list.real_list.remove(item) self._items.remove(item) def index(self, x): """ See :meth:`~pluginsmanager.observer.observable_list.ObservableList.index()` method """ return self.real_list.index(x) def insert(self, index, x): """ See :meth:`~pluginsmanager.observer.observable_list.ObservableList.insert()` method """ self.check_insertion(x) self.real_list.insert(index, x) self._items |= {x} def pop(self, index=None): """ See :meth:`~pluginsmanager.observer.observable_list.ObservableList.pop()` method """ if index is None: index = len(self.real_list) - 1 effect = self[index] returned = self.real_list.pop(index) self._items.remove(effect) return returned def __len__(self): """ See :meth:`~pluginsmanager.observer.observable_list.ObservableList.__len__()` method """ return len(self.real_list) def __getitem__(self, index): """ See :meth:`~pluginsmanager.observer.observable_list.ObservableList.__getitem__()` method """ return self.real_list.__getitem__(index) def __setitem__(self, index, val): """ See :meth:`~pluginsmanager.observer.observable_list.ObservableList.__setitem__()` method Swap doesn't works:: >>> builder = Lv2EffectBuilder() >>> effects = EffectsList() >>> effects.append(builder.build('http://calf.sourceforge.net/plugins/Reverb')) >>> effects.append(builder.build('http://guitarix.sourceforge.net/plugins/gx_fuzzfacefm_#_fuzzfacefm_')) >>> effects[0], effects[1] = effects[1], effects[0] pluginsmanager.model.effects_list.AlreadyAddedError: The effect 'GxFuzzFaceFullerMod' already added """ if val == self[index]: return self.check_insertion(val) old = self.real_list[index] self._items.remove(old) self.real_list.__setitem__(index, val) self._items |= {val} def __delitem__(self, sliced): """ See :meth:`~pluginsmanager.observer.observable_list.ObservableList.__delitem__()` method """ item = self.real_list[sliced] self.real_list.__delitem__(sliced) self._items.remove(item) def __contains__(self, item): """ See :meth:`~pluginsmanager.observer.observable_list.ObservableList.__contains__()` method """ return item in self._items def __iter__(self): """ See :meth:`~pluginsmanager.observer.observable_list.ObservableList.__iter__()` method """ return iter(self.real_list) def move(self, item, new_position): """ See :meth:`~pluginsmanager.observer.observable_list.ObservableList.move()` method """ self.real_list.move(item, new_position)
class BanksManager(object): """ BanksManager manager the banks. In these is possible add banks, obtains the banks and register observers for will be notified when occurs changes (like added new pedalboard, rename bank, set effect param value or state) For use details, view Readme.rst example documentation. :param list[Bank] banks: Banks that will be added in this. Useful for loads banks previously loaded, like banks persisted and recovered. """ def __init__(self, banks=None): self.banks = ObservableList() self.banks.observer = self._banks_observer banks = [] if banks is None else banks self.observer_manager = ObserverManager() for bank in banks: self.append(bank) def __iter__(self): """ Iterates banks of the banksmanager:: >>> banks_manager = BanksManager() >>> for index, bank in enumerate(banks_manager): ... print(index, '-', bank) :return: Iterator for banks list """ return self.banks.__iter__() def register(self, observer): """ Register an observer for it be notified when occurs changes. For more details, see :class:`.UpdatesObserver` :param UpdatesObserver observer: Observer that will be notified then occurs changes """ self.observer_manager.append(observer) observer.manager = self def unregister(self, observer): """ Remove the observers of the observers list. It will not receive any more notifications when occurs changes. :param UpdatesObserver observer: Observer you will not receive any more notifications then occurs changes. """ self.observer_manager.observers.remove(observer) observer.manager = None def append(self, bank): """ Append the bank in banks manager. It will be monitored, changes in this will be notified for the notifiers. :param Bank bank: Bank that will be added in this """ self.banks.append(bank) def _banks_observer(self, update_type, bank, index, **kwargs): if update_type == UpdateType.CREATED: self._init_bank(bank) elif update_type == UpdateType.UPDATED: self._init_bank(bank) old_bank = kwargs['old'] if old_bank not in self: self._clear_bank(old_bank) elif update_type == UpdateType.DELETED: self._clear_bank(bank) self.observer_manager.on_bank_updated(bank, update_type, index=index, origin=self, **kwargs) def _init_bank(self, bank): bank.manager = self bank.observer = self.observer_manager def _clear_bank(self, bank): bank.manager = None bank.observer_manager = MagicMock() def enter_scope(self, observer): """ Informs that changes occurs by the ``observer`` and isn't necessary informs the changes for observer :param UpdatesObserver observer: Observer that causes changes """ self.observer_manager.enter_scope(observer) def exit_scope(self): """ Closes the last observer scope added """ self.observer_manager.exit_scope() @property def observers(self): """ :return: Observers registered in BanksManager instance """ return self.observer_manager.observers