<commit_before>#!/usr/bin/env python
import email
import email.header
import sys
import traceback
import json
import urllib2
import datetime
import ConfigParser
import argparse

DEFAULT_LOGFILE = '/var/log/slackmail.log'
DEFAULT_CONFIGFILE = '/etc/slackmail.conf'
def read_config(filename):
    config = ConfigParser.RawConfigParser()
    if not config.has_section('log'):
        config.add_section('log')
    config.set('log', 'logfile', DEFAULT_LOGFILE)
    config.readfp(open(filename, 'r'))
    return config

parser = argparse.ArgumentParser(description='Send stadard out to slack')
parser.add_argument('--config', dest='configfile', action='store',
                    default=DEFAULT_CONFIGFILE,
                    help='Config file to read (default %s)' % DEFAULT_CONFIGFILE)
parser.add_argument('--username', dest='username', action='store',
    default=None,
    help='Username to post as')
parser.add_argument('--tee', dest='tee', action='store_true',
    default=False,
    help='Also send output to standard out')

args = parser.parse_args()

config = read_config(args.configfile)
logfile = config.get('log', 'logfile')
webhook_url = config.get('slack', 'webhook_url')

while True:
    inputstring = sys.stdin.readline()
    if inputstring == '': break
    if args.tee:
        print inputstring,
    try:
        payload = {
            'text': inputstring.rstrip(),
            'icon_emoji': ':robot_face:'
        }
        if args.username is not None:
            payload['username'] = args.username
        if config.has_option('slack', 'channel'):
            payload['channel'] = config.get('slack', 'channel')
        payload = json.dumps(payload)
        urllib2.urlopen(webhook_url, payload)
    except Exception, e:
        print >> stderr, "Error posting to Slack:"
        traceback.print_exc()
        print >> stderr, "Payload:"
        print >> stderr, payload<commit_msg>Improvement to slackcat to buffer up input and send it to slack as fewer mesasges<commit_after>#!/usr/bin/env python
import sys
import traceback
import json
import urllib2
import datetime
import ConfigParser
import argparse
import time
import select

DEFAULT_LOGFILE = '/var/log/slackmail.log'
DEFAULT_CONFIGFILE = '/etc/slackmail.conf'
def read_config(filename):
    config = ConfigParser.RawConfigParser()
    if not config.has_section('log'):
        config.add_section('log')
    config.set('log', 'logfile', DEFAULT_LOGFILE)
    config.readfp(open(filename, 'r'))
    return config

parser = argparse.ArgumentParser(description='Send stadard out to slack')
parser.add_argument('--config', dest='configfile', action='store',
                    default=DEFAULT_CONFIGFILE,
                    help='Config file to read (default %s)' % DEFAULT_CONFIGFILE)
parser.add_argument('--username', dest='username', action='store',
    default=None,
    help='Username to post as')
parser.add_argument('--tee', dest='tee', action='store_true',
    default=False,
    help='Also send output to standard out')

args = parser.parse_args()

config = read_config(args.configfile)
logfile = config.get('log', 'logfile')
webhook_url = config.get('slack', 'webhook_url')

linebuffer = []
last_send = 0

def flush(linebuffer):
    if not linebuffer: return
    try:
        payload = {
            'text': "".join(linebuffer),
            'icon_emoji': ':robot_face:'
        }
        if args.username is not None:
            payload['username'] = args.username
        if config.has_option('slack', 'channel'):
            payload['channel'] = config.get('slack', 'channel')
        payload = json.dumps(payload)
        urllib2.urlopen(webhook_url, payload)
    except Exception, e:
        print >> stderr, "Error posting to Slack:"
        traceback.print_exc()
        print >> stderr, "Payload:"
        print >> stderr, payload

while True:
    if linebuffer:
        (r,w,e) = select.select([sys.stdin], [], [], 2)
        if r:
            do_read = True
        else:
            do_read = False
    else:
        do_read = True

    if do_read:
        inputstring = sys.stdin.readline()
        if inputstring == '':
            flush(linebuffer)
            break
        if args.tee:
            print inputstring,
        linebuffer.append(inputstring)

    if time.time() - last_send < 2:
        continue
    flush(linebuffer)
    linebuffer = []
    last_send = time.time()
{'fraction_non_alphanumeric': 0.1252852578731173, 'fraction_numerical': 0.001597444089456869, 'mean_word_length': 2.9881710646041855, 'pattern_counts': {'":': 0, '<': 4, '<?xml version=': 0, '>': 15, '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': '6556749', 'n_tokens_mistral': 1309, 'n_tokens_neox': 1295, 'n_words': 318}