Esempio n. 1
0
File: cli.py Progetto: anthonyt/3ad
 def __init__(self, namespace=None):
     ColoredManhole.__init__(self, namespace)
     self.completer = Completer(namespace=self.namespace)
     self.completeState = 0
     self.lastTabbed = ''
     self.lastSuggested = ''
     self.sem = defer.DeferredLock()
     self.enabled = True
Esempio n. 2
0
 def __init__(self,namespace=None,global_namespace=None):
   Completer.__init__(self, namespace, global_namespace )
   self.matchers = [ self.global_matches ]
Esempio n. 3
0
 def __init__(self,namespace=None,global_namespace=None):
   Completer.__init__(self, namespace, global_namespace )
   self.matchers = [ self.global_matches ]
Esempio n. 4
0
File: cli.py Progetto: anthonyt/3ad
class ConsoleManhole(ColoredManhole):
    """
    Adapted from this awesome mailing list entry:
    http://twistedmatrix.com/pipermail/twisted-python/2009-January/019003.html
    """
    def __init__(self, namespace=None):
        ColoredManhole.__init__(self, namespace)
        self.completer = Completer(namespace=self.namespace)
        self.completeState = 0
        self.lastTabbed = ''
        self.lastSuggested = ''
        self.sem = defer.DeferredLock()
        self.enabled = True

    def connectionLost(self, reason):
        ColoredManhole.connectionLost(self, reason)
        reactor.stop()
        print ""

    def connectionMade(self):
        ColoredManhole.connectionMade(self)
        self.interpreter = ConsoleManholeInterpreter(self, self.namespace)

    def disableInput(self):
        #print "Disabling\r"
        self.enabled = False

    def enableInput(self):
        #print "Enabling\r"
        self.enabled = True

    def lineReceived(self, line):
        #print "\r"
        def fn():
            df = defer.Deferred()
            def fn2():
                #print "Running lineReceived\r"
                a = ColoredManhole.lineReceived(self, line)
                df.callback(a)

            reactor.callLater(0, fn2)
            return df

        self.sem.run(fn)

    def keystrokeReceived(self, keyID, modifier):
        if keyID in self.keyHandlers or self.enabled:
            ColoredManhole.keystrokeReceived(self, keyID, modifier)
        else:
            #print "ignoring\r"
            pass

    def handle_TAB(self):
        s = "".join(self.lineBuffer)

        if s == self.lastTabbed + self.lastSuggested:
            # If the user has typed nothing since the last tab,
            # erase the last suggestion
            for i in range(0, len(self.lastSuggested)):
                self.handle_BACKSPACE()

            s = s[:len(self.lastTabbed)]
        else:
            # if the user has typed something since the last tab,
            # keep the current text and reset the tab counter
            self.completeState = 0
            self.lastTabbed = s

        # When matching, match only against the last space-separated word.
        s = s.strip()
        try:
            s = s[s.rindex(' '):].strip()
        except ValueError:
            pass

        c = self.completer.complete(s, self.completeState)

        if c is None:
            c = ''
            self.completeState = 0
        else:
            self.completeState += 1
            c = c[len(s):]

        self.lastSuggested = c

        for ch in c:
            self.lineBuffer[self.lineBufferIndex:self.lineBufferIndex+1] = [ch]
            self.lineBufferIndex += len(ch)
            self.terminal.write(ch)