<commit_before>#!/usr/bin/python

import sys
sys.dont_write_bytecode = True

import glob
import yaml
import json
import os
import sys
import time

from slackclient import SlackClient

def dbg(debug_string):
    if debug:
        print debug_string

class RtmBot(object):
    def __init__(self, token):
        self.token = token
        self.bot_plugins = []
        self.slack_client = None
    def connect(self):
        """Convenience method that creates Server instance"""
        self.slack_client = SlackClient(self.token)
        self.slack_client.connect()
    def start(self):
        self.connect()
        self.load_plugins()
        while True:
            for reply in self.slack_client.read():
                self.input(reply)
            self.crons()
            self.output()
            time.sleep(.1)
    def input(self, data):
        if "type" in data:
            function_name = "process_" + data["type"]
            dbg(function_name)
            for plugin in self.bot_plugins:
                plugin.do(function_name, data)
    def output(self):
        for plugin in self.bot_plugins:
            for output in plugin.do_output():
                channel = self.slack_client.server.channels.find(output[0])
                channel.send_message("%s" % output[1])
    def crons(self):
        for plugin in self.bot_plugins:
            plugin.do_jobs()
    def load_plugins(self):
        path = os.path.dirname(sys.argv[0])
        for plugin in glob.glob(path+'/plugins/*'):
            sys.path.insert(0, plugin)
        for plugin in glob.glob(path+'/plugins/*/*.py'):
            print plugin
            name = plugin.split('/')[-1][:-2]
#            try:
            self.bot_plugins.append(Plugin(name))
#            except:
#                print "error loading plugin %s" % name

class Plugin(object):
    def __init__(self, name):
        self.name = name
        self.jobs = []
        self.module = __import__(name)
        self.register_jobs()
        self.outputs = []
    def register_jobs(self):
        if 'crontable' in dir(self.module):
            for interval, function in self.module.crontable:
                self.jobs.append(Job(interval, eval("self.module."+function)))
            print self.module.crontable
    def do(self, function_name, data):
        if function_name in dir(self.module):
            try:
                eval("self.module."+function_name)(data)
            except:
                dbg("problem in module")
    def do_jobs(self):
        for job in self.jobs:
            job.check()
    def do_output(self):
        output = []
        while True:
            if 'outputs' in dir(self.module):
                if len(self.module.outputs) > 0:
                    output.append(self.module.outputs.pop(0))
                else:
                    break
        return output

class Job(object):
    def __init__(self, interval, function):
        self.function = function
        self.interval = interval
        self.lastrun = 0<commit_msg>create arrays if not created in plugin<commit_after>#!/usr/bin/python

import sys
sys.dont_write_bytecode = True

import glob
import yaml
import json
import os
import sys
import time

from slackclient import SlackClient

def dbg(debug_string):
    if debug:
        print debug_string

class RtmBot(object):
    def __init__(self, token):
        self.token = token
        self.bot_plugins = []
        self.slack_client = None
    def connect(self):
        """Convenience method that creates Server instance"""
        self.slack_client = SlackClient(self.token)
        self.slack_client.connect()
    def start(self):
        self.connect()
        self.load_plugins()
        while True:
            for reply in self.slack_client.read():
                self.input(reply)
            self.crons()
            self.output()
            time.sleep(.1)
    def input(self, data):
        if "type" in data:
            function_name = "process_" + data["type"]
            dbg(function_name)
            for plugin in self.bot_plugins:
                plugin.do(function_name, data)
    def output(self):
        for plugin in self.bot_plugins:
            for output in plugin.do_output():
                channel = self.slack_client.server.channels.find(output[0])
                channel.send_message("%s" % output[1])
    def crons(self):
        for plugin in self.bot_plugins:
            plugin.do_jobs()
    def load_plugins(self):
        path = os.path.dirname(sys.argv[0])
        for plugin in glob.glob(path+'/plugins/*'):
            sys.path.insert(0, plugin)
        for plugin in glob.glob(path+'/plugins/*/*.py'):
            print plugin
            name = plugin.split('/')[-1][:-2]
#            try:
            self.bot_plugins.append(Plugin(name))
#            except:
#                print "error loading plugin %s" % name

class Plugin(object):
    def __init__(self, name):
        self.name = name
        self.jobs = []
        self.module = __import__(name)
        self.register_jobs()
        self.outputs = []
    def register_jobs(self):
        if 'crontable' in dir(self.module):
            for interval, function in self.module.crontable:
                self.jobs.append(Job(interval, eval("self.module."+function)))
            print self.module.crontable
        else:
            self.module.crontable = []
    def do(self, function_name, data):
        if function_name in dir(self.module):
            try:
                eval("self.module."+function_name)(data)
            except:
                dbg("problem in module")
    def do_jobs(self):
        for job in self.jobs:
            job.check()
    def do_output(self):
        output = []
        while True:
            if 'outputs' in dir(self.module):
                if len(self.module.outputs) > 0:
                    output.append(self.module.outputs.pop(0))
                else:
                    break
            else:
                self.module.outputs = []
        return output

class Job(object):
    def __init__(self, interval, function):
        self.function = function
        self.interval = interval
        self.lastrun = 0
{'fraction_non_alphanumeric': 0.10821318645715211, 'fraction_numerical': 0.0032399157621901833, 'mean_word_length': 1.817891373801917, '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': '1134113', 'n_tokens_mistral': 1786, 'n_tokens_neox': 1722, 'n_words': 417}