Example #1
0
 def test_get_module_name(self):
     ends_with_init = 'my/dirname/__init__.py'
     python_file = 'my/path/test.py'
     ends_with_init = util.get_module_name(ends_with_init)
     python_file = util.get_module_name(python_file)
     self.assertEqual(ends_with_init, 'dirname')
     self.assertEqual(python_file, 'test')
Example #2
0
 def test_get_module_name(self):
     ends_with_init = 'my/dirname/__init__.py'
     python_file = 'my/path/test.py'
     ends_with_init = util.get_module_name(ends_with_init)
     python_file = util.get_module_name(python_file)
     self.assertEqual(ends_with_init, 'dirname')
     self.assertEqual(python_file, 'test')
Example #3
0
    def load_modules(self, filepaths):
        """
        Loads the modules from their `filepaths`. A filepath may be
        a directory filepath if there is an `__init__.py` file in the
        directory.

        If a filepath errors, the exception will be caught and logged
        in the logger.

        Returns a list of modules.
        """
        # removes filepaths from processed if they are not in sys.modules
        self._update_loaded_modules()
        filepaths = util.return_set(filepaths)

        modules = []
        for filepath in filepaths:
            filepath = self._clean_filepath(filepath)
            # check to see if already processed and move onto next if so
            if self._processed_filepath(filepath):
                continue

            module_name = util.get_module_name(filepath)
            plugin_module_name = util.create_unique_module_name(module_name)

            try:
                module = load_source(plugin_module_name, filepath)
            # Catch all exceptions b/c loader will return errors
            # within the code itself, such as Syntax, NameErrors, etc.
            except Exception:
                exc_info = sys.exc_info()
                self._log.error(msg=self._error_string.format(filepath),
                                exc_info=exc_info)
                continue

            self.loaded_modules.add(module.__name__)
            modules.append(module)
            self.processed_filepaths[module.__name__] = filepath

        return modules
    def load_modules(self, filepaths):
        """
        Loads the modules from their `filepaths`. A filepath may be
        a directory filepath if there is an `__init__.py` file in the
        directory.

        If a filepath errors, the exception will be caught and logged
        in the logger.

        Returns a list of modules.
        """
        # removes filepaths from processed if they are not in sys.modules
        self._update_loaded_modules()
        filepaths = util.return_set(filepaths)

        modules = []
        for filepath in filepaths:
            filepath = self._clean_filepath(filepath)
            # check to see if already processed and move onto next if so
            if self._processed_filepath(filepath):
                continue

            module_name = util.get_module_name(filepath)
            plugin_module_name = util.create_unique_module_name(module_name)

            try:
                module = load_source(plugin_module_name, filepath)
            # Catch all exceptions b/c loader will return errors
            # within the code itself, such as Syntax, NameErrors, etc.
            except Exception:
                exc_info = sys.exc_info()
                self._log.error(msg=self._error_string.format(filepath),
                                exc_info=exc_info)
                continue

            self.loaded_modules.add(module.__name__)
            modules.append(module)
            self.processed_filepaths[module.__name__] = filepath

        return modules