[gnome-keysign: 8/65] bluetoothreceive: add a main to allow direct execution



commit 32896af164fe2725ad8406c0b5f89a301481e45c
Author: RyuzakiKK <aasonykk gmail com>
Date:   Mon Aug 7 19:17:22 2017 +0200

    bluetoothreceive: add a main to allow direct execution

 keysign/bluetoothreceive.py | 40 +++++++++++++++++++++++++++++++++++-----
 1 file changed, 35 insertions(+), 5 deletions(-)
---
diff --git a/keysign/bluetoothreceive.py b/keysign/bluetoothreceive.py
index 18e2474..831493e 100644
--- a/keysign/bluetoothreceive.py
+++ b/keysign/bluetoothreceive.py
@@ -1,11 +1,15 @@
 import logging
 from bluetooth import *
+
+if __name__ == "__main__":
+    import gi
+    gi.require_version('Gtk', '3.0')
+    from twisted.internet import gtk3reactor
+    gtk3reactor.install()
+    from twisted.internet import reactor
 from twisted.internet import threads
 from twisted.internet.defer import inlineCallbacks, returnValue
 
-from .util import strip_fingerprint
-
-
 log = logging.getLogger(__name__)
 
 
@@ -16,8 +20,7 @@ class BluetoothReceive:
         self.client_socket = None
 
     @inlineCallbacks
-    def find_key(self, code):
-        mac = strip_fingerprint(code)
+    def find_key(self, mac):
         self.client_socket = BluetoothSocket(RFCOMM)
         try:
             yield threads.deferToThread(self.client_socket.connect, (mac, self.port))
@@ -40,3 +43,30 @@ class BluetoothReceive:
     def stop(self):
         if self.client_socket:
             self.client_socket.close()
+
+
+def main(args):
+    log.debug('Running main with args: %s', args)
+    if not args:
+        raise ValueError("You must provide an argument with the bluetooth code")
+
+    def _received(result):
+        key_data, success, error_message = result
+        if success:
+            print(key_data)
+        else:
+            print(error_message)
+
+        reactor.callFromThread(reactor.stop)
+
+    print("Trying to download the key, please wait")
+    bt_mac = args[0]
+    receive = BluetoothReceive()
+    d = receive.find_key(bt_mac)
+    d.addCallback(_received)
+    reactor.run()
+
+if __name__ == "__main__":
+    logging.basicConfig(level=logging.INFO)
+    import sys
+    main(sys.argv[1:])


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