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

source_changes at macosforge.org source_changes at macosforge.org
Mon Jun 27 15:16:18 PDT 2011


Revision: 7678
          http://trac.macosforge.org/projects/calendarserver/changeset/7678
Author:   wsanchez at apple.com
Date:     2011-06-27 15:16:18 -0700 (Mon, 27 Jun 2011)
Log Message:
-----------
Add tokenizer, command dispatch, start of heirarchy

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

Modified: CalendarServer/trunk/calendarserver/tools/shell.py
===================================================================
--- CalendarServer/trunk/calendarserver/tools/shell.py	2011-06-27 20:38:02 UTC (rev 7677)
+++ CalendarServer/trunk/calendarserver/tools/shell.py	2011-06-27 22:16:18 UTC (rev 7678)
@@ -22,6 +22,7 @@
 
 import os
 import sys
+from shlex import shlex
 
 #from twisted.python import log
 from twisted.python.text import wordWrap
@@ -92,6 +93,25 @@
         """
 
 
+class Directory(object):
+    """
+    Location in virtual data hierarchy.
+    """
+    def __init__(self, path):
+        self.path = path
+
+    def __str__(self):
+        return "/" + "/".join(self.path)
+
+
+class RootDirectory(Directory):
+    """
+    Root of virtual data hierarchy.
+    """
+    def __init__(self):
+        Directory.__init__(self, ())
+
+
 class ShellProtocol(HistoricRecvLine):
     """
     Data store shell protocol.
@@ -102,6 +122,7 @@
     # * Backspace transposes characters in the terminal.
 
     ps = ("ds% ", "... ")
+    wd = RootDirectory()
 
     def connectionMade(self):
         HistoricRecvLine.connectionMade(self)
@@ -116,6 +137,8 @@
         self.keyHandlers[CTRL_L        ] = self.handle_FF
         self.keyHandlers[CTRL_BACKSLASH] = self.handle_QUIT
 
+        wd = RootDirectory()
+
     def handle_INT(self):
         """
         Handle ^C as an interrupt keystroke by resetting the current input
@@ -150,9 +173,31 @@
         self.terminal.loseConnection()
 
     def lineReceived(self, line):
-        print "---> %s" % (line,)
+        print "-> %s" % (line,)
 
+        lexer = shlex(line)
+        tokens = []
+        while True:
+            token = lexer.get_token()
+            if not token:
+                break
+            tokens.append(token)
 
+        if tokens:
+            cmd = tokens.pop()
+            m = getattr(self, "cmd_%s" % (cmd,), None)
+            if m:
+                m(tokens)
+            else:
+                print "Unknown command: %s" % (cmd,)
+
+    def cmd_pwd(self, tokens):
+        if tokens:
+            print "Unknown arguments: %s" % (tokens,)
+            return
+        print self.wd
+
+
 def main(argv=sys.argv, stderr=sys.stderr, reactor=None):
     """
     Do the export.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.macosforge.org/pipermail/calendarserver-changes/attachments/20110627/bd9145e6/attachment.html>


More information about the calendarserver-changes mailing list