<commit_before>#!/usr/bin/env python

import optparse
import traceback

from feather import servers, connections, requests


DEFAULT_HOST = ""
DEFAULT_PORT = 9000

connected = {}

class RequestHandler(requests.RequestHandler):
    def handle(self, request):
        self.connection.broadcast("%s: %s" % (
                self.connection.username, request.rstrip('\r\n')))
        return [], None


class ConnectionHandler(connections.TCPConnection):
    request_handler = RequestHandler

    def __init__(self, *args, **kwargs):
        super(ConnectionHandler, self).__init__(*args, **kwargs)
        self.sockfile = self.socket.makefile('r')

    def get_request(self):
        request = self.sockfile.readline()
        if not request:
            self.closing = True
            return None
        return request

    def setup(self):
        super(ConnectionHandler, self).setup()

        self.socket.sendall("** input your name (20 chars or less):\r\n")
        name = self.sockfile.readline().rstrip('\r\n')

        if len(name) > 20:
            self.closing = True
            self.socket.sendall("** nope sucka, that's too long.\r\n")
        elif name in connected:
            self.closing = True
            self.socket.sendall("** sorry, name %s is taken." % name)
        else:
            connected[name] = self
            self.username = name
            self.broadcast("** %s has entered the room." % name)

    def broadcast(self, msg):
        for username, connection in connected.items():
            if username != self.username:
                connection.socket.sendall(msg + "\r\n")

    def cleanup(self):
        self.broadcast("** %s has left the building." % self.username)
        super(ConnectionHandler, self).cleanup()
        del connected[self.username]

    def log_error(self, klass, exc, tb):
        traceback.print_exception(klass, exc, tb)


class ChatServer(servers.TCPServer):
    worker_count = 1
    connection_handler = ConnectionHandler


if __name__ == "__main__":
    parser = optparse.OptionParser()
    parser.add_option("-H", "--host", default=DEFAULT_HOST)
    parser.add_option("-P", "--port", type="int", default=DEFAULT_PORT)

    options, args = parser.parse_args()
    ChatServer((options.host, options.port)).serve()<commit_msg>fixes to the nc-chat example, and print out tracebacks in the future.<commit_after>#!/usr/bin/env python

import optparse
import traceback

from feather import servers, connections, requests
import greenhouse


DEFAULT_HOST = ""
DEFAULT_PORT = 9000

connected = {}

class RequestHandler(requests.RequestHandler):
    def handle(self, request):
        self.connection.broadcast("%s: %s" % (
                self.connection.username, request.rstrip('\r\n')))
        return [], None


class ConnectionHandler(connections.TCPConnection):
    request_handler = RequestHandler

    def __init__(self, *args, **kwargs):
        super(ConnectionHandler, self).__init__(*args, **kwargs)
        self.sockfile = self.socket.makefile('r')

    def get_request(self):
        request = self.sockfile.readline()
        if not request:
            self.closing = True
            return None
        return request

    def setup(self):
        super(ConnectionHandler, self).setup()

        self.socket.sendall("** input your name (20 chars or less):\r\n")
        name = self.sockfile.readline().rstrip('\r\n')

        if len(name) > 20:
            self.closing = True
            self.socket.sendall("** nope sucka, that's too long.\r\n")
        elif name in connected:
            self.closing = True
            self.socket.sendall("** sorry, name %s is taken.\r\n" % name)
        else:
            connected[name] = self
            self.username = name
            self.broadcast("** %s has entered the room." % name)

    def broadcast(self, msg):
        for username, connection in connected.items():
            if username != self.username:
                connection.socket.sendall(msg + "\r\n")

    def cleanup(self):
        if hasattr(self, "username"):
            self.broadcast("** %s has left the building." % self.username)
        self.sockfile.close()
        super(ConnectionHandler, self).cleanup()
        del connected[self.username]

    def log_error(self, klass, exc, tb):
        traceback.print_exception(klass, exc, tb)


class ChatServer(servers.TCPServer):
    worker_count = 1
    connection_handler = ConnectionHandler


if __name__ == "__main__":
    greenhouse.add_exception_handler(traceback.print_exception)
    parser = optparse.OptionParser()
    parser.add_option("-H", "--host", default=DEFAULT_HOST)
    parser.add_option("-P", "--port", type="int", default=DEFAULT_PORT)

    options, args = parser.parse_args()
    ChatServer((options.host, options.port)).serve()
{'fraction_non_alphanumeric': 0.12567736556898707, 'fraction_numerical': 0.003751563151313047, 'mean_word_length': 2.649429657794677, 'pattern_counts': {'":': 2, '<': 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': '6496216', 'n_tokens_mistral': 1392, 'n_tokens_neox': 1349, 'n_words': 337}