Honour --no-err-windows in more places

Fixes #3053
This commit is contained in:
Florian Bruhin 2017-10-03 21:28:55 +02:00
parent 16d369d98c
commit d70bdb5552
3 changed files with 63 additions and 16 deletions

View File

@ -52,22 +52,20 @@ class _Button:
default = attr.ib(default=False) default = attr.ib(default=False)
class _Dialog(QDialog): def _other_backend(backend):
"""Get the other backend enum/setting for a given backend."""
other_backend = {
usertypes.Backend.QtWebKit: usertypes.Backend.QtWebEngine,
usertypes.Backend.QtWebEngine: usertypes.Backend.QtWebKit,
}[backend]
other_setting = other_backend.name.lower()[2:]
return (other_backend, other_setting)
"""A dialog which gets shown if there are issues with the backend."""
def __init__(self, because, text, backend, buttons=None, parent=None): def _error_text(because, text, backend):
super().__init__(parent) """Get an error text for the given information."""
vbox = QVBoxLayout(self) other_backend, other_setting = _other_backend(backend)
return ("<b>Failed to start with the {backend} backend!</b>"
other_backend = {
usertypes.Backend.QtWebKit: usertypes.Backend.QtWebEngine,
usertypes.Backend.QtWebEngine: usertypes.Backend.QtWebKit,
}[backend]
other_setting = other_backend.name.lower()[2:]
label = QLabel(
"<b>Failed to start with the {backend} backend!</b>"
"<p>qutebrowser tried to start with the {backend} backend but " "<p>qutebrowser tried to start with the {backend} backend but "
"failed because {because}.</p>{text}" "failed because {because}.</p>{text}"
"<p><b>Forcing the {other_backend.name} backend</b></p>" "<p><b>Forcing the {other_backend.name} backend</b></p>"
@ -76,8 +74,21 @@ class _Dialog(QDialog):
"(if you have a <i>config.py</i> file, you'll need to set " "(if you have a <i>config.py</i> file, you'll need to set "
"this manually).</p>".format( "this manually).</p>".format(
backend=backend.name, because=because, text=text, backend=backend.name, because=because, text=text,
other_backend=other_backend, other_setting=other_setting), other_backend=other_backend, other_setting=other_setting))
wordWrap=True)
class _Dialog(QDialog):
"""A dialog which gets shown if there are issues with the backend."""
def __init__(self, because, text, backend, buttons=None, parent=None):
super().__init__(parent)
vbox = QVBoxLayout(self)
other_backend, other_setting = _other_backend(backend)
text = _error_text(because, text, backend)
label = QLabel(text, wordWrap=True)
label.setTextFormat(Qt.RichText) label.setTextFormat(Qt.RichText)
vbox.addWidget(label) vbox.addWidget(label)
@ -118,6 +129,12 @@ class _Dialog(QDialog):
def _show_dialog(*args, **kwargs): def _show_dialog(*args, **kwargs):
"""Show a dialog for a backend problem.""" """Show a dialog for a backend problem."""
cmd_args = objreg.get('args')
if cmd_args.no_err_windows:
text = _error_text(*args, **kwargs)
print(text, file=sys.stderr)
sys.exit(usertypes.Exit.err_init)
dialog = _Dialog(*args, **kwargs) dialog = _Dialog(*args, **kwargs)
status = dialog.exec_() status = dialog.exec_()

View File

@ -19,10 +19,21 @@
"""Convenience functions to show message boxes.""" """Convenience functions to show message boxes."""
import sys
from PyQt5.QtCore import Qt from PyQt5.QtCore import Qt
from PyQt5.QtWidgets import QMessageBox from PyQt5.QtWidgets import QMessageBox
from qutebrowser.utils import objreg
class DummyBox:
"""A dummy QMessageBox returned when --no-err-windows is used."""
def exec_(self):
pass
def msgbox(parent, title, text, *, icon, buttons=QMessageBox.Ok, def msgbox(parent, title, text, *, icon, buttons=QMessageBox.Ok,
on_finished=None, plain_text=None): on_finished=None, plain_text=None):
@ -40,6 +51,11 @@ def msgbox(parent, title, text, *, icon, buttons=QMessageBox.Ok,
Return: Return:
A new QMessageBox. A new QMessageBox.
""" """
args = objreg.get('args')
if args.no_err_windows:
print('Message box: {}; {}'.format(title, text), file=sys.stderr)
return DummyBox()
box = QMessageBox(parent) box = QMessageBox(parent)
box.setAttribute(Qt.WA_DeleteOnClose) box.setAttribute(Qt.WA_DeleteOnClose)
box.setIcon(icon) box.setIcon(icon)

View File

@ -27,6 +27,11 @@ from PyQt5.QtCore import Qt
from PyQt5.QtWidgets import QMessageBox, QWidget from PyQt5.QtWidgets import QMessageBox, QWidget
@pytest.fixture(autouse=True)
def patch_args(fake_args):
fake_args.no_err_windows = False
def test_attributes(qtbot): def test_attributes(qtbot):
"""Test basic QMessageBox attributes.""" """Test basic QMessageBox attributes."""
title = 'title' title = 'title'
@ -85,3 +90,12 @@ def test_information(qtbot):
assert box.windowTitle() == 'foo' assert box.windowTitle() == 'foo'
assert box.text() == 'bar' assert box.text() == 'bar'
assert box.icon() == QMessageBox.Information assert box.icon() == QMessageBox.Information
def test_no_err_windows(fake_args, capsys):
fake_args.no_err_windows = True
box = msgbox.information(parent=None, title='foo', text='bar')
box.exec_() # should do nothing
out, err = capsys.readouterr()
assert not out
assert err == 'Message box: foo; bar\n'