<commit_before>from gevent import monkey
monkey.patch_all()

import time
import socket
from uuid import uuid1
try:
    import simplejson as json
except ImportError:
    import json


class AMIClient(object):

    def __init__(self, username, password, host='localhost', port=5038,
                 **kwargs):

        self._cbs_events = {}
        self._cbs_actions = {}
        self._cbs_global = {}
        self._sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
        self._sock.connect((host, port))
        self._version = ''
        while not self.version.endswith('\r\n'):
            self._version += self.sock.recv(1024)
            time.sleep(0)
        self.action('login', username=username, secret=password,
                    callback=self._login, **kwargs)

    def _login(self, evt, obj, ami):
        if obj['Response'] != 'Success':
            raise Exception('Login Error')

    @property
    def sock(self):
        return self._sock

    @property
    def version(self):
        return self._version

    def on(self, event, callback):
        if event in ['all', 'event', 'response', 'raw']:
            self._cbs_global[event] = callback
        else:
            self._cbs_events[event] = callback

    def dispatch(self, event):
        if 'all' in self._cbs_global:
            self._cbs_global['all']('all', event, self)
            time.sleep(0)

        if 'event' in self._cbs_global and 'Event' in event:
            self._cbs_global['event']('event', event, self)
            time.sleep(0)

        if 'response' in self._cbs_global and 'ActionID' in event:
            self._cbs_global['response']('response', event, self)
            time.sleep(0)

        if 'ActionID' in event and event['ActionID'] in self._cbs_actions:
            callback = self._cbs_actions.pop(event['ActionID'])
            callback(event['ActionID'], event, self)
            time.sleep(0)

        elif 'Event' in event and event['Event'] in self._cbs_events:
            callback = self._cbs_events.get(event['Event'])
            callback(event['Event'], event, self)
            time.sleep(0)

        elif 'RawData' in event and 'raw' in self._cbs_global:
            self._cbs_global['raw']('raw', event, self)
            time.sleep(0)

    def parser(self, data):
        res = data

        try:
            res = res.replace('"', '\\"')
            res = res.replace('\r\n', '", "')
            res = res.replace(': ', '": "')
            res = json.loads('{"' + res + '"}')
        except:
            res = {'RawData': data}

        return res
<commit_msg>simplified callbacks<commit_after>from gevent import monkey
monkey.patch_all()

import time
import socket
from uuid import uuid1
try:
    import simplejson as json
except ImportError:
    import json


class AMIClient(object):

    def __init__(self, username, password, host='localhost', port=5038,
                 **kwargs):

        self._cbs_events = {}
        self._cbs_actions = {}
        self._cbs_global = {}
        self._sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
        self._sock.connect((host, port))
        self._version = ''
        while not self.version.endswith('\r\n'):
            self._version += self.sock.recv(1024)
            time.sleep(0)
        self.action('login', username=username, secret=password,
                    callback=self._login, **kwargs)

    def _login(self, evt):
        if evt['Response'] != 'Success':
            raise Exception('Login Error')

    @property
    def sock(self):
        return self._sock

    @property
    def version(self):
        return self._version

    def on(self, event, callback):
        if event in ['all', 'event', 'response', 'raw']:
            self._cbs_global[event] = callback
        else:
            self._cbs_events[event] = callback

    def dispatch(self, event):
        if 'all' in self._cbs_global:
            self._cbs_global['all'](event)
            time.sleep(0)

        if 'event' in self._cbs_global and 'Event' in event:
            self._cbs_global['event'](event)
            time.sleep(0)

        if 'response' in self._cbs_global and 'ActionID' in event:
            self._cbs_global['response'](event)
            time.sleep(0)

        if 'ActionID' in event and event['ActionID'] in self._cbs_actions:
            callback = self._cbs_actions.pop(event['ActionID'])
            callback(event)
            time.sleep(0)

        elif 'Event' in event and event['Event'] in self._cbs_events:
            callback = self._cbs_events.get(event['Event'])
            callback(event)
            time.sleep(0)

        elif 'RawData' in event and 'raw' in self._cbs_global:
            self._cbs_global['raw'](event)
            time.sleep(0)

    def parser(self, data):
        res = data

        try:
            res = res.replace('"', '\\"')
            res = res.replace('\r\n', '", "')
            res = res.replace(': ', '": "')
            res = json.loads('{"' + res + '"}')
        except:
            res = {'RawData': data}

        return res

{'fraction_non_alphanumeric': 0.1328602404888626, 'fraction_numerical': 0.006307904592943032, 'mean_word_length': 2.0492788461538463, 'pattern_counts': {'":': 2, '<': 3, '<?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': '1471142', 'n_tokens_mistral': 1535, 'n_tokens_neox': 1502, 'n_words': 355}