[tor-commits] [bridgedb/master] Change qrcode module import to be attempted only once.

isis at torproject.org isis at torproject.org
Tue Feb 3 02:31:03 UTC 2015


commit 438021c278906a08c2a74e154673a5e26cbfbedc
Author: Isis Lovecruft <isis at torproject.org>
Date:   Sun Feb 1 00:13:25 2015 +0000

    Change qrcode module import to be attempted only once.
---
 lib/bridgedb/qrcodes.py |   17 +++++++++++------
 1 file changed, 11 insertions(+), 6 deletions(-)

diff --git a/lib/bridgedb/qrcodes.py b/lib/bridgedb/qrcodes.py
index ae25f06..a76cf8f 100644
--- a/lib/bridgedb/qrcodes.py
+++ b/lib/bridgedb/qrcodes.py
@@ -16,6 +16,14 @@
 import cStringIO
 import logging
 
+try:
+    import qrcode
+except ImportError:
+    qrcode = False
+    logging.warn("Could not import Python qrcode module.")
+    logging.debug(("You'll need the qrcode Python module for this to "
+                   "work. On Debian-based systems, this should be in the "
+                   "python-qrcode package."))
 
 def generateQR(bridgelines, imageFormat=u'JPEG'):
     """Generate a QRCode for the client's bridge lines.
@@ -31,8 +39,10 @@ def generateQR(bridgelines, imageFormat=u'JPEG'):
     if not bridgelines:
         return
 
+    if not qrcode:
+        logging.info("Not creating QRCode for bridgelines; no qrcode module.")
+
     try:
-        import qrcode
 
         qr = qrcode.QRCode()
         qr.add_data(bridgelines)
@@ -45,11 +55,6 @@ def generateQR(bridgelines, imageFormat=u'JPEG'):
         
         return imgstr
 
-    except ImportError as error:
-        logging.error(str(error))
-        logging.debug(("You'll need the qrcode Python module for this to "
-                       "work. On Debian-based systems, this should be in the "
-                       "python-qrcode package."))
     except KeyError as error:
         logging.error(str(error))
         logging.debug(("It seems python-imaging doesn't understand how to "





More information about the tor-commits mailing list