def __init__(self, dispatcher, connection): super(ConsoleChannel, self).__init__(dispatcher, connection) self.number = self.counter.increment() self.buffer = ByteBuffer() self.setup_console()
class ConsoleChannel(AsyncChannel): counter = Counter() def __init__(self, dispatcher, connection): super(ConsoleChannel, self).__init__(dispatcher, connection) self.number = self.counter.increment() self.buffer = ByteBuffer() self.setup_console() def setup_console(self): self.namespace = {'loadtools': self.loadtools} self.console = InteractiveSession(self) self.set_terminator('\r\n') self.console.start() self.initprompt() def initprompt(self): banner = [sys.version] banner.append("%s" % self.dispatcher) banner.append(str(self.dispatcher)) banner.append(str(self)) banner.append('<loadtools() will add standard tools>') self.console.prompt("\n".join(banner)) def push(self, data): producer = SimpleProducer(data) producer = ByteProducer(producer) producer = QuoteProducer(producer) producer = LineProducer(producer) return self.push_with_producer(producer) def loadtools(self): from mpx.lib import msglog from mpx.lib.node import as_node from mpx.lib.node import as_node_url self.namespace['msglog'] = msglog self.namespace['as_node'] = as_node self.namespace['root'] = as_node('/') self.namespace['as_node_url'] = as_node_url def handle_connect(self): pass def handle_expt(self): self.debugout('%s handling exceptional event.', self, level=0) self.close() def handle_error(self): self.debugout('%s closing due to exception.', self, level=0) msglog.exception(prefix='handled') self.close() def close(self): self.console.stop() super(ConsoleChannel, self).close() self.debugout('%s closed and removed.', self, level=1) def recv(self, buffer_size): data = super(ConsoleChannel, self).recv(buffer_size) unquoted = urllib.unquote(data) self.debugout('%s << %r (%r)', self, data, unquoted, level=2) return data def send(self, data): result = super(ConsoleChannel, self).send(data) unquoted = urllib.unquote(data) self.debugout('%s >> %r (%r)', self, data, unquoted, level=2) return result def collect_incoming_data(self, bytes): self.buffer.write(bytes) def found_terminator(self): quoted = self.buffer.read() command = urllib.unquote(quoted) self.debugout('%s console.handle(%r)', self, command, level=1) self.console.handle(urllib.unquote(command)) def __str__(self): status = [type(self).__name__] status.append('#%03d' % self.number) return ' '.join(status) def __repr__(self): status = [str(self)] return '<%s at %#x>' % (status, id(self)) def debugout(self, message, *args, **kw): self.dispatcher.debugout(message, *args, **kw)
class ConsoleChannel(AsyncChannel): counter = Counter() def __init__(self, dispatcher, connection): super(ConsoleChannel, self).__init__(dispatcher, connection) self.number = self.counter.increment() self.buffer = ByteBuffer() self.setup_console() def setup_console(self): self.namespace = {'loadtools': self.loadtools} self.console = InteractiveSession(self) self.set_terminator('\r\n') self.console.start() self.initprompt() def initprompt(self): banner = [sys.version] banner.append("%s" % self.dispatcher) banner.append(str(self.dispatcher)) banner.append(str(self)) banner.append('<loadtools() will add standard tools>') self.console.prompt("\n".join(banner)) def push(self, data): producer = SimpleProducer(data) producer = ByteProducer(producer) producer = QuoteProducer(producer) producer = LineProducer(producer) return self.push_with_producer(producer) def loadtools(self): from mpx.lib import msglog from mpx.lib.node import as_node from mpx.lib.node import as_node_url self.namespace['msglog'] = msglog self.namespace['as_node'] = as_node self.namespace['root'] = as_node('/') self.namespace['as_node_url'] = as_node_url def handle_connect(self): pass def handle_expt(self): self.debugout('%s handling exceptional event.', self, level=0) self.close() def handle_error(self): self.debugout('%s closing due to exception.', self, level=0) msglog.exception(prefix = 'handled') self.close() def close(self): self.console.stop() super(ConsoleChannel, self).close() self.debugout('%s closed and removed.', self, level=1) def recv(self, buffer_size): data = super(ConsoleChannel, self).recv(buffer_size) unquoted = urllib.unquote(data) self.debugout('%s << %r (%r)', self, data, unquoted, level=2) return data def send(self, data): result = super(ConsoleChannel, self).send(data) unquoted = urllib.unquote(data) self.debugout('%s >> %r (%r)', self, data, unquoted, level=2) return result def collect_incoming_data(self, bytes): self.buffer.write(bytes) def found_terminator(self): quoted = self.buffer.read() command = urllib.unquote(quoted) self.debugout('%s console.handle(%r)', self, command, level=1) self.console.handle(urllib.unquote(command)) def __str__(self): status = [type(self).__name__] status.append('#%03d' % self.number) return ' '.join(status) def __repr__(self): status = [str(self)] return '<%s at %#x>' % (status, id(self)) def debugout(self, message, *args, **kw): self.dispatcher.debugout(message, *args, **kw)