<commit_before>import importlib, operator, os, re, sys, traceback
from pyheufybot.logger import log
from pyheufybot.message import IRCMessage
from pyheufybot.utils.fileutils import createDirs
from enum import Enum
from glob import glob

class Module(object):
    def __init__(self, bot):
        self.bot = bot
        self.name = None
        self.trigger = ""
        self.moduleType = ModuleType.PASSIVE
        self.modulePriority = ModulePriority.NORMAL
        self.messageTypes = []
        self.helpText = "No help available for this module."

    def excecute(self, message):
        pass

    def onModuleLoaded(self):
        pass

    def onModuleUnloaded(self):
        pass

    def reloadData(self):
        pass

    def getHelp(self, command):
        return self.helpText

class ModuleInterface(object):
    def __init__(self, bot):
        self.bot = bot
        self.modules = {}
        self.server = bot.factory.config.getSettingWithDefault("server", "irc.foo.bar")
        self.commandPrefix = bot.factory.config.getSettingWithDefault("commandPrefix", "!")
        createDirs(os.path.join("data", self.server))
        self.dataPath = os.path.join("data", self.server)

    def loadModule(self, moduleName):
        moduleName = moduleName.lower()

        # Check if the module is loaded already.
        if moduleName in self.modules:
            return [False, "Module \"{}\" is already loaded.".format(self.modules[moduleName].name)]

        # Try to load the module
        try:
            src = importlib.import_module("pyheufybot.modules.{}".format(moduleName))
        except (ImportError, SyntaxError, AttributeError) as e:
            errorMsg = "Module \"{}\" could not be loaded ({}).".format(moduleName, e)
            log("[{}] ERROR: {}".format(self.server, errorMsg), None)
            return [False, errorMsg]
        
        reload(src)

        try:
            module = src.ModuleSpawner(self.bot)

            # Check if the module has all the required fields.
            errorMsg = "Module \"{}\" is missing one or more of the required fields and cannot be loaded.".format(moduleName)
            if not (hasattr(module, "name") and hasattr(module, "moduleType") and hasattr(module, "modulePriority") and hasattr(module, "messageTypes") and hasattr(module, "helpText")):
                log("[{}] ERROR: {}".format(self.server, errorMsg), None)
                return [False, errorMsg]
                if module.moduleType == ModuleType.COMMAND:
                    if not hasattr(module, "trigger"):
                        log("[{}] ERROR: {}".format(self.server, errorMsg), None)
                        return [False, errorMsg]

            # Module is valid and can be loaded.
            self.modules[moduleName] = module
            module.onModuleLoaded()
            log("[{}] ->- Loaded module \"{}\".".format(self.server, module.name), None)
        except Exception as e:
            errorMsg = "An exception occurred while loading module \"{}\" ({}).".format(moduleName, e)
            log("[{}] ERROR: {}".format(self.server, errorMsg), None)
            return [False, errorMsg]

        return [True, module.name]

    def unloadModule(self, moduleName):
        moduleName = moduleName.lower()
        
        if moduleName in self.modules:
            try:
                module = self.modules[moduleName]
                module.onModuleUnloaded()
                del self.modules[moduleName]<commit_msg>Fix the module validity check<commit_after>import importlib, operator, os, re, sys, traceback
from pyheufybot.logger import log
from pyheufybot.message import IRCMessage
from pyheufybot.utils.fileutils import createDirs
from enum import Enum
from glob import glob

class Module(object):
    def __init__(self, bot):
        self.bot = bot
        self.name = None
        self.trigger = ""
        self.moduleType = ModuleType.PASSIVE
        self.modulePriority = ModulePriority.NORMAL
        self.messageTypes = []
        self.helpText = "No help available for this module."

    def excecute(self, message):
        pass

    def onModuleLoaded(self):
        pass

    def onModuleUnloaded(self):
        pass

    def reloadData(self):
        pass

    def getHelp(self, command):
        return self.helpText

class ModuleInterface(object):
    def __init__(self, bot):
        self.bot = bot
        self.modules = {}
        self.server = bot.factory.config.getSettingWithDefault("server", "irc.foo.bar")
        self.commandPrefix = bot.factory.config.getSettingWithDefault("commandPrefix", "!")
        createDirs(os.path.join("data", self.server))
        self.dataPath = os.path.join("data", self.server)

    def loadModule(self, moduleName):
        moduleName = moduleName.lower()

        # Check if the module is loaded already.
        if moduleName in self.modules:
            return [False, "Module \"{}\" is already loaded.".format(self.modules[moduleName].name)]

        # Try to load the module
        try:
            src = importlib.import_module("pyheufybot.modules.{}".format(moduleName))
        except (ImportError, SyntaxError, AttributeError) as e:
            errorMsg = "Module \"{}\" could not be loaded ({}).".format(moduleName, e)
            log("[{}] ERROR: {}".format(self.server, errorMsg), None)
            return [False, errorMsg]
        
        reload(src)

        try:
            module = src.ModuleSpawner(self.bot)

            # Check if the module has all the required fields.
            attributes = ["command", "helpText", "messageTypes", "modulePriority", "moduleType", "name"]
            for attr in attributes:
                if not hasattr(module, attr):
                    errorMsg = "Module \"{}\" is missing the required field \"{}\" and cannot be loaded.".format(moduleName, attr)
                    log("[{}] ERROR: {}".format(self.server, errorMsg), None)
                    return [False, errorMsg]

            # Module is valid and can be loaded.
            self.modules[moduleName] = module
            module.onModuleLoaded()
            log("[{}] ->- Loaded module \"{}\".".format(self.server, module.name), None)
        except Exception as e:
            errorMsg = "An exception occurred while loading module \"{}\" ({}).".format(moduleName, e)
            log("[{}] ERROR: {}".format(self.server, errorMsg), None)
            return [False, errorMsg]

        return [True, module.name]

    def unloadModule(self, moduleName):
        moduleName = moduleName.lower()
        
        if moduleName in self.modules:
            try:
                module = self.modules[moduleName]
                module.onModuleUnloaded()
                del self.modules[moduleName]
{'fraction_non_alphanumeric': 0.1175334323922734, 'fraction_numerical': 0.0, 'mean_word_length': 2.262724188075618, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 5, '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': '5499447', 'n_tokens_mistral': 1860, 'n_tokens_neox': 1801, 'n_words': 504}