Exemplo n.º 1
0
    def parse(self):

        args = self.parser.parse_args()
        self.session.gui = True

        if args.color:
            self.session.color_cli = True
            os.environ['OA_CLICOLOR'] = '1'

        if args.list_plugins:
            self.session.gui = False
            import pkg_resources
            from openalea.core.plugin import iter_groups

            if args.list_plugins in ['summary', 'all']:
                prefixes = ['oalab', 'vpltk', 'openalea']
            else:
                prefixes = [args.list_plugins]
            for category in sorted(iter_groups()):
                match = False
                for prefix in prefixes:
                    if category.startswith(prefix):
                        match = True
                        break
                if match:
                    eps = [ep for ep in pkg_resources.iter_entry_points(category)]
                    if args.list_plugins == 'summary':
                        print '  - \033[91m%s\033[0m (%d plugins)' % (category, len(eps))
                    else:
                        print '\033[91m%s\033[0m' % category
                        print
                        for ep in eps:
                            print_plugin_name(ep)
                        print
                        print

        if args.debug_plugins:
            debug = args.debug_plugins.split(',')
            if 'oalab.lab' not in debug:
                debug.append('oalab.lab')
            from openalea.core.service.plugin import PluginInstanceManager
            pim = PluginInstanceManager()
            pim.debug = debug

        self.session.extension = args.extension
        self.args = args
Exemplo n.º 2
0
    def __init__(self):
        import traceback
        traceback.print_stack(file=sys.__stdout__)

        self._project = None
        self._is_proj = False
        self._debug = False
        self.gui = True

        self.tmpdir = path(get_openalea_tmp_dir())

        self._config = MainConfig()
        self.extension = None

        self.applet = {}
        self.manager = {}

        self.package_manager = package_manager
        self.control_manager = ControlManager()
        self.project_manager = ProjectManager()
        self.plugin_manager = PluginManager()
        self.plugin_instance_manager = PluginInstanceManager()

        self.manager['control'] = self.control_manager
        self.manager['package'] = self.package_manager
        self.manager['project'] = self.project_manager
        self.manager['plugin'] = self.plugin_manager
        self.manager['plugin_instance'] = self.plugin_instance_manager

        self.world = World()

        self.interpreter = interpreter()

        # Hack if interpreter is an object from class TerminalInteractiveShell
        if not hasattr(self.interpreter, "shell"):
            self.interpreter.shell = self.interpreter
        if hasattr(self.interpreter.shell, "events"):
            self.interpreter.shell.events.register("post_execute",
                                                   self.add_to_history)
        else:
            print("You need ipython >= 2.0 to use history.")

#         self.project_manager.set_shell(self.interpreter.shell)

        self.interpreter.locals['session'] = self

        self.old_syspath = sys.path

        self.load_default()

        self.__class__.instantiated = True
Exemplo n.º 3
0
    def setUp(self):
        self.pm = PluginManager()
        self.pm.debug = False

        self.pim = PluginInstanceManager(self.pm)
Exemplo n.º 4
0
class TestPluginManager(unittest.TestCase):

    @classmethod
    def setupClass(cls):
        cls.tmppath, cls.pythonpath = install_package()

        import pkg_resources
        dist = list(pkg_resources.find_distributions(cls.pythonpath))[0]
        pkg_resources.working_set.add(dist)

    def setUp(self):
        self.pm = PluginManager()
        self.pm.debug = False

        self.pim = PluginInstanceManager(self.pm)

    def test_autoload(self):
        pm = PluginManager()
        pm.items('test.c1')
        assert 'test.c1' in pm._item
        assert 'tstpkg1.plugin:C1Plugin1' in pm._item['test.c1']

        pm = PluginManager(autoload=[])
        pm.items('test.c1')
        assert 'test.c1' in pm._item
        assert 'tstpkg1.plugin:C1Plugin1' not in pm._item['test.c1']

    def test_module_plugin_def(self):
        assert self.pm.items('test.c1')

        # check if right plugins have been found
        assert self.pm.item('MyPlugin1', 'test.c1') is not None
        assert self.pm.item('MyPlugin2', 'test.c1') is not None

    def test_manual_plugin_def(self):
        assert self.pm.items('test.c2')

        # check if right plugins have been found
        assert self.pm.item('C2Plugin1', 'test.c2') is not None
        assert self.pm.item('C2Plugin2', 'test.c2') is not None

    def test_interface_filter(self):
        plugins = self.pm.items('test.c1')
        assert len(plugins) == 2
        plugins = self.pm.items('test.c1', criteria=dict(implement='IClass1'))
        assert len(plugins) == 1
        assert plugins[0].name == 'MyPlugin1'

    def test_dynamic_plugin(self):
        pl = self.pm.add(AlgoPlugin1, 'test.dynamic3')
        self.pm.add(AlgoPlugin2, 'test.dynamic3')
        assert len(self.pm.items('test.dynamic3')) == 2

        objc3c1 = self.pim.instance('test.dynamic3', 'algo_1')
        assert objc3c1
        assert isinstance(objc3c1, Algo)
        objc3c2 = self.pim.instance('test.dynamic3', 'algo_2')
        assert objc3c2
        assert isinstance(objc3c1, Algo)

    def atest_proxy_plugin(self):
        from openalea.core.plugin.manager import SimpleClassPluginProxy
        self.pm.add('test.dynamic4', Algo, plugin_proxy=SimpleClassPluginProxy)
        objc4c1 = self.pim.instance('test.dynamic4', 'Algo')
        objc4c1_2 = self.pim.instance('test.dynamic4', 'Algo')
        objc4c1_3 = self.pim.new('test.dynamic4', 'Algo')

        assert objc4c1
        self.assertIsInstance(objc4c1, Algo)
        assert objc4c1 is objc4c1_2
        assert objc4c1 is not objc4c1_3

    def test_plugin_name(self):
        import tstpkg1.plugin
        import tstpkg1.impl

        # Check manager use plugin name attribute if defined (instead of class name)
        c1plugin1 = self.pm.item('MyPlugin1', 'test.c1')
        self.assertIsInstance(c1plugin1, tstpkg1.plugin.C1Plugin1)

        # Check there is no conflict if two plugins with same alias (in setup.py) but in different categories
        # Check plugin name use class name if no attribute "name"
        c2plugin1 = self.pm.item('C2Plugin1', 'test.c2')
        self.assertIsInstance(c2plugin1, tstpkg1.plugin.C2Plugin1)

    def test_new_instance(self):
        import tstpkg1.impl

        objc1c1 = self.pim.instance('test.c1', 'MyPlugin1')
        assert objc1c1
        assert isinstance(objc1c1, tstpkg1.impl.C1Class1)

        objc1c2 = self.pim.instance('test.c1', 'MyPlugin2')
        assert objc1c2
        assert isinstance(objc1c2, tstpkg1.impl.C1Class2)

        objc2c1 = self.pim.instance('test.c2', 'C2Plugin1')
        assert objc2c1
        assert isinstance(objc2c1, tstpkg1.impl.C2Class1)

        objc2c2 = self.pim.instance('test.c2', 'C2Plugin2')
        assert objc2c1
        assert isinstance(objc2c2, tstpkg1.impl.C2Class2)

        # Check instance return existing instance
        objc1c1_2 = self.pim.instance('test.c1', 'MyPlugin1')
        assert objc1c1 is objc1c1_2
        assert self.pim.instances('test.c1', 'MyPlugin1')[0] is objc1c1_2

    def test_multiple_instance(self):
        # Assert weakref work correctly and plugin instances have been lost
        assert not self.pim.instances('test.c1', 'MyPlugin1')

        objc1c1_0 = self.pim.instance('test.c1', 'MyPlugin1')
        objc1c1_1 = self.pim.new('test.c1', 'MyPlugin1')
        objc1c1_2 = self.pim.new('test.c1', 'MyPlugin1')

        # Assert all object have been created correctly
        assert objc1c1_0
        assert objc1c1_1
        assert objc1c1_2

        # Assert all instances are different as we use new instead of instance
        assert objc1c1_0 is not objc1c1_1
        assert objc1c1_1 is not objc1c1_2

        objc1c2 = self.pim.new('test.c1', 'MyPlugin2')

        assert len(self.pim.instances('test.c1', 'MyPlugin1')) == 3
        assert len(self.pim.instances('test.c1')) == 4

        del objc1c1_2

        assert len(self.pim.instances('test.c1', 'MyPlugin1')) == 2
        assert len(self.pim.instances('test.c1')) == 3

        del objc1c2

        assert len(self.pim.instances('test.c1', 'MyPlugin1')) == 2
        assert len(self.pim.instances('test.c1', 'MyPlugin2')) == 0
        assert len(self.pim.instances('test.c1')) == 2

    def test_debug_ep_load(self):

        self.pm.debug = True
        with self.assertRaises(ImportError):
            self.pm.items('test.err1')

        self.pm.clear()
        self.pm.debug = False
        self.pm.items('test.err1')

    @classmethod
    def tearDownClass(cls):
        cls.tmppath.rmtree()
Exemplo n.º 5
0
    def setUp(self):
        self.pm = PluginManager()
        self.pm.debug = False

        self.pim = PluginInstanceManager(self.pm)
Exemplo n.º 6
0
class TestPluginManager(unittest.TestCase):
    @classmethod
    def setupClass(cls):
        cls.tmppath, cls.pythonpath = install_package()

        import pkg_resources

        dist = list(pkg_resources.find_distributions(cls.pythonpath))[0]
        pkg_resources.working_set.add(dist)

    def setUp(self):
        self.pm = PluginManager()
        self.pm.debug = False

        self.pim = PluginInstanceManager(self.pm)

    def test_autoload(self):
        pm = PluginManager()
        pm.items("test.c1")
        assert "test.c1" in pm._item
        assert "tstpkg1.plugin:C1Plugin1" in pm._item["test.c1"]

        pm = PluginManager(autoload=[])
        pm.items("test.c1")
        assert "test.c1" in pm._item
        assert "tstpkg1.plugin:C1Plugin1" not in pm._item["test.c1"]

    def test_module_plugin_def(self):
        assert self.pm.items("test.c1")

        # check if right plugins have been found
        assert self.pm.item("MyPlugin1", "test.c1") is not None
        assert self.pm.item("MyPlugin2", "test.c1") is not None

    def test_manual_plugin_def(self):
        assert self.pm.items("test.c2")

        # check if right plugins have been found
        assert self.pm.item("C2Plugin1", "test.c2") is not None
        assert self.pm.item("C2Plugin2", "test.c2") is not None

    def test_interface_filter(self):
        plugins = self.pm.items("test.c1")
        assert len(plugins) == 2
        plugins = self.pm.items("test.c1", criteria=dict(implement="IClass1"))
        assert len(plugins) == 1
        assert plugins[0].name == "MyPlugin1"

    def test_dynamic_plugin(self):
        pl = self.pm.add(AlgoPlugin1, "test.dynamic3")
        self.pm.add(AlgoPlugin2, "test.dynamic3")
        assert len(self.pm.items("test.dynamic3")) == 2

        objc3c1 = self.pim.instance("test.dynamic3", "algo_1")
        assert objc3c1
        assert isinstance(objc3c1, Algo)
        objc3c2 = self.pim.instance("test.dynamic3", "algo_2")
        assert objc3c2
        assert isinstance(objc3c1, Algo)

    def atest_proxy_plugin(self):
        from openalea.core.plugin.manager import SimpleClassPluginProxy

        self.pm.add("test.dynamic4", Algo, plugin_proxy=SimpleClassPluginProxy)
        objc4c1 = self.pim.instance("test.dynamic4", "Algo")
        objc4c1_2 = self.pim.instance("test.dynamic4", "Algo")
        objc4c1_3 = self.pim.new("test.dynamic4", "Algo")

        assert objc4c1
        self.assertIsInstance(objc4c1, Algo)
        assert objc4c1 is objc4c1_2
        assert objc4c1 is not objc4c1_3

    def test_plugin_name(self):
        import tstpkg1.plugin
        import tstpkg1.impl

        # Check manager use plugin name attribute if defined (instead of class name)
        c1plugin1 = self.pm.item("MyPlugin1", "test.c1")
        self.assertIsInstance(c1plugin1, tstpkg1.plugin.C1Plugin1)

        # Check there is no conflict if two plugins with same alias (in setup.py) but in different categories
        # Check plugin name use class name if no attribute "name"
        c2plugin1 = self.pm.item("C2Plugin1", "test.c2")
        self.assertIsInstance(c2plugin1, tstpkg1.plugin.C2Plugin1)

    def test_new_instance(self):
        import tstpkg1.impl

        objc1c1 = self.pim.instance("test.c1", "MyPlugin1")
        assert objc1c1
        assert isinstance(objc1c1, tstpkg1.impl.C1Class1)

        objc1c2 = self.pim.instance("test.c1", "MyPlugin2")
        assert objc1c2
        assert isinstance(objc1c2, tstpkg1.impl.C1Class2)

        objc2c1 = self.pim.instance("test.c2", "C2Plugin1")
        assert objc2c1
        assert isinstance(objc2c1, tstpkg1.impl.C2Class1)

        objc2c2 = self.pim.instance("test.c2", "C2Plugin2")
        assert objc2c1
        assert isinstance(objc2c2, tstpkg1.impl.C2Class2)

        # Check instance return existing instance
        objc1c1_2 = self.pim.instance("test.c1", "MyPlugin1")
        assert objc1c1 is objc1c1_2
        assert self.pim.instances("test.c1", "MyPlugin1")[0] is objc1c1_2

    def test_multiple_instance(self):
        # Assert weakref work correctly and plugin instances have been lost
        assert not self.pim.instances("test.c1", "MyPlugin1")

        objc1c1_0 = self.pim.instance("test.c1", "MyPlugin1")
        objc1c1_1 = self.pim.new("test.c1", "MyPlugin1")
        objc1c1_2 = self.pim.new("test.c1", "MyPlugin1")

        # Assert all object have been created correctly
        assert objc1c1_0
        assert objc1c1_1
        assert objc1c1_2

        # Assert all instances are different as we use new instead of instance
        assert objc1c1_0 is not objc1c1_1
        assert objc1c1_1 is not objc1c1_2

        objc1c2 = self.pim.new("test.c1", "MyPlugin2")

        assert len(self.pim.instances("test.c1", "MyPlugin1")) == 3
        assert len(self.pim.instances("test.c1")) == 4

        del objc1c1_2

        assert len(self.pim.instances("test.c1", "MyPlugin1")) == 2
        assert len(self.pim.instances("test.c1")) == 3

        del objc1c2

        assert len(self.pim.instances("test.c1", "MyPlugin1")) == 2
        assert len(self.pim.instances("test.c1", "MyPlugin2")) == 0
        assert len(self.pim.instances("test.c1")) == 2

    def test_debug_ep_load(self):

        self.pm.debug = True
        with self.assertRaises(ImportError):
            self.pm.items("test.err1")

        self.pm.clear()
        self.pm.debug = False
        self.pm.items("test.err1")

    @classmethod
    def tearDownClass(cls):
        cls.tmppath.rmtree()