def _create_kernel(self):
        from .internal_ipkernel import InternalIPKernel

        # This shouldn't happen with a normal lifecycle, but add a warning
        # just in case.
        if self._kernel is not None:
            warnings.warn(
                "A kernel already exists. "
                "No new kernel will be created.",
                RuntimeWarning,
            )
            return

        logger.debug("Creating the embedded IPython kernel")
        kernel = self._kernel = InternalIPKernel()
        bind_extension_point(kernel, 'initial_namespace', IPYTHON_NAMESPACE,
                             self.application)
        if self.init_ipkernel:
            kernel.init_ipkernel()
        else:
            warnings.warn(
                ("In the future, the IPython kernel will be initialized "
                 "automatically at creation time. To enable this "
                 "future behaviour now, create the plugin using "
                 "IPythonKernelPlugin(init_ipkernel=True)"),
                DeprecationWarning,
            )

        return kernel
    def test_explicit_extension_registry(self):
        """ explicit extension registry """

        registry = self.extension_registry

        # Add an extension point.
        registry.add_extension_point(self._create_extension_point('my.ep'))

        # Add an extension.
        registry.add_extension('my.ep', 42)

        # Declare a class that consumes the extension.
        class Foo(HasTraits):
            x = List

        f = Foo()
        f.on_trait_change(listener)

        # Create an empty extension registry use that in the binding.
        extension_registry = MutableExtensionRegistry()

        # Make some bindings.
        bind_extension_point(f, 'x', 'my.ep', extension_registry)

        # Make sure that we pick up the empty extension registry and not the
        # default one.
        self.assertEqual(0, len(f.x))
    def test_should_be_able_to_bind_multiple_traits_on_a_single_object(self):

        registry = self.extension_registry

        # Add 2 extension points.
        registry.add_extension_point(self._create_extension_point('my.ep'))
        registry.add_extension_point(self._create_extension_point('another.ep'))

        # Declare a class that consumes both of the extension points.
        class Foo(HasTraits):
            x = List
            y = List

        f = Foo()

        # Bind two different traits on the object to the extension points.
        bind_extension_point(f, 'x', 'my.ep', registry)
        bind_extension_point(f, 'y', 'another.ep', registry)
        self.assertEqual(0, len(f.x))
        self.assertEqual(0, len(f.y))

        # Add some contributions to the extension points.
        registry.add_extension('my.ep', 42)
        registry.add_extensions('another.ep', [98, 99, 100])

        # Make sure both traits were bound correctly.
        self.assertEqual(1, len(f.x))
        self.assertEqual(3, len(f.y))
    def test_explicit_extension_registry(self):
        """ explicit extension registry """

        registry = self.extension_registry

        # Add an extension point.
        registry.add_extension_point(self._create_extension_point('my.ep'))

        # Add an extension.
        registry.add_extension('my.ep', 42)

        # Declare a class that consumes the extension.
        class Foo(HasTraits):
            x = List

        f = Foo()
        f.on_trait_change(listener)

        # Create an empty extension registry use that in the binding.
        extension_registry = MutableExtensionRegistry()

        # Make some bindings.
        bind_extension_point(f, 'x', 'my.ep', extension_registry)

        # Make sure that we pick up the empty extension registry and not the
        # default one.
        self.assertEqual(0, len(f.x))

        return
    def test_should_be_able_to_bind_multiple_traits_on_a_single_object(self):

        registry = self.extension_registry

        # Add 2 extension points.
        registry.add_extension_point(self._create_extension_point('my.ep'))
        registry.add_extension_point(self._create_extension_point('another.ep'))

        # Declare a class that consumes both of the extension points.
        class Foo(HasTraits):
            x = List
            y = List

        f = Foo()

        # Bind two different traits on the object to the extension points.
        bind_extension_point(f, 'x', 'my.ep', registry)
        bind_extension_point(f, 'y', 'another.ep', registry)
        self.assertEqual(0, len(f.x))
        self.assertEqual(0, len(f.y))

        # Add some contributions to the extension points.
        registry.add_extension('my.ep', 42)
        registry.add_extensions('another.ep', [98, 99, 100])

        # Make sure both traits were bound correctly.
        self.assertEqual(1, len(f.x))
        self.assertEqual(3, len(f.y))

        return
    def test_set_extensions_via_trait(self):
        """ set extensions via trait """

        registry = self.extension_registry

        # Add an extension point.
        registry.add_extension_point(self._create_extension_point('my.ep'))

        # Add an extension.
        registry.add_extension('my.ep', 42)

        # Declare a class that consumes the extension.
        class Foo(HasTraits):
            x = List

        f = Foo()
        f.on_trait_change(listener)

        # Make some bindings.
        bind_extension_point(f, 'x', 'my.ep')

        # Make sure that the object was initialized properly.
        self.assertEqual(1, len(f.x))
        self.assertEqual(42, f.x[0])

        # Set the extensions.
        f.x = ['a string']

        # Make sure that the object picked up the new extension...
        self.assertEqual(1, len(f.x))
        self.assert_('a string' in f.x)

        self.assertEqual(1, len(registry.get_extensions('my.ep')))
        self.assert_('a string' in registry.get_extensions('my.ep'))

        # ... and that the correct trait change event was fired.
        self.assertEqual(f, listener.obj)
        self.assertEqual('x', listener.trait_name)
        self.assertEqual(1, len(listener.new))
        self.assert_('a string' in listener.new)

        return
    def test_set_extensions_via_trait(self):
        """ set extensions via trait """

        registry = self.extension_registry

        # Add an extension point.
        registry.add_extension_point(self._create_extension_point('my.ep'))

        # Add an extension.
        registry.add_extension('my.ep', 42)

        # Declare a class that consumes the extension.
        class Foo(HasTraits):
            x = List

        f = Foo()
        f.on_trait_change(listener)

        # Make some bindings.
        bind_extension_point(f, 'x', 'my.ep')

        # Make sure that the object was initialized properly.
        self.assertEqual(1, len(f.x))
        self.assertEqual(42, f.x[0])

        # Set the extensions.
        f.x = ['a string']

        # Make sure that the object picked up the new extension...
        self.assertEqual(1, len(f.x))
        self.assert_('a string' in f.x)

        self.assertEqual(1, len(registry.get_extensions('my.ep')))
        self.assert_('a string' in registry.get_extensions('my.ep'))

        # ... and that the correct trait change event was fired.
        self.assertEqual(f, listener.obj)
        self.assertEqual('x', listener.trait_name)
        self.assertEqual(1, len(listener.new))
        self.assert_('a string' in listener.new)

        return
    def test_untyped_extension_point(self):
        """ untyped extension point """

        registry = self.extension_registry

        # Add an extension point.
        registry.add_extension_point(self._create_extension_point('my.ep'))

        # Add an extension.
        registry.add_extension('my.ep', 42)

        # Declare a class that consumes the extension.
        class Foo(HasTraits):
            x = List

        f = Foo()
        f.on_trait_change(listener)

        # Make some bindings.
        bind_extension_point(f, 'x', 'my.ep')

        # Make sure that the object was initialized properly.
        self.assertEqual(1, len(f.x))
        self.assertEqual(42, f.x[0])

        # Add another extension.
        registry.add_extension('my.ep', 'a string')

        # Make sure that the object picked up the new extension...
        self.assertEqual(2, len(f.x))
        self.assert_(42 in f.x)
        self.assert_('a string' in f.x)

        # ... and that the correct trait change event was fired.
        self.assertEqual(f, listener.obj)
        self.assertEqual('x_items', listener.trait_name)
        self.assertEqual(1, len(listener.new.added))
        self.assert_('a string' in listener.new.added)

        return
    def test_untyped_extension_point(self):
        """ untyped extension point """

        registry = self.extension_registry

        # Add an extension point.
        registry.add_extension_point(self._create_extension_point('my.ep'))

        # Add an extension.
        registry.add_extension('my.ep', 42)

        # Declare a class that consumes the extension.
        class Foo(HasTraits):
            x = List

        f = Foo()
        f.on_trait_change(listener)

        # Make some bindings.
        bind_extension_point(f, 'x', 'my.ep')

        # Make sure that the object was initialized properly.
        self.assertEqual(1, len(f.x))
        self.assertEqual(42, f.x[0])

        # Add another extension.
        registry.add_extension('my.ep', 'a string')

        # Make sure that the object picked up the new extension...
        self.assertEqual(2, len(f.x))
        self.assert_(42 in f.x)
        self.assert_('a string' in f.x)

        # ... and that the correct trait change event was fired.
        self.assertEqual(f, listener.obj)
        self.assertEqual('x_items', listener.trait_name)
        self.assertEqual(1, len(listener.new.added))
        self.assert_('a string' in listener.new.added)

        return
    def test_set_extensions_via_registry(self):
        """ set extensions via registry """

        registry = self.extension_registry

        # Add an extension point.
        registry.add_extension_point(self._create_extension_point("my.ep"))

        # Add an extension.
        registry.add_extension("my.ep", 42)

        # Declare a class that consumes the extension.
        class Foo(HasTraits):
            x = List

        f = Foo()
        f.on_trait_change(listener)

        # Make some bindings.
        bind_extension_point(f, "x", "my.ep")

        # Make sure that the object was initialized properly.
        self.assertEqual(1, len(f.x))
        self.assertEqual(42, f.x[0])

        # Set the extensions.
        registry.set_extensions("my.ep", ["a string"])

        # Make sure that the object picked up the new extension...
        self.assertEqual(1, len(f.x))
        self.assertTrue("a string" in f.x)

        # ... and that the correct trait change event was fired.
        self.assertEqual(f, listener.obj)
        self.assertEqual("x", listener.trait_name)
        self.assertEqual(1, len(listener.new))
        self.assertTrue("a string" in listener.new)
 def _kernel_default(self):
     from .internal_ipkernel import InternalIPKernel
     kernel = InternalIPKernel()
     bind_extension_point(kernel, 'initial_namespace', IPYTHON_NAMESPACE,
                          self.application)
     return kernel
Exemple #12
0
 def _kernel_default(self):
     from .internal_ipkernel import InternalIPKernel
     kernel = InternalIPKernel()
     bind_extension_point(kernel, 'initial_namespace',
                          IPYTHON_NAMESPACE, self.application)
     return kernel