# Path: mozsvc/config.py
# def load_into_settings(filename, settings):
# def get_configurator(global_config, **settings):
# 
# Path: mozsvc/plugin.py
# def load_and_register(section_name, config, interface=None, registry_name=""):
#     """Load a plugin from the named configuration section.
# 
#     Given a Pyramid Configurator object and the name of a config section,
#     this function loads the plugin as specified in that section and then
#     registers it into the Pyramid registry as a utility for each of the
#     interfaces that it provides.
#     """
#     settings = config.registry.settings
#     # Load using the Config object if available, since it's faster.
#     # If not then fall back to loading from dotted setting names.
#     if "config" in settings:
#         plugin = load_from_config(section_name, settings["config"])
#     else:
#         plugin = load_from_settings(section_name, settings)
#     if interface is not None:
#         interfaces = [interface]
#     else:
#         interfaces = providedBy(plugin)
#     # Register the plugin for each interface that it provides.
#     # Use the Configurators delayed-registration machinery to get
#     # conflict-resolution and so-forth for free.
#     for interface in interfaces:
# 
#         def register(interface=interface):
#             config.registry.registerUtility(plugin, interface, registry_name)
# 
#         config.action((interface, registry_name), register)
#     # And return it for user convenience.
#     return plugin


import unittest
from io import StringIO
from textwrap import dedent
from zope.interface import Interface, implements
from zope.interface.verify import verifyObject
from pyramid.config import Configurator, ConfigurationConflictError
from mozsvc.config import Config
from mozsvc.plugin import load_and_register

    def test_loading_several_plugins(self):
        settings = {
            "test1.backend": "mozsvc.tests.test_plugin.Test1",
            "test1.hello": "world",
            "test2.backend": "mozsvc.tests.test_plugin.Test2",
        }
        config = Configurator(settings=settings)
        plugin1 = load_and_register("test1", config)
        plugin2 = load_and_register("test2", config)
        config.commit()

        self.assertTrue(verifyObject(ITest1, plugin1))
        self.assertTrue(isinstance(plugin1, Test1))
        self.assertEqual(plugin1.kwds["hello"], "world")
        self.assertEqual(len(plugin1.kwds), 1)
        self.assertTrue(config.registry.queryUtility(ITest1) is plugin1)

        self.assertTrue(verifyObject(ITest2, plugin2))
        self.assertTrue(isinstance(plugin2, Test2))
        self.assertEqual(len(plugin2.kwds), 0)
        self.assertTrue(config.registry.queryUtility(ITest2) is plugin2)

    def test_loading_with_conflict_detection(self):
        settings = {
            "test1.backend": "mozsvc.tests.test_plugin.Test1",
            "test_both.backend": "mozsvc.tests.test_plugin.Test1And2",
        }
        config = Configurator(settings=settings)
        load_and_register("test1", config)

# Path: mozsvc/config.py
# def load_into_settings(filename, settings):
# def get_configurator(global_config, **settings):
# 
# Path: mozsvc/plugin.py
# def load_and_register(section_name, config, interface=None, registry_name=""):
#     """Load a plugin from the named configuration section.
# 
#     Given a Pyramid Configurator object and the name of a config section,
#     this function loads the plugin as specified in that section and then
#     registers it into the Pyramid registry as a utility for each of the
#     interfaces that it provides.
#     """
#     settings = config.registry.settings
#     # Load using the Config object if available, since it's faster.
#     # If not then fall back to loading from dotted setting names.
#     if "config" in settings:
#         plugin = load_from_config(section_name, settings["config"])
#     else:
#         plugin = load_from_settings(section_name, settings)
#     if interface is not None:
#         interfaces = [interface]
#     else:
#         interfaces = providedBy(plugin)
#     # Register the plugin for each interface that it provides.
#     # Use the Configurators delayed-registration machinery to get
#     # conflict-resolution and so-forth for free.
#     for interface in interfaces:
# 
#         def register(interface=interface):
#             config.registry.registerUtility(plugin, interface, registry_name)
# 
#         config.action((interface, registry_name), register)
#     # And return it for user convenience.
#     return plugin


# Path: mozsvc/tests/test_plugin.py
import unittest
from io import StringIO
from textwrap import dedent
from zope.interface import Interface, implements
from zope.interface.verify import verifyObject
from pyramid.config import Configurator, ConfigurationConflictError
from mozsvc.config import Config
from mozsvc.plugin import load_and_register


    def test_loading_several_plugins(self):
        settings = {
            "test1.backend": "mozsvc.tests.test_plugin.Test1",
            "test1.hello": "world",
            "test2.backend": "mozsvc.tests.test_plugin.Test2",
        }
        config = Configurator(settings=settings)
        plugin1 = load_and_register("test1", config)
        plugin2 = load_and_register("test2", config)
        config.commit()

        self.assertTrue(verifyObject(ITest1, plugin1))
        self.assertTrue(isinstance(plugin1, Test1))
        self.assertEqual(plugin1.kwds["hello"], "world")
        self.assertEqual(len(plugin1.kwds), 1)
        self.assertTrue(config.registry.queryUtility(ITest1) is plugin1)

        self.assertTrue(verifyObject(ITest2, plugin2))
        self.assertTrue(isinstance(plugin2, Test2))
        self.assertEqual(len(plugin2.kwds), 0)
        self.assertTrue(config.registry.queryUtility(ITest2) is plugin2)

    def test_loading_with_conflict_detection(self):
        settings = {
            "test1.backend": "mozsvc.tests.test_plugin.Test1",
            "test_both.backend": "mozsvc.tests.test_plugin.Test1And2",
        }
        config = Configurator(settings=settings)
        load_and_register("test1", config)

        load_and_register("test_both", config)