Skip to content

Commit

Permalink
Add a new qt_args setting
Browse files Browse the repository at this point in the history
See #2589
  • Loading branch information
The-Compiler committed Sep 17, 2017
1 parent 3404252 commit fe05947
Show file tree
Hide file tree
Showing 7 changed files with 97 additions and 69 deletions.
10 changes: 10 additions & 0 deletions doc/help/settings.asciidoc
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -208,6 +208,7 @@
|<<new_instance_open_target_window,new_instance_open_target_window>>|Which window to choose when opening links as new tabs. |<<new_instance_open_target_window,new_instance_open_target_window>>|Which window to choose when opening links as new tabs.
|<<prompt.filebrowser,prompt.filebrowser>>|Show a filebrowser in upload/download prompts. |<<prompt.filebrowser,prompt.filebrowser>>|Show a filebrowser in upload/download prompts.
|<<prompt.radius,prompt.radius>>|The rounding radius for the edges of prompts. |<<prompt.radius,prompt.radius>>|The rounding radius for the edges of prompts.
|<<qt_args,qt_args>>|Additional arguments to pass to Qt, without leading `--`.
|<<scrolling.bar,scrolling.bar>>|Show a scrollbar. |<<scrolling.bar,scrolling.bar>>|Show a scrollbar.
|<<scrolling.smooth,scrolling.smooth>>|Enable smooth scrolling for web pages. |<<scrolling.smooth,scrolling.smooth>>|Enable smooth scrolling for web pages.
|<<session_default_name,session_default_name>>|The name of the session to save by default. |<<session_default_name,session_default_name>>|The name of the session to save by default.
Expand Down Expand Up @@ -2604,6 +2605,15 @@ Type: <<types,Int>>


Default: +pass:[8]+ Default: +pass:[8]+


[[qt_args]]
=== qt_args
Additional arguments to pass to Qt, without leading `--`.
With QtWebEngine, some Chromium arguments (see https://peter.sh/experiments/chromium-command-line-switches/ for a list) will work.

Type: <<types,List>>

Default: empty

[[scrolling.bar]] [[scrolling.bar]]
=== scrolling.bar === scrolling.bar
Show a scrollbar. Show a scrollbar.
Expand Down
2 changes: 1 addition & 1 deletion qutebrowser/app.py
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -770,7 +770,7 @@ def __init__(self, args):
""" """
self._last_focus_object = None self._last_focus_object = None


qt_args = qtutils.get_args(args) qt_args = config.qt_args(args)
log.init.debug("Qt arguments: {}, based on {}".format(qt_args, args)) log.init.debug("Qt arguments: {}, based on {}".format(qt_args, args))
super().__init__(qt_args) super().__init__(qt_args)


Expand Down
23 changes: 23 additions & 0 deletions qutebrowser/config/config.py
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@


"""Configuration storage and config-related utilities.""" """Configuration storage and config-related utilities."""


import sys
import copy import copy
import contextlib import contextlib
import functools import functools
Expand Down Expand Up @@ -704,3 +705,25 @@ def late_init(save_manager):


instance.init_save_manager(save_manager) instance.init_save_manager(save_manager)
configfiles.state.init_save_manager(save_manager) configfiles.state.init_save_manager(save_manager)


def qt_args(namespace):
"""Get the Qt QApplication arguments based on an argparse namespace.
Args:
namespace: The argparse namespace.
Return:
The argv list to be passed to Qt.
"""
argv = [sys.argv[0]]

if namespace.qt_flag is not None:
argv += ['--' + flag[0] for flag in namespace.qt_flag]

if namespace.qt_arg is not None:
for name, value in namespace.qt_arg:
argv += ['--' + name, value]

argv += ['--' + arg for arg in val.qt_args]
return argv
13 changes: 13 additions & 0 deletions qutebrowser/config/configdata.yml
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -85,6 +85,19 @@ session_default_name:
If this is set to null, the session which was last loaded is saved. If this is set to null, the session which was last loaded is saved.
qt_args:
type:
name: List
valtype: String
none_ok: true
default: []
desc: >-
Additional arguments to pass to Qt, without leading `--`.
With QtWebEngine, some Chromium arguments (see
https://peter.sh/experiments/chromium-command-line-switches/ for a list)
will work.
## auto_save ## auto_save


auto_save.interval: auto_save.interval:
Expand Down
22 changes: 0 additions & 22 deletions qutebrowser/utils/qtutils.py
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@


import io import io
import os import os
import sys
import operator import operator
import contextlib import contextlib


Expand Down Expand Up @@ -138,27 +137,6 @@ def check_overflow(arg, ctype, fatal=True):
return arg return arg




def get_args(namespace):
"""Get the Qt QApplication arguments based on an argparse namespace.
Args:
namespace: The argparse namespace.
Return:
The argv list to be passed to Qt.
"""
argv = [sys.argv[0]]

if namespace.qt_flag is not None:
argv += ['--' + flag[0] for flag in namespace.qt_flag]

if namespace.qt_arg is not None:
for name, value in namespace.qt_arg:
argv += ['--' + name, value]

return argv


def check_print_compat(): def check_print_compat():
"""Check if printing should work in the given Qt version.""" """Check if printing should work in the given Qt version."""
# WORKAROUND (remove this when we bump the requirements to 5.3.0) # WORKAROUND (remove this when we bump the requirements to 5.3.0)
Expand Down
50 changes: 50 additions & 0 deletions tests/unit/config/test_config.py
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@


"""Tests for qutebrowser.config.config.""" """Tests for qutebrowser.config.config."""


import sys
import copy import copy
import types import types
import logging import logging
Expand All @@ -27,6 +28,7 @@
from PyQt5.QtCore import QObject, QUrl from PyQt5.QtCore import QObject, QUrl
from PyQt5.QtGui import QColor from PyQt5.QtGui import QColor


from qutebrowser import qutebrowser
from qutebrowser.commands import cmdexc from qutebrowser.commands import cmdexc
from qutebrowser.config import config, configdata, configexc, configfiles from qutebrowser.config import config, configdata, configexc, configfiles
from qutebrowser.utils import objreg, usertypes from qutebrowser.utils import objreg, usertypes
Expand Down Expand Up @@ -982,3 +984,51 @@ def test_late_init(init_patch, monkeypatch, fake_save_manager, mocker, errors):
assert '<b>Error text</b>: Exception' in text assert '<b>Error text</b>: Exception' in text
else: else:
assert not msgbox_mock.called assert not msgbox_mock.called


class TestQtArgs:

@pytest.fixture
def parser(self, mocker):
"""Fixture to provide an argparser.
Monkey-patches .exit() of the argparser so it doesn't exit on errors.
"""
parser = qutebrowser.get_argparser()
mocker.patch.object(parser, 'exit', side_effect=Exception)
return parser

@pytest.mark.parametrize('args, expected', [
# No Qt arguments
(['--debug'], [sys.argv[0]]),
# Qt flag
(['--debug', '--qt-flag', 'reverse'], [sys.argv[0], '--reverse']),
# Qt argument with value
(['--qt-arg', 'stylesheet', 'foo'],
[sys.argv[0], '--stylesheet', 'foo']),
# --qt-arg given twice
(['--qt-arg', 'stylesheet', 'foo', '--qt-arg', 'geometry', 'bar'],
[sys.argv[0], '--stylesheet', 'foo', '--geometry', 'bar']),
# --qt-flag given twice
(['--qt-flag', 'foo', '--qt-flag', 'bar'],
[sys.argv[0], '--foo', '--bar']),
])
def test_qt_args(self, config_stub, args, expected, parser):
"""Test commandline with no Qt arguments given."""
parsed = parser.parse_args(args)
assert config.qt_args(parsed) == expected

def test_qt_both(self, config_stub, parser):
"""Test commandline with a Qt argument and flag."""
args = parser.parse_args(['--qt-arg', 'stylesheet', 'foobar',
'--qt-flag', 'reverse'])
qt_args = config.qt_args(args)
assert qt_args[0] == sys.argv[0]
assert '--reverse' in qt_args
assert '--stylesheet' in qt_args
assert 'foobar' in qt_args

def test_with_settings(self, config_stub, parser):
parsed = parser.parse_args(['--qt-flag', 'foo'])
config_stub.val.qt_args = ['bar']
assert config.qt_args(parsed) == [sys.argv[0], '--foo', '--bar']
46 changes: 0 additions & 46 deletions tests/unit/utils/test_qtutils.py
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -36,7 +36,6 @@
from PyQt5.QtCore import (QDataStream, QPoint, QUrl, QByteArray, QIODevice, from PyQt5.QtCore import (QDataStream, QPoint, QUrl, QByteArray, QIODevice,
QTimer, QBuffer, QFile, QProcess, QFileDevice) QTimer, QBuffer, QFile, QProcess, QFileDevice)


from qutebrowser import qutebrowser
from qutebrowser.utils import qtutils from qutebrowser.utils import qtutils
import overflow_test_cases import overflow_test_cases


Expand Down Expand Up @@ -119,51 +118,6 @@ def test_bad_values_nonfatal(self, ctype, val, repl):
assert newval == repl assert newval == repl




class TestGetQtArgs:

"""Tests for get_args."""

@pytest.fixture
def parser(self, mocker):
"""Fixture to provide an argparser.
Monkey-patches .exit() of the argparser so it doesn't exit on errors.
"""
parser = qutebrowser.get_argparser()
mocker.patch.object(parser, 'exit', side_effect=Exception)
return parser

@pytest.mark.parametrize('args, expected', [
# No Qt arguments
(['--debug'], [sys.argv[0]]),
# Qt flag
(['--debug', '--qt-flag', 'reverse'], [sys.argv[0], '--reverse']),
# Qt argument with value
(['--qt-arg', 'stylesheet', 'foo'],
[sys.argv[0], '--stylesheet', 'foo']),
# --qt-arg given twice
(['--qt-arg', 'stylesheet', 'foo', '--qt-arg', 'geometry', 'bar'],
[sys.argv[0], '--stylesheet', 'foo', '--geometry', 'bar']),
# --qt-flag given twice
(['--qt-flag', 'foo', '--qt-flag', 'bar'],
[sys.argv[0], '--foo', '--bar']),
])
def test_qt_args(self, args, expected, parser):
"""Test commandline with no Qt arguments given."""
parsed = parser.parse_args(args)
assert qtutils.get_args(parsed) == expected

def test_qt_both(self, parser):
"""Test commandline with a Qt argument and flag."""
args = parser.parse_args(['--qt-arg', 'stylesheet', 'foobar',
'--qt-flag', 'reverse'])
qt_args = qtutils.get_args(args)
assert qt_args[0] == sys.argv[0]
assert '--reverse' in qt_args
assert '--stylesheet' in qt_args
assert 'foobar' in qt_args


@pytest.mark.parametrize('os_name, qversion, expected', [ @pytest.mark.parametrize('os_name, qversion, expected', [
('linux', '5.2.1', True), # unaffected OS ('linux', '5.2.1', True), # unaffected OS
('linux', '5.4.1', True), # unaffected OS ('linux', '5.4.1', True), # unaffected OS
Expand Down

0 comments on commit fe05947

Please sign in to comment.