2016-06-04 06:07:50 +00:00
|
|
|
from __future__ import print_function, unicode_literals
|
2015-09-28 06:21:12 +00:00
|
|
|
import os, six
|
2015-02-15 01:48:38 +00:00
|
|
|
from .wordlist import (byte_to_even_word, byte_to_odd_word,
|
|
|
|
even_words_lowercase, odd_words_lowercase)
|
2015-02-11 02:34:13 +00:00
|
|
|
|
2015-03-24 06:53:28 +00:00
|
|
|
def make_code(channel_id, code_length):
|
2016-06-04 20:06:27 +00:00
|
|
|
assert isinstance(channel_id, type("")), type(channel_id)
|
2015-03-24 06:53:28 +00:00
|
|
|
words = []
|
|
|
|
for i in range(code_length):
|
|
|
|
# we start with an "odd word"
|
|
|
|
if i % 2 == 0:
|
|
|
|
words.append(byte_to_odd_word[os.urandom(1)].lower())
|
|
|
|
else:
|
|
|
|
words.append(byte_to_even_word[os.urandom(1)].lower())
|
2016-06-04 20:06:27 +00:00
|
|
|
return "%s-%s" % (channel_id, "-".join(words))
|
2015-02-11 02:34:13 +00:00
|
|
|
|
|
|
|
def extract_channel_id(code):
|
|
|
|
channel_id = int(code.split("-")[0])
|
|
|
|
return channel_id
|
2015-02-15 01:48:38 +00:00
|
|
|
|
|
|
|
class CodeInputter:
|
2017-01-06 17:05:34 +00:00
|
|
|
def __init__(self, initial_channelids, get_channel_ids, code_length,
|
|
|
|
used_completion_f):
|
2015-11-15 18:53:13 +00:00
|
|
|
self._initial_channelids = initial_channelids
|
|
|
|
self._get_channel_ids = get_channel_ids
|
2015-03-24 06:53:28 +00:00
|
|
|
self.code_length = code_length
|
2015-02-15 01:48:38 +00:00
|
|
|
self.last_text = None # memoize for a speedup
|
|
|
|
self.last_matches = None
|
2017-01-06 17:05:34 +00:00
|
|
|
self._used_completion_f = used_completion_f
|
2015-02-15 01:48:38 +00:00
|
|
|
|
2015-11-15 18:53:13 +00:00
|
|
|
def get_current_channel_ids(self):
|
|
|
|
if self._initial_channelids is not None:
|
|
|
|
channelids = self._initial_channelids
|
|
|
|
self._initial_channelids = None
|
|
|
|
return channelids
|
|
|
|
return self._get_channel_ids()
|
|
|
|
|
2015-02-15 01:48:38 +00:00
|
|
|
def wrap_completer(self, text, state):
|
|
|
|
try:
|
|
|
|
return self.completer(text, state)
|
|
|
|
except Exception as e:
|
|
|
|
# completer exceptions are normally silently discarded, which
|
|
|
|
# makes debugging challenging
|
|
|
|
print("completer exception: %s" % e)
|
|
|
|
raise e
|
|
|
|
|
|
|
|
def completer(self, text, state):
|
2017-01-06 17:05:34 +00:00
|
|
|
self._used_completion_f()
|
2015-03-13 08:50:21 +00:00
|
|
|
#if state == 0:
|
|
|
|
# print("", file=sys.stderr)
|
|
|
|
#print("completer: '%s' %d '%d'" % (text, state,
|
|
|
|
# readline.get_completion_type()),
|
|
|
|
# file=sys.stderr)
|
|
|
|
#sys.stderr.flush()
|
2015-02-15 01:48:38 +00:00
|
|
|
pieces = text.split("-")
|
|
|
|
last = pieces[-1].lower()
|
2015-03-13 08:50:21 +00:00
|
|
|
if text == self.last_text and len(pieces) >= 2:
|
|
|
|
# if len(pieces) == 1, skip the cache, so we can re-fetch the
|
|
|
|
# channel_id list
|
2015-02-15 01:48:38 +00:00
|
|
|
matches = self.last_matches
|
|
|
|
#print(" old matches", len(matches), file=sys.stderr)
|
|
|
|
else:
|
2015-03-24 06:53:28 +00:00
|
|
|
if len(pieces) <= 1:
|
2015-11-15 18:53:13 +00:00
|
|
|
channel_ids = self.get_current_channel_ids()
|
2015-03-13 08:50:21 +00:00
|
|
|
matches = [str(channel_id) for channel_id in channel_ids
|
2015-02-15 01:48:38 +00:00
|
|
|
if str(channel_id).startswith(last)]
|
|
|
|
else:
|
|
|
|
if len(pieces) % 2 == 0:
|
|
|
|
words = odd_words_lowercase
|
|
|
|
else:
|
|
|
|
words = even_words_lowercase
|
|
|
|
so_far = "-".join(pieces[:-1]) + "-"
|
|
|
|
matches = sorted([so_far+word for word in words
|
|
|
|
if word.startswith(last)])
|
|
|
|
self.last_text = text
|
|
|
|
self.last_matches = matches
|
|
|
|
#print(" new matches:", matches, file=sys.stderr)
|
|
|
|
if state >= len(matches):
|
|
|
|
return None
|
|
|
|
match = matches[state]
|
2015-03-24 06:53:28 +00:00
|
|
|
if len(pieces) < 1+self.code_length:
|
2015-02-15 01:48:38 +00:00
|
|
|
match += "-"
|
2015-03-13 08:50:21 +00:00
|
|
|
#print(" match: '%s'" % match, file=sys.stderr)
|
|
|
|
#sys.stderr.flush()
|
2015-02-15 01:48:38 +00:00
|
|
|
return match
|
|
|
|
|
|
|
|
|
2015-11-15 18:53:13 +00:00
|
|
|
def input_code_with_completion(prompt, initial_channelids, get_channel_ids,
|
|
|
|
code_length):
|
2017-01-06 17:05:34 +00:00
|
|
|
used_completion = []
|
|
|
|
def used_completion_f():
|
|
|
|
used_completion.append(True)
|
2016-02-27 22:16:58 +00:00
|
|
|
try:
|
|
|
|
import readline
|
2017-01-06 17:05:34 +00:00
|
|
|
c = CodeInputter(initial_channelids, get_channel_ids, code_length,
|
|
|
|
used_completion_f)
|
2016-06-04 07:52:27 +00:00
|
|
|
if "libedit" in readline.__doc__:
|
|
|
|
readline.parse_and_bind("bind ^I rl_complete")
|
|
|
|
else:
|
|
|
|
readline.parse_and_bind("tab: complete")
|
2016-02-27 22:16:58 +00:00
|
|
|
readline.set_completer(c.wrap_completer)
|
|
|
|
readline.set_completer_delims("")
|
|
|
|
except ImportError:
|
|
|
|
pass
|
2015-09-28 06:21:12 +00:00
|
|
|
code = six.moves.input(prompt)
|
2015-10-07 03:04:56 +00:00
|
|
|
# Code is str(bytes) on py2, and str(unicode) on py3. We want unicode.
|
|
|
|
if isinstance(code, bytes):
|
|
|
|
code = code.decode("utf-8")
|
2017-01-06 17:05:34 +00:00
|
|
|
return (code, bool(used_completion))
|
2015-02-15 01:48:38 +00:00
|
|
|
|
|
|
|
if __name__ == "__main__":
|
2016-12-09 07:47:53 +00:00
|
|
|
code = input_code_with_completion("Enter wormhole code: ",
|
|
|
|
[], lambda: [], 2)
|
2015-02-15 01:48:38 +00:00
|
|
|
print("code is:", code)
|