[tor-commits] [nyx/master] Added InterpreterClosed exception Added /help and /quit commands

atagar at torproject.org atagar at torproject.org
Sun Jul 31 23:32:40 UTC 2016


commit 8fbe69eacb90e4a133edd82a9ad4d9d055248c50
Author: Sambuddha Basu <sambuddhabasu1 at gmail.com>
Date:   Sun Jun 19 19:21:36 2016 -0700

    Added InterpreterClosed exception Added /help and /quit commands
---
 nyx/panel/interpreter.py | 15 +++++++++------
 nyx/tor_interpreter.py   | 43 ++++++++++++++++++++++++++++++++++---------
 2 files changed, 43 insertions(+), 15 deletions(-)

diff --git a/nyx/panel/interpreter.py b/nyx/panel/interpreter.py
index 2769529..2e3ec0d 100644
--- a/nyx/panel/interpreter.py
+++ b/nyx/panel/interpreter.py
@@ -7,7 +7,7 @@ import nyx.controller
 import nyx.curses
 
 from nyx.curses import GREEN, CYAN, BOLD, HIGHLIGHT
-from nyx.tor_interpreter import handle_query
+from nyx.tor_interpreter import handle_query, InterpreterClosed
 from nyx import panel
 
 
@@ -38,11 +38,14 @@ class InterpreterPanel(panel.Panel):
         if not user_input:
           is_done = True
         else:
-          input_entry, output_entry = handle_query(user_input)
-          input_entry.insert(0, (PROMPT, GREEN, BOLD))
-          PROMPT_LINE.insert(len(PROMPT_LINE) - 1, input_entry)
-          for line in output_entry:
-            PROMPT_LINE.insert(len(PROMPT_LINE) - 1, [line])
+          try:
+            input_entry, output_entry = handle_query(user_input)
+            input_entry.insert(0, (PROMPT, GREEN, BOLD))
+            PROMPT_LINE.insert(len(PROMPT_LINE) - 1, input_entry)
+            for line in output_entry:
+              PROMPT_LINE.insert(len(PROMPT_LINE) - 1, [line])
+          except InterpreterClosed:
+            is_done = True
 
         if is_done:
           self._is_input_mode = False
diff --git a/nyx/tor_interpreter.py b/nyx/tor_interpreter.py
index 64d6554..bf94403 100644
--- a/nyx/tor_interpreter.py
+++ b/nyx/tor_interpreter.py
@@ -1,8 +1,19 @@
-from nyx.curses import GREEN, CYAN, BOLD, HIGHLIGHT
+from nyx.curses import GREEN, CYAN, RED, MAGENTA, BOLD, HIGHLIGHT
 from nyx import tor_controller
 
 
 def handle_query(user_input):
+  """
+  Processes the given input. Requests starting with a '/' are special
+  commands to the interpretor, and anything else is sent to the control port.
+  This returns an input/output tuple, each entry being a list of lines, each
+  line having a list of (msg, format) tuples for the content to be displayed.
+  This raises a InterpretorClosed if the interpretor should be shut down.
+
+  Arguments:
+    user_input - user input to be processed
+  """
+
   user_input = user_input.strip()
 
   input_entry, output_entry = [], []
@@ -10,14 +21,28 @@ def handle_query(user_input):
   if " " in user_input: cmd, arg = user_input.split(" ", 1)
   else: cmd, arg = user_input, ""
 
-  cmd = cmd.upper()
-  input_entry.append((cmd + " ", GREEN, BOLD))
-  if arg:
-    input_entry.append((arg, CYAN, BOLD))
+  if cmd.startswith("/"):
+    input_entry.append((cmd, MAGENTA, BOLD))
+    if cmd == "/quit": raise InterpreterClosed()
+    else:
+      output_entry.append(("Not yet implemented...", RED, BOLD))
+  else:
+    cmd = cmd.upper()
+    input_entry.append((cmd + " ", GREEN, BOLD))
+    if arg:
+      input_entry.append((arg, CYAN, BOLD))
 
-  if cmd == "GETINFO":
-    resp = tor_controller().get_info(arg)
-    for line in resp.split('\n'):
-      output_entry.append((line, CYAN,))
+    if cmd == "GETINFO":
+      resp = tor_controller().get_info(arg)
+      for line in resp.split('\n'):
+        output_entry.append((line, CYAN,))
     
   return input_entry, output_entry
+
+
+class InterpreterClosed(Exception):
+  """
+  Exception raised when the interpreter should be shut down.
+  """
+
+  pass





More information about the tor-commits mailing list