<commit_before>import os
import logging
from uuid import uuid4
from slacker import Slacker
from redis import StrictRedis
from collections import defaultdict

log = logging.getLogger('slackn')

class Attachment(object):
    """ Nagios notification formatted as a Slack attachment """
    def __init__(self, title, fields):
        self.props = { 'mrkdwn_in': ['fields'],
                       'title': title,
                       'color': 'good',
                       'fields': [] }
        for f in fields:
            self.append_field(f)

    def append_field(self, text, bold=True):
        self._set_color(text)
        if bold:
            text = '*%s*' % text
        self.props['fields'].append({ 'short': False, 'value': text })

    def _set_color(self, text):
        if 'CRITICAL' in text:
            self.props['color'] = 'danger'
        if 'WARNING' in text:
            self.props['color'] = 'warning'

class Notifier(object):
    def __init__(self, slack_token, slack_channel):
        self.slack = Slacker(slack_token)
        if slack_channel.startswith('#'):
            self.channel = slack_channel
        else:
            self.channel = '#' + slack_channel
        self.attachments = []

    def send(self):
        attach = [ a.props for a in self.attachments ]

        if not attach:
            log.info('no notifications to send')
            return

        res = self.slack.chat.post_message(self.channel, '', 'nagios',
                    attachments=attach)
        log.info('slack notification sent')
        if not res.successful:
            log.error('slack notification failed:\n%s' % res.error)

    def add_host(self, hostname, messages):
        self.attachments.append(Attachment(hostname, messages))

class Queue(object):
    def __init__(self, redis_host, redis_port):
        self.redis = StrictRedis(host=redis_host, port=int(redis_port),
                                 decode_responses=True)

    def submit(self, notify_args):
        key = 'slackn:%s' % notify_args['hostname']
        notify_msg = self._format(notify_args)

        self.redis.lpush(key, notify_msg)
        self.increment('queued', 1)
        log.debug('notification queued: %s' % notify_msg) 

    def dump(self):
        ret = {}<commit_msg>add icon to slack post<commit_after>import os
import logging
from uuid import uuid4
from slacker import Slacker
from redis import StrictRedis
from collections import defaultdict

log = logging.getLogger('slackn')
icon_url = 'https://slack.global.ssl.fastly.net/4324/img/services/nagios_48.png'

class Attachment(object):
    """ Nagios notification formatted as a Slack attachment """
    def __init__(self, title, fields):
        self.props = { 'mrkdwn_in': ['fields'],
                       'title': title,
                       'color': 'good',
                       'fields': [] }
        for f in fields:
            self.append_field(f)

    def append_field(self, text, bold=True):
        self._set_color(text)
        if bold:
            text = '*%s*' % text
        self.props['fields'].append({ 'short': False, 'value': text })

    def _set_color(self, text):
        if 'CRITICAL' in text:
            self.props['color'] = 'danger'
        if 'WARNING' in text:
            self.props['color'] = 'warning'

class Notifier(object):
    def __init__(self, slack_token, slack_channel):
        self.slack = Slacker(slack_token)
        if slack_channel.startswith('#'):
            self.channel = slack_channel
        else:
            self.channel = '#' + slack_channel
        self.attachments = []

    def send(self):
        attach = [ a.props for a in self.attachments ]

        if not attach:
            log.info('no notifications to send')
            return

        res = self.slack.chat.post_message(self.channel, '', 'nagios',
                attachments=attach, icon_url=icon_url)
        log.info('slack notification sent')
        if not res.successful:
            log.error('slack notification failed:\n%s' % res.error)

    def add_host(self, hostname, messages):
        self.attachments.append(Attachment(hostname, messages))

class Queue(object):
    def __init__(self, redis_host, redis_port):
        self.redis = StrictRedis(host=redis_host, port=int(redis_port),
                                 decode_responses=True)

    def submit(self, notify_args):
        key = 'slackn:%s' % notify_args['hostname']
        notify_msg = self._format(notify_args)

        self.redis.lpush(key, notify_msg)
        self.increment('queued', 1)
        log.debug('notification queued: %s' % notify_msg) 

    def dump(self):
        ret = {}
{'fraction_non_alphanumeric': 0.1182934712346477, 'fraction_numerical': 0.0021547080370609784, 'mean_word_length': 2.282885431400283, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 1, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2076161', 'n_tokens_mistral': 1406, 'n_tokens_neox': 1365, 'n_words': 325}