qrreader.get_qr_reader: raise instead of returning None
move MissingQrDetectionLib to core lib
This commit is contained in:
@@ -29,6 +29,7 @@ from PyQt5.QtWidgets import QMessageBox, QWidget
|
|||||||
from electrum.i18n import _
|
from electrum.i18n import _
|
||||||
from electrum.util import UserFacingException
|
from electrum.util import UserFacingException
|
||||||
from electrum.logging import get_logger
|
from electrum.logging import get_logger
|
||||||
|
from electrum.qrreader import MissingQrDetectionLib
|
||||||
|
|
||||||
from electrum.gui.qt.util import MessageBoxMixin, custom_message_box
|
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],
|
callback: Callable[[bool, str, Optional[str]], None],
|
||||||
) -> None:
|
) -> None:
|
||||||
try:
|
try:
|
||||||
from .qtmultimedia import QrReaderCameraDialog, CameraError, MissingQrDetectionLib
|
from .qtmultimedia import QrReaderCameraDialog, CameraError
|
||||||
except ImportError as e:
|
except ImportError as e:
|
||||||
icon = QMessageBox.Warning
|
icon = QMessageBox.Warning
|
||||||
title = _("QR Reader Error")
|
title = _("QR Reader Error")
|
||||||
|
|||||||
@@ -26,7 +26,7 @@
|
|||||||
from typing import Mapping
|
from typing import Mapping
|
||||||
|
|
||||||
from .camera_dialog import (QrReaderCameraDialog, CameraError, NoCamerasFound,
|
from .camera_dialog import (QrReaderCameraDialog, CameraError, NoCamerasFound,
|
||||||
NoCameraResolutionsFound, MissingQrDetectionLib)
|
NoCameraResolutionsFound)
|
||||||
from .validator import (QrReaderValidatorResult, AbstractQrReaderValidator,
|
from .validator import (QrReaderValidatorResult, AbstractQrReaderValidator,
|
||||||
QrReaderValidatorCounting, QrReaderValidatorColorizing,
|
QrReaderValidatorCounting, QrReaderValidatorColorizing,
|
||||||
QrReaderValidatorStrong, QrReaderValidatorCounted)
|
QrReaderValidatorStrong, QrReaderValidatorCounted)
|
||||||
|
|||||||
@@ -36,7 +36,7 @@ from PyQt5.QtCore import QSize, QRect, Qt, pyqtSignal, PYQT_VERSION
|
|||||||
|
|
||||||
from electrum.simple_config import SimpleConfig
|
from electrum.simple_config import SimpleConfig
|
||||||
from electrum.i18n import _
|
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.logging import Logger
|
||||||
|
|
||||||
from electrum.gui.qt.util import MessageBoxMixin, FixedAspectRatioLayout, ImageGraphicsEffect
|
from electrum.gui.qt.util import MessageBoxMixin, FixedAspectRatioLayout, ImageGraphicsEffect
|
||||||
@@ -58,10 +58,6 @@ class NoCamerasFound(CameraError):
|
|||||||
class NoCameraResolutionsFound(CameraError):
|
class NoCameraResolutionsFound(CameraError):
|
||||||
''' Raised internally if no usable camera resolutions were found. '''
|
''' 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):
|
class QrReaderCameraDialog(Logger, MessageBoxMixin, QDialog):
|
||||||
"""
|
"""
|
||||||
Dialog for reading QR codes from a camera
|
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
|
# Try to get the QR reader for this system
|
||||||
self.qrreader = get_qr_reader()
|
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
|
# Set up the window, add the maximize button
|
||||||
flags = self.windowFlags()
|
flags = self.windowFlags()
|
||||||
|
|||||||
@@ -35,15 +35,17 @@ from .abstract_base import AbstractQrCodeReader, QrCodeResult
|
|||||||
_logger = get_logger(__name__)
|
_logger = get_logger(__name__)
|
||||||
|
|
||||||
|
|
||||||
class MissingLib(RuntimeError):
|
class MissingQrDetectionLib(RuntimeError):
|
||||||
''' Raised by underlying implementation if missing libs '''
|
''' Raised if we can't find zbar or whatever other platform lib
|
||||||
pass
|
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:
|
try:
|
||||||
from .zbar import ZbarQrCodeReader
|
from .zbar import ZbarQrCodeReader
|
||||||
return ZbarQrCodeReader()
|
return ZbarQrCodeReader()
|
||||||
@@ -59,5 +61,13 @@ def get_qr_reader() -> Optional[AbstractQrCodeReader]:
|
|||||||
"""
|
"""
|
||||||
except MissingLib as e:
|
except MissingLib as e:
|
||||||
_logger.exception("")
|
_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
|
||||||
|
|||||||
Reference in New Issue
Block a user