<commit_before>import json
import logging
import threading

log = logging.getLogger('pajbot')


class WebSocketServer:
    clients = []

    def __init__(self, manager, port, secure=False, key_path=None, crt_path=None):
        self.manager = manager
        from twisted.internet import reactor, ssl

        from autobahn.twisted.websocket import WebSocketServerFactory, \
                WebSocketServerProtocol

        class MyServerProtocol(WebSocketServerProtocol):
            def onConnect(self, request):
                # log.info(self.factory)
                # log.info('Client connecting: {0}'.format(request.peer))
                pass

            def onOpen(self):
                log.info('WebSocket connection open')
                WebSocketServer.clients.append(self)

            def onMessage(self, payload, isBinary):
                if isBinary:
                    log.info('Binary message received: {0} bytes'.format(len(payload)))
                else:
                    log.info('Text message received: {0}'.format(payload.decode('utf8')))

            def onClose(self, wasClean, code, reason):
                log.info('WebSocket connection closed: {0}'.format(reason))
                try:
                    WebSocketServer.clients.remove(self)
                except:
                    pass

        factory = WebSocketServerFactory()
        factory.protocol = MyServerProtocol

        def reactor_run(reactor, factory, port, context_factory=None):
            if context_factory:
                log.info('wss secure')
                reactor.listenSSL(port, factory, context_factory)
            else:
                log.info('ws unsecure')
                reactor.listenTCP(port, factory)
            reactor.run(installSignalHandlers=0)

        if secure:
            context_factory = ssl.DefaultOpenSSLContextFactory(key_path, crt_path)
        else:
            context_factory = None

        reactor_thread = threading.Thread(target=reactor_run,
                args=(reactor,
                    factory,
                    port,
                    context_factory),
                name='WebSocketThread')
        reactor_thread.daemon = True
        reactor_thread.start()


class WebSocketManager:
    def __init__(self, bot):
        self.clients = []
        self.server = None
        self.bot = bot

        if 'websocket' not in bot.config:
            log.debug('WebSocket support not set up, check out https://github.com/pajlada/pajbot/wiki/Config-File#websocket')
            return

        cfg = bot.config['websocket']

        try:
            if cfg['enabled'] == '1':
                # Initialize twisted logging
                try:
                    from twisted.python import log as twisted_log
                    twisted_log.addObserver(WebSocketManager.on_log_message)
                except ImportError:
                    log.error('twisted is not installed, websocket cannot be initialized.')
                    return
                except:
                    log.exception('Uncaught exception')
                    return

                ssl = bool(cfg.get('ssl', '0') == '1')
                port = int(cfg.get('port', '443' if ssl else '80'))
                key_path = cfg.get('key_path', '')
                crt_path = cfg.get('crt_path', '')

                if ssl:
                    if key_path == '' or crt_path == '':
                        log.error('SSL enabled in config, but missing key_path or crt_path')
                        return

                self.server = WebSocketServer(self, port, ssl, key_path, crt_path)
        except:
            log.exception('Uncaught exception in WebSocketManager')

    def emit(self, event, data={}):
        if self.server:
            payload = json.dumps({
                'event': event,
                'data': data,
                }).encode('utf8')
            for client in self.server.clients:
                client.sendMessage(payload, False)

    def on_log_message(message, isError=False, printed=False):
        if isError:
            log.error(message['message'])
        else:
            pass
            # log.debug('on_log_message({})'.format(message['message']))<commit_msg>add unix_socket argument to websocket config section<commit_after>import json
import logging
import threading

log = logging.getLogger('pajbot')


class WebSocketServer:
    clients = []

    def __init__(self, manager, port, secure=False, key_path=None, crt_path=None, unix_socket_path=None):
        self.manager = manager
        from twisted.internet import reactor, ssl

        from autobahn.twisted.websocket import WebSocketServerFactory, \
                WebSocketServerProtocol

        class MyServerProtocol(WebSocketServerProtocol):
            def onConnect(self, request):
                # log.info(self.factory)
                # log.info('Client connecting: {0}'.format(request.peer))
                pass

            def onOpen(self):
                log.info('WebSocket connection open')
                WebSocketServer.clients.append(self)

            def onMessage(self, payload, isBinary):
                if isBinary:
                    log.info('Binary message received: {0} bytes'.format(len(payload)))
                else:
                    log.info('Text message received: {0}'.format(payload.decode('utf8')))

            def onClose(self, wasClean, code, reason):
                log.info('WebSocket connection closed: {0}'.format(reason))
                try:
                    WebSocketServer.clients.remove(self)
                except:
                    pass

        factory = WebSocketServerFactory()
        factory.protocol = MyServerProtocol

        def reactor_run(reactor, factory, port, context_factory=None, unix_socket_path=None):
            if unix_socket_path:
                reactor.listenUNIX(unix_socket_path, factory)
            else:
                if context_factory:
                    log.info('wss secure')
                    reactor.listenSSL(port, factory, context_factory)
                else:
                    log.info('ws unsecure')
                    reactor.listenTCP(port, factory)
            reactor.run(installSignalHandlers=0)

        if secure:
            context_factory = ssl.DefaultOpenSSLContextFactory(key_path, crt_path)
        else:
            context_factory = None

        reactor_thread = threading.Thread(target=reactor_run,
                args=(reactor,
                    factory,
                    port,
                    context_factory,
                    unix_socket_path),
                name='WebSocketThread')
        reactor_thread.daemon = True
        reactor_thread.start()


class WebSocketManager:
    def __init__(self, bot):
        self.clients = []
        self.server = None
        self.bot = bot

        if 'websocket' not in bot.config:
            log.debug('WebSocket support not set up, check out https://github.com/pajlada/pajbot/wiki/Config-File#websocket')
            return

        cfg = bot.config['websocket']

        try:
            if cfg['enabled'] == '1':
                # Initialize twisted logging
                try:
                    from twisted.python import log as twisted_log
                    twisted_log.addObserver(WebSocketManager.on_log_message)
                except ImportError:
                    log.error('twisted is not installed, websocket cannot be initialized.')
                    return
                except:
                    log.exception('Uncaught exception')
                    return

                ssl = bool(cfg.get('ssl', '0') == '1')
                port = int(cfg.get('port', '443' if ssl else '80'))
                key_path = cfg.get('key_path', '')
                crt_path = cfg.get('crt_path', '')
                unix_socket_path = cfg.get('unix_socket', None)

                if ssl:
                    if key_path == '' or crt_path == '':
                        log.error('SSL enabled in config, but missing key_path or crt_path')
                        return

                self.server = WebSocketServer(self, port, ssl, key_path, crt_path, unix_socket_path)
        except:
            log.exception('Uncaught exception in WebSocketManager')

    def emit(self, event, data={}):
        if self.server:
            payload = json.dumps({
                'event': event,
                'data': data,
                }).encode('utf8')
            for client in self.server.clients:
                client.sendMessage(payload, False)

    def on_log_message(message, isError=False, printed=False):
        if isError:
            log.error(message['message'])
        else:
            pass
            # log.debug('on_log_message({})'.format(message['message']))
{'fraction_non_alphanumeric': 0.09483344663494221, 'fraction_numerical': 0.003399048266485384, 'mean_word_length': 1.65234375, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 2, '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': '2240357', 'n_tokens_mistral': 2372, 'n_tokens_neox': 2216, 'n_words': 503}