Пример #1
0
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 ?"
Пример #2
0
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
Пример #3
0
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
Пример #4
0
 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)
Пример #5
0
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 ?"
Пример #6
0
 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)
Пример #7
0
 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)
Пример #8
0
 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)
Пример #9
0
 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)
Пример #10
0
 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)
Пример #11
0
 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")
Пример #12
0
 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)
Пример #13
0
 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)
Пример #14
0
    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)
Пример #15
0
 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)
Пример #16
0
 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()
Пример #17
0
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()
Пример #18
0
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()
Пример #19
0
 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)
Пример #20
0
 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)
Пример #21
0
 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()
Пример #22
0
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)
Пример #23
0
 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)
Пример #24
0
 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)
Пример #25
0
 def setUp(self):
     """Creates misc. shapes"""
     self.square = Square()
     self.triangle = Triangle()
     weaver.weave_methods(Shape, DispatcherAspect, define_rules())
Пример #26
0
 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]
Пример #27
0
 def setUp(self):
     """Creates misc. shapes"""
     self.square = Square()
     self.triangle = Triangle()
     weaver.weave_methods(Shape, DispatcherAspect, define_rules())
Пример #28
0
 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]