<commit_before>import importlib
import json
import irc.bot
import irc.strings


def to_camel_case(s):
    if s.__contains__("_"):
        out = ""
        for part in s.split("_"):
            out += part.title()
        return out
    else:
        return s.title()

class Config:

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

        print("Reading configuration from %s" % path)
        fh = open(path, 'r')
        self.settings = json.load(fh)
        fh.close()

class PyBot(irc.bot.SingleServerIRCBot):

    def __init__(self):
        self.config = Config("config.json")
        self.plugins = {}
        self.prefix = self.config.settings['prefix']
        self.channel = self.config.settings['channel']

        server = self.config.settings['server']
        port = self.config.settings['port']
        nickname = self.config.settings['nickname']

        irc.bot.SingleServerIRCBot.__init__(self, [(server, port)], nickname, nickname)
        self.connection.buffer_class.errors = 'replace' # Should prevent clients sending messages in latin-1 from craching the client

        for name in self.config.settings['plugins']:
            module = __import__("plugins."+name, fromlist=(name))
            print("Loading %sPlugin" % to_camel_case(name))
            self.plugins[name] = getattr(module, to_camel_case(name)+"Plugin")()

    def on_nicknameinuse(self, c, e):
        print("Nick in use, appending _")
        c.nick(c.get_nickname() + "_")

    def on_welcome(self, c, e):
        c.join(self.channel)

    def on_privmsg(self, c, e):
        response = self.parse_user_command(e.arguments[0])
        # send response to user

    def on_pubmsg(self, c, e):
        response = self.parse_user_command(e.arguments[0])
        if (response):
            c.privmsg(self.channel, response)

    def parse_user_command(self, msg):
        if msg[0] == self.prefix:
            command = msg[1:].split(" ", 1)
            command[0] = command[0].lower()
            if command[0] == "die":
                self.connection.quit("arggh!")
                exit()
            else:
                for plugin in self.plugins:
                    if len(command) == 1:
                        arguments = None
                    else:
                        arguments = command[1]
                    response = self.plugins[plugin].parse_user_command(self, command[0], arguments)
                    if response:
                        print(response)
                        return response
            return "Invalid command"
        return None



def main():
    # TODO allow passing config path as argument
    bot = PyBot()
    bot.start()

if __name__ == "__main__":
    main()

<commit_msg>Added privmsg support<commit_after>import importlib
import json
import irc.bot
import irc.strings


def to_camel_case(s):
    if s.__contains__("_"):
        out = ""
        for part in s.split("_"):
            out += part.title()
        return out
    else:
        return s.title()

class Config:

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

        print("Reading configuration from %s" % path)
        fh = open(path, 'r')
        self.settings = json.load(fh)
        fh.close()

class PyBot(irc.bot.SingleServerIRCBot):

    def __init__(self):
        self.config = Config("config.json")
        self.plugins = {}
        self.prefix = self.config.settings['prefix']
        self.channel = self.config.settings['channel']

        server = self.config.settings['server']
        port = self.config.settings['port']
        nickname = self.config.settings['nickname']

        irc.bot.SingleServerIRCBot.__init__(self, [(server, port)], nickname, nickname)
        self.connection.buffer_class.errors = 'replace' # Should prevent clients sending messages in latin-1 from craching the client

        for name in self.config.settings['plugins']:
            module = __import__("plugins."+name, fromlist=(name))
            print("Loading %sPlugin" % to_camel_case(name))
            self.plugins[name] = getattr(module, to_camel_case(name)+"Plugin")()

    def on_nicknameinuse(self, c, e):
        print("Nick in use, appending _")
        c.nick(c.get_nickname() + "_")

    def on_welcome(self, c, e):
        c.join(self.channel)

    def on_privmsg(self, c, e):
        if e.arguments[0] != self.prefix:
            e.arguments[0] = self.prefix + e.arguments[0]
        response = self.parse_user_command(e.arguments[0])
        if (response):
            c.privmsg(e.source.nick, response)

    def on_pubmsg(self, c, e):
        response = self.parse_user_command(e.arguments[0])
        if (response):
            c.privmsg(self.channel, response)

    def parse_user_command(self, msg):
        if msg[0] == self.prefix:
            command = msg[1:].split(" ", 1)
            command[0] = command[0].lower()
            if command[0] == "die":
                self.connection.quit("arggh!")
                exit()
            else:
                for plugin in self.plugins:
                    if len(command) == 1:
                        arguments = None
                    else:
                        arguments = command[1]
                    response = self.plugins[plugin].parse_user_command(self, command[0], arguments)
                    if response:
                        print(response)
                        return response
            return "Invalid command"
        return None



def main():
    # TODO allow passing config path as argument
    bot = PyBot()
    bot.start()

if __name__ == "__main__":
    main()


{'fraction_non_alphanumeric': 0.12115796997855612, 'fraction_numerical': 0.004824874910650465, 'mean_word_length': 1.966083730789613, 'pattern_counts': {'":': 4, '<': 3, '<?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': '28756', 'n_tokens_mistral': 1661, 'n_tokens_neox': 1585, 'n_words': 374}