[gnome-keysign: 1/3] Allocate Bluetooth code with inlineCallbacks



commit 470710a838fe2677d69f11b5cd996a4b81c3e783
Author: RyuzakiKK <aasonykk gmail com>
Date:   Fri Oct 5 22:28:56 2018 +0200

    Allocate Bluetooth code with inlineCallbacks
    
    This fix is related to the PR #52
    Now we defer `get_local_bt_address` to a different thread. In this way
    we don't stall the UI waiting for this method reply.

 keysign/bluetoothoffer.py | 49 ++++++++++++++++++++++++++---------------------
 keysign/offer.py          |  2 +-
 2 files changed, 28 insertions(+), 23 deletions(-)
---
diff --git a/keysign/bluetoothoffer.py b/keysign/bluetoothoffer.py
index 9a28ad7..8ef6dbe 100644
--- a/keysign/bluetoothoffer.py
+++ b/keysign/bluetoothoffer.py
@@ -75,14 +75,14 @@ class BluetoothOffer:
 
         returnValue((success, message))
 
+    @inlineCallbacks
     def allocate_code(self):
         """Acquires and returns a string suitable for finding the key via Bluetooth.
         Returns None if no powered on adapter could be found."""
-        # TODO: when we have magic-wormhole we should perform this operation in async
-        # and show the loading spinning wheel
         bt_data = None
         try:
-            code = get_local_bt_address().upper()
+            code = yield threads.deferToThread(get_local_bt_address)
+            code = code.upper()
         except NoBluezDbus as e:
             log.debug("Bluetooth service seems to be unavailable: %s", e)
         except NoAdapter as e:
@@ -101,7 +101,7 @@ class BluetoothOffer:
             port = self.server_socket.getsockname()[1]
             log.info("BT Code: %s %s", code, port)
             bt_data = "BT={0};PT={1}".format(code, port)
-        return bt_data
+        returnValue(bt_data)
 
     def stop(self):
         log.debug("Stopping bt receive")
@@ -114,10 +114,27 @@ class BluetoothOffer:
 
 def main(args):
     if not args:
-        raise ValueError("You must provide an argument to identify the key")
+        raise ValueError(_("You must provide an argument to identify the key"))
+
+    def code_generated(data):
+        if data:
+            # getting the code from "BT=code;...."
+            code = data.split("=", 1)[1]
+            code = code.split(";", 1)[0]
+            port = data.rsplit("=", 1)[1]
+            offer.start().addCallback(_received)
+            print(_("Offering key: {}").format(key))
+            print(_("Discovery info: {}").format(code))
+            print(_("HMAC: {}").format(hmac))
+            print(_("Port: {}").format(port))
+            # Wait for the user without blocking everything
+        else:
+            print(_("Bluetooth not available"))
+
+        reactor.callInThread(cancel)
 
     def cancel():
-        input("Press Enter to cancel")
+        input(_("Press Enter to cancel"))
         offer.stop()
         reactor.callFromThread(reactor.stop)
 
@@ -134,22 +151,10 @@ def main(args):
     file_key_data = get_public_key_data(key.fingerprint)
     hmac = mac_generate(key.fingerprint.encode('ascii'), file_key_data)
     offer = BluetoothOffer(key)
-    data = offer.allocate_code()
-    if data:
-        # getting the code from "BT=code;...."
-        code = data.split("=", 1)[1]
-        code = code.split(";", 1)[0]
-        port = data.rsplit("=", 1)[1]
-        offer.start().addCallback(_received)
-        print(_("Offering key: {}").format(key))
-        print(_("Discovery info: {}").format(code))
-        print(_("HMAC: {}").format(hmac))
-        print(_("Port: {}").format(port))
-        # Wait for the user without blocking everything
-        reactor.callInThread(cancel)
-        reactor.run()
-    else:
-        print(_("Bluetooth not available"))
+
+    offer.allocate_code().addCallback(code_generated)
+    reactor.run()
+
 
 if __name__ == "__main__":
     import sys
diff --git a/keysign/offer.py b/keysign/offer.py
index e71faca..4328417 100644
--- a/keysign/offer.py
+++ b/keysign/offer.py
@@ -35,7 +35,7 @@ class Offer:
                 discovery_data.append(w_data)
         if BluetoothOffer:
             self.bt_offer = BluetoothOffer(self.key)
-            self.b_data = self.bt_offer.allocate_code()
+            self.b_data = yield self.bt_offer.allocate_code()
             if self.b_data:
                 discovery_data.append(self.b_data)
         discovery_data = ";".join(discovery_data)


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]