qutebrowser/qutebrowser/commands/runners.py

330 lines
11 KiB
Python
Raw Normal View History

2014-06-19 09:04:37 +02:00
# vim: ft=python fileencoding=utf-8 sts=4 sw=4 et:
2016-01-04 07:12:39 +01:00
# Copyright 2014-2016 Florian Bruhin (The Compiler) <mail@qutebrowser.org>
2014-02-24 19:11:43 +01:00
#
# This file is part of qutebrowser.
#
# qutebrowser is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# qutebrowser is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with qutebrowser. If not, see <http://www.gnu.org/licenses/>.
"""Module containing command managers (SearchRunner and CommandRunner)."""
2014-02-24 19:11:43 +01:00
import collections
import traceback
from PyQt5.QtCore import pyqtSlot, QUrl, QObject
2014-02-24 19:11:43 +01:00
from qutebrowser.config import config, configexc
from qutebrowser.commands import cmdexc, cmdutils
from qutebrowser.utils import message, objreg, qtutils, utils
from qutebrowser.misc import split
2014-02-24 19:11:43 +01:00
2014-02-24 16:47:32 +01:00
ParseResult = collections.namedtuple('ParseResult', ['cmd', 'args', 'cmdline',
'count'])
last_command = {}
def _current_url(tabbed_browser):
"""Convenience method to get the current url."""
try:
return tabbed_browser.current_url()
except qtutils.QtValueError as e:
msg = "Current URL is invalid"
if e.reason:
msg += " ({})".format(e.reason)
msg += "!"
raise cmdexc.CommandError(msg)
2014-09-28 23:23:02 +02:00
def replace_variables(win_id, arglist):
"""Utility function to replace variables like {url} in a list of args."""
variables = {
'{url}': lambda: _current_url(tabbed_browser).toString(
QUrl.FullyEncoded | QUrl.RemovePassword),
'{url:pretty}': lambda: _current_url(tabbed_browser).toString(
QUrl.RemovePassword),
'{clipboard}': utils.get_clipboard,
'{primary}': lambda: utils.get_clipboard(selection=True),
}
values = {}
args = []
2014-09-28 23:23:02 +02:00
tabbed_browser = objreg.get('tabbed-browser', scope='window',
window=win_id)
try:
for arg in arglist:
for var, func in variables.items():
if var in arg:
if var not in values:
values[var] = func()
arg = arg.replace(var, values[var])
args.append(arg)
2016-08-10 12:33:01 +02:00
except utils.ClipboardError as e:
raise cmdexc.CommandError(e)
return args
2014-09-29 20:16:38 +02:00
class CommandRunner(QObject):
2014-02-24 19:11:43 +01:00
"""Parse and run qutebrowser commandline commands.
2014-02-24 19:11:43 +01:00
Attributes:
2014-09-28 22:13:14 +02:00
_win_id: The window this CommandRunner is associated with.
_partial_match: Whether to allow partial command matches.
2014-02-24 19:11:43 +01:00
"""
def __init__(self, win_id, partial_match=False, parent=None):
2014-09-29 20:16:38 +02:00
super().__init__(parent)
self._partial_match = partial_match
2014-09-28 22:13:14 +02:00
self._win_id = win_id
2014-02-24 19:11:43 +01:00
2016-06-12 23:04:14 +02:00
def _get_alias(self, text, default=None):
2014-06-03 06:46:13 +02:00
"""Get an alias from the config.
Args:
text: The text to parse.
2016-06-30 12:40:52 +02:00
default : Default value to return when alias was not found.
2014-06-03 06:46:13 +02:00
Return:
The new command string if an alias was found. Default value
otherwise.
2014-06-03 06:46:13 +02:00
"""
parts = text.strip().split(maxsplit=1)
try:
alias = config.get('aliases', parts[0])
except (configexc.NoOptionError, configexc.NoSectionError):
return default
2014-06-03 06:46:13 +02:00
try:
new_cmd = '{} {}'.format(alias, parts[1])
except IndexError:
new_cmd = alias
2014-06-03 06:46:13 +02:00
if text.endswith(' '):
new_cmd += ' '
return new_cmd
2016-06-12 23:04:14 +02:00
def parse_all(self, text, aliases=True, *args, **kwargs):
"""Split a command on ;; and parse all parts.
If the first command in the commandline is a non-split one, it only
returns that.
Args:
text: Text to parse.
aliases: Whether to handle aliases.
*args/**kwargs: Passed to parse().
Yields:
ParseResult tuples.
"""
if not text.strip():
raise cmdexc.NoSuchCommandError("No command given")
if aliases:
text = self._get_alias(text, text)
if ';;' in text:
# Get the first command and check if it doesn't want to have ;;
# split.
first = text.split(';;')[0]
result = self.parse(first, *args, **kwargs)
if result.cmd.no_cmd_split:
sub_texts = [text]
else:
sub_texts = [e.strip() for e in text.split(';;')]
else:
sub_texts = [text]
for sub in sub_texts:
yield self.parse(sub, *args, **kwargs)
2015-11-16 20:24:27 +01:00
def _parse_count(self, cmdstr):
"""Split a count prefix off from a command for parse().
Args:
cmdstr: The command/args including the count.
Return:
A (count, cmdstr) tuple, with count being None or int.
"""
if ':' not in cmdstr:
return (None, cmdstr)
count, cmdstr = cmdstr.split(':', maxsplit=1)
try:
count = int(count)
except ValueError:
# We just ignore invalid prefixes
count = None
return (count, cmdstr)
2016-06-06 17:09:16 +02:00
def _parse_fallback(self, text, count, keep):
"""Parse the given commandline without a valid command."""
if keep:
cmdstr, sep, argstr = text.partition(' ')
cmdline = [cmdstr, sep] + argstr.split()
else:
cmdline = text.split()
return ParseResult(cmd=None, args=None, cmdline=cmdline, count=count)
def parse(self, text, *, fallback=False, keep=False):
2014-02-24 19:11:43 +01:00
"""Split the commandline text into command and arguments.
Args:
text: Text to parse.
2014-06-03 06:46:13 +02:00
fallback: Whether to do a fallback splitting when the command was
unknown.
2014-11-06 07:15:02 +01:00
keep: Whether to keep special chars and whitespace
2014-02-24 19:11:43 +01:00
2014-04-09 06:58:17 +02:00
Return:
A ParseResult tuple.
2014-02-24 19:11:43 +01:00
"""
2014-11-06 07:15:02 +01:00
cmdstr, sep, argstr = text.partition(' ')
2015-11-16 20:24:27 +01:00
count, cmdstr = self._parse_count(cmdstr)
2014-11-10 07:59:39 +01:00
if not cmdstr and not fallback:
2014-08-26 19:10:14 +02:00
raise cmdexc.NoSuchCommandError("No command given")
2016-06-06 17:09:16 +02:00
if self._partial_match:
cmdstr = self._completion_match(cmdstr)
2016-04-28 15:20:16 +02:00
2014-02-24 19:11:43 +01:00
try:
cmd = cmdutils.cmd_dict[cmdstr]
2014-02-24 19:11:43 +01:00
except KeyError:
2016-06-06 17:09:16 +02:00
if not fallback:
raise cmdexc.NoSuchCommandError(
'{}: no such command'.format(cmdstr))
return self._parse_fallback(text, count, keep)
args = self._split_args(cmd, argstr, keep)
if keep and args:
cmdline = [cmdstr, sep + args[0]] + args[1:]
elif keep:
cmdline = [cmdstr, sep]
2014-11-06 07:15:02 +01:00
else:
2016-06-06 17:09:16 +02:00
cmdline = [cmdstr] + args[:]
return ParseResult(cmd=cmd, args=args, cmdline=cmdline, count=count)
2014-11-06 07:15:02 +01:00
2016-04-28 15:20:16 +02:00
def _completion_match(self, cmdstr):
"""Replace cmdstr with a matching completion if there's only one match.
Args:
cmdstr: The string representing the entered command so far
Return:
cmdstr modified to the matching completion or unmodified
"""
matches = []
2016-07-11 13:13:16 +02:00
for valid_command in cmdutils.cmd_dict:
2016-04-28 15:20:16 +02:00
if valid_command.find(cmdstr) == 0:
matches.append(valid_command)
if len(matches) == 1:
cmdstr = matches[0]
return cmdstr
def _split_args(self, cmd, argstr, keep):
2014-11-06 07:15:02 +01:00
"""Split the arguments from an arg string.
2014-11-06 07:15:02 +01:00
Args:
cmd: The command we're currently handling.
2014-11-06 07:15:02 +01:00
argstr: An argument string.
keep: Whether to keep special chars and whitespace
Return:
2015-10-04 15:41:42 +02:00
A list containing the split strings.
2014-11-06 07:15:02 +01:00
"""
if not argstr:
return []
elif cmd.maxsplit is None:
return split.split(argstr, keep=keep)
2014-02-24 19:11:43 +01:00
else:
2014-09-02 21:54:07 +02:00
# If split=False, we still want to split the flags, but not
# everything after that.
# We first split the arg string and check the index of the first
# non-flag args, then we re-split again properly.
# example:
#
# input: "--foo -v bar baz"
# first split: ['--foo', '-v', 'bar', 'baz']
# 0 1 2 3
# second split: ['--foo', '-v', 'bar baz']
# (maxsplit=2)
split_args = split.simple_split(argstr, keep=keep)
flag_arg_count = 0
2014-09-02 21:54:07 +02:00
for i, arg in enumerate(split_args):
arg = arg.strip()
if arg.startswith('-'):
if arg in cmd.flags_with_args:
flag_arg_count += 1
else:
maxsplit = i + cmd.maxsplit + flag_arg_count
return split.simple_split(argstr, keep=keep,
maxsplit=maxsplit)
2015-12-01 20:53:17 +01:00
# If there are only flags, we got it right on the first try
# already.
return split_args
2014-02-24 19:11:43 +01:00
2014-04-30 10:41:25 +02:00
def run(self, text, count=None):
2014-09-02 21:54:07 +02:00
"""Parse a command from a line of text and run it.
2014-02-24 19:11:43 +01:00
Args:
text: The text to parse.
count: The count to pass to the command.
"""
for result in self.parse_all(text):
mode_manager = objreg.get('mode-manager', scope='window',
window=self._win_id)
cur_mode = mode_manager.mode
if result.cmd.no_replace_variables:
args = result.args
else:
args = replace_variables(self._win_id, result.args)
if count is not None:
if result.count is not None:
raise cmdexc.CommandMetaError("Got count via command and "
"prefix!")
result.cmd.run(self._win_id, args, count=count)
elif result.count is not None:
result.cmd.run(self._win_id, args, count=result.count)
else:
result.cmd.run(self._win_id, args)
2014-04-30 10:41:25 +02:00
if result.cmdline[0] != 'repeat-command':
last_command[cur_mode] = (
self._parse_count(text)[1],
count if count is not None else result.count)
2016-06-27 17:38:11 +02:00
2014-04-30 10:41:25 +02:00
@pyqtSlot(str, int)
@pyqtSlot(str)
2014-04-30 10:41:25 +02:00
def run_safely(self, text, count=None):
"""Run a command and display exceptions in the statusbar."""
try:
self.run(text, count)
2014-08-26 19:10:14 +02:00
except (cmdexc.CommandMetaError, cmdexc.CommandError) as e:
message.error(self._win_id, e, immediately=True,
stack=traceback.format_exc())
@pyqtSlot(str, int)
def run_safely_init(self, text, count=None):
"""Run a command and display exceptions in the statusbar.
Contrary to run_safely, error messages are queued so this is more
2016-04-08 07:35:53 +02:00
suitable to use while initializing.
"""
try:
self.run(text, count)
2014-08-26 19:10:14 +02:00
except (cmdexc.CommandMetaError, cmdexc.CommandError) as e:
message.error(self._win_id, e, stack=traceback.format_exc())