Exemplo n.º 1
0
 def test_add(self):
     functions = {}
     fn = '<function>'
     interval = '<interval>'
     with patch('gofer.agent.decorator.Actions.functions', functions):
         Actions.add(fn, interval)
         self.assertEqual(functions, {fn: interval})
Exemplo n.º 2
0
    def _load(plugin):
        """
        Import a module by file name.
        :param plugin: A plugin to load.
        :type plugin: Plugin
        :return: The loaded plugin.
        :rtype: Plugin
        """
        Remote.clear()
        Actions.clear()
        Plugin.add(plugin)
        try:
            path = plugin.descriptor.main.plugin
            if path:
                Plugin.add(plugin, path)
                plugin.impl = __import__(path, {}, {}, [path.split('.')[-1]])
            else:
                path = PluginLoader._find(plugin.name)
                plugin.impl = imp.load_source(plugin.name, path)

            log.info('plugin:%s loaded using: %s', plugin.name, path)

            for fn in Remote.find(plugin.impl.__name__):
                fn.gofer.plugin = plugin

            plugin.dispatcher += Remote.collated()
            plugin.actions = Actions.collated()
            plugin.delegate = Delegate()
            plugin.load()
            return plugin
        except Exception:
            log.exception('plugin:%s, import failed', plugin.name)
            Plugin.delete(plugin)
Exemplo n.º 3
0
    def _load(plugin):
        """
        Import a module by file name.
        :param plugin: A plugin to load.
        :type plugin: Plugin
        :return: The loaded plugin.
        :rtype: Plugin
        """
        Remote.clear()
        Actions.clear()
        Plugin.add(plugin)
        try:
            path = plugin.descriptor.main.plugin
            if path:
                Plugin.add(plugin, path)
                plugin.impl = __import__(path, {}, {}, [path.split('.')[-1]])
            else:
                path = PluginLoader._find(plugin.name)
                plugin.impl = imp.load_source(plugin.name, path)

            log.info('plugin:%s loaded using: %s', plugin.name, path)

            for fn in Remote.find(plugin.impl.__name__):
                fn.gofer.plugin = plugin

            plugin.dispatcher += Remote.collated()
            plugin.actions = Actions.collated()
            plugin.delegate = Delegate()
            plugin.load()
            return plugin
        except Exception:
            log.exception('plugin:%s, import failed', plugin.name)
            Plugin.delete(plugin)
Exemplo n.º 4
0
 def test_add(self):
     functions = {}
     fn = "<function>"
     interval = "<interval>"
     with patch("gofer.agent.decorator.Actions.functions", functions):
         Actions.add(fn, interval)
         self.assertEqual(functions, {fn: interval})
Exemplo n.º 5
0
 def test_add(self):
     functions = {}
     fn = '<function>'
     interval = '<interval>'
     with patch('gofer.agent.decorator.Actions.functions', functions):
         Actions.add(fn, interval)
         self.assertEqual(functions, {fn: interval})
Exemplo n.º 6
0
 def help(self):
     """
     Show information about loaded plugins.
     :return: Information
     :rtype: str
     """
     return loaded(self.container, Actions())
Exemplo n.º 7
0
    def test_collated(self, collate, action):
        functions = (
            (Mock(__name__='m1'), dict(days=30)),
            (Mock(__name__='m2'), dict(minutes=40)),
            (Mock(__name__='fn1'), dict(hours=10)),
            (Mock(__name__='fn2'), dict(seconds=20)),
        )
        class_1 = Mock(__name__='class_1')
        mod_1 = Mock(__name__='mod_1')
        collate.return_value = ({
            class_1: functions[0:2]
        }, {
            mod_1: functions[2:4]
        })
        actions = [
            Mock(),
            Mock(),
            Mock(),
            Mock(),
        ]
        action.side_effect = actions

        with patch('gofer.agent.decorator.Actions.functions', dict(functions)):
            collated = Actions.collated()
            self.assertEqual(action.call_args_list, [
                ((class_1().m1, ), functions[0][1]),
                ((class_1().m2, ), functions[1][1]),
                ((functions[2][0], ), functions[2][1]),
                ((functions[3][0], ), functions[3][1]),
            ])
            self.assertEqual(collated, actions)
Exemplo n.º 8
0
    def test_collated(self, collate, action):
        functions = (
            (Mock(__name__="m1"), dict(days=30)),
            (Mock(__name__="m2"), dict(minutes=40)),
            (Mock(__name__="fn1"), dict(hours=10)),
            (Mock(__name__="fn2"), dict(seconds=20)),
        )
        class_1 = Mock(__name__="class_1")
        mod_1 = Mock(__name__="mod_1")
        collate.return_value = ({class_1: functions[0:2]}, {mod_1: functions[2:4]})
        actions = [Mock(), Mock(), Mock(), Mock()]
        action.side_effect = actions

        with patch("gofer.agent.decorator.Actions.functions", dict(functions)):
            collated = Actions.collated()
            self.assertEqual(
                action.call_args_list,
                [
                    ((class_1().m1,), functions[0][1]),
                    ((class_1().m2,), functions[1][1]),
                    ((functions[2][0],), functions[2][1]),
                    ((functions[3][0],), functions[3][1]),
                ],
            )
            self.assertEqual(collated, actions)
Exemplo n.º 9
0
    def test_collated(self, collate):
        def fn1():
            pass

        def fn2():
            pass

        def fn3():
            pass

        def fn4():
            pass

        class T(object):
            def m1(self):
                pass

            def m2(self):
                pass

            def m3(self):
                pass

            def m4(self):
                pass

        methods = [
            Method(T.m1, dict(days=30)),
            Method(T.m2, dict(minutes=40)),
            Method(T.m3, dict(hours=10)),
            Method(T.m4, dict(seconds=20)),
        ]
        functions = [
            Function(fn1, dict(days=30)),
            Function(fn2, dict(minutes=40)),
            Function(fn3, dict(hours=10)),
            Function(fn4, dict(seconds=20)),
        ]

        functions = [
            Module(Mock(__name__='M1'),
                   functions={f.name: f
                              for f in functions})
        ]
        classes = [
            Class(Mock(__name__='C1'), methods={m.name: m
                                                for m in methods})
        ]

        collate.return_value = (classes, functions)

        actual = sorted(Actions.collated(), key=lambda a: a.name)

        expected = []
        for ns in classes + functions:
            for target in ns:
                expected.append(Action(str(target), target, **target.options))

        expected = sorted(expected, key=lambda a: a.name)
        self.assertEqual(expected, actual)
Exemplo n.º 10
0
    def test_collated(self, collate):
        def fn1(): pass

        def fn2(): pass

        def fn3(): pass

        def fn4(): pass

        class T(object):
            def m1(self): pass

            def m2(self): pass

            def m3(self): pass

            def m4(self): pass

        methods = [
            Method(T.m1, dict(days=30)),
            Method(T.m2, dict(minutes=40)),
            Method(T.m3, dict(hours=10)),
            Method(T.m4, dict(seconds=20)),
        ]
        functions = [
            Function(fn1, dict(days=30)),
            Function(fn2, dict(minutes=40)),
            Function(fn3, dict(hours=10)),
            Function(fn4, dict(seconds=20)),
        ]

        functions = [
            Module(Mock(__name__='M1'), functions={f.name: f for f in functions})
        ]
        classes = [
            Class(Mock(__name__='C1'), methods={m.name: m for m in methods})
        ]

        collate.return_value = (classes, functions)

        actual = sorted(Actions.collated(), key=lambda a: a.name)

        expected = []
        for ns in classes + functions:
            for target in ns:
                expected.append(Action(str(target), target, **target.options))

        expected = sorted(expected, key=lambda a: a.name)
        self.assertEqual(expected, actual)
Exemplo n.º 11
0
 def help(self):
     """
     Show information about loaded plugins.
     :return: Information
     :rtype: str
     """
     s = list()
     s.append('Plugins:')
     container = Container()
     for p in container.all():
         if not p.enabled:
             continue
         # plugin
         s.append('')
         s.append(indent('<plugin> %s', 2, p.name))
         # classes
         s.append(indent('Classes:', 4))
         for n, v in p.dispatcher.catalog.items():
             if inspect.ismodule(v):
                 continue
             s.append(indent('<class> %s', 6, n))
             s.append(indent('methods:', 8))
             for n, v in inspect.getmembers(v, inspect.ismethod):
                 fn = v.im_func
                 if not hasattr(fn, 'gofer'):
                     continue
                 s.append(indent(signature(n, fn), 10))
         # functions
         s.append(indent('Functions:', 4))
         for n, v in p.dispatcher.catalog.items():
             if not inspect.ismodule(v):
                 continue
             for n, v in inspect.getmembers(v, inspect.isfunction):
                 fn = v
                 if not hasattr(fn, 'gofer'):
                     continue
                 s.append(indent(signature(n, fn), 6))
     s.append('')
     s.append('Actions:')
     for a in [(a.name(), a.interval) for a in Actions().collated()]:
         s.append('  %s %s' % a)
     return '\n'.join(s)
Exemplo n.º 12
0
 def inner(fn):
     Actions.add(fn, interval or dict(days=0x8E94))
     return fn
Exemplo n.º 13
0
 def inner(fn):
     Actions.add(fn, interval or dict(days=0x8E94))
     return fn