<commit_before>"""
#. Runs a websocket server that talks to the beatlounge console.

built on https://github.com/rlotun/txWebSocket
"""

import argparse
import StringIO, sys, time
from datetime import datetime

from twisted.internet.protocol import Protocol, Factory
from twisted.python import log
from twisted.web import resource
from twisted.web.static import File
from twisted.internet import task
from twisted.conch.manhole import ManholeInterpreter

from websocket import WebSocketHandler, WebSocketSite

import pprint
pp = pprint.PrettyPrinter(indent=2)
pp = pp.pprint


class CodeHandler(WebSocketHandler):

    special_messages_to_method = {'restart': 'restart'}

    def __init__(self, transport):
        WebSocketHandler.__init__(self, transport)
        self.startInterpreter()

    def startInterpreter(self):
        self.interpreter = ManholeInterpreter(self)
        self.interpreter.runcode("""
from txbeatlounge.scheduler import clock as reactor
reactor.synthAudioDevice = "coreaudio"
reactor.run()
#from comps.core import *
import pprint
pp = pprint.PrettyPrinter(indent=2)
pp = pp.pprint
""")

    def __del__(self):
        print 'Deleting handler'

    def frameReceived(self, frame):
        """ """
        #print 'Peer: ', self.transport.getPeer()

        if not self.interpreter:
            self.startInterpreter()

        if frame in self.special_messages_to_method.keys():
            getattr(self, self.special_messages_to_method[frame])()
        else:
            self.interpreter.runcode(frame)

    def addOutput(self, data, async=False):
        self.transport.write(data)
        #log.msg(data)

    def connectionMade(self):
        print 'Connected to client.'

    def connectionLost(self, reason):
        print 'Lost connection.', reason
        reason.printTraceback()

    def restart(self):
        """Method that doesn't work @drew TODO :)
        """
        self.interpreter.runcode("reactor.task.stop()")
        self.interpreter.runcode("""
from txbeatlounge.instrument.fsynth import ChordPlayerMixin
l = locals()
for v in l.values():
    if isinstance(v, ChordPlayerMixin):
        v.stopall()

""")
        self.interpreter = None
        #time.sleep(3)
        #self.startInterpreter()



if __name__ == "__main__":

    # Note, the port will need to be changed in the index.html
    parser = argparse.ArgumentParser()
    parser.add_argument('-p', default=8080)
    args = parser.parse_args()
    port = int(args.p)


    from twisted.internet import reactor
    log.startLogging(sys.stdout)

    root = File('.')
    site = WebSocketSite(root)
    site.addHandler('/code', CodeHandler)
    reactor.listenTCP(port, site, interface='127.0.0.1')
    reactor.run()

<commit_msg>some tweaks to web.server: start clock in main rather than on connection, update interpreter namespace with console.consoleNamespace, convert print stmts to log.msg<commit_after>"""
#. Runs a websocket server that talks to the beatlounge console.

built on https://github.com/rlotun/txWebSocket
"""

import argparse
import StringIO, sys, time
from datetime import datetime
import pprint

from twisted.internet.protocol import Protocol, Factory
from twisted.python import log
from twisted.web import resource
from twisted.web.static import File
from twisted.internet import task
from twisted.conch.manhole import ManholeInterpreter

from websocket import WebSocketHandler, WebSocketSite

from txbeatlounge.console import consoleNamespace

class CodeHandler(WebSocketHandler):

    special_messages_to_method = {'restart': 'restart'}

    def __init__(self, transport):
        WebSocketHandler.__init__(self, transport)
        self.startInterpreter()

    def startInterpreter(self):
        log.msg('starting interpreter')
        self.interpreter = ManholeInterpreter(self)
        self.interpreter.locals = dict(consoleNamespace)
        pp = pprint.PrettyPrinter(indent=2)
        pp = pp.pprint
        self.interpreter.locals['pp'] = pp

    def __del__(self):
        log.msg('Deleting handler')

    def frameReceived(self, frame):
        """ """
        #print 'Peer: ', self.transport.getPeer()

        if not self.interpreter:
            self.startInterpreter()

        if frame in self.special_messages_to_method.keys():
            getattr(self, self.special_messages_to_method[frame])()
        else:
            self.interpreter.runcode(frame)

    def addOutput(self, data, async=False):
        self.transport.write(data)
        #log.msg(data)

    def connectionMade(self):
        log.msg('Connected to client.')

    def connectionLost(self, reason):
        log.msg('Lost connection.')
        log.err(reason)

    def restart(self):
        """
        Method that doesn't work @drew TODO :)

        I'm working on it ... sort of (in my head now) - but i think the right thing to
        do is have some global registry of players and scheduled events and then stop
        everything in the registry - players, then scheduled events for plain tasks. (drew)
        """

        # NOTE
        # please don't alias clock as "reactor" anymore - i'm sorry i did
        # that before - eventually BeatClock probably shouldn't even inherit
        # from SelectReactor etc. and this get's confusing because it's hard to
        # tell one from the other in code - they're not the same (e.g. Twisted's
        # global singleton reactor and our custom reactor - BeatClock)

        #self.interpreter.runcode("reactor.task.stop()")
        #self.interpreter.runcode("""
#from txbeatlounge.instrument.fsynth import ChordPlayerMixin
#l = locals()
#for v in l.values():
#    if isinstance(v, ChordPlayerMixin):
#        v.stopall()
#
#""")
#        self.interpreter = None
        #time.sleep(3)
        #self.startInterpreter()



if __name__ == "__main__":
    from twisted.internet import reactor
    from txbeatlounge.scheduler import clock
    
    # Note, the port will need to be changed in the index.html
    parser = argparse.ArgumentParser()
    parser.add_argument('-p', default=8080)
    args = parser.parse_args()
    port = int(args.p)

    log.startLogging(sys.stdout)

    root = File('.')
    site = WebSocketSite(root)
    site.addHandler('/code', CodeHandler)
    reactor.listenTCP(port, site, interface='127.0.0.1')
    reactor.callWhenRunning(clock.run)
    reactor.run()


{'fraction_non_alphanumeric': 0.10167087011349306, 'fraction_numerical': 0.003940731399747793, 'mean_word_length': 3.1228070175438596, 'pattern_counts': {'":': 2, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 2, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 2, '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': '1331295', 'n_tokens_mistral': 1888, 'n_tokens_neox': 1838, 'n_words': 512}