當前位置: 首頁>>代碼示例>>Python>>正文


Python PluginManager.set_plugins方法代碼示例

本文整理匯總了Python中pluginmanager.PluginManager.set_plugins方法的典型用法代碼示例。如果您正苦於以下問題:Python PluginManager.set_plugins方法的具體用法?Python PluginManager.set_plugins怎麽用?Python PluginManager.set_plugins使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在pluginmanager.PluginManager的用法示例。


在下文中一共展示了PluginManager.set_plugins方法的1個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Python代碼示例。

示例1: TestPluginManager

# 需要導入模塊: from pluginmanager import PluginManager [as 別名]
# 或者: from pluginmanager.PluginManager import set_plugins [as 別名]
class TestPluginManager(unittest.TestCase):
    def setUp(self):
        self.instance = InstanceClass()
        self.plugin_manager = PluginManager()
        self.plugin_manager.add_plugins(self.instance)

    def test_add_instances(self):
        self.plugin_manager.unique_instances = False
        instance = InstanceClass()
        self.plugin_manager.add_plugins(instance)
        instances = self.plugin_manager.get_plugins()
        self.assertIn(instance, instances)
        self.plugin_manager.add_plugins(InstanceClass())
        instances = self.plugin_manager.get_plugins()
        self.assertTrue(len(instances) > 2)
        uniq = self.plugin_manager._unique_class(InstanceClass)
        self.assertFalse(uniq)

    def test_register_class(self):
        class TestClass:
            pass

        self.assertFalse(issubclass(TestClass, IPlugin))
        self.plugin_manager.register_classes(TestClass)
        self.assertTrue(issubclass(TestClass, IPlugin))

    def test_class_in_blacklist(self):
        self.plugin_manager.set_plugins([])
        self.plugin_manager.add_blacklisted_plugins(InstanceClass)
        self.plugin_manager._handle_object_instance(self.instance)
        plugins = self.plugin_manager.get_plugins()
        self.assertEqual(plugins, [])

    def test_blacklist_plugins(self):
        self.plugin_manager.add_blacklisted_plugins(InstanceClass)
        blacklisted = self.plugin_manager.get_blacklisted_plugins()
        self.assertIn(InstanceClass, blacklisted)

    def test_handle_classs_instance(self):
        self.plugin_manager.instantiate_classes = False
        is_none = self.plugin_manager._handle_class_instance(5)
        self.assertIsNone(is_none)

    def test_class_instance_not_unique(self):
        self.plugin_manager.unique_instances = False
        num_plugins = len(self.plugin_manager.plugins)
        self.plugin_manager._handle_class_instance(InstanceClass)
        self.assertTrue(len(self.plugin_manager.plugins) > num_plugins)

    def test_class_instance_unique(self):
        num_plugins = len(self.plugin_manager.plugins)
        self.plugin_manager.unique_instances = True
        self.plugin_manager._handle_class_instance(InstanceClass)
        self.assertTrue(len(self.plugin_manager.plugins) == num_plugins)

    def test_get_plugins(self):
        self.plugin_manager.unique_instances = False
        instance_2 = InstanceClass()
        instance_2.name = 'red'
        self.plugin_manager.add_plugins(instance_2)

        filtered_plugins = self.plugin_manager.get_plugins(_test_filter)
        self.assertNotIn(self.instance, filtered_plugins)
        self.assertIn(instance_2, filtered_plugins)

    def test_set_plugins(self):
        instance_2 = InstanceClass()
        self.plugin_manager.set_plugins(instance_2)
        plugins = self.plugin_manager.get_plugins()
        self.assertIn(instance_2, plugins)
        self.assertNotIn(self.instance, plugins)

    def test_remove_plugin(self):
        self.plugin_manager.remove_plugins(self.instance)
        plugins = self.plugin_manager.get_plugins()
        self.assertNotIn(self.instance, plugins)

    def test_remove_instance(self):
        self.plugin_manager.remove_instance(self.instance)
        plugins = self.plugin_manager.get_plugins()
        self.assertNotIn(self.instance, plugins)

    def test_get_instances(self):
        self.plugin_manager.unique_instances = False
        instance_2 = InstanceClass()
        instance_2.name = 'red'
        self.plugin_manager.add_plugins((instance_2, 5.0))
        instances = self.plugin_manager.get_instances((IPlugin, InstanceClass))
        self.assertIn(instance_2, instances)
        self.assertIn(self.instance, instances)
        self.assertNotIn(5.0, instances)
        filtered_instances = self.plugin_manager.get_instances(_test_filter)
        self.assertIn(instance_2, filtered_instances)
        self.assertNotIn(self.instance, filtered_instances)
        self.assertNotIn(5.0, filtered_instances)
        all_instances = self.plugin_manager.get_instances(None)
        self.assertIn(self.instance, all_instances)
        self.assertIn(instance_2, all_instances)
        self.assertIn(5.0, all_instances)

#.........這裏部分代碼省略.........
開發者ID:benhoff,項目名稱:pluginmanager,代碼行數:103,代碼來源:test_plugin_manager.py


注:本文中的pluginmanager.PluginManager.set_plugins方法示例由純淨天空整理自Github/MSDocs等開源代碼及文檔管理平台,相關代碼片段篩選自各路編程大神貢獻的開源項目,源碼版權歸原作者所有,傳播和使用請參考對應項目的License;未經允許,請勿轉載。