make blocking.Wormhole into a context manager
This commit is contained in:
parent
a881d6055f
commit
80beb20631
11
docs/api.md
11
docs/api.md
|
@ -56,6 +56,11 @@ suffer longer invitation codes as a result. To encourage `close()`, the
|
||||||
library will log an error if a Wormhole object is destroyed before being
|
library will log an error if a Wormhole object is destroyed before being
|
||||||
closed.
|
closed.
|
||||||
|
|
||||||
|
To make it easier to call `close()`, the blocking Wormhole objects can be
|
||||||
|
used as a context manager. Just put your code in the body of a `with
|
||||||
|
Wormhole(ARGS) as w:` statement, and `close()` will automatically be called
|
||||||
|
when the block exits (either successfully or due to an exception).
|
||||||
|
|
||||||
## Examples
|
## Examples
|
||||||
|
|
||||||
The synchronous+blocking flow looks like this:
|
The synchronous+blocking flow looks like this:
|
||||||
|
@ -64,12 +69,11 @@ The synchronous+blocking flow looks like this:
|
||||||
from wormhole.blocking.transcribe import Wormhole
|
from wormhole.blocking.transcribe import Wormhole
|
||||||
from wormhole.public_relay import RENDEZVOUS_RELAY
|
from wormhole.public_relay import RENDEZVOUS_RELAY
|
||||||
mydata = b"initiator's data"
|
mydata = b"initiator's data"
|
||||||
i = Wormhole(u"appid", RENDEZVOUS_RELAY)
|
with Wormhole(u"appid", RENDEZVOUS_RELAY) as i:
|
||||||
code = i.get_code()
|
code = i.get_code()
|
||||||
print("Invitation Code: %s" % code)
|
print("Invitation Code: %s" % code)
|
||||||
i.send_data(mydata)
|
i.send_data(mydata)
|
||||||
theirdata = i.get_data()
|
theirdata = i.get_data()
|
||||||
i.close()
|
|
||||||
print("Their data: %s" % theirdata.decode("ascii"))
|
print("Their data: %s" % theirdata.decode("ascii"))
|
||||||
```
|
```
|
||||||
|
|
||||||
|
@ -79,11 +83,10 @@ from wormhole.blocking.transcribe import Wormhole
|
||||||
from wormhole.public_relay import RENDEZVOUS_RELAY
|
from wormhole.public_relay import RENDEZVOUS_RELAY
|
||||||
mydata = b"receiver's data"
|
mydata = b"receiver's data"
|
||||||
code = sys.argv[1]
|
code = sys.argv[1]
|
||||||
r = Wormhole(u"appid", RENDEZVOUS_RELAY)
|
with Wormhole(u"appid", RENDEZVOUS_RELAY) as r:
|
||||||
r.set_code(code)
|
r.set_code(code)
|
||||||
r.send_data(mydata)
|
r.send_data(mydata)
|
||||||
theirdata = r.get_data()
|
theirdata = r.get_data()
|
||||||
r.close()
|
|
||||||
print("Their data: %s" % theirdata.decode("ascii"))
|
print("Their data: %s" % theirdata.decode("ascii"))
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
|
@ -203,6 +203,12 @@ class Wormhole:
|
||||||
self._got_data = set()
|
self._got_data = set()
|
||||||
self._closed = False
|
self._closed = False
|
||||||
|
|
||||||
|
def __enter__(self):
|
||||||
|
return self
|
||||||
|
def __exit__(self, exc_type, exc_val, exc_tb):
|
||||||
|
self.close()
|
||||||
|
return False
|
||||||
|
|
||||||
def handle_welcome(self, welcome):
|
def handle_welcome(self, welcome):
|
||||||
if ("motd" in welcome and
|
if ("motd" in welcome and
|
||||||
not self.motd_displayed):
|
not self.motd_displayed):
|
||||||
|
|
|
@ -12,8 +12,7 @@ def receive(args):
|
||||||
from .progress import start_progress, update_progress, finish_progress
|
from .progress import start_progress, update_progress, finish_progress
|
||||||
assert isinstance(args.relay_url, type(u""))
|
assert isinstance(args.relay_url, type(u""))
|
||||||
|
|
||||||
if True:
|
with Wormhole(APPID, args.relay_url) as w:
|
||||||
w = Wormhole(APPID, args.relay_url)
|
|
||||||
if args.zeromode:
|
if args.zeromode:
|
||||||
assert not args.code
|
assert not args.code
|
||||||
args.code = u"0-"
|
args.code = u"0-"
|
||||||
|
@ -30,12 +29,10 @@ def receive(args):
|
||||||
them_bytes = w.get_data()
|
them_bytes = w.get_data()
|
||||||
except WrongPasswordError as e:
|
except WrongPasswordError as e:
|
||||||
print("ERROR: " + e.explain(), file=sys.stderr)
|
print("ERROR: " + e.explain(), file=sys.stderr)
|
||||||
w.close()
|
|
||||||
return 1
|
return 1
|
||||||
them_d = json.loads(them_bytes.decode("utf-8"))
|
them_d = json.loads(them_bytes.decode("utf-8"))
|
||||||
if "error" in them_d:
|
if "error" in them_d:
|
||||||
print("ERROR: " + them_d["error"], file=sys.stderr)
|
print("ERROR: " + them_d["error"], file=sys.stderr)
|
||||||
w.close()
|
|
||||||
return 1
|
return 1
|
||||||
|
|
||||||
if "message" in them_d:
|
if "message" in them_d:
|
||||||
|
@ -43,18 +40,15 @@ def receive(args):
|
||||||
print(them_d["message"])
|
print(them_d["message"])
|
||||||
data = json.dumps({"message_ack": "ok"}).encode("utf-8")
|
data = json.dumps({"message_ack": "ok"}).encode("utf-8")
|
||||||
w.send_data(data)
|
w.send_data(data)
|
||||||
w.close()
|
|
||||||
return 0
|
return 0
|
||||||
|
|
||||||
if not "file" in them_d:
|
if not "file" in them_d:
|
||||||
print("I don't know what they're offering\n")
|
print("I don't know what they're offering\n")
|
||||||
print(them_d)
|
print(them_d)
|
||||||
w.close()
|
|
||||||
return 1
|
return 1
|
||||||
|
|
||||||
if "error" in them_d:
|
if "error" in them_d:
|
||||||
print("ERROR: " + data["error"], file=sys.stderr)
|
print("ERROR: " + data["error"], file=sys.stderr)
|
||||||
w.close()
|
|
||||||
return 1
|
return 1
|
||||||
|
|
||||||
file_data = them_d["file"]
|
file_data = them_d["file"]
|
||||||
|
@ -68,7 +62,6 @@ def receive(args):
|
||||||
print("Error: refusing to overwrite existing file %s" % (filename,))
|
print("Error: refusing to overwrite existing file %s" % (filename,))
|
||||||
data = json.dumps({"error": "file already exists"}).encode("utf-8")
|
data = json.dumps({"error": "file already exists"}).encode("utf-8")
|
||||||
w.send_data(data)
|
w.send_data(data)
|
||||||
w.close()
|
|
||||||
return 1
|
return 1
|
||||||
|
|
||||||
print("Receiving file (%d bytes) into: %s" % (filesize, filename))
|
print("Receiving file (%d bytes) into: %s" % (filesize, filename))
|
||||||
|
@ -79,7 +72,6 @@ def receive(args):
|
||||||
print("transfer rejected", file=sys.stderr)
|
print("transfer rejected", file=sys.stderr)
|
||||||
data = json.dumps({"error": "transfer rejected"}).encode("utf-8")
|
data = json.dumps({"error": "transfer rejected"}).encode("utf-8")
|
||||||
w.send_data(data)
|
w.send_data(data)
|
||||||
w.close()
|
|
||||||
return 1
|
return 1
|
||||||
|
|
||||||
transit_receiver = TransitReceiver(args.transit_helper)
|
transit_receiver = TransitReceiver(args.transit_helper)
|
||||||
|
@ -91,7 +83,6 @@ def receive(args):
|
||||||
},
|
},
|
||||||
}).encode("utf-8")
|
}).encode("utf-8")
|
||||||
w.send_data(data)
|
w.send_data(data)
|
||||||
w.close()
|
|
||||||
|
|
||||||
# now receive the rest of the owl
|
# now receive the rest of the owl
|
||||||
tdata = them_d["transit"]
|
tdata = them_d["transit"]
|
||||||
|
|
|
@ -46,8 +46,7 @@ def send(args):
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
|
|
||||||
if True:
|
with Wormhole(APPID, args.relay_url) as w:
|
||||||
w = Wormhole(APPID, args.relay_url)
|
|
||||||
if args.zeromode:
|
if args.zeromode:
|
||||||
assert not args.code
|
assert not args.code
|
||||||
args.code = u"0-"
|
args.code = u"0-"
|
||||||
|
@ -78,7 +77,6 @@ def send(args):
|
||||||
reject_data = json.dumps({"error": "verification rejected",
|
reject_data = json.dumps({"error": "verification rejected",
|
||||||
}).encode("utf-8")
|
}).encode("utf-8")
|
||||||
w.send_data(reject_data)
|
w.send_data(reject_data)
|
||||||
w.close()
|
|
||||||
return 1
|
return 1
|
||||||
|
|
||||||
my_phase1_bytes = json.dumps(phase1).encode("utf-8")
|
my_phase1_bytes = json.dumps(phase1).encode("utf-8")
|
||||||
|
@ -87,30 +85,24 @@ def send(args):
|
||||||
them_phase1_bytes = w.get_data()
|
them_phase1_bytes = w.get_data()
|
||||||
except WrongPasswordError as e:
|
except WrongPasswordError as e:
|
||||||
print("ERROR: " + e.explain(), file=sys.stderr)
|
print("ERROR: " + e.explain(), file=sys.stderr)
|
||||||
w.close()
|
|
||||||
return 1
|
return 1
|
||||||
them_phase1 = json.loads(them_phase1_bytes.decode("utf-8"))
|
them_phase1 = json.loads(them_phase1_bytes.decode("utf-8"))
|
||||||
|
|
||||||
if sending_message:
|
if sending_message:
|
||||||
if them_phase1["message_ack"] == "ok":
|
if them_phase1["message_ack"] == "ok":
|
||||||
print("text message sent")
|
print("text message sent")
|
||||||
w.close()
|
|
||||||
return 0
|
return 0
|
||||||
print("error sending text: %r" % (them_phase1,))
|
print("error sending text: %r" % (them_phase1,))
|
||||||
w.close()
|
|
||||||
return 1
|
return 1
|
||||||
|
|
||||||
if "error" in them_phase1:
|
if "error" in them_phase1:
|
||||||
print("remote error: %s" % them_phase1["error"])
|
print("remote error: %s" % them_phase1["error"])
|
||||||
print("transfer abandoned")
|
print("transfer abandoned")
|
||||||
w.close()
|
|
||||||
return 1
|
return 1
|
||||||
if them_phase1.get("file_ack") != "ok":
|
if them_phase1.get("file_ack") != "ok":
|
||||||
print("ambiguous response from remote: %s" % (them_phase1,))
|
print("ambiguous response from remote: %s" % (them_phase1,))
|
||||||
print("transfer abandoned")
|
print("transfer abandoned")
|
||||||
w.close()
|
|
||||||
return 1
|
return 1
|
||||||
w.close()
|
|
||||||
|
|
||||||
tdata = them_phase1["transit"]
|
tdata = them_phase1["transit"]
|
||||||
transit_key = w.derive_key(APPID+"/transit-key")
|
transit_key = w.derive_key(APPID+"/transit-key")
|
||||||
|
|
Loading…
Reference in New Issue
Block a user