<commit_before># Quark's Twisted Runtime and associated

__version__ = "0.1.1"

from StringIO import StringIO

from quark_runtime import Buffer
from autobahn.twisted.websocket import WebSocketClientProtocol, WebSocketClientFactory, connectWS

from twisted.internet import defer, reactor, ssl
from twisted.python import log
from twisted.web.client import Agent, FileBodyProducer, readBody
from twisted.web.http_headers import Headers


def _dumper(reason):
    log.err(reason, "From the future!")
    return reason


class Future(object):

    def __init__(self):
        self.deferred = defer.Deferred()
        self.deferred.addErrback(_dumper)

    def succeed(self, value):
        self.deferred.callback(value)

    def fail(self, reason):
        self.deferred.errback(Exception(reason))

    def getResult(self, callback, errback=None):
        if errback:
            self.deferred.addErrback(errback)
        self.deferred.addCallback(callback)


class _QWSCProtocol(WebSocketClientProtocol):

    def onOpen(self):
        self.factory.qws.is_open = True
        self.factory.qws.handler.onWSConnected(self.factory.qws)

    def onMessage(self, payload, isBinary):
        if isBinary:
            self.factory.qws.handler.onWSBinary(self.factory.qws, Buffer(payload))
        else:
            self.factory.qws.handler.onWSMessage(self.factory.qws, payload.decode("utf-8"))

    def onClose(self, wasClean, code, reason):
        if wasClean:
            self.factory.qws.handler.onWSClosed(self.factory.qws)
        else:
            self.factory.qws.handler.onWSError(self.factory.qws)
        self.factory.qws.handler.onWSFinal(self.factory.qws)


class _QWSCFactory(WebSocketClientFactory):

    def buildProtocol(self, addr):
        p = _QWSCProtocol()
        p.factory = self
        self.qws.protocol = p
        return p


class _QuarkWebSocket(WebSocketClientProtocol):

    def __init__(self, factory, handler):
        factory.qws = self
        self.protocol = None  # filled in by the factory
        self.handler = handler
        self.is_open = False
        self.handler.onWSInit(self)

    def send(self, message):
        if self.is_open:
            self.protocol.sendMessage(message.encode('utf-8', false))
            return 1
        return 0

    def sendBinary(self, message):
        if self.is_open:
            self.protocol.sendMessage(message.data, true)
            return 1
        return 0


class _QuarkRequest(object):

    def __init__(self, agent, request, handler):
        self.request = request
        self.handler = handler
        self.response = None

        headers = {key.encode("utf-8"): [str(value).encode("utf-8")] for key, value in request.headers.items()}<commit_msg>fixed NameError<commit_after># Quark's Twisted Runtime and associated

__version__ = "0.1.2"

from StringIO import StringIO

from quark_runtime import Buffer
from autobahn.twisted.websocket import WebSocketClientProtocol, WebSocketClientFactory, connectWS

from twisted.internet import defer, reactor, ssl
from twisted.python import log
from twisted.web.client import Agent, FileBodyProducer, readBody
from twisted.web.http_headers import Headers


def _dumper(reason):
    log.err(reason, "From the future!")
    return reason


class Future(object):

    def __init__(self):
        self.deferred = defer.Deferred()
        self.deferred.addErrback(_dumper)

    def succeed(self, value):
        self.deferred.callback(value)

    def fail(self, reason):
        self.deferred.errback(Exception(reason))

    def getResult(self, callback, errback=None):
        if errback:
            self.deferred.addErrback(errback)
        self.deferred.addCallback(callback)


class _QWSCProtocol(WebSocketClientProtocol):

    def onOpen(self):
        self.factory.qws.is_open = True
        self.factory.qws.handler.onWSConnected(self.factory.qws)

    def onMessage(self, payload, isBinary):
        if isBinary:
            self.factory.qws.handler.onWSBinary(self.factory.qws, Buffer(payload))
        else:
            self.factory.qws.handler.onWSMessage(self.factory.qws, payload.decode("utf-8"))

    def onClose(self, wasClean, code, reason):
        if wasClean:
            self.factory.qws.handler.onWSClosed(self.factory.qws)
        else:
            self.factory.qws.handler.onWSError(self.factory.qws)
        self.factory.qws.handler.onWSFinal(self.factory.qws)


class _QWSCFactory(WebSocketClientFactory):

    def buildProtocol(self, addr):
        p = _QWSCProtocol()
        p.factory = self
        self.qws.protocol = p
        return p


class _QuarkWebSocket(WebSocketClientProtocol):

    def __init__(self, factory, handler):
        factory.qws = self
        self.protocol = None  # filled in by the factory
        self.handler = handler
        self.is_open = False
        self.handler.onWSInit(self)

    def send(self, message):
        if self.is_open:
            self.protocol.sendMessage(message.encode('utf-8', False))
            return 1
        return 0

    def sendBinary(self, message):
        if self.is_open:
            self.protocol.sendMessage(message.data, True)
            return 1
        return 0


class _QuarkRequest(object):

    def __init__(self, agent, request, handler):
        self.request = request
        self.handler = handler
        self.response = None

        headers = {key.encode("utf-8"): [str(value).encode("utf-8")] for key, value in request.headers.items()}
{'fraction_non_alphanumeric': 0.09886488465763457, 'fraction_numerical': 0.004027828634199927, 'mean_word_length': 2.804317548746518, 'pattern_counts': {'":': 0, '<': 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': '238347', 'n_tokens_mistral': 1677, 'n_tokens_neox': 1613, 'n_words': 356}