Ejemplo n.º 1
0
    def setUp(self):
        self.plugin_manager = PluginManager()

        self.plugin_a = Plugin.wrap_function("test_plugin_a",
                                             "testing plugin a",
                                             lambda ctx: "execute a")
        self.plugin_b = Plugin.wrap_function("test_plugin_b",
                                             "testing plugin b",
                                             lambda ctx: "execute b")

        self.plugin_manager.register(self.plugin_a)
        self.plugin_manager.register(self.plugin_b)
Ejemplo n.º 2
0
    def test_getitem(self):
        plugin_a = Plugin.wrap_function("test_plugin_a", "testing plugin a",
                                        lambda ctx: "execute a")

        plugin_b = Plugin.wrap_function("test_plugin_b", "testing plugin b",
                                        lambda ctx: "execute b")

        plugin_manager_a = PluginManager()
        plugin_manager_a.register(plugin_a)
        plugin_manager_a.register(plugin_b)

        plugin_a1 = Plugin.wrap_function("test_plugin_a", "testing plugin a 1",
                                         lambda ctx: "execute a 1")

        plugin_manager_b = PluginManager()
        plugin_manager_b.register(plugin_a1)

        plugin_mgr_chain = PluginManagerChain(plugin_manager_b,
                                              plugin_manager_a)
        plugin_mgr_chain.sys_prepare(None)

        # 优先获取最优先的插件
        self.assertEquals(plugin_mgr_chain["test_plugin_a"].execute(None),
                          "execute a 1")
        self.assertEquals(plugin_mgr_chain["test_plugin_b"].execute(None),
                          "execute b")

        self.failUnlessException(PluginNotFoundException,
                                 PluginManagerChain.plugin, plugin_mgr_chain,
                                 "test_plugin_c")
Ejemplo n.º 3
0
    def setUp(self):
        self.plugin_manager = PluginManager()

        self.plugin_a = Plugin.wrap_function(
            "test_plugin_a", "testing plugin a", lambda ctx: "execute a")
        self.plugin_b = Plugin.wrap_function(
            "test_plugin_b", "testing plugin b", lambda ctx: "execute b")

        self.plugin_manager.register(self.plugin_a)
        self.plugin_manager.register(self.plugin_b)
Ejemplo n.º 4
0
class PluginManagerTestCase(GirlFriendTestCase):
    """PluginManager类的测试用例
    """
    def setUp(self):
        self.plugin_manager = PluginManager()

        self.plugin_a = Plugin.wrap_function("test_plugin_a",
                                             "testing plugin a",
                                             lambda ctx: "execute a")
        self.plugin_b = Plugin.wrap_function("test_plugin_b",
                                             "testing plugin b",
                                             lambda ctx: "execute b")

        self.plugin_manager.register(self.plugin_a)
        self.plugin_manager.register(self.plugin_b)

    def test_register(self):
        """测试注册类对象
        """
        self.plugin_manager.sys_prepare(None, "test_plugin_a")

        plugin_a = self.plugin_manager["test_plugin_a"]
        self.assertEquals(plugin_a.execute(None), "execute a")

        # 未进行初始化插件
        plugin_b = self.plugin_manager["test_plugin_b"]
        self.failUnlessException(PluginUnPreparedException, Plugin.execute,
                                 plugin_b, None)

        # 测试重复注册
        self.failUnlessException(PluginAlreadyRegisteredException,
                                 PluginManager.register, self.plugin_manager,
                                 self.plugin_a)

    def test_remove(self):
        """测试将插件移除
        """
        del self.plugin_manager["test_plugin_a"]
        self.failUnlessException(PluginNotFoundException, PluginManager.remove,
                                 self.plugin_manager, "test_plugin_a")
        self.failUnlessException(PluginNotFoundException, PluginManager.plugin,
                                 self.plugin_manager, "test_plugin_a")

    def test_replace(self):
        """测试插件替换
        """
        plugin_a = Plugin.wrap_function("test_plugin_a", "testing plugin a",
                                        lambda ctx: "execute new a")
        self.plugin_manager.replace(plugin_a)
        self.plugin_manager.sys_prepare(None)
        self.assertEquals(self.plugin_manager["test_plugin_a"].execute(None),
                          "execute new a")
Ejemplo n.º 5
0
class PluginMgrFixture(fixtures.Fixture):

    """创建一个新的PluginManager并添加几个简单插件
    """

    def __init__(self, config, *plugins):
        if plugins:
            self._plugins = plugins
        else:
            self._plugins = [
                Plugin.wrap_function(
                    "plugin_a", "plugin a", lambda ctx: "plugin a"),
                Plugin.wrap_function(
                    "plugin_b", "plugin b", lambda ctx: "plugin b")
            ]
        self._config = config

    @property
    def plugin_manager(self):
        return self._plugin_manager

    def setUp(self):
        self._plugin_manager = PluginManager()
        for plugin in self._plugins:
            self._plugin_manager.register(plugin)
        self._plugin_manager.sys_prepare(self._config)

    def cleanUp(self):
        self._plugin_manager.sys_cleanup(self._config)
Ejemplo n.º 6
0
class PluginMgrFixture(fixtures.Fixture):
    """创建一个新的PluginManager并添加几个简单插件
    """
    def __init__(self, config, *plugins):
        if plugins:
            self._plugins = plugins
        else:
            self._plugins = [
                Plugin.wrap_function("plugin_a", "plugin a",
                                     lambda ctx: "plugin a"),
                Plugin.wrap_function("plugin_b", "plugin b",
                                     lambda ctx: "plugin b")
            ]
        self._config = config

    @property
    def plugin_manager(self):
        return self._plugin_manager

    def setUp(self):
        self._plugin_manager = PluginManager()
        for plugin in self._plugins:
            self._plugin_manager.register(plugin)
        self._plugin_manager.sys_prepare(self._config)

    def cleanUp(self):
        self._plugin_manager.sys_cleanup(self._config)
Ejemplo n.º 7
0
    def test_getitem(self):
        plugin_a = Plugin.wrap_function(
            "test_plugin_a",
            "testing plugin a",
            lambda ctx: "execute a"
        )

        plugin_b = Plugin.wrap_function(
            "test_plugin_b",
            "testing plugin b",
            lambda ctx: "execute b"
        )

        plugin_manager_a = PluginManager()
        plugin_manager_a.register(plugin_a)
        plugin_manager_a.register(plugin_b)

        plugin_a1 = Plugin.wrap_function(
            "test_plugin_a",
            "testing plugin a 1",
            lambda ctx: "execute a 1"
        )

        plugin_manager_b = PluginManager()
        plugin_manager_b.register(plugin_a1)

        plugin_mgr_chain = PluginManagerChain(
            plugin_manager_b, plugin_manager_a)
        plugin_mgr_chain.sys_prepare(None)

        # 优先获取最优先的插件
        self.assertEquals(
            plugin_mgr_chain["test_plugin_a"].execute(None), "execute a 1")
        self.assertEquals(
            plugin_mgr_chain["test_plugin_b"].execute(None), "execute b")

        self.failUnlessException(
            PluginNotFoundException,
            PluginManagerChain.plugin,
            plugin_mgr_chain, "test_plugin_c")
Ejemplo n.º 8
0
 def setUp(self):
     self._plugin_manager = PluginManager()
     for plugin in self._plugins:
         self._plugin_manager.register(plugin)
     self._plugin_manager.sys_prepare(self._config)
Ejemplo n.º 9
0
class PluginManagerTestCase(GirlFriendTestCase):

    """PluginManager类的测试用例
    """

    def setUp(self):
        self.plugin_manager = PluginManager()

        self.plugin_a = Plugin.wrap_function(
            "test_plugin_a", "testing plugin a", lambda ctx: "execute a")
        self.plugin_b = Plugin.wrap_function(
            "test_plugin_b", "testing plugin b", lambda ctx: "execute b")

        self.plugin_manager.register(self.plugin_a)
        self.plugin_manager.register(self.plugin_b)

    def test_register(self):
        """测试注册类对象
        """
        self.plugin_manager.sys_prepare(None, "test_plugin_a")

        plugin_a = self.plugin_manager["test_plugin_a"]
        self.assertEquals(plugin_a.execute(None), "execute a")

        # 未进行初始化插件
        plugin_b = self.plugin_manager["test_plugin_b"]
        self.failUnlessException(
            PluginUnPreparedException,
            Plugin.execute,
            plugin_b, None)

        # 测试重复注册
        self.failUnlessException(
            PluginAlreadyRegisteredException,
            PluginManager.register,
            self.plugin_manager, self.plugin_a
        )

    def test_remove(self):
        """测试将插件移除
        """
        del self.plugin_manager["test_plugin_a"]
        self.failUnlessException(
            PluginNotFoundException,
            PluginManager.remove,
            self.plugin_manager, "test_plugin_a")
        self.failUnlessException(
            PluginNotFoundException,
            PluginManager.plugin,
            self.plugin_manager, "test_plugin_a")

    def test_replace(self):
        """测试插件替换
        """
        plugin_a = Plugin.wrap_function(
            "test_plugin_a",
            "testing plugin a",
            lambda ctx: "execute new a")
        self.plugin_manager.replace(plugin_a)
        self.plugin_manager.sys_prepare(None)
        self.assertEquals(
            self.plugin_manager["test_plugin_a"].execute(None),
            "execute new a")
Ejemplo n.º 10
0
import argparse
import os.path
from girlfriend.plugin import Plugin, PluginManager
from girlfriend.workflow.gfworkflow import Job
from girlfriend.workflow.protocol import Env

# cmd args parser
cmd_parser = argparse.ArgumentParser(u"加1减2")
cmd_parser.add_argument("--start-num", dest="start_num",
                        help=u"起始值", default="0")

# 插件
plugins = [
    "multiply",
]
plugin_manager = PluginManager()


def multiply(ctx, a, b):
    return a * b
plugin_manager.register(
    Plugin.wrap_function("multiply", "multiply number", multiply))


def add(ctx, a, b):
    return a + b
plugin_manager.register(Plugin.wrap_function("add", "add number", add))


def minus(ctx, a, b):
    result = a - b
Ejemplo n.º 11
0
    def setUp(self):
        # 构建测试所需要的插件
        plugin_mgr = PluginManager()

        def add_one(ctx, num):
            return num + 1
        add_one_plugin = Plugin.wrap_function("add_one", "add one", add_one)
        plugin_mgr.register(add_one_plugin)

        def add_two(ctx, num):
            return num + 2
        add_two_plugin = Plugin.wrap_function("add_two", "add two", add_two)
        plugin_mgr.register(add_two_plugin)

        def add_three(ctx, num):
            value = num + 3
            ctx["add_three"] = value
        add_three_plugin = Plugin.wrap_function(
            "add_three", "add three", add_three)
        plugin_mgr.register(add_three_plugin)

        def division(ctx, a, b):
            return a / b
        division_plugin = Plugin.wrap_function(
            "division", "division", division)
        plugin_mgr.register(division_plugin)

        def add_four(ctx, num):
            if num < 0:
                raise InvalidArgumentException("num must more than zero!")
            return num + 4
        add_four_plugin = Plugin.wrap_function(
            "add_four", "add four", add_four)
        plugin_mgr.register(add_four_plugin)

        plugin_mgr.sys_prepare(None)
        self.plugin_mgr = plugin_mgr
Ejemplo n.º 12
0
    def setUp(self):
        # 构建测试所需要的插件
        plugin_mgr = PluginManager()

        def add_one(ctx, num):
            return num + 1

        add_one_plugin = Plugin.wrap_function("add_one", "add one", add_one)
        plugin_mgr.register(add_one_plugin)

        def add_two(ctx, num):
            return num + 2

        add_two_plugin = Plugin.wrap_function("add_two", "add two", add_two)
        plugin_mgr.register(add_two_plugin)

        def add_three(ctx, num):
            value = num + 3
            ctx["add_three"] = value

        add_three_plugin = Plugin.wrap_function("add_three", "add three",
                                                add_three)
        plugin_mgr.register(add_three_plugin)

        def division(ctx, a, b):
            return a / b

        division_plugin = Plugin.wrap_function("division", "division",
                                               division)
        plugin_mgr.register(division_plugin)

        def add_four(ctx, num):
            if num < 0:
                raise InvalidArgumentException("num must more than zero!")
            return num + 4

        add_four_plugin = Plugin.wrap_function("add_four", "add four",
                                               add_four)
        plugin_mgr.register(add_four_plugin)

        plugin_mgr.sys_prepare(None)
        self.plugin_mgr = plugin_mgr
Ejemplo n.º 13
0
 def setUp(self):
     self._plugin_manager = PluginManager()
     for plugin in self._plugins:
         self._plugin_manager.register(plugin)
     self._plugin_manager.sys_prepare(self._config)