From 21b282ce29edfc7b65b0f8867de1c66a5be72322 Mon Sep 17 00:00:00 2001 From: Florian Bruhin Date: Tue, 5 Jul 2016 13:30:13 +0200 Subject: [PATCH] Get rid of _DummyUserscriptRunner This simplifies the code a bit and only provides userscript.run_async (and not the UserscriptRunner class) as entrypoint. --- qutebrowser/browser/commands.py | 7 +++- qutebrowser/browser/hints.py | 7 +++- qutebrowser/commands/userscripts.py | 50 +++++++++---------------- tests/unit/commands/test_userscripts.py | 11 +++--- 4 files changed, 33 insertions(+), 42 deletions(-) diff --git a/qutebrowser/browser/commands.py b/qutebrowser/browser/commands.py index d43116a1a..476453525 100644 --- a/qutebrowser/browser/commands.py +++ b/qutebrowser/browser/commands.py @@ -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): diff --git a/qutebrowser/browser/hints.py b/qutebrowser/browser/hints.py index d025f9385..dd403c5df 100644 --- a/qutebrowser/browser/hints.py +++ b/qutebrowser/browser/hints.py @@ -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. diff --git a/qutebrowser/commands/userscripts.py b/qutebrowser/commands/userscripts.py index 368ab2bf2..487330679 100644 --- a/qutebrowser/commands/userscripts.py +++ b/qutebrowser/commands/userscripts.py @@ -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))) diff --git a/tests/unit/commands/test_userscripts.py b/tests/unit/commands/test_userscripts.py index b19753745..a67b76338 100644 --- a/tests/unit/commands/test_userscripts.py +++ b/tests/unit/commands/test_userscripts.py @@ -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