Пример #1
0
    def __init__(self, app=None, **configuration):
        r"""
        :param Flask app:
            The Flask app (or Blueprint) to wrap.
        :param \**configuration:
            Configuration options that define in what way a :any:`MicronMethod`
            that is created using this Micron instance must behave. These
            configuration options can be overridden by method-specific
            configuration options, defined in the ``@micron.method()``
            decorator.

        Example::

            from flask import Flask
            from flask_micron import Micron

            app = Flask(__name__)
            micron = Micron(app)

            @micron.method()
            def hello():
                return "Hello, world!"
        """
        self.config = MicronMethodConfig(**configuration)

        self.plugins = plugin.Container(json_input.Plugin(),
                                        normalize_input.Plugin(),
                                        call_function.Plugin(),
                                        json_output.Plugin())

        self.app = None
        if app is not None:
            self.init_app(app)
Пример #2
0
 def test_CallOneFollowsChainOfCommandPattern(self):
     container = plugin.Container()
     container.add(CallOneTestPlugin('A', True),
                   CallOneTestPlugin('B', True),
                   CallOneTestPlugin('C', False))
     ctx = plugin.Context()
     container.call_one(ctx, 'read_input', 'input')
     self.assertEqual('B', ctx.input)
Пример #3
0
 def test_CallAllVisitsAllPlugins(self):
     container = plugin.Container()
     container.add(CallAllTestPlugin('A'), CallAllTestPlugin('B'),
                   CallAllTestPlugin('C'))
     ctx = plugin.Context()
     ctx.input = 'START'
     container.call_all(ctx, 'normalize_input')
     self.assertEqual('START|A|B|C', ctx.input)
Пример #4
0
 def test_HookMethodsInPluginsCanBeRun(self):
     container = plugin.Container()
     container.add(Dummy())
     config = MicronMethodConfig(dommy='yo')
     ctx = plugin.Context()
     ctx.config = config
     container.call_all(ctx, 'process_output')
     self.assertEqual('I made it, yo', ctx.output)
Пример #5
0
 def test_PluginsCanBeDuckTyped(self):
     container = plugin.Container()
     container.add(Ducky())
     config = MicronMethodConfig()
     ctx = plugin.Context()
     ctx.config = config
     container.call_all(ctx, 'normalize_input')
     container.call_all(ctx, 'process_output')
     self.assertEqual('Quack!', ctx.output)
Пример #6
0
 def test_callOneAcceptsSettingMonitorFieldToNoneValue(self):
     container = plugin.Container()
     container.add(CallOneTestPlugin('A', True),
                   CallOneTestPlugin('B', True),
                   CallOneTestPlugin(None, True))
     ctx = plugin.Context()
     self.assertFalse(ctx.is_assigned('input'))
     container.call_one(ctx, 'read_input', 'input')
     self.assertTrue(ctx.is_assigned('input'))
     self.assertEqual(None, ctx.input)
Пример #7
0
    def test_CanCheckForContainmentBasedOnType(self):
        container = plugin.Container()
        dummy = Dummy()
        dommy = Dummy()
        ducky = Ducky()

        self.assertFalse(Ducky in container)
        self.assertFalse(ducky in container)
        self.assertFalse(dommy in container)
        self.assertFalse("Thing" in container)
        self.assertFalse(Dummy in container)
        self.assertFalse(dummy in container)
        self.assertFalse(object in container)

        container._plugins = [dummy]
        self.assertFalse(Ducky in container)
        self.assertFalse(ducky in container)
        self.assertFalse(dommy in container)
        self.assertFalse("Thing" in container)
        self.assertTrue(Dummy in container)
        self.assertTrue(dummy in container)
        self.assertTrue(object in container)
Пример #8
0
 def test_CanRegisterPluginWithContainer(self):
     container = plugin.Container()
     container.add(Dummy())
     self.assertTrue(Dummy in container)