From 94a3be91b2f77d4cb092fb7419681b3809529b5a Mon Sep 17 00:00:00 2001 From: Brian Warner Date: Fri, 15 Apr 2016 17:27:32 -0700 Subject: [PATCH] add tab-completion to twisted-style input_code() --- src/wormhole/scripts/cmd_receive_twisted.py | 13 ++++++++++++- src/wormhole/twisted/transcribe.py | 18 ++++++++++++++++++ 2 files changed, 30 insertions(+), 1 deletion(-) diff --git a/src/wormhole/scripts/cmd_receive_twisted.py b/src/wormhole/scripts/cmd_receive_twisted.py index 2f8e38f..fb7dd3d 100644 --- a/src/wormhole/scripts/cmd_receive_twisted.py +++ b/src/wormhole/scripts/cmd_receive_twisted.py @@ -60,7 +60,7 @@ class TwistedReceiver(BlockingReceiver): @inlineCallbacks def _go(self, w, tor_manager): - self.handle_code(w) + yield self.handle_code(w) verifier = yield w.get_verifier() self.show_verifier(verifier) them_d = yield self.get_data(w) @@ -90,6 +90,17 @@ class TwistedReceiver(BlockingReceiver): returnValue(1) returnValue(0) + @inlineCallbacks + def handle_code(self, w): + code = self.args.code + if self.args.zeromode: + assert not code + code = u"0-" + if not code: + code = yield w.input_code("Enter receive wormhole code: ", + self.args.code_length) + yield w.set_code(code) + @inlineCallbacks def get_data(self, w): try: diff --git a/src/wormhole/twisted/transcribe.py b/src/wormhole/twisted/transcribe.py index bb31cd9..585fbfd 100644 --- a/src/wormhole/twisted/transcribe.py +++ b/src/wormhole/twisted/transcribe.py @@ -4,6 +4,7 @@ from six.moves.urllib_parse import urlencode from binascii import hexlify, unhexlify from zope.interface import implementer from twisted.internet import reactor, defer +from twisted.internet.threads import deferToThread, blockingCallFromThread from twisted.internet.defer import inlineCallbacks, returnValue from twisted.web import client as web_client from twisted.web import error as web_error @@ -319,6 +320,23 @@ class Wormhole: self._start() returnValue(code) + @inlineCallbacks + def input_code(self, prompt="Enter wormhole code: ", code_length=2): + def _lister(): + return blockingCallFromThread(reactor, + self._channel_manager.list_channels) + # fetch the list of channels ahead of time, to give us a chance to + # discover the welcome message (and warn the user about an obsolete + # client) + initial_channelids = yield self._channel_manager.list_channels() + _start = self._timing.add_event("input code", waiting="user") + code = yield deferToThread(codes.input_code_with_completion, + prompt, + initial_channelids, _lister, + code_length) + self._timing.finish_event(_start) + returnValue(code) + def set_code(self, code): if not isinstance(code, type(u"")): raise TypeError(type(code)) if self.code is not None: raise UsageError