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

[tor-commits] [stem/master] Support SETEVENTS with tor-prompt's run argument



commit 9302fd23ab19cf2c294a376392525c88c3ef0436
Author: Damian Johnson <atagar@xxxxxxxxxxxxxx>
Date:   Wed Apr 19 10:43:28 2017 -0700

    Support SETEVENTS with tor-prompt's run argument
    
    Adding special handling for SETEVENTS so running...
    
      tor-prompt --run "SETEVENTS BW DEBUG"
    
    ... will listen for events until the user presses a key. These events can be
    piped to a file. Something like this was requested by anrelanos on...
    
      https://trac.torproject.org/projects/tor/ticket/21590
---
 stem/interpreter/__init__.py | 23 ++++++++++++++++++++++-
 1 file changed, 22 insertions(+), 1 deletion(-)

diff --git a/stem/interpreter/__init__.py b/stem/interpreter/__init__.py
index 8cdbb3c..033d819 100644
--- a/stem/interpreter/__init__.py
+++ b/stem/interpreter/__init__.py
@@ -126,7 +126,28 @@ def main():
     interpreter = stem.interpreter.commands.ControlInterpreter(controller)
 
     if args.run_cmd:
-      interpreter.run_command(args.run_cmd, print_response = True)
+      if args.run_cmd.upper().startswith('SETEVENTS '):
+        # TODO: we can use a lambda here when dropping python 2.x support, but
+        # until then print's status as a keyword prevents it from being used in
+        # lambdas
+
+        def handle_event(event_message):
+          print(format(str(event_message), *STANDARD_OUTPUT))
+
+        controller._handle_event = handle_event
+
+        if sys.stdout.isatty():
+          events = args.run_cmd.upper().split(' ', 1)[1]
+          print(format('Listening to %s events. Press any key to quit.\n' % events, *HEADER_BOLD_OUTPUT))
+
+        controller.msg(args.run_cmd)
+
+        try:
+          raw_input()
+        except (KeyboardInterrupt, stem.SocketClosed) as exc:
+          pass
+      else:
+        interpreter.run_command(args.run_cmd, print_response = True)
     elif args.run_path:
       try:
         for line in open(args.run_path).readlines():

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