make blocking/send-text work on py3, add dependency on 'six'

* use modern/portable "next(iter)" instead of "iter.next()"
* use six.moves.input() instead of raw_input()
* tell requests' Response.iter_lines that we want str, not bytes
This commit is contained in:
Brian Warner 2015-09-27 23:21:12 -07:00
parent a7213d9c9a
commit 8fe41e135d
4 changed files with 16 additions and 9 deletions

View File

@ -20,7 +20,8 @@ setup(name="magic-wormhole",
package_data={"wormhole": ["db-schemas/*.sql"]},
entry_points={"console_scripts":
["wormhole = wormhole.scripts.runner:entry"]},
install_requires=["spake2==0.3", "pynacl", "requests", "argparse"],
install_requires=["spake2==0.3", "pynacl", "requests", "argparse",
"six"],
test_suite="wormhole.test",
cmdclass=commands,
)

View File

@ -1,3 +1,4 @@
import six
import requests
class EventSourceFollower:
@ -13,11 +14,12 @@ class EventSourceFollower:
def _get_fields(self, lines):
while True:
first_line = lines.next() # raises StopIteration when closed
first_line = next(lines) # raises StopIteration when closed
assert isinstance(first_line, type(six.u(""))), type(first_line)
fieldname, data = first_line.split(": ", 1)
data_lines = [data]
while True:
next_line = lines.next()
next_line = next(lines)
if not next_line: # empty string, original was "\n"
yield (fieldname, "\n".join(data_lines))
break
@ -30,12 +32,16 @@ class EventSourceFollower:
# for a long time. I'd prefer that chunk_size behaved like
# read(size), and gave you 1<=x<=size bytes in response.
eventtype = "message"
lines_iter = self.resp.iter_lines(chunk_size=1)
lines_iter = self.resp.iter_lines(chunk_size=1, decode_unicode=True)
for (fieldname, data) in self._get_fields(lines_iter):
# fieldname/data are unicode on both py2 and py3. On py2, where
# ("abc"==u"abc" is True), this compares unicode against str,
# which matches. On py3, where (b"abc"=="abc" is False), this
# compares unicode against unicode, which matches.
if fieldname == "data":
yield (eventtype, data)
eventtype = "message"
elif fieldname == "event":
eventtype = data
else:
print("weird fieldname", fieldname, data)
print("weird fieldname", fieldname, type(fieldname), data)

View File

@ -1,5 +1,5 @@
from __future__ import print_function
import os
import os, six
from .wordlist import (byte_to_even_word, byte_to_odd_word,
even_words_lowercase, odd_words_lowercase)
@ -81,7 +81,7 @@ def input_code_with_completion(prompt, get_channel_ids, code_length):
readline.parse_and_bind("tab: complete")
readline.set_completer(c.wrap_completer)
readline.set_completer_delims("")
code = raw_input(prompt)
code = six.moves.input(prompt)
return code
if __name__ == "__main__":

View File

@ -1,5 +1,5 @@
from __future__ import print_function
import sys, json, binascii
import sys, json, binascii, six
from ..errors import handle_server_error
APPID = b"lothar.com/wormhole/text-xfer"
@ -31,7 +31,7 @@ def send_text(args):
if args.verify:
verifier = binascii.hexlify(w.get_verifier())
while True:
ok = raw_input("Verifier %s. ok? (yes/no): " % verifier)
ok = six.moves.input("Verifier %s. ok? (yes/no): " % verifier)
if ok.lower() == "yes":
break
if ok.lower() == "no":