Esempio n. 1
0
    def callback(klass):
        """ Called when the class has been created. """

        # At this point:-
        #
        # klass is the callable (usually a class) that takes one argument (the
        # adaptee) and returns an appropriate adapter (or None if the adaptation
        # is not possible).

        # What the adapters created by the factory will adapt from.
        if type(from_) is not list:
            from_protocols = [from_]

        else:
            from_protocols = from_

        # What the adapters created by the factory will adapt to.
        if type(to) is not list:
            to_protocols = [to]

        else:
            to_protocols = to

        if factory is None:
            # If the adapter is cached or has a 'when' expression then create a
            # default factory:

            adapter_factory = klass

            if when != '':

                def _conditional_factory(adaptee, *args, **kw):
                    namespace = {'adaptee': adaptee}

                    if eval(when, namespace, namespace):
                        return klass(adaptee, *args, **kw)

                    return None

                adapter_factory = _conditional_factory

            if cached:
                adapter_factory = CachedAdapterFactory(factory=adapter_factory)

        else:
            adapter_factory = factory

        for from_protocol in from_protocols:
            for to_protocol in to_protocols:
                register_factory(adapter_factory, from_protocol, to_protocol)

        for to_protocol in to_protocols:
            # We cannot register adapter factories that are functions. (This is
            # ony relevant when using 'adapts' as a function.
            if isinstance(klass, type):
                # We use type(to_protocol) in case the to_protocols implements
                # its own 'register' method which overrides the ABC method.
                type(to_protocol).register(to_protocol, klass)

        return klass
Esempio n. 2
0
    def callback(klass):
        """ Called when the class has been created. """

        # At this point:-
        #
        # klass is the callable (usually a class) that takes one argument (the
        # adaptee) and returns an appropriate adapter (or None if the adaptation
        # is not possible).

        # What the adapters created by the factory will adapt from.
        if type(from_) is not list:
            from_protocols = [from_]

        else:
            from_protocols = from_

        # What the adapters created by the factory will adapt to.
        if type(to) is not list:
            to_protocols = [to]

        else:
            to_protocols = to

        if factory is None:
            # If the adapter is cached or has a 'when' expression then create a
            # default factory:

            adapter_factory = klass

            if when != '':
                def _conditional_factory(adaptee, *args, **kw):
                    namespace = {'adaptee': adaptee}

                    if eval(when, namespace, namespace):
                        return klass(adaptee, *args, **kw)

                    return None

                adapter_factory = _conditional_factory

            if cached:
                adapter_factory = CachedAdapterFactory(factory=adapter_factory)

        else:
            adapter_factory = factory

        for from_protocol in from_protocols:
            for to_protocol in to_protocols:
                register_factory(adapter_factory, from_protocol, to_protocol)

        for to_protocol in to_protocols:
            # We cannot register adapter factories that are functions. (This is
            # ony relevant when using 'adapts' as a function.
            if isinstance(klass, type):
                # We use type(to_protocol) in case the to_protocols implements
                # its own 'register' method which overrides the ABC method.
                type(to_protocol).register(to_protocol, klass)

        return klass
Esempio n. 3
0
def declareAdapter(factory, provides,
                   forTypes=(), forProtocols=(), forObjects=()):

    from traits.adaptation.api import register_factory
    from itertools import chain

    for from_protocol in chain(forTypes, forProtocols, forObjects):
        for to_protocol in provides:
            register_factory(factory, from_protocol, to_protocol)
Esempio n. 4
0
def declareAdapter(factory,
                   provides,
                   forTypes=(),
                   forProtocols=(),
                   forObjects=()):

    from traits.adaptation.api import register_factory
    from itertools import chain

    for from_protocol in chain(forTypes, forProtocols, forObjects):
        for to_protocol in provides:
            register_factory(factory, from_protocol, to_protocol)
Esempio n. 5
0
    def test_global_convenience_functions(self):
        ex = self.examples

        # Global `register_factory`.
        register_factory(factory=ex.UKStandardToEUStandard,
                         from_protocol=ex.UKStandard,
                         to_protocol=ex.EUStandard)

        uk_plug = ex.UKPlug()
        # Global `adapt`.
        eu_plug = adapt(uk_plug, ex.EUStandard)
        self.assertIsNotNone(eu_plug)
        self.assertIsInstance(eu_plug, ex.UKStandardToEUStandard)

        # Global `provides_protocol`.
        self.assertTrue(provides_protocol(ex.UKPlug, ex.UKStandard))

        # Global `supports_protocol`.
        self.assertTrue(supports_protocol(uk_plug, ex.EUStandard))
    def test_global_convenience_functions(self):
        ex = self.examples

        # Global `register_factory`.
        register_factory(
            factory       = ex.UKStandardToEUStandard,
            from_protocol = ex.UKStandard,
            to_protocol   = ex.EUStandard
        )

        uk_plug = ex.UKPlug()
        # Global `adapt`.
        eu_plug = adapt(uk_plug, ex.EUStandard)
        self.assertIsNotNone(eu_plug)
        self.assertIsInstance(eu_plug, ex.UKStandardToEUStandard)

        # Global `provides_protocol`.
        self.assertTrue(provides_protocol(ex.UKPlug, ex.UKStandard))

        # Global `supports_protocol`.
        self.assertTrue(supports_protocol(uk_plug, ex.EUStandard))