def __set__(self, instance, value): try: try: old = self._getter(instance) except AttributeError: old = NoDefault super(trait, self).__set__(instance, value) except Exception as exc: self.trigger(instance, "error", exc=exc) raise self.trigger(instance, "changed", old=old, new=value) if hasattr(old, "__snitch__"): disconnect(instance, old, self) if hasattr(value, "__snitch__"): connect(instance, value, self)
def test_connected(self): obj1 = AnyObject() obj2 = AnyObject() obj3 = AnyObject() init_events(obj1, obj2, obj3) self.assertFalse(connected(obj1, obj2)) connect(obj1, obj2, 'attr') self.assertTrue(connected(obj1, obj2)) self.assertFalse(connected(obj2, obj1)) connect(obj2, obj3, 'attr') self.assertTrue(connected(obj1, obj3)) self.assertTrue(connected(obj2, obj3)) self.assertFalse(connected(obj3, obj2)) self.assertFalse(connected(obj3, obj1))
def __set__(self, instance, value): try: try: old = self._getter(instance) except AttributeError: old = NoDefault super(trait, self).__set__(instance, value) except Exception as exc: self.trigger(instance, 'error', exc=exc) raise self.trigger(instance, 'changed', old=old, new=value) if hasattr(old, '__snitch__'): disconnect(instance, old, self) if hasattr(value, '__snitch__'): connect(instance, value, self)
def test_connected(self): obj1 = AnyObject() obj2 = AnyObject() obj3 = AnyObject() init_events(obj1, obj2, obj3) self.assertFalse(connected(obj1, obj2)) connect(obj1, obj2, 'attr') self.assertTrue(connected(obj1, obj2)) self.assertFalse(connected(obj2, obj1)) connect(obj2, obj3, 'attr') self.assertTrue(connected(obj1, obj3)) self.assertTrue(connected(obj2, obj3)) self.assertFalse(connected(obj3,obj2)) self.assertFalse(connected(obj3, obj1))
def test_chain(self): global id1, id2 id1, id2 = 0, 0 obj1 = AnyObject() obj2 = AnyObject() obj3 = AnyObject() obj4 = AnyObject() init_events( obj1, obj2, obj3, obj4, ) connect(obj1, obj2, 'c12') connect(obj2, obj3, 'c23') connect(obj2, obj4, 'c24') def seen3(event): global id1 id1 = True self.assertEqual(event.target, ('c12', 'c23', 'x')) events(obj1).listen(('c12', 'c23', 'x'), seen3) events(obj3).etrigger('x') self.assertTrue(id1) disconnect(obj1, obj2, 'c12') id1 = False events(obj3).etrigger('x') self.assertFalse(id1) connect(obj1, obj2, 'c12') del obj2 #Brake Link gc.collect() events(obj3).etrigger('x') self.assertFalse(id1)
def test_event_cycle(self): obj1 = AnyObject() obj2 = AnyObject() obj3 = AnyObject() init_events(obj1, obj2, obj3) self.assertFalse(connected(obj1, obj2)) connect(obj1, obj2, 'attr') connect(obj2, obj3, 'attr') with self.assertRaises(EventCycleError): connect(obj3, obj1, 'attr')
def test_chain(self): global id1, id2 id1, id2 = 0, 0 obj1 = AnyObject() obj2 = AnyObject() obj3 = AnyObject() obj4 = AnyObject() init_events(obj1, obj2, obj3, obj4,) connect(obj1, obj2, 'c12') connect(obj2, obj3, 'c23') connect(obj2, obj4, 'c24') def seen3(event): global id1 id1 = True self.assertEqual(event.target, ('c12', 'c23', 'x')) events(obj1).listen(('c12', 'c23', 'x'), seen3) events(obj3).etrigger('x') self.assertTrue(id1) disconnect(obj1, obj2, 'c12') id1 = False events(obj3).etrigger('x') self.assertFalse(id1) connect(obj1, obj2, 'c12') del obj2 #Brake Link gc.collect() events(obj3).etrigger('x') self.assertFalse(id1)