<commit_before># coding: utf-8
from __future__ import unicode_literals
from gevent.monkey import patch_all
patch_all()

import gevent
import logging
from gevent.pool import Pool
from redis import StrictRedis
from importlib import import_module
from slackclient import SlackClient

from settings import APPS, SLACK_TOKEN, REDIS_URL


pool = Pool(20)

CMD_PREFIX = '!'
logger = logging.getLogger()


class RedisBrain(object):
    def __init__(self):
        try:
            self.redis = StrictRedis(host=REDIS_URL)
        except Exception as e:
            logger.error(e)
            self.redis = None

    def set(self, key, value):
        if self.redis:
            self.redis.set(key, value)
            return True
        else:
            return False

    def get(self, key):
        if self.redis:
            return self.redis.get(key)
        return None

    def lpush(self, key, value):
        if self.redis:
            self.redis.lpush(key, value)
            return True
        else:
            return False

    def lpop(self, key):
        if self.redis:
            return self.redis.lpop(key)
        return None

    def lindex(self, key):
        if self.redis:
            return self.redis.lindex(key)
        return None


class Robot(object):
    def __init__(self):
        self.client = SlackClient(SLACK_TOKEN)
        self.brain = RedisBrain()
        self.apps, self.docs = self.load_apps()

    def load_apps(self):
        docs = ['='*14, '홍모아 사용방법', '='*14]
        apps = {}

        for name in APPS:
            app = import_module('apps.%s' % name)
            docs.append(
                '!%s: %s' % (', '.join(app.run.commands), app.run.__doc__)
            )
            for command in app.run.commands:
                apps[command] = app

        return apps, docs

    def handle_messages(self, messages):
        for channel, user, text in messages:
            command, payloads = self.extract_command(text)
            if not command:
                continue

            app = self.apps.get(command, None)
            if not app:
                continue

            pool.apply_async(
                func=app.run, args=(self, channel, user, payloads)
            )

    def extract_messages(self, events):
        messages = []
        for event in events:
            channel = event.get('channel', '')
            user = event.get('user', '')
            text = event.get('text', '')
            if channel and user and text:
                messages.append((channel, user, text))
        return messages

    def extract_command(self, text):
        if CMD_PREFIX != text[0]:
            return (None, None)

        tokens = text.split(' ', 1)
        if 1 < len(tokens):
            return tokens[0][1:], tokens[1]
        else:
            return (text[1:], '')

    def run(self):
        if self.client.rtm_connect():
            while True:
                events = self.client.rtm_read()
                if events:
                    messages = self.extract_messages(events)
                    self.handle_messages(messages)
                gevent.sleep(0.3)<commit_msg>add log messages when error occurs on connect client to slack or read text from channel<commit_after># coding: utf-8
from __future__ import unicode_literals
from gevent.monkey import patch_all
patch_all()

import gevent
import logging
from gevent.pool import Pool
from redis import StrictRedis
from importlib import import_module
from slackclient import SlackClient

from settings import APPS, SLACK_TOKEN, REDIS_URL


pool = Pool(20)

CMD_PREFIX = '!'
logger = logging.getLogger()


class RedisBrain(object):
    def __init__(self):
        try:
            self.redis = StrictRedis(host=REDIS_URL)
        except Exception as e:
            logger.error(e)
            self.redis = None

    def set(self, key, value):
        if self.redis:
            self.redis.set(key, value)
            return True
        else:
            return False

    def get(self, key):
        if self.redis:
            return self.redis.get(key)
        return None

    def lpush(self, key, value):
        if self.redis:
            self.redis.lpush(key, value)
            return True
        else:
            return False

    def lpop(self, key):
        if self.redis:
            return self.redis.lpop(key)
        return None

    def lindex(self, key):
        if self.redis:
            return self.redis.lindex(key)
        return None


class Robot(object):
    def __init__(self):
        self.client = SlackClient(SLACK_TOKEN)
        self.brain = RedisBrain()
        self.apps, self.docs = self.load_apps()

    def load_apps(self):
        docs = ['='*14, '홍모아 사용방법', '='*14]
        apps = {}

        for name in APPS:
            app = import_module('apps.%s' % name)
            docs.append(
                '!%s: %s' % (', '.join(app.run.commands), app.run.__doc__)
            )
            for command in app.run.commands:
                apps[command] = app

        return apps, docs

    def handle_messages(self, messages):
        for channel, user, text in messages:
            command, payloads = self.extract_command(text)
            if not command:
                continue

            app = self.apps.get(command, None)
            if not app:
                continue

            pool.apply_async(
                func=app.run, args=(self, channel, user, payloads)
            )

    def extract_messages(self, events):
        messages = []
        for event in events:
            channel = event.get('channel', '')
            user = event.get('user', '')
            text = event.get('text', '')
            if channel and user and text:
                messages.append((channel, user, text))
        return messages

    def extract_command(self, text):
        if CMD_PREFIX != text[0]:
            return (None, None)

        tokens = text.split(' ', 1)
        if 1 < len(tokens):
            return tokens[0][1:], tokens[1]
        else:
            return (text[1:], '')

    def rtm_connect(self):
        conn = None
        try:
            conn = self.client.rtm_connect()
        except Exception as e:
            logger.error(e)
        return conn

    def read_message(self):
        events = None
        try:
            events = self.client.rtm_read()
        except Exception as e:
            logger.error(e)
        return events

    def run(self):
        if not self.rtm_connect():
            raise RuntimeError(
                'Can not connect to slack client. Check your settings.'
            )

        while True:
            events = self.read_message()
            if events:
                messages = self.extract_messages(events)
                self.handle_messages(messages)
            gevent.sleep(0.3)
{'fraction_non_alphanumeric': 0.09834140613532952, 'fraction_numerical': 0.0046969029795978275, 'mean_word_length': 1.6805664830841858, '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': '7597951', 'n_tokens_mistral': 2015, 'n_tokens_neox': 1944, 'n_words': 532}