Exemplo n.º 1
0
    def test_auto_importer(self):
        module = mock.MagicMock(
            spec=object(),
            __all__=('A', 'B', 'C'),
            __name__ = 'napixd.auto.module',
            A=object(),
            B=type('Manager', (Manager, ), {
                '__module__': 'napixd.auto.module',
                'resource_fields': {
                    's': {'example': 1}
                },
                'name': 'my_manager',
                'configure': mock.MagicMock(__doc__='{ "a" : 1 }')
            }),
        )
        with mock.patch('os.listdir', return_value=['b.pyc', 'b.py', '.b.swp']):
            with mock.patch.object(self.ai, 'import_module', return_value=module) as meth_import:
                modules, errors = self.ai.load()

        meth_import.assert_called_once_with('b.py')
        self.assertEqual(
            modules, [ManagerImport(module.B, 'my_manager', {'a': 1})])
        self.assertEqual(len(errors), 1)
        error = errors[0]
        self.assertEqual(error.module, 'napixd.auto.module')
Exemplo n.º 2
0
 def test_load(self):
     m = ManagerImport(self.manager, 'alias', {})
     self.importer.load.return_value = ([m], [])
     load = self.loader.load()
     self.assertEqual(load.managers, set([m]))
     self.assertEqual(load.old_managers, set())
     self.assertEqual(load.new_managers, set([m]))
     self.assertEqual(load.error_managers, set())
Exemplo n.º 3
0
    def test_load_identical(self):
        manager = mock.MagicMock(
            __module__='a.b',
            __name__='Manager',
            spec=Manager,
            get_managed_classes=mock.Mock(return_value=[]),
            _resource_fields=mock.MagicMock(spec=ResourceFields)
        )

        m1 = ManagerImport(self.manager, 'alias', {})
        m2 = ManagerImport(manager, 'alias', {})

        self.importer.load.return_value = ([m1, m2], [])
        load = self.loader.load()
        self.assertEqual(load.managers, set([m1]))
        self.assertEqual(load.old_managers, set())
        self.assertEqual(load.new_managers, set([m1]))
        self.assertEqual(load.error_managers, set())
Exemplo n.º 4
0
    def load_module(self, module):
        """
        Explore a module and search for
        :class:`napixd.managers.base.Manager` subclasses.
        The method :meth:`~napixd.managers.base.Manager.detect` is called and
        if it returns False, the manager is ignored.

        The configuration is loaded from the docstring
        of the :meth:`~napixd.managers.base.Manager.configure` method.
        """

        managers, errors = [], []
        content = getattr(module, '__all__', False) or dir(module)
        for manager_name in content:
            try:
                obj = getattr(module, manager_name)
            except AttributeError as e:
                errors.append(ManagerImportError(
                    module.__name__, manager_name, e))
                continue

            if not isinstance(obj, type) or not issubclass(obj, Manager):
                continue

            try:
                detect = obj.detect()
            except Exception as e:
                logger.error('Error while running detect of manager %s.%s',
                             module.__name__, manager_name)
                errors.append(ManagerImportError(
                    module.__name__, manager_name, e))
                continue

            if not detect:
                logger.info('Manager %s.%s not detected',
                            module.__name__, manager_name)
                continue

            try:
                manager = self.setup(obj)
            except NapixImportError as e:
                errors.append(e)
            except Exception as e:
                errors.append(ManagerImportError(
                    module.__name__, manager_name, e))
            else:
                logger.info('Found Manager %s.%s', module.__name__, manager_name)
                managers.append(ManagerImport(
                    manager, manager.get_name(), self.get_config_from(manager)))

        return managers, errors
Exemplo n.º 5
0
    def test_load_module_error(self):
        m = ManagerImport(self.manager, 'alias', {})
        self.importer.load.return_value = ([m], [])
        self.loader.load()

        error = ImportError()
        self.importer.load.return_value = (
            [], [ModuleImportError('a.b', error)])
        load = self.loader.load()

        me = ManagerError(m.manager, 'alias', error)

        self.assertEqual(load.managers, set())
        self.assertEqual(load.old_managers, set([m]))
        self.assertEqual(load.new_managers, set())
        self.assertEqual(load.error_managers, set([me]))
Exemplo n.º 6
0
 def load(self):
     """
     Load the managers with the conf
     return a list of Manager subclasses
     """
     managers, errors = [], []
     for alias, manager_path in self.managers.items():
         try:
             manager = self.import_manager(manager_path)
             logger.info('load %s from conf', manager_path)
             config = self.conf.get('Manager ' + alias)
             manager = self.setup(manager)
         except NapixImportError as e:
             if self.should_raise:
                 raise
             errors.append(e)
         else:
             import_ = ManagerImport(manager, alias, config)
             managers.append(import_)
     return managers, errors
Exemplo n.º 7
0
    def load(self):
        managers, errors = [], []
        for alias, spec in self.managers.items():
            manager, conf = self._parse(spec)

            if conf is None:
                conf = EmptyConf()
            elif not isinstance(conf, BaseConf):
                conf = Conf(conf)

            logger.info('Import fixed %s', manager)
            try:
                manager = self.import_manager(manager)
                manager = self.setup(manager)
            except NapixImportError as e:
                if self.should_raise:
                    raise
                errors.append(e)
            else:
                managers.append(ManagerImport(manager, alias, conf))
        return managers, errors