[CalendarServer-changes] [8451] CalendarServer/trunk/calendarserver/tools/shell.py

source_changes at macosforge.org source_changes at macosforge.org
Wed Dec 14 18:05:00 PST 2011


Revision: 8451
          http://trac.macosforge.org/projects/calendarserver/changeset/8451
Author:   wsanchez at apple.com
Date:     2011-12-14 18:04:59 -0800 (Wed, 14 Dec 2011)
Log Message:
-----------
Start plumbing for tab completion.

Modified Paths:
--------------
    CalendarServer/trunk/calendarserver/tools/shell.py

Modified: CalendarServer/trunk/calendarserver/tools/shell.py
===================================================================
--- CalendarServer/trunk/calendarserver/tools/shell.py	2011-12-15 01:27:46 UTC (rev 8450)
+++ CalendarServer/trunk/calendarserver/tools/shell.py	2011-12-15 02:04:59 UTC (rev 8451)
@@ -162,6 +162,7 @@
         self.keyHandlers['\x04'] = self.handle_EOF   # Control-D
         self.keyHandlers['\x1c'] = self.handle_QUIT  # Control-\
         self.keyHandlers['\x0c'] = self.handle_FF    # Control-L
+       #self.keyHandlers['\t'  ] = self.handle_TAB   # Tab
 
         if self.emulate == EMULATE_EMACS:
             # EMACS key bindinds
@@ -207,15 +208,38 @@
     def handle_QUIT(self):
         self.exit()
 
+    def handle_TAB(self):
+        # Tokenize the text before the cursor
+        tokens = self.tokenize("".join(self.lineBuffer[:self.lineBufferIndex]))
+
+        if tokens:
+            cmd = tokens.pop(0)
+
+            if tokens:
+                # Completing arguments
+
+                m = getattr(self, "complete_%s" % (cmd,), None)
+                if not m:
+                    return
+                completions = m(tokens)
+            else:
+                # Completing command name
+
+                completions = set()
+                for name, m in self.commands():
+                    if name.startswith(cmd):
+                        completions.add(name)
+        else:
+            # Completing command names
+            pass
+
+        log.msg("TAB: %r :: %r" % ("".join(self.lineBuffer), completions))
+
     def exit(self):
         self.terminal.loseConnection()
         self.service.reactor.stop()
 
-    def lineReceived(self, line):
-        if self.activeCommand is not None:
-            self.inputLines.append(line)
-            return
-
+    def tokenize(self, line):
         lexer = shlex(line)
         lexer.whitespace_split = True
 
@@ -226,6 +250,15 @@
                 break
             tokens.append(token)
 
+        return tokens
+
+    def lineReceived(self, line):
+        if self.activeCommand is not None:
+            self.inputLines.append(line)
+            return
+
+        tokens = self.tokenize(line)
+
         if tokens:
             cmd = tokens.pop(0)
             #print "Arguments: %r" % (tokens,)
@@ -282,6 +315,13 @@
         else:
             returnValue((self.wd,))
 
+    def commands(self):
+        for attr in dir(self):
+            if attr.startswith("cmd_"):
+                m = getattr(self, attr)
+                if not hasattr(m, "hidden"):
+                    yield (attr[4:], m)
+
     def cmd_help(self, tokens):
         """
         Show help.
@@ -326,28 +366,20 @@
             result = []
             max_len = 0
 
-            for attr in dir(self):
-                if attr.startswith("cmd_"):
-                    m = getattr(self, attr)
+            for name, m in self.commands():
+                for line in m.__doc__.split("\n"):
+                    line = line.strip()
+                    if line:
+                        doc = line
+                        break
+                else:
+                    doc = "(no info available)"
 
-                    if hasattr(m, "hidden"):
-                        continue
+                if len(name) > max_len:
+                    max_len = len(name)
 
-                    for line in m.__doc__.split("\n"):
-                        line = line.strip()
-                        if line:
-                            doc = line
-                            break
-                    else:
-                        doc = "(no info available)"
+                result.append((name, doc))
 
-                    name = attr[4:]
-
-                    if len(name) > max_len:
-                        max_len = len(name)
-
-                    result.append((name, doc))
-
             format = "  %%%ds - %%s\n" % (max_len,)
 
             for info in sorted(result):
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.macosforge.org/pipermail/calendarserver-changes/attachments/20111214/6b6e6915/attachment.html>


More information about the calendarserver-changes mailing list