Get rid of _DummyUserscriptRunner
This simplifies the code a bit and only provides userscript.run_async (and not the UserscriptRunner class) as entrypoint.
This commit is contained in:
parent
a6307497c0
commit
21b282ce29
@ -1048,8 +1048,11 @@ class CommandDispatcher:
|
||||
else:
|
||||
env['QUTE_URL'] = url.toString(QUrl.FullyEncoded)
|
||||
|
||||
userscripts.run_async(tab, cmd, *args, win_id=self._win_id, env=env,
|
||||
verbose=verbose)
|
||||
try:
|
||||
userscripts.run_async(tab, cmd, *args, win_id=self._win_id,
|
||||
env=env, verbose=verbose)
|
||||
except userscripts.UnsupportedError as e:
|
||||
raise cmdexc.CommandError(e)
|
||||
|
||||
@cmdutils.register(instance='command-dispatcher', scope='window')
|
||||
def quickmark_save(self):
|
||||
|
@ -580,8 +580,11 @@ class HintManager(QObject):
|
||||
if url is not None:
|
||||
env['QUTE_URL'] = url.toString(QUrl.FullyEncoded)
|
||||
|
||||
userscripts.run_async(context.tab, cmd, *args, win_id=self._win_id,
|
||||
env=env)
|
||||
try:
|
||||
userscripts.run_async(context.tab, cmd, *args, win_id=self._win_id,
|
||||
env=env)
|
||||
except userscripts.UnsupportedError as e:
|
||||
message.error(self._win_id, str(e), immediately=True)
|
||||
|
||||
def _spawn(self, url, context):
|
||||
"""Spawn a simple command from a hint.
|
||||
|
@ -330,43 +330,20 @@ class _WindowsUserscriptRunner(_BaseUserscriptRunner):
|
||||
return
|
||||
|
||||
|
||||
class _DummyUserscriptRunner(QObject):
|
||||
class UnsupportedError(Exception):
|
||||
|
||||
"""Simple dummy runner which displays an error when using userscripts.
|
||||
"""Raised when userscripts aren't supported on this platform."""
|
||||
|
||||
Used on unknown systems since we don't know what (or if any) approach will
|
||||
work there.
|
||||
|
||||
Signals:
|
||||
finished: Always emitted.
|
||||
"""
|
||||
|
||||
finished = pyqtSignal()
|
||||
|
||||
def __init__(self, win_id, parent=None):
|
||||
# pylint: disable=unused-argument
|
||||
super().__init__(parent)
|
||||
|
||||
def prepare_run(self, *args, **kwargs):
|
||||
"""Print an error as userscripts are not supported."""
|
||||
# pylint: disable=unused-argument,unused-variable
|
||||
self.finished.emit()
|
||||
raise cmdexc.CommandError(
|
||||
"Userscripts are not supported on this platform!")
|
||||
|
||||
|
||||
# Here we basically just assign a generic UserscriptRunner class which does the
|
||||
# right thing depending on the platform.
|
||||
if os.name == 'posix':
|
||||
UserscriptRunner = _POSIXUserscriptRunner
|
||||
elif os.name == 'nt': # pragma: no cover
|
||||
UserscriptRunner = _WindowsUserscriptRunner
|
||||
else: # pragma: no cover
|
||||
UserscriptRunner = _DummyUserscriptRunner
|
||||
def __str__(self):
|
||||
return "Userscripts are not supported on this platform!"
|
||||
|
||||
|
||||
def run_async(tab, cmd, *args, win_id, env, verbose=False):
|
||||
"""Convenience method to run a userscript.
|
||||
"""Run an userscript after dumping page html/source.
|
||||
|
||||
Raises:
|
||||
UnsupportedError if userscripts are not supported on the current
|
||||
platform.
|
||||
|
||||
Args:
|
||||
tab: The WebKitTab/WebEngineTab to get the source from.
|
||||
@ -379,7 +356,14 @@ def run_async(tab, cmd, *args, win_id, env, verbose=False):
|
||||
tabbed_browser = objreg.get('tabbed-browser', scope='window',
|
||||
window=win_id)
|
||||
commandrunner = runners.CommandRunner(win_id, parent=tabbed_browser)
|
||||
runner = UserscriptRunner(win_id, tabbed_browser)
|
||||
|
||||
if os.name == 'posix':
|
||||
runner = _POSIXUserscriptRunner(win_id, tabbed_browser)
|
||||
elif os.name == 'nt': # pragma: no cover
|
||||
runner = _WindowsUserscriptRunner(win_id, tabbed_browser)
|
||||
else: # pragma: no cover
|
||||
raise UnsupportedError
|
||||
|
||||
runner.got_cmd.connect(
|
||||
lambda cmd:
|
||||
log.commands.debug("Got userscript command: {}".format(cmd)))
|
||||
|
@ -231,8 +231,9 @@ def test_temporary_files_failed_cleanup(caplog, qtbot, py_proc, runner):
|
||||
assert caplog.records[0].message.startswith(expected)
|
||||
|
||||
|
||||
def test_dummy_runner(qtbot):
|
||||
runner = userscripts._DummyUserscriptRunner(0)
|
||||
with pytest.raises(cmdexc.CommandError):
|
||||
with qtbot.waitSignal(runner.finished):
|
||||
runner.prepare_run('cmd', 'arg')
|
||||
def test_unsupported(monkeypatch, tabbed_browser_stubs):
|
||||
monkeypatch.setattr(userscripts.os, 'name', 'toaster')
|
||||
with pytest.raises(userscripts.UnsupportedError) as excinfo:
|
||||
userscripts.run_async(tab=None, cmd=None, win_id=0, env=None)
|
||||
expected ="Userscripts are not supported on this platform!"
|
||||
assert str(excinfo.value) == expected
|
||||
|
Loading…
Reference in New Issue
Block a user