Created
April 17, 2012 09:51
-
-
Save bgnori/2404972 to your computer and use it in GitHub Desktop.
manhole sample.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# | |
# from http://hinoglu.blogspot.jp/2009/08/code-completion-in-twisted-manhole.html | |
# added import string. | |
# | |
import string | |
from twisted.internet import reactor | |
from twisted.cred import portal, checkers | |
from twisted.conch import manhole, manhole_ssh | |
from rlcompleter import Completer | |
class EnhancedColoredManhole(manhole.ColoredManhole): | |
def find_common(self, l): | |
''' find common parts in thelist items | |
ex: 'ab' for ['abcd','abce','abf'] | |
requires an ordered list | |
''' | |
if len(l) == 1: | |
return l[0] | |
init = l[0] | |
for item in l[1:]: | |
for i, (x,y) in enumerate(zip(init, item)): | |
if x != y: | |
init = "".join(init[:i]) | |
break | |
if not init: | |
return None | |
return init | |
def handle_TAB(self): | |
necessarypart = "".join(self.lineBuffer).split(' ')[-1] | |
completer = Completer(globals()) | |
if completer.complete(necessarypart, 0): | |
matches = list(set(completer.matches)) # has multiples | |
if len(matches) == 1: | |
length = len(necessarypart) | |
self.lineBuffer = self.lineBuffer[:-length] | |
self.lineBuffer.extend(matches[0]) | |
self.lineBufferIndex = len(self.lineBuffer) | |
else: | |
matches.sort() | |
commons = self.find_common(matches) | |
if commons: | |
length = len(necessarypart) | |
self.lineBuffer = self.lineBuffer[:-length] | |
self.lineBuffer.extend(commons) | |
self.lineBufferIndex = len(self.lineBuffer) | |
self.terminal.nextLine() | |
while matches: | |
matches, part = matches[4:], matches[:4] | |
for item in part: | |
self.terminal.write('%s' % item.ljust(30)) | |
self.terminal.write('\n') | |
self.terminal.nextLine() | |
self.terminal.eraseLine() | |
self.terminal.cursorBackward(self.lineBufferIndex + 5) | |
self.terminal.write("%s %s" % (self.ps[self.pn], "".join(self.lineBuffer))) | |
def keystrokeReceived(self, keyID, modifier): | |
self.keyHandlers.update({'\b':self.handle_BACKSPACE}) # my terminal needed this | |
m = self.keyHandlers.get(keyID) | |
if m is not None: | |
m() | |
elif keyID in string.printable: | |
self.characterReceived(keyID, False) | |
def getManholeFactory(namespace): | |
realm = manhole_ssh.TerminalRealm() | |
def getManhole(_): | |
return EnhancedColoredManhole(namespace) | |
realm.chainedProtocolFactory.protocolFactory = getManhole | |
p = portal.Portal(realm) | |
p.registerChecker( | |
checkers.InMemoryUsernamePasswordDatabaseDontUse(admin='foobar')) | |
f = manhole_ssh.ConchFactory(p) | |
return f | |
if __name__ == "__main__": | |
reactor.listenTCP(2222, getManholeFactory(globals())) | |
reactor.run() | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Twisted==12.0.0 | |
pyasn1==0.1.2 | |
pycrypto==2.5 | |
zope.interface==3.8.0 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
python version 2.6.6 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment