def add_subset(self, subset): # Check if subset already exists in viewer if not self.allow_duplicate_subset and subset in self._layer_artist_container: return True # Create layer artist and add to container. First check whether any # plugins want to make a custom layer artist. layer = get_layer_artist_from_registry( subset, self) or self.get_subset_layer_artist(subset) if layer is None: return False # When adding a layer artist to the layer artist container, zorder # gets set automatically - however since we call a forced update of the # layer after adding it to the container we can ignore any callbacks # related to zorder. with ignore_callback(layer.state, 'zorder'): self._layer_artist_container.append(layer) layer.update() self.draw_legend( ) # need to be called here because callbacks are ignored in previous step return True
def test_ignore_global_callback(): # Regression test to make sure that ignore_callback works for global # callbacks too. state = State() test1 = MagicMock() state.add_callback('a', test1) test2 = MagicMock() state.add_global_callback(test2) with ignore_callback(state, 'a'): state.a = 100 assert test1.call_count == 0 assert test2.call_count == 0 assert test1.call_count == 0 assert test2.call_count == 0 test2.reset_mock() with ignore_callback(state, 'a'): state.b = 200 assert test2.call_count == 1 test2.assert_called_once_with(b=200) test2.reset_mock() with ignore_callback(state, 'a', 'b'): state.a = 300 state.b = 400 assert test2.call_count == 0 assert test2.call_count == 0
def test_ignore_in_ignored_callback(): # Regression test for a bug that occurred if a delayed callback included # a delay itself. state = State() def callback(*args, **kwargs): with ignore_callback(state, 'a'): state.a = 2 state.add_callback('a', callback) with ignore_callback(state, 'a', 'b'): state.a = 100
def test_ignore_global_callback_stub(): # Make sure that adding the global callback ignore functionality doesn't # break things when we are dealing with a plain class without HasCallbackProperties stub = Stub() test1 = MagicMock() add_callback(stub, 'prop1', test1) with ignore_callback(stub, 'prop1'): stub.prop1 = 100 assert test1.call_count == 0 assert test1.call_count == 0
def test_ignore_multiple(): stub = Stub() test = MagicMock() test2 = MagicMock() add_callback(stub, 'prop1', test) add_callback(stub, 'prop2', test2) with ignore_callback(stub, 'prop1', 'prop2'): stub.prop1 = 100 stub.prop2 = 200 assert test.call_count == 0 assert test2.call_count == 0 assert test.call_count == 0 assert test2.call_count == 0
def add_data(self, data): # Check if data already exists in viewer if not self.allow_duplicate_data and data in self._layer_artist_container: return True if self.large_data_size is not None and data.size >= self.large_data_size: proceed = self.warn('Add large data set?', 'Data set {0:s} has {1:d} points, and ' 'may render slowly.'.format( data.label, data.size), default='Cancel', setting='show_large_data_warning') if not proceed: return False if data not in self.session.data_collection: raise IncompatibleDataException("Data not in DataCollection") # Create layer artist and add to container. First check whether any # plugins want to make a custom layer artist. layer = get_layer_artist_from_registry( data, self) or self.get_data_layer_artist(data) if layer is None: return False # When adding a layer artist to the layer artist container, zorder # gets set automatically - however since we call a forced update of the # layer after adding it to the container we can ignore any callbacks # related to zorder. We also then need to set layer.state.zorder manually. with ignore_callback(layer.state, 'zorder'): self._layer_artist_container.append(layer) layer.update() self.draw_legend( ) # need to be called here because callbacks are ignored in previous step # Add existing subsets to viewer for subset in data.subsets: self.add_subset(subset) return True
def callback(*args, **kwargs): with ignore_callback(state, 'a'): state.a = 2