<commit_before>import threading
import cherrypy
from tangelo.ws4py.server.cherrypyserver import WebSocketPlugin
import tangelo.ws4py.websocket
import tangelo.autobahn.websocket as ab_websocket
import tangelo.autobahn.wamp as wamp
import twisted.internet

import tangelo


class WebSocketLowPriorityPlugin(WebSocketPlugin):
    def __init__(self, *pargs, **kwargs):
        WebSocketPlugin.__init__(self, *pargs, **kwargs)

    # This version of start() differs only in that it has an assigned priority.
    # The default priority is 50, which is what the actual WebSocketPlugin's
    # start method gets, which means it runs before the privilege drop gets a
    # chance to (priority 77, slightly lower than the 75 of the engine start
    # itself).  For some reason if this runs before the priv drop, things get
    # screwed up.
    def start(self):
        WebSocketPlugin.start(self)
    start.priority = 80


class WebSocketHandler(object):
    @cherrypy.expose
    def index(self):
        return "OK"

    @cherrypy.expose
    def ws(self):
        pass


def VTKWebSocketAB(url, relay):
    class RegisteringWebSocketClientFactory(wamp.WampClientFactory):
        def register(self, client):
            self.client = client

    class Protocol(wamp.WampClientProtocol):
        def onOpen(self):
            self.factory.register(self)

        def onMessage(self, msg, is_binary):
            relay.send(msg)

    class Connection(threading.Thread):
        def run(self):
            self.factory = RegisteringWebSocketClientFactory(url)
            self.factory.protocol = Protocol
            twisted.internet.reactor.callFromThread(ab_websocket.connectWS,
                                                    self.factory)

        def send(self, data):
            twisted.internet.reactor.callFromThread(Protocol.sendMessage,
                                                    self.factory.client,<commit_msg>Adding serverside utilities to manage the creation of websockets<commit_after>import threading
import cherrypy
from tangelo.ws4py.server.cherrypyserver import WebSocketPlugin
import tangelo.ws4py.websocket
import tangelo.autobahn.websocket as ab_websocket
import tangelo.autobahn.wamp as wamp
import twisted.internet

import tangelo


class WebSocketLowPriorityPlugin(WebSocketPlugin):
    def __init__(self, *pargs, **kwargs):
        WebSocketPlugin.__init__(self, *pargs, **kwargs)

    # This version of start() differs only in that it has an assigned priority.
    # The default priority is 50, which is what the actual WebSocketPlugin's
    # start method gets, which means it runs before the privilege drop gets a
    # chance to (priority 77, slightly lower than the 75 of the engine start
    # itself).  For some reason if this runs before the priv drop, things get
    # screwed up.
    def start(self):
        WebSocketPlugin.start(self)
    start.priority = 80


class WebSocketHandler(object):
    @cherrypy.expose
    def index(self):
        pass

    @cherrypy.expose
    def ws(self):
        pass


def mount(name, handler_cls, protocols=None):
    if protocols is None:
        protocols = []

    cherrypy.tree.mount(WebSocketHandler(),
                        "/ws/%s" % (name),
                        config={"/ws": {"tools.websocket.on": True,
                                        "tools.websocket.handler_cls": handler_cls,
                                        "tools.websocket.protocols": protocols}})


def unmount(name):
    try:
        del cherrypy.tree.apps["/ws/%s" % (name)]
    except KeyError:
        raise KeyError("no such websocket path: %s" % (name))


def VTKWebSocketAB(url, relay):
    class RegisteringWebSocketClientFactory(wamp.WampClientFactory):
        def register(self, client):
            self.client = client

    class Protocol(wamp.WampClientProtocol):
        def onOpen(self):
            self.factory.register(self)

        def onMessage(self, msg, is_binary):
            relay.send(msg)

    class Connection(threading.Thread):
        def run(self):
            self.factory = RegisteringWebSocketClientFactory(url)
            self.factory.protocol = Protocol
            twisted.internet.reactor.callFromThread(ab_websocket.connectWS,
                                                    self.factory)

        def send(self, data):
            twisted.internet.reactor.callFromThread(Protocol.sendMessage,
                                                    self.factory.client,
{'fraction_non_alphanumeric': 0.07998212689901697, 'fraction_numerical': 0.004468275245755138, 'mean_word_length': 2.4411990776325903, 'pattern_counts': {'":': 4, '<': 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': 10, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4485233', 'n_tokens_mistral': 1246, 'n_tokens_neox': 1177, 'n_words': 334}