[tor-commits] [arm/master] Interpretor function for writing backlog

atagar at torproject.org atagar at torproject.org
Wed Aug 31 03:48:11 UTC 2011


commit 42fee2eceb349dc04288b0b49a2a5dac17578878
Author: Damian Johnson <atagar at torproject.org>
Date:   Tue Aug 30 19:09:55 2011 -0700

    Interpretor function for writing backlog
    
    Implementing interpretor '/write [path]' function. This simply dumps the
    backlog to the given path. If no path is supplied then it uses the last
    location written to.
---
 src/util/torInterpretor.py |   33 +++++++++++++++++++++++++++++++--
 1 files changed, 31 insertions(+), 2 deletions(-)

diff --git a/src/util/torInterpretor.py b/src/util/torInterpretor.py
index 266e9e4..0be511d 100644
--- a/src/util/torInterpretor.py
+++ b/src/util/torInterpretor.py
@@ -4,7 +4,7 @@ adds usability features like IRC style interpretor commands and, when ran
 directly, history and tab completion.
 """
 
-import readline # simply importing this provides history to raw_input
+import readline
 
 import version
 
@@ -193,6 +193,7 @@ class ControlInterpretor:
   def __init__(self):
     self.backlog = []   # prior requests the user has made
     self.contents = []  # (msg, format list) tuples for what's been displayed
+    self.lastWritePath = "/tmp/torInterpretor_output"
   
   def getBacklog(self):
     """
@@ -215,6 +216,24 @@ class ControlInterpretor:
       return self.contents + [appendPrompt]
     else: return self.contents
   
+  def writeContents(self, path):
+    """
+    Attempts to write the display contents to a given path, raising an IOError
+    if unsuccessful.
+    
+    Arguments:
+      path - location to write the interpretor content to
+    """
+    
+    outputLines = []
+    
+    for line in self.contents:
+      outputLines.append("".join([msg for msg, _ in line]))
+    
+    outputFile = open(path, "w")
+    outputFile.write("\n".join(outputLines))
+    outputFile.close()
+  
   def handleQuery(self, input):
     """
     Processes the given input. Requests starting with a '/' are special
@@ -250,11 +269,21 @@ class ControlInterpretor:
       
       if input == "/quit":
         raise InterpretorClosed()
+      elif input.startswith("/write"):
+        if " " in input: writePath = input.split(" ", 1)[1]
+        else: writePath = self.lastWritePath
+        
+        try:
+          self.writeContents(writePath)
+          outputEntry.append(("Interpretor backlog written to: %s" % writePath, OUTPUT_FORMAT))
+        except IOError, exc:
+          outputEntry.append(("Unable to write to '%s': %s" % (writePath, exc), ERROR_FORMAT))
+        
+        self.lastWritePath = writePath
       else:
         outputEntry.append(("Not yet implemented...", ERROR_FORMAT)) # TODO: implement
       
       # TODO: add /help option
-      # TODO: add /write option
     else:
       # controller command
       if " " in input: cmd, arg = input.split(" ", 1)





More information about the tor-commits mailing list