<commit_before>from flask import Flask, request
import sys
import json
import ConfigParser
from optparse import OptionParser
import os
import pprint

#TODO : add exception handling when there are no branches_to_channels
#TODO : add global default channel for every unconfigured channel

app = Flask(__name__)

__report_indent = [0]

def debug(fn):
    def wrap(self, *params,**kwargs):
        call = wrap.callcount = wrap.callcount + 1

        indent = ' ' * __report_indent[0]
        fc = "%s(%s)" % (fn.__name__, ', '.join(
            [a.__repr__() for a in params] +
            ["%s = %s" % (a, repr(b)) for a,b in kwargs.items()]
        ))

        #debug = True
        if self.debug == 'True':
            print "%s%s called [#%s]" % (indent, fc, call)
            __report_indent[0] += 1
            ret = fn(self, *params,**kwargs)
            __report_indent[0] -= 1
            print "%s%s returned %s [#%s]" % (indent, fc, repr(ret), call)
            return ret
        else:
            ret = fn(self, *params,**kwargs)
            return ret
    wrap.callcount = 0
    return wrap


class Options:
    """
    @summary: Class responsible for handling options
    """
    def __init__(self, args):
        self.parser = OptionParser()
        self.args = args
        self.parser.add_option("--config",
                               dest="config",
                               default="yageins.cfg",
                               help="Path to yageins cofig file",
                               metavar="CONFIG")
        self.parser.add_option("--debug",
                               dest="debug",
                               default=False,
                               help="Enable debug True|False",
                               metavar="DEBUG")
        (self.options, self.args) = self.parser.parse_args()
        self.debug = self.options.debug

    def __repr__(self):
        return 'Options'

    @debug
    def get_options(self):
	    return self.options

class Config():
    def __init__(self, options):
        self.options = options
        self.debug = self.options.debug
        self.config = ConfigParser.RawConfigParser()
        self.config.read(self.options.config)

    @debug
    def get_config(self):
        return self.config


class IrcChannel:
    def __init__(self, channel_name, config, options):
        self.name = channel_name
        self.path = config.get(channel_name, 'path')
        self.debug = options.debug
        #echo %s | timeout -k 6 3 tee -a '%s' >/dev/null
        self.write_command = config.get('global', 'write_command')

    def __repr__(self):
        return 'IrcChannel'

    @debug
    def write_to_channel(self, message):
        command = self.write_command % (message, self.path)
        os.system(command)
        return True


class Yageins:
    def __init__(self, config, options):
        self.options = options
        self.config = config
        self.debug = self.options.debug
        self.host = self.config.get('global', 'host')
        self.port = self.config.getint('global', 'port')
        self.event_messages = {
                                "push" : "%s pushed to %s: %s",
                                "create" : "%s created branch %s %s",
                                "delete" : "%s deleted branch %s %s",
                                "pull_request" : "%s changed pull request state to '%s' for branch %s %s",
                                "issues" : "%s changed issue state for %s to %s %s",
                                "issue_comment" : "%s commented on issue '%s' %s",
                                "pull_request_review_comment" : "%s commented on '%s' %s"
                              }

    def __repr__(self):
        return 'Yageins'

    @debug
    def _write_to_channel(self, channel_name, message, message_type='push'):
        channel = IrcChannel(channel_name, config, self.options)
        return channel.write_to_channel(message)

    @debug
    def _get_event_message(self, event_name):
        pass

    @debug
    def _parse_channels(self, repo_name):
        """ Should return dictionary of repo_branch : channel_name """
        channels = {}
        try:
            channels_map = self.config.get(repo_name, 'branches_to_channels').split(',')
            for channel_pair in channels_map:
                branch_name, channel_name = channel_pair.split(':')
                channels[branch_name] = channel_name
        except Exception, e:
            print 'No channel configured for %s' % repo_name
            channels[repo_name] = self.config.get('global', 'default_channel')
        return channels

    @debug
    def _channel_for(self, repo_name, branch_name):
        channels = self._parse_channels(repo_name)
        try:
            channel_name = channels[branch_name]
        except Exception, e:
            channel_name = self.config.get('global', 'default_channel')
        return channel_name

    @debug
    def _handle_push(self, req_data, action):
        repo_name = req_data['repository']['full_name']
        pusher = req_data['pusher']['name']
        compare_url = req_data['compare']
        branch_name = req_data['ref'].replace('refs/heads/','')
        message = self.event_messages[action] % (pusher, branch_name, compare_url)<commit_msg>Handle pushing branches for deletion<commit_after>from flask import Flask, request
import sys
import json
import ConfigParser
from optparse import OptionParser
import os
import pprint

#TODO : add exception handling when there are no branches_to_channels
#TODO : add global default channel for every unconfigured channel

app = Flask(__name__)

__report_indent = [0]

def debug(fn):
    def wrap(self, *params,**kwargs):
        call = wrap.callcount = wrap.callcount + 1

        indent = ' ' * __report_indent[0]
        fc = "%s(%s)" % (fn.__name__, ', '.join(
            [a.__repr__() for a in params] +
            ["%s = %s" % (a, repr(b)) for a,b in kwargs.items()]
        ))

        #debug = True
        if self.debug == 'True':
            print "%s%s called [#%s]" % (indent, fc, call)
            __report_indent[0] += 1
            ret = fn(self, *params,**kwargs)
            __report_indent[0] -= 1
            print "%s%s returned %s [#%s]" % (indent, fc, repr(ret), call)
            return ret
        else:
            ret = fn(self, *params,**kwargs)
            return ret
    wrap.callcount = 0
    return wrap


class Options:
    """
    @summary: Class responsible for handling options
    """
    def __init__(self, args):
        self.parser = OptionParser()
        self.args = args
        self.parser.add_option("--config",
                               dest="config",
                               default="yageins.cfg",
                               help="Path to yageins cofig file",
                               metavar="CONFIG")
        self.parser.add_option("--debug",
                               dest="debug",
                               default=False,
                               help="Enable debug True|False",
                               metavar="DEBUG")
        (self.options, self.args) = self.parser.parse_args()
        self.debug = self.options.debug

    def __repr__(self):
        return 'Options'

    @debug
    def get_options(self):
	    return self.options

class Config():
    def __init__(self, options):
        self.options = options
        self.debug = self.options.debug
        self.config = ConfigParser.RawConfigParser()
        self.config.read(self.options.config)

    @debug
    def get_config(self):
        return self.config


class IrcChannel:
    def __init__(self, channel_name, config, options):
        self.name = channel_name
        self.path = config.get(channel_name, 'path')
        self.debug = options.debug
        #echo %s | timeout -k 6 3 tee -a '%s' >/dev/null
        self.write_command = config.get('global', 'write_command')

    def __repr__(self):
        return 'IrcChannel'

    @debug
    def write_to_channel(self, message):
        command = self.write_command % (message, self.path)
        os.system(command)
        return True


class Yageins:
    def __init__(self, config, options):
        self.options = options
        self.config = config
        self.debug = self.options.debug
        self.host = self.config.get('global', 'host')
        self.port = self.config.getint('global', 'port')
        self.event_messages = {
                                "push" : "%s pushed to %s %s: %s",
                                "create" : "%s created branch %s %s",
                                "delete" : "%s deleted branch %s %s",
                                "pull_request" : "%s changed pull request state to '%s' for branch %s %s",
                                "issues" : "%s changed issue state for %s to %s %s",
                                "issue_comment" : "%s commented on issue '%s' %s",
                                "pull_request_review_comment" : "%s commented on '%s' %s"
                              }

    def __repr__(self):
        return 'Yageins'

    @debug
    def _write_to_channel(self, channel_name, message, message_type='push'):
        channel = IrcChannel(channel_name, config, self.options)
        return channel.write_to_channel(message)

    @debug
    def _get_event_message(self, event_name):
        pass

    @debug
    def _parse_channels(self, repo_name):
        """ Should return dictionary of repo_branch : channel_name """
        channels = {}
        try:
            channels_map = self.config.get(repo_name, 'branches_to_channels').split(',')
            for channel_pair in channels_map:
                branch_name, channel_name = channel_pair.split(':')
                channels[branch_name] = channel_name
        except Exception, e:
            print 'No channel configured for %s' % repo_name
            channels[repo_name] = self.config.get('global', 'default_channel')
        return channels

    @debug
    def _channel_for(self, repo_name, branch_name):
        channels = self._parse_channels(repo_name)
        try:
            channel_name = channels[branch_name]
        except Exception, e:
            channel_name = self.config.get('global', 'default_channel')
        return channel_name

    @debug
    def _handle_push(self, req_data, action):
        repo_name = req_data['repository']['full_name']
        pusher = req_data['pusher']['name']
        deleted = req_data['deleted']
        if deleted == True:
            deleted_message = 'and deleted it'
        else:
            deleted_message = ''
        compare_url = req_data['compare']
        branch_name = req_data['ref'].replace('refs/heads/','')
        message = self.event_messages[action] % (pusher, branch_name, deleted_message, compare_url)
{'fraction_non_alphanumeric': 0.11275279342506234, 'fraction_numerical': 0.0018468926031951242, 'mean_word_length': 1.9105079279763504, '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': '3562288', 'n_tokens_mistral': 3087, 'n_tokens_neox': 3008, 'n_words': 778}