[Author Prev][Author Next][Thread Prev][Thread Next][Author Index][Thread Index]

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



commit 42fee2eceb349dc04288b0b49a2a5dac17578878
Author: Damian Johnson <atagar@xxxxxxxxxxxxxx>
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)



_______________________________________________
tor-commits mailing list
tor-commits@xxxxxxxxxxxxxxxxxxxx
https://lists.torproject.org/cgi-bin/mailman/listinfo/tor-commits