commit eed49b22e06b9a0ea549c68a23e3cb60a4ad7e8d Author: Damian Johnson atagar@torproject.org Date: Sun Apr 6 13:46:56 2014 -0700
Basic interpretor prompt
Passing raw input to the control port and presenting the result. --- stem/interpretor/__init__.py | 24 +++++++++++++++++++++++- stem/interpretor/arguments.py | 14 +++++++++----- 2 files changed, 32 insertions(+), 6 deletions(-)
diff --git a/stem/interpretor/__init__.py b/stem/interpretor/__init__.py index ffe0012..72711c0 100644 --- a/stem/interpretor/__init__.py +++ b/stem/interpretor/__init__.py @@ -12,8 +12,23 @@ import sys
import stem.connection import stem.interpretor.arguments +import stem.prereq + +from stem.util.term import Attr, Color, format + +# We can only present a color prompt with python 2.7 or later... +# +# http://bugs.python.org/issue12972 + +if stem.prereq.is_python_27(): + PROMPT = format(">>> ", Color.GREEN, Attr.BOLD) +else: + PROMPT = ">>> " +
def main(): + import readline + try: args = stem.interpretor.arguments.parse(sys.argv[1:]) except ValueError as exc: @@ -30,10 +45,17 @@ def main(): controller = stem.connection.connect( control_port = control_port, control_socket = control_socket, + password_prompt = True, )
if controller is None: sys.exit(1)
with controller: - print controller.get_version() + while True: + try: + user_input = raw_input(PROMPT) + print controller.msg(user_input) + except KeyboardInterrupt as exc: + print # move cursor to the following line + break diff --git a/stem/interpretor/arguments.py b/stem/interpretor/arguments.py index f7feac4..bd6655d 100644 --- a/stem/interpretor/arguments.py +++ b/stem/interpretor/arguments.py @@ -8,7 +8,9 @@ Commandline argument parsing for arm. import collections import getopt
-DEFAULT_ARGS = { +import stem.util.connection + +DEFAULT_ARGS = { 'control_address': '127.0.0.1', 'control_port': 9051, 'user_provided_port': False, @@ -19,7 +21,7 @@ DEFAULT_ARGS = {
OPT = 'i:s:h'
-OPT_EXPANDED = [ +OPT_EXPANDED = [ 'interface=', 'socket=', 'help', @@ -39,6 +41,7 @@ prompt -i 1643 attach to control port 1643 prompt -s ~/.tor/socket attach to a control socket in your home directory """
+ def parse(argv): """ Parses our arguments, providing a named tuple with their values. @@ -55,7 +58,7 @@ def parse(argv): try: getopt_results = getopt.getopt(argv, OPT, OPT_EXPANDED)[0] except getopt.GetoptError as exc: - raise ValueError(msg('usage.invalid_arguments', error = exc)) + raise ValueError('%s (for usage provide --help)' % exc)
for opt, arg in getopt_results: if opt in ('-i', '--interface'): @@ -66,12 +69,12 @@ def parse(argv):
if address is not None: if not stem.util.connection.is_valid_ipv4_address(address): - raise ValueError(msg('usage.not_a_valid_address', address_input = address)) + raise ValueError("'%s' isn't a valid IPv4 address" % address)
args['control_address'] = address
if not stem.util.connection.is_valid_port(port): - raise ValueError(msg('usage.not_a_valid_port', port_input = port)) + raise ValueError("'%s' isn't a valid port number" % port)
args['control_port'] = int(port) args['user_provided_port'] = True @@ -86,6 +89,7 @@ def parse(argv): Args = collections.namedtuple('Args', args.keys()) return Args(**args)
+ def get_help(): """ Provides our --help usage information.
tor-commits@lists.torproject.org