<commit_before>#
# ModuleManager.py -- Simple class to dynamically manage module imports.
#
# This is open-source software licensed under a BSD license.
# Please see the file LICENSE.txt for details.
#
"""
Dynamically manage module imports.

The ModuleManager class
"""

from __future__ import absolute_import

import sys
import os
import importlib

__all__ = ['ModuleManager']


def my_import(name, path=None):
    """Return imported module for the given name."""

    # Documentation for importlib says this may be needed to pick up
    # modules created after the program has started
    if hasattr(importlib, 'invalidate_caches'):
        # python 3.3+
        importlib.invalidate_caches()

    if path is not None:
        directory, src_file = os.path.split(path)

        # it would be better to use the importlib.util machinery
        # but it is not supported on python 3 versions < 3.5
        sys.path.insert(0, directory)
        try:
            mod = importlib.import_module(name)
        finally:
            sys.path.pop(0)

    else:
        mod = importlib.import_module(name)

    return mod


class ModuleManagerError(Exception):
    """Exception related to module manager."""
    pass


class ModuleManager(object):
    """Simple class to dynamically manage module imports."""

    def __init__(self, logger):
        self.logger = logger

        self.module = {}

    def load_module(self, module_name, pfx=None, path=None):
        """Load/reload module from the given name."""
        try:
            if module_name in sys.modules:
                module = sys.modules[module_name]
                self.logger.info("Reloading module '%s'..." % module_name)
                if hasattr(importlib, 'reload'):
                    # python 3.4+
                    module = importlib.reload(module)

                else:
                    import imp
                    module = imp.reload(module)

            else:
                if pfx:
                    name = pfx + '.' + module_name
                else:
                    name = module_name

                self.logger.info("Loading module '%s'..." % module_name)
                module = my_import(name, path=path)

            self.module[module_name] = module
            return module

        except Exception as e:
            self.logger.error("Failed to load module '%s': %s" % (
                module_name, str(e)))
            raise ModuleManagerError(e)

    def get_module(self, module_name):
        """Return loaded module from the given name."""
        try:
            return self.module[module_name]

        except KeyError:
            return sys.modules[module_name]<commit_msg>Fix for reloading modules with a prefix<commit_after>#
# ModuleManager.py -- Simple class to dynamically manage module imports.
#
# This is open-source software licensed under a BSD license.
# Please see the file LICENSE.txt for details.
#
"""
Dynamically manage module imports.

The ModuleManager class
"""

from __future__ import absolute_import

import sys
import os
import importlib

__all__ = ['ModuleManager']


def my_import(name, path=None):
    """Return imported module for the given name."""

    # Documentation for importlib says this may be needed to pick up
    # modules created after the program has started
    if hasattr(importlib, 'invalidate_caches'):
        # python 3.3+
        importlib.invalidate_caches()

    if path is not None:
        directory, src_file = os.path.split(path)

        # it would be better to use the importlib.util machinery
        # but it is not supported on python 3 versions < 3.5
        sys.path.insert(0, directory)
        try:
            mod = importlib.import_module(name)
        finally:
            sys.path.pop(0)

    else:
        mod = importlib.import_module(name)

    return mod


class ModuleManagerError(Exception):
    """Exception related to module manager."""
    pass


class ModuleManager(object):
    """Simple class to dynamically manage module imports."""

    def __init__(self, logger):
        self.logger = logger

        self.module = {}

    def load_module(self, module_name, pfx=None, path=None):
        """Load/reload module from the given name."""
        try:
            if pfx:
                name = pfx + '.' + module_name
            else:
                name = module_name

            if name in sys.modules:
                module = sys.modules[name]
                self.logger.info("Reloading module '%s'..." % module_name)
                if hasattr(importlib, 'reload'):
                    # python 3.4+
                    module = importlib.reload(module)

                else:
                    import imp
                    module = imp.reload(module)

            else:
                self.logger.info("Loading module '%s'..." % module_name)
                module = my_import(name, path=path)

            self.module[module_name] = module
            return module

        except Exception as e:
            self.logger.error("Failed to load module '%s': %s" % (
                module_name, str(e)))
            raise ModuleManagerError(e)

    def get_module(self, module_name):
        """Return loaded module from the given name."""
        try:
            return self.module[module_name]

        except KeyError:
            return sys.modules[module_name]
{'fraction_non_alphanumeric': 0.09006326758466691, 'fraction_numerical': 0.0033494603647190174, 'mean_word_length': 1.9778393351800554, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6930515', 'n_tokens_mistral': 1453, 'n_tokens_neox': 1378, 'n_words': 455}