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:
Florian Bruhin 2016-07-05 13:30:13 +02:00
parent a6307497c0
commit 21b282ce29
4 changed files with 33 additions and 42 deletions

View File

@ -1048,8 +1048,11 @@ class CommandDispatcher:
else: else:
env['QUTE_URL'] = url.toString(QUrl.FullyEncoded) env['QUTE_URL'] = url.toString(QUrl.FullyEncoded)
userscripts.run_async(tab, cmd, *args, win_id=self._win_id, env=env, try:
verbose=verbose) 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') @cmdutils.register(instance='command-dispatcher', scope='window')
def quickmark_save(self): def quickmark_save(self):

View File

@ -580,8 +580,11 @@ class HintManager(QObject):
if url is not None: if url is not None:
env['QUTE_URL'] = url.toString(QUrl.FullyEncoded) env['QUTE_URL'] = url.toString(QUrl.FullyEncoded)
userscripts.run_async(context.tab, cmd, *args, win_id=self._win_id, try:
env=env) 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): def _spawn(self, url, context):
"""Spawn a simple command from a hint. """Spawn a simple command from a hint.

View File

@ -330,43 +330,20 @@ class _WindowsUserscriptRunner(_BaseUserscriptRunner):
return 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 def __str__(self):
work there. return "Userscripts are not supported on this platform!"
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 run_async(tab, cmd, *args, win_id, env, verbose=False): 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: Args:
tab: The WebKitTab/WebEngineTab to get the source from. 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', tabbed_browser = objreg.get('tabbed-browser', scope='window',
window=win_id) window=win_id)
commandrunner = runners.CommandRunner(win_id, parent=tabbed_browser) 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( runner.got_cmd.connect(
lambda cmd: lambda cmd:
log.commands.debug("Got userscript command: {}".format(cmd))) log.commands.debug("Got userscript command: {}".format(cmd)))

View File

@ -231,8 +231,9 @@ def test_temporary_files_failed_cleanup(caplog, qtbot, py_proc, runner):
assert caplog.records[0].message.startswith(expected) assert caplog.records[0].message.startswith(expected)
def test_dummy_runner(qtbot): def test_unsupported(monkeypatch, tabbed_browser_stubs):
runner = userscripts._DummyUserscriptRunner(0) monkeypatch.setattr(userscripts.os, 'name', 'toaster')
with pytest.raises(cmdexc.CommandError): with pytest.raises(userscripts.UnsupportedError) as excinfo:
with qtbot.waitSignal(runner.finished): userscripts.run_async(tab=None, cmd=None, win_id=0, env=None)
runner.prepare_run('cmd', 'arg') expected ="Userscripts are not supported on this platform!"
assert str(excinfo.value) == expected