[CalendarServer-changes] [9022] CalendarServer/trunk/calendarserver/tools/shell

source_changes at macosforge.org source_changes at macosforge.org
Tue Apr 10 15:36:06 PDT 2012


Revision: 9022
          http://trac.macosforge.org/projects/calendarserver/changeset/9022
Author:   wsanchez at apple.com
Date:     2012-04-10 15:36:06 -0700 (Tue, 10 Apr 2012)
Log Message:
-----------
More docs, tests.

complete_commands now completes hidden commands if no non-hidden
commands will complete.

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

Modified: CalendarServer/trunk/calendarserver/tools/shell/cmd.py
===================================================================
--- CalendarServer/trunk/calendarserver/tools/shell/cmd.py	2012-04-10 22:35:03 UTC (rev 9021)
+++ CalendarServer/trunk/calendarserver/tools/shell/cmd.py	2012-04-10 22:36:06 UTC (rev 9022)
@@ -66,6 +66,10 @@
 
     @inlineCallbacks
     def getTargets(self, tokens):
+        """
+        For each given C{token}, locate a File to operate on.
+        @return: iterable of File objects.
+        """
         if tokens:
             result = []
             for token in tokens:
@@ -74,26 +78,61 @@
         else:
             returnValue((self.wd,))
 
-    def commands(self):
+    def commands(self, showHidden=False):
+        """
+        @return: an iterable of C{(name, method)} tuples, where
+        C{name} is the name of the command and C{method} is the method
+        that implements it.
+        """
         for attr in dir(self):
             if attr.startswith("cmd_"):
                 m = getattr(self, attr)
-                if not hasattr(m, "hidden"):
+                if showHidden or not hasattr(m, "hidden"):
                     yield (attr[4:], m)
 
     @staticmethod
     def complete(word, items):
+        """
+        List completions for the given C{word} from the given
+        C{items}.
+
+        Completions are the remaining portions of words in C{items}
+        that start with C{word}.
+
+        For example, if C{"foobar"} and C{"foo"} are in C{items}, then
+        C{""} and C{"bar"} are completions when C{word} C{"foo"}.
+
+        @return: an iterable of completions.
+        """
         for item in items:
             if item.startswith(word):
                 yield item[len(word):]
 
     def complete_commands(self, word):
-        return self.complete(word, (name for name, method in self.commands()))
+        """
+        @return: an iterable of command name completions.
+        """
+        def complete(showHidden):
+            return self.complete(
+                word,
+                (name for name, method in self.commands(showHidden=showHidden))
+            )
 
+        completions = tuple(complete(False))
+
+        # If no completions are found, try hidden commands.
+        if not completions:
+            completions = complete(True)
+
+        return completions
+
     @inlineCallbacks
     def complete_files(self, tokens, filter=None):
+        """
+        @return: an iterable of C{File} path completions.
+        """
         if filter is None:
-            filter = lambda items: True
+            filter = lambda item: True
 
         files = (
             self.listEntryToString(item)
@@ -110,6 +149,10 @@
 
     @staticmethod
     def listEntryToString(entry):
+        """
+        Converts an entry returned by File.list() into a
+        user-displayable string.
+        """
         klass = entry[0]
         name  = entry[1]
 

Modified: CalendarServer/trunk/calendarserver/tools/shell/test/test_cmd.py
===================================================================
--- CalendarServer/trunk/calendarserver/tools/shell/test/test_cmd.py	2012-04-10 22:35:03 UTC (rev 9021)
+++ CalendarServer/trunk/calendarserver/tools/shell/test/test_cmd.py	2012-04-10 22:36:06 UTC (rev 9022)
@@ -25,7 +25,6 @@
 
 
 class TestCommandsBase(twisted.trial.unittest.TestCase):
-
     def setUp(self):
         self.protocol = ShellProtocol(None, commandsClass=CommandsBase)
         self.commands = self.protocol.commands
@@ -54,16 +53,10 @@
         allCommands = self.commands.commands()
         self.assertEquals(sorted(allCommands), [])
 
+        allCommands = self.commands.commands(showHidden=True)
+        self.assertEquals(sorted(allCommands), [])
+
     def test_commandsSome(self):
-        class SomeCommands(CommandsBase):
-            def cmd_a(self, tokens):
-                pass
-            def cmd_b(self, tokens):
-                pass
-            def cmd_hidden(self, tokens):
-                pass
-            cmd_hidden.hidden = "Hidden"
-
         protocol = ShellProtocol(None, commandsClass=SomeCommands)
         commands = protocol.commands
 
@@ -71,9 +64,24 @@
 
         self.assertEquals(
             sorted(allCommands),
-            [ ("a", commands.cmd_a), ("b", commands.cmd_b) ]
+            [
+                ("a", commands.cmd_a),
+                ("b", commands.cmd_b),
+            ]
         )
 
+        allCommands = commands.commands(showHidden=True)
+
+        self.assertEquals(
+            sorted(allCommands),
+            [
+                ("a", commands.cmd_a),
+                ("b", commands.cmd_b),
+                ("hidden", commands.cmd_hidden),
+            ]
+        )
+
+
     def test_complete(self):
         items = (
             "foo",
@@ -86,6 +94,7 @@
         def c(word):
             return sorted(CommandsBase.complete(word, items))
 
+        self.assertEquals(c(""       ), sorted(items))
         self.assertEquals(c("f"      ), ["oo", "oobar"])
         self.assertEquals(c("foo"    ), ["", "bar"])
         self.assertEquals(c("foobar" ), [""])
@@ -93,3 +102,25 @@
         self.assertEquals(c("baz"    ), [""])
         self.assertEquals(c("q"      ), ["uux"])
         self.assertEquals(c("xyzzy"  ), [])
+
+    def test_completeCommands(self):
+        protocol = ShellProtocol(None, commandsClass=SomeCommands)
+        commands = protocol.commands
+
+        def c(word):
+            return sorted(commands.complete_commands(word))
+
+        self.assertEquals(c("" ), ["a", "b"])
+        self.assertEquals(c("a"), [""])
+        self.assertEquals(c("h"), ["idden"])
+        self.assertEquals(c("f"), [])
+
+
+class SomeCommands(CommandsBase):
+    def cmd_a(self, tokens):
+        pass
+    def cmd_b(self, tokens):
+        pass
+    def cmd_hidden(self, tokens):
+        pass
+    cmd_hidden.hidden = "Hidden"
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.macosforge.org/pipermail/calendarserver-changes/attachments/20120410/cf9e605c/attachment-0001.html>


More information about the calendarserver-changes mailing list