1
0

qrscanner: nicer error messages

This commit is contained in:
SomberNight
2021-02-23 22:38:53 +01:00
parent 22a14d42b2
commit 4a8286c744
4 changed files with 22 additions and 6 deletions

View File

@@ -27,6 +27,8 @@ import os
import sys
import ctypes
from .util import UserFacingException
from .i18n import _
from .logging import get_logger
@@ -53,7 +55,7 @@ except BaseException as e1:
def scan_barcode_ctypes(device='', timeout=-1, display=True, threaded=False):
if libzbar is None:
raise RuntimeError("Cannot start QR scanner; zbar not available.")
raise UserFacingException("Cannot start QR scanner: zbar not available.")
libzbar.zbar_symbol_get_data.restype = ctypes.c_char_p
libzbar.zbar_processor_create.restype = ctypes.POINTER(ctypes.c_int)
libzbar.zbar_processor_get_results.restype = ctypes.POINTER(ctypes.c_int)
@@ -62,7 +64,9 @@ def scan_barcode_ctypes(device='', timeout=-1, display=True, threaded=False):
proc = libzbar.zbar_processor_create(threaded)
libzbar.zbar_processor_request_size(proc, 640, 480)
if libzbar.zbar_processor_init(proc, device.encode('utf-8'), display) != 0:
raise RuntimeError("Can not start QR scanner; initialization failed.")
raise UserFacingException(
_("Cannot start QR scanner: initialization failed.") + "\n" +
_("Make sure you have a camera connected and enabled."))
libzbar.zbar_processor_set_visible(proc)
if libzbar.zbar_process_one(proc, timeout):
symbols = libzbar.zbar_processor_get_results(proc)
@@ -85,7 +89,7 @@ def scan_barcode_osx(*args_ignored, **kwargs_ignored):
root_ec_dir = os.path.abspath(os.path.dirname(__file__) + "/../")
prog = root_ec_dir + "/" + "contrib/osx/CalinsQRReader/build/Release/CalinsQRReader.app/Contents/MacOS/CalinsQRReader"
if not os.path.exists(prog):
raise RuntimeError("Cannot start QR scanner; helper app not found.")
raise UserFacingException("Cannot start QR scanner: helper app not found.")
data = ''
try:
# This will run the "CalinsQRReader" helper app (which also gets bundled with the built .app)
@@ -96,7 +100,7 @@ def scan_barcode_osx(*args_ignored, **kwargs_ignored):
data = p.stdout.read().decode('utf-8').strip()
return data
except OSError as e:
raise RuntimeError("Cannot start camera helper app; {}".format(e.strerror))
raise UserFacingException("Cannot start camera helper app: {}".format(e.strerror))
scan_barcode = scan_barcode_osx if sys.platform == 'darwin' else scan_barcode_ctypes