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"]}, package_data={"wormhole": ["db-schemas/*.sql"]},
entry_points={"console_scripts": entry_points={"console_scripts":
["wormhole = wormhole.scripts.runner:entry"]}, ["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", test_suite="wormhole.test",
cmdclass=commands, cmdclass=commands,
) )

View File

@ -1,3 +1,4 @@
import six
import requests import requests
class EventSourceFollower: class EventSourceFollower:
@ -13,11 +14,12 @@ class EventSourceFollower:
def _get_fields(self, lines): def _get_fields(self, lines):
while True: 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) fieldname, data = first_line.split(": ", 1)
data_lines = [data] data_lines = [data]
while True: while True:
next_line = lines.next() next_line = next(lines)
if not next_line: # empty string, original was "\n" if not next_line: # empty string, original was "\n"
yield (fieldname, "\n".join(data_lines)) yield (fieldname, "\n".join(data_lines))
break break
@ -30,12 +32,16 @@ class EventSourceFollower:
# for a long time. I'd prefer that chunk_size behaved like # for a long time. I'd prefer that chunk_size behaved like
# read(size), and gave you 1<=x<=size bytes in response. # read(size), and gave you 1<=x<=size bytes in response.
eventtype = "message" 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): 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": if fieldname == "data":
yield (eventtype, data) yield (eventtype, data)
eventtype = "message" eventtype = "message"
elif fieldname == "event": elif fieldname == "event":
eventtype = data eventtype = data
else: else:
print("weird fieldname", fieldname, data) print("weird fieldname", fieldname, type(fieldname), data)

View File

@ -1,5 +1,5 @@
from __future__ import print_function from __future__ import print_function
import os import os, six
from .wordlist import (byte_to_even_word, byte_to_odd_word, from .wordlist import (byte_to_even_word, byte_to_odd_word,
even_words_lowercase, odd_words_lowercase) 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.parse_and_bind("tab: complete")
readline.set_completer(c.wrap_completer) readline.set_completer(c.wrap_completer)
readline.set_completer_delims("") readline.set_completer_delims("")
code = raw_input(prompt) code = six.moves.input(prompt)
return code return code
if __name__ == "__main__": if __name__ == "__main__":

View File

@ -1,5 +1,5 @@
from __future__ import print_function from __future__ import print_function
import sys, json, binascii import sys, json, binascii, six
from ..errors import handle_server_error from ..errors import handle_server_error
APPID = b"lothar.com/wormhole/text-xfer" APPID = b"lothar.com/wormhole/text-xfer"
@ -31,7 +31,7 @@ def send_text(args):
if args.verify: if args.verify:
verifier = binascii.hexlify(w.get_verifier()) verifier = binascii.hexlify(w.get_verifier())
while True: 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": if ok.lower() == "yes":
break break
if ok.lower() == "no": if ok.lower() == "no":