Merge branch 'issue1940' of https://github.com/HolySmoke86/qutebrowser into HolySmoke86-issue1940

This commit is contained in:
Florian Bruhin 2016-10-03 06:41:19 +02:00
commit c7b74edfbe
3 changed files with 62 additions and 7 deletions

View File

@ -1126,7 +1126,7 @@ class CommandDispatcher:
try:
userscripts.run_async(tab, cmd, *args, win_id=self._win_id,
env=env, verbose=verbose)
except userscripts.UnsupportedError as e:
except userscripts.Error as e:
raise cmdexc.CommandError(e)
@cmdutils.register(instance='command-dispatcher', scope='window')

View File

@ -315,7 +315,34 @@ class _WindowsUserscriptRunner(_BaseUserscriptRunner):
return
class UnsupportedError(Exception):
class Error(Exception):
"""Base class for userscript exceptions."""
class NotFoundError(Error):
"""Raised when spawning a userscript that doesn't exist.
Attributes:
script_name: name of the userscript as called
paths: path names that were searched for the userscript
"""
def __init__(self, script_name, paths=None):
super().__init__()
self.script_name = script_name
self.paths = paths
def __str__(self):
msg = "Userscript '{}' not found".format(self.script_name)
if self.paths:
msg += " in userscript directories {}".format(
', '.join(repr(path) for path in self.paths))
return msg
class UnsupportedError(Error):
"""Raised when userscripts aren't supported on this platform."""
@ -323,12 +350,37 @@ class UnsupportedError(Exception):
return "Userscripts are not supported on this platform!"
def _lookup_path(cmd):
"""Search userscript directories for given command.
Raises:
NotFoundError if the command could not be found.
Args:
cmd: The command to look for.
Returns:
A path to the userscript.
"""
directories = [
os.path.join(standarddir.data(), "userscripts"),
os.path.join(standarddir.system_data(), "userscripts"),
]
for directory in directories:
cmd_path = os.path.join(directory, cmd)
if os.path.exists(cmd_path):
return cmd_path
raise NotFoundError(cmd, directories)
def run_async(tab, cmd, *args, win_id, env, verbose=False):
"""Run a userscript after dumping page html/source.
Raises:
UnsupportedError if userscripts are not supported on the current
platform.
NotFoundError if the command could not be found.
Args:
tab: The WebKitTab/WebEngineTab to get the source from.
@ -371,10 +423,9 @@ def run_async(tab, cmd, *args, win_id, env, verbose=False):
# ~/.local/share/qutebrowser/userscripts (or $XDG_DATA_DIR)
if not os.path.isabs(cmd_path):
log.misc.debug("{} is no absolute path".format(cmd_path))
cmd_path = os.path.join(standarddir.data(), "userscripts", cmd)
if not os.path.exists(cmd_path):
cmd_path = os.path.join(standarddir.system_data(),
"userscripts", cmd)
cmd_path = _lookup_path(cmd)
elif not os.path.exists(cmd_path):
raise NotFoundError(cmd_path)
log.misc.debug("Userscript to run: {}".format(cmd_path))
runner.finished.connect(commandrunner.deleteLater)

View File

@ -10,7 +10,11 @@ Feature: :spawn
Scenario: Starting a userscript which doesn't exist
When I run :spawn -u this_does_not_exist
Then the error "Error while spawning userscript: The process failed to start." should be shown
Then the error "Userscript 'this_does_not_exist' not found in userscript directories *" should be shown
Scenario: Starting a userscript with absoloute path which doesn't exist
When I run :spawn -u /this_does_not_exist
Then the error "Userscript '/this_does_not_exist' not found" should be shown
# https://github.com/The-Compiler/qutebrowser/issues/1614
@posix