I like Python's Twisted and Cmd. I want to use them together.
I got some things working, but so far I haven't figured out how to make tab-completion work, because I don't see how to receive tab keypres events right away (without pressing Enter) in Twisted's LineReceiver.
Here's my code so far:
#!/usr/bin/env python
from cmd import Cmd
from twisted.internet import reactor
from twisted.internet.stdio import StandardIO
from twisted.protocols.basic import LineReceiver
class CommandProcessor(Cmd):
def do_EOF(self, line):
return True
class LineProcessor(LineReceiver):
from os import linesep as delimiter # makes newline work
def __init__(self):
self.processor = CommandProcessor()
self.setRawMode()
def connectionMade(self):
self.transport.write('>>> ')
def rawDataReceived(self, data):
self.processor.onecmd(data)
self.transport.write('>>> ')
StandardIO(LineProcessor())
reactor.run()
Apart from tab completion, this somewhat works. I can enter a command like "help" and the Cmd module will print the results. But I've lost the nifty tab-complete functionality of the Cmd module, because Twisted is buffering one line at a time. I tried setting LineProcessor.delimiter
to the empty string, to no avail. Maybe I need to find some other piece of Twisted to use instead of LineReceiver? Or maybe there's a simpler approach that will avoid my having to process every character one-by-one?
I can't use Cmd alone, because I want to make this a network application, where some commands will result in sending data, and receiving data from the network will happen asynchronously (and be displayed to the user).
So whether we start from the above code or something completely different, I'd like to build a nice, friendly terminal application in Python that responds to network events and also to tab completion. I hope I can use what's already out there and not have to implement too much myself.