qrreader.get_qr_reader: raise instead of returning None

move MissingQrDetectionLib to core lib
This commit is contained in:
SomberNight
2022-06-01 18:52:11 +02:00
parent 01d20cba49
commit 9d125118da
4 changed files with 20 additions and 15 deletions

View File

@@ -29,6 +29,7 @@ from PyQt5.QtWidgets import QMessageBox, QWidget
from electrum.i18n import _
from electrum.util import UserFacingException
from electrum.logging import get_logger
from electrum.qrreader import MissingQrDetectionLib
from electrum.gui.qt.util import MessageBoxMixin, custom_message_box
@@ -102,7 +103,7 @@ def _scan_qrcode_using_qtmultimedia(
callback: Callable[[bool, str, Optional[str]], None],
) -> None:
try:
from .qtmultimedia import QrReaderCameraDialog, CameraError, MissingQrDetectionLib
from .qtmultimedia import QrReaderCameraDialog, CameraError
except ImportError as e:
icon = QMessageBox.Warning
title = _("QR Reader Error")

View File

@@ -26,7 +26,7 @@
from typing import Mapping
from .camera_dialog import (QrReaderCameraDialog, CameraError, NoCamerasFound,
NoCameraResolutionsFound, MissingQrDetectionLib)
NoCameraResolutionsFound)
from .validator import (QrReaderValidatorResult, AbstractQrReaderValidator,
QrReaderValidatorCounting, QrReaderValidatorColorizing,
QrReaderValidatorStrong, QrReaderValidatorCounted)

View File

@@ -36,7 +36,7 @@ from PyQt5.QtCore import QSize, QRect, Qt, pyqtSignal, PYQT_VERSION
from electrum.simple_config import SimpleConfig
from electrum.i18n import _
from electrum.qrreader import get_qr_reader, QrCodeResult
from electrum.qrreader import get_qr_reader, QrCodeResult, MissingQrDetectionLib
from electrum.logging import Logger
from electrum.gui.qt.util import MessageBoxMixin, FixedAspectRatioLayout, ImageGraphicsEffect
@@ -58,10 +58,6 @@ class NoCamerasFound(CameraError):
class NoCameraResolutionsFound(CameraError):
''' Raised internally if no usable camera resolutions were found. '''
class MissingQrDetectionLib(RuntimeError):
''' Raised if we can't find zbar or whatever other platform lib
we require to detect QR in image frames. '''
class QrReaderCameraDialog(Logger, MessageBoxMixin, QDialog):
"""
Dialog for reading QR codes from a camera
@@ -97,8 +93,6 @@ class QrReaderCameraDialog(Logger, MessageBoxMixin, QDialog):
# Try to get the QR reader for this system
self.qrreader = get_qr_reader()
if not self.qrreader:
raise MissingQrDetectionLib(_("The platform QR detection library is not available."))
# Set up the window, add the maximize button
flags = self.windowFlags()

View File

@@ -35,15 +35,17 @@ from .abstract_base import AbstractQrCodeReader, QrCodeResult
_logger = get_logger(__name__)
class MissingLib(RuntimeError):
''' Raised by underlying implementation if missing libs '''
pass
class MissingQrDetectionLib(RuntimeError):
''' Raised if we can't find zbar or whatever other platform lib
we require to detect QR in image frames. '''
def get_qr_reader() -> Optional[AbstractQrCodeReader]:
def get_qr_reader() -> AbstractQrCodeReader:
"""
Get the Qr code reader for the current platform
Get the Qr code reader for the current platform.
Might raise exception: MissingQrDetectionLib.
"""
excs = []
try:
from .zbar import ZbarQrCodeReader
return ZbarQrCodeReader()
@@ -59,5 +61,13 @@ def get_qr_reader() -> Optional[AbstractQrCodeReader]:
"""
except MissingLib as e:
_logger.exception("")
excs.append(e)
return None
raise MissingQrDetectionLib(f"The platform QR detection library is not available.\nerrors: {excs!r}")
# --- Internals below (not part of external API)
class MissingLib(RuntimeError):
''' Raised by underlying implementation if missing libs '''
pass