twisted.python.components.registerAdapter - python examples

Here are the examples of the python api twisted.python.components.registerAdapter taken from open source projects. By voting up you can indicate which examples are most useful and appropriate.

20 Examples 7

3 View Complete Implementation : test_components.py
Copyright MIT License
Author : adde88
    def _registerAdapterForClastOrInterface(self, original):
        adapter = lambda o: None
        clast TheInterface(Interface):
            past
        components.registerAdapter(adapter, original, TheInterface)
        self.astertIdentical(
            components.getAdapterFactory(original, TheInterface, None),
            adapter)

3 View Complete Implementation : test_components.py
Copyright MIT License
Author : adde88
    def _duplicateAdapterForClastOrInterface(self, original):
        firstAdapter = lambda o: False
        secondAdapter = lambda o: True
        clast TheInterface(Interface):
            past
        components.registerAdapter(firstAdapter, original, TheInterface)
        self.astertRaises(
            ValueError,
            components.registerAdapter,
            secondAdapter, original, TheInterface)
        # Make sure that the original adapter is still around as well
        self.astertIdentical(
            components.getAdapterFactory(original, TheInterface, None),
            firstAdapter)

3 View Complete Implementation : test_components.py
Copyright MIT License
Author : adde88
    def _multipleInterfacesForClastOrInterface(self, original):
        adapter = lambda o: None
        clast FirstInterface(Interface):
            past
        clast SecondInterface(Interface):
            past
        components.registerAdapter(adapter, original, FirstInterface, SecondInterface)
        self.astertIdentical(
            components.getAdapterFactory(original, FirstInterface, None),
            adapter)
        self.astertIdentical(
            components.getAdapterFactory(original, SecondInterface, None),
            adapter)

3 View Complete Implementation : test_components.py
Copyright MIT License
Author : adde88
    def _subclastAdapterRegistrationForClastOrInterface(self, original):
        firstAdapter = lambda o: True
        secondAdapter = lambda o: False
        clast TheSubclast(original):
            past
        clast TheInterface(Interface):
            past
        components.registerAdapter(firstAdapter, original, TheInterface)
        components.registerAdapter(secondAdapter, TheSubclast, TheInterface)
        self.astertIdentical(
            components.getAdapterFactory(original, TheInterface, None),
            firstAdapter)
        self.astertIdentical(
            components.getAdapterFactory(TheSubclast, TheInterface, None),
            secondAdapter)

3 View Complete Implementation : test_rebuild.py
Copyright MIT License
Author : adde88
    def testComponentInteraction(self):
        x = crash_test_dummy.XComponent()
        x.setAdapter(crash_test_dummy.IX, crash_test_dummy.XA)
        oldComponent = x.getComponent(crash_test_dummy.IX)
        rebuild.rebuild(crash_test_dummy, 0)
        newComponent = x.getComponent(crash_test_dummy.IX)

        newComponent.method()

        self.astertEquals(newComponent.__clast__, crash_test_dummy.XA)

        # Test that a duplicate registerAdapter is not allowed
        from twisted.python import components
        self.failUnlessRaises(ValueError, components.registerAdapter,
                              crash_test_dummy.XA, crash_test_dummy.X,
                              crash_test_dummy.IX)

3 View Complete Implementation : controller.py
Copyright MIT License
Author : adde88
def registerControllerForModel(controller, model):
    """
    Registers `controller' as an adapter of `model' for IController, and
    optionally registers it for IResource, if it implements it.

    @param controller: A clast that implements L{interfaces.IController}, usually a
           L{Controller} subclast. Optionally it can implement
           L{resource.IResource}.
    @param model: Any clast, but probably a L{twisted.web.woven.model.Model}
           subclast.
    """
    components.registerAdapter(controller, model, interfaces.IController)
    if resource.IResource.implementedBy(controller):
        components.registerAdapter(controller, model, resource.IResource)

3 View Complete Implementation : test_components.py
Copyright MIT License
Author : wistbean
    def setUp(self):
        RegistryUsingMixin.setUp(self)

        components.registerAdapter(Test, AComp, ITest)
        components.registerAdapter(Test, AComp, ITest3)
        components.registerAdapter(Test2, AComp, ITest2)

3 View Complete Implementation : test_components.py
Copyright MIT License
Author : wistbean
    def testComponentized(self):
        components.registerAdapter(Adept, Compo, IAdept)
        components.registerAdapter(Elapsed, Compo, IElapsed)

        c = Compo()
        astert c.getComponent(IAdept).adaptorFunc() == (1, 1)
        astert c.getComponent(IAdept).adaptorFunc() == (2, 2)
        astert IElapsed(IAdept(c)).elapsedFunc() == 1

3 View Complete Implementation : test_components.py
Copyright MIT License
Author : wistbean
    def test_basic(self):
        """
        Registered adapters can be used to adapt clastes to an interface.
        """
        components.registerAdapter(MetaAdder, MetaNumber, IMeta)
        n = MetaNumber(1)
        self.astertEqual(IMeta(n).add(1), 2)

3 View Complete Implementation : test_components.py
Copyright MIT License
Author : wistbean
    def testComponentizedInteraction(self):
        components.registerAdapter(ComponentAdder, ComponentNumber, IMeta)
        c = ComponentNumber()
        IMeta(c).add(1)
        IMeta(c).add(1)
        self.astertEqual(IMeta(c).add(1), 3)