def run(): """This is an example of how contracts work """ print "*" * 30 # Create a stack which can hold 3 elements stack = StackImpl(10) from logilab.aspects.weaver import weaver from logilab.aspects.lib.logger import LoggerAspect import sys weaver.weave_methods(stack, LoggerAspect, sys.stderr) print "Tracing stack's method calls" stack.is_empty() print "-" * 20 stack.push("un élément") print "-" * 20 stack.push("un autre élément") print "-" * 20 stack.pop() print "*" * 30 print "Unweaving Logger Aspect ..." weaver.unweave_methods(stack, LoggerAspect) print "Now, calling a stack's method shouldn't be traced" stack.push("Un autre élement") print "Did it work ?"
def run(): """Example """ rules = define_rules() circle = Circle(12) square = Square(12) triangle = Triangle(10, 15) print "*" * 30 print "Trying to combine a square in a triangle : " triangle.combine_shape(square) print "Now, weave multimethods aspect, this should make a difference !" weaver.weave_methods(Shape, DispatcherAspect, rules) # Combining Triangle print "COMBINING Triangle with all types of shapes" triangle.combine_shape(square) triangle.combine_shape(circle) triangle.combine_shape(triangle) # Combining Circle print "COMBINING Circles with all types of shapes" circle.combine_shape(square) circle.combine_shape(circle) circle.combine_shape(triangle) # Combining Square print "COMBINING Square with all types of shapes" square.combine_shape(square) square.combine_shape(circle) square.combine_shape(triangle) print "*" * 30
def run(): """Example """ rules = define_rules() circle = Circle(12) square = Square(12) triangle = Triangle(10, 15) print "*"*30 print "Trying to combine a square in a triangle : " triangle.combine_shape(square) print "Now, weave multimethods aspect, this should make a difference !" weaver.weave_methods(Shape, DispatcherAspect, rules) # Combining Triangle print "COMBINING Triangle with all types of shapes" triangle.combine_shape(square) triangle.combine_shape(circle) triangle.combine_shape(triangle) # Combining Circle print "COMBINING Circles with all types of shapes" circle.combine_shape(square) circle.combine_shape(circle) circle.combine_shape(triangle) # Combining Square print "COMBINING Square with all types of shapes" square.combine_shape(square) square.combine_shape(circle) square.combine_shape(triangle) print "*" * 30
def test_weave_class(self): """Tests if all new instances of a class are also weaved """ weaver.weave_methods(self.klass, ContractAspect) self.assertRaises(ContractFailedError, self.instance.foo, 15) another_instance = Test() self.assertRaises(ContractFailedError, another_instance.foo, 15)
def run(): """This is an example of how contracts work """ print "*"*30 # Create a stack which can hold 3 elements stack = StackImpl(10) from logilab.aspects.weaver import weaver from logilab.aspects.lib.logger import LoggerAspect import sys weaver.weave_methods(stack, LoggerAspect, sys.stderr) print "Tracing stack's method calls" stack.is_empty() print "-"*20 stack.push("un élément") print "-"*20 stack.push("un autre élément") print "-"*20 stack.pop() print "*"*30 print "Unweaving Logger Aspect ..." weaver.unweave_methods(stack, LoggerAspect) print "Now, calling a stack's method shouldn't be traced" stack.push("Un autre élement") print "Did it work ?"
def test_weave_instance(self): """Tests if only the right instance is weaved """ weaver.weave_methods(self.instance, ContractAspect) self.assertRaises(ContractFailedError, self.instance.foo, 15) another_instance = Test() # In another instance : contracts are not activated, this should pass ! self.assertRaises(ValueError, another_instance.foo, 15)
def setUp(self): """setUp method for tests """ self.a = SillyClass() self.b = SillySubclass() weaver.weave_methods(self.a, ContractAspect) weaver.weave_methods(self.b, ContractAspect) weaver.get_aspect(ContractAspect)
def cb_plug_class(self, option, opt_name, value, parser): """optik callback to plug the aspect on a class""" parts = value.split('.') modulename, klassname = '.'.join(parts[:-1]), parts[-1] module = load_module_from_name(modulename) klass = getattr(module, klassname) #self._weaved[modulename] = module weaver.weave_methods(klass, LoggerAspect, sys.stderr, self.config)
def test_func_dict(self): """Checks if the wrapped func's global_dict is corretcly taken in account. """ weaver.weave_methods(module_test.Sorter, ContractAspect) sorter = module_test.Sorter() sorter.sort([12, 1, 5]) self.assertRaises(ContractFailedError, sorter.sort, "bad input")
def test_weaver_str(self): """ensures str cannot raise an exception""" weaver.weave_methods(self.instance, ContractAspect) str(weaver) class SomeClass: """some class to weave""" def some_method(self): """a method""" weaver.weave_methods(SomeClass, ContractAspect) str(weaver)
def test_weave_methods(self): """Tests if the weaver weaves correctly methods """ old_foo = getattr(self.instance, 'foo') old_bar = getattr(self.instance, 'bar') weaver.weave_methods(self.instance, ContractAspect) self.assert_(self.instance in weaver._Weaver__woven_dict) self.assert_(self.klass not in weaver._Weaver__woven_dict) new_foo = getattr(self.instance, 'foo') new_bar = getattr(self.instance, 'bar') self.assert_(old_foo != new_foo) self.assert_(old_bar != new_bar)
def test_multiple_weaving(self): """Tests if multiple aspects can be (un)weaved """ log_device = open('tests.log', 'w') weaver.weave_methods(self.instance, ContractAspect) weaver.weave_methods(self.instance, LoggerAspect, log_device) self.assert_(len(weaver.get_aspects(self.instance, 'foo')) == 2) self.instance.foo(5) self.assertRaises(ContractFailedError, self.instance.foo, 15) weaver.unweave_methods(self.instance, ContractAspect) self.assert_(len(weaver.get_aspects(self.instance, 'foo')) == 1) # Tests if the remaining Aspect is not the Contract one. self.assertRaises(ValueError, self.instance.foo, 15) log_device.close()
def run(): """main func """ print "This window will ask confirmation before quitting, and " \ "creating an new document." win = AppWindow('simple_window.glade') weaver.weave_methods(win, ConfirmationGTK2Aspect, win.main_window) weaver._unweave_method(win,'show',ConfirmationGTK2Aspect) weaver._unweave_method(win,'connect_signals',ConfirmationGTK2Aspect) win.show() win.connect_signals() gtk.main()
def run(): """main func """ print "This window will ask confirmation before quitting, and " \ "creating an new document." win = AppWindow('simple_window.glade') weaver.weave_methods(win, ConfirmationGTK2Aspect, win.main_window) weaver._unweave_method(win, 'show', ConfirmationGTK2Aspect) weaver._unweave_method(win, 'connect_signals', ConfirmationGTK2Aspect) win.show() win.connect_signals() gtk.main()
def test_weave_same_aspect(self): """Checks that weaver forbids weaving multiple time the same aspect on a method. """ weaver.weave_object(self.klass, ContractAspect) old_aspects = weaver.get_aspects(self.klass, 'foo') # Trying to weave a second time weaver.weave_object(self.klass, ContractAspect) new_aspects = weaver.get_aspects(self.klass, 'foo') self.assert_(old_aspects == new_aspects) # Now trying to weave on self.instance should produce warnings old_foo = self.instance.foo weaver.weave_methods(self.instance, ContractAspect) # self.instance.foo should not have been modified ! self.assertEquals(old_foo, self.instance.foo)
def test_multiple_weaving(self): """Tests if multiple aspects can be (un)weaved """ log_device = open('tests.log','w') weaver.weave_methods(self.instance, ContractAspect) weaver.weave_methods(self.instance, LoggerAspect, log_device) self.assert_(len(weaver.get_aspects(self.instance, 'foo')) == 2) self.instance.foo(5) self.assertRaises(ContractFailedError, self.instance.foo, 15) weaver.unweave_methods(self.instance, ContractAspect) self.assert_(len(weaver.get_aspects(self.instance, 'foo')) == 1) # Tests if the remaining Aspect is not the Contract one. self.assertRaises(ValueError, self.instance.foo, 15) log_device.close()
def run(): """Run example """ writer = MyClass() weaver.weave_methods(MyClass, ProfilerAspect) print "Warning : this can take a while ...., be patient" writer.write_one_char('a') writer.write_two_chars('a') writer.write_thousands_chars('a') writer.write_thousands_stringio('a') writer.write_thousands_cstringio('a') writer.write_thousands_join('a') writer.write_one_char('a') import sys aspect_instance = weaver.get_aspect(ProfilerAspect) aspect_instance.dump_readable_profiles(sys.stdout)
def test_unweave_methods(self): """Tests if the weaver unweaves correctly methods """ weaver.weave_methods(self.klass, ContractAspect) self.assertRaises(ContractFailedError, self.instance.foo, 15) weaver.unweave_methods(self.klass, ContractAspect) # Contracts are no more activated : this should pass ! self.assertRaises(ValueError, self.instance.foo, 15) # Test on instances another_instance = Test() weaver.weave_methods(self.instance, ContractAspect) weaver.weave_methods(another_instance, ContractAspect) self.assertRaises(ContractFailedError, self.instance.foo, 15) self.assertRaises(ContractFailedError, another_instance.foo, 15) weaver.unweave_methods(self.instance, ContractAspect) # Contracts are no more activated on self.instance: this should pass ! self.assertRaises(ValueError, self.instance.foo, 15) # They should be still activated on another_instance self.assertRaises(ContractFailedError, another_instance.foo, 15)
def setUp(self): """Creates misc. shapes""" self.square = Square() self.triangle = Triangle() weaver.weave_methods(Shape, DispatcherAspect, define_rules())
def setUp(self): """setUp method for tests """ self.foo = Foo() weaver.weave_methods(self.foo, EmptyAspect, 10, 20) self.aspect = weaver.get_aspects(self.foo, 'silly_method')[0]