2014-06-19 09:04:37 +02:00
|
|
|
# vim: ft=python fileencoding=utf-8 sts=4 sw=4 et:
|
|
|
|
|
2015-01-03 15:51:31 +01:00
|
|
|
# Copyright 2014-2015 Florian Bruhin (The Compiler) <mail@qutebrowser.org>
|
2014-02-06 14:01:23 +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/>.
|
2014-01-20 15:58:49 +01:00
|
|
|
|
2014-04-17 17:44:27 +02:00
|
|
|
"""Contains various command utils and a global command dict.
|
|
|
|
|
|
|
|
Module attributes:
|
|
|
|
cmd_dict: A mapping from command-strings to command objects.
|
|
|
|
"""
|
2014-02-17 12:23:52 +01:00
|
|
|
|
2015-04-06 17:25:42 +02:00
|
|
|
from qutebrowser.utils import qtutils, log
|
2014-09-15 07:42:21 +02:00
|
|
|
from qutebrowser.commands import command, cmdexc
|
2014-01-27 21:35:12 +01:00
|
|
|
|
2014-01-16 17:12:55 +01:00
|
|
|
cmd_dict = {}
|
2014-09-13 00:22:27 +02:00
|
|
|
aliases = []
|
2014-01-16 17:12:55 +01:00
|
|
|
|
2014-01-28 23:04:02 +01:00
|
|
|
|
2014-05-14 23:29:18 +02:00
|
|
|
def check_overflow(arg, ctype):
|
|
|
|
"""Check if the given argument is in bounds for the given type.
|
|
|
|
|
|
|
|
Args:
|
|
|
|
arg: The argument to check
|
|
|
|
ctype: The C/Qt type to check as a string.
|
|
|
|
"""
|
|
|
|
try:
|
2014-06-23 07:45:04 +02:00
|
|
|
qtutils.check_overflow(arg, ctype)
|
2014-05-14 23:29:18 +02:00
|
|
|
except OverflowError:
|
2014-08-26 19:10:14 +02:00
|
|
|
raise cmdexc.CommandError(
|
|
|
|
"Numeric argument is too large for internal {} "
|
|
|
|
"representation.".format(ctype))
|
2014-05-14 23:29:18 +02:00
|
|
|
|
|
|
|
|
2014-05-09 19:12:08 +02:00
|
|
|
def arg_or_count(arg, count, default=None, countzero=None):
|
|
|
|
"""Get a value based on an argument and count given to a command.
|
|
|
|
|
|
|
|
If both arg and count are set, ValueError is raised.
|
|
|
|
If only arg/count is set, it is used.
|
|
|
|
If none is set, a default is returned or ValueError is raised.
|
|
|
|
|
|
|
|
Args:
|
|
|
|
arg: The argument given to a command.
|
|
|
|
count: The count given to a command.
|
|
|
|
countzero: Special value if count is 0.
|
|
|
|
|
|
|
|
Return:
|
|
|
|
The value to use.
|
|
|
|
"""
|
|
|
|
if count is not None and arg is not None:
|
|
|
|
raise ValueError("Both count and argument given!")
|
|
|
|
elif arg is not None:
|
2014-09-02 21:54:07 +02:00
|
|
|
return arg
|
2014-05-09 19:12:08 +02:00
|
|
|
elif count is not None:
|
|
|
|
if countzero is not None and count == 0:
|
|
|
|
return countzero
|
|
|
|
else:
|
2014-09-02 21:54:07 +02:00
|
|
|
return count
|
2014-05-09 19:12:08 +02:00
|
|
|
elif default is not None:
|
2014-09-02 21:54:07 +02:00
|
|
|
return default
|
2014-05-09 19:12:08 +02:00
|
|
|
else:
|
|
|
|
raise ValueError("Either count or argument have to be set!")
|
|
|
|
|
|
|
|
|
2014-10-07 20:39:02 +02:00
|
|
|
def check_exclusive(flags, names):
|
|
|
|
"""Check if only one flag is set with exclusive flags.
|
|
|
|
|
|
|
|
Raise a CommandError if not.
|
|
|
|
|
|
|
|
Args:
|
|
|
|
flags: An iterable of booleans to check.
|
|
|
|
names: An iterable of flag names for the error message.
|
|
|
|
"""
|
|
|
|
if sum(1 for e in flags if e) > 1:
|
|
|
|
argstr = '/'.join('-' + e for e in names)
|
|
|
|
raise cmdexc.CommandError("Only one of {} can be given!".format(
|
|
|
|
argstr))
|
|
|
|
|
|
|
|
|
2014-04-16 11:05:58 +02:00
|
|
|
class register: # pylint: disable=invalid-name
|
2014-03-03 21:19:29 +01:00
|
|
|
|
2014-03-03 06:09:23 +01:00
|
|
|
"""Decorator to register a new command handler.
|
|
|
|
|
|
|
|
This could also be a function, but as a class (with a "wrong" name) it's
|
|
|
|
much cleaner to implement.
|
2014-02-28 17:55:17 +01:00
|
|
|
|
2014-03-04 07:02:45 +01:00
|
|
|
Attributes:
|
2014-09-25 07:41:51 +02:00
|
|
|
_instance: The object from the object registry to be used as "self".
|
2014-09-24 22:17:53 +02:00
|
|
|
_name: The name (as string) or names (as list) of the command.
|
2015-04-06 17:25:42 +02:00
|
|
|
_kwargs: The arguments to pass to Command.
|
2014-02-28 17:55:17 +01:00
|
|
|
"""
|
2014-03-03 06:09:23 +01:00
|
|
|
|
2015-04-06 17:25:42 +02:00
|
|
|
def __init__(self, *, instance=None, name=None, **kwargs):
|
2014-03-24 11:48:56 +01:00
|
|
|
"""Save decorator arguments.
|
|
|
|
|
|
|
|
Gets called on parse-time with the decorator arguments.
|
2014-03-04 07:02:45 +01:00
|
|
|
|
2014-04-17 17:44:27 +02:00
|
|
|
Args:
|
2014-03-04 07:02:45 +01:00
|
|
|
See class attributes.
|
|
|
|
"""
|
2014-09-24 22:17:53 +02:00
|
|
|
self._instance = instance
|
2015-04-06 17:25:42 +02:00
|
|
|
self._name = name
|
|
|
|
self._kwargs = kwargs
|
2014-03-03 06:09:23 +01:00
|
|
|
|
2014-09-15 07:42:21 +02:00
|
|
|
def _get_names(self, func):
|
2014-09-02 21:54:07 +02:00
|
|
|
"""Get the name(s) which should be used for the current command.
|
|
|
|
|
2015-03-31 20:49:29 +02:00
|
|
|
If the name hasn't been overridden explicitly, the function name is
|
2014-09-02 21:54:07 +02:00
|
|
|
transformed.
|
|
|
|
|
|
|
|
If it has been set, it can either be a string which is
|
|
|
|
used directly, or an iterable.
|
|
|
|
|
2014-09-15 07:42:21 +02:00
|
|
|
Args:
|
|
|
|
func: The function to get the name of.
|
|
|
|
|
2014-09-02 21:54:07 +02:00
|
|
|
Return:
|
|
|
|
A list of names, with the main name being the first item.
|
|
|
|
"""
|
2014-09-24 22:17:53 +02:00
|
|
|
if self._name is None:
|
2014-09-15 07:42:21 +02:00
|
|
|
return [func.__name__.lower().replace('_', '-')]
|
2014-09-24 22:17:53 +02:00
|
|
|
elif isinstance(self._name, str):
|
|
|
|
return [self._name]
|
2014-05-16 23:01:40 +02:00
|
|
|
else:
|
2014-09-24 22:17:53 +02:00
|
|
|
return self._name
|
2014-09-02 21:54:07 +02:00
|
|
|
|
2014-09-15 07:42:21 +02:00
|
|
|
def __call__(self, func):
|
|
|
|
"""Register the command before running the function.
|
2014-09-14 23:34:55 +02:00
|
|
|
|
2014-09-15 07:42:21 +02:00
|
|
|
Gets called when a function should be decorated.
|
2014-09-03 10:47:27 +02:00
|
|
|
|
2014-09-15 07:42:21 +02:00
|
|
|
Doesn't actually decorate anything, but creates a Command object and
|
|
|
|
registers it in the cmd_dict.
|
2014-03-03 06:09:23 +01:00
|
|
|
|
|
|
|
Args:
|
2014-09-15 07:42:21 +02:00
|
|
|
func: The function to be decorated.
|
2014-03-03 06:09:23 +01:00
|
|
|
|
|
|
|
Return:
|
2014-09-15 07:42:21 +02:00
|
|
|
The original function (unmodified).
|
2014-09-03 10:47:27 +02:00
|
|
|
"""
|
2014-09-15 07:42:21 +02:00
|
|
|
global aliases
|
|
|
|
names = self._get_names(func)
|
|
|
|
log.commands.vdebug("Registering command {}".format(names[0]))
|
|
|
|
for name in names:
|
|
|
|
if name in cmd_dict:
|
|
|
|
raise ValueError("{} is already registered!".format(name))
|
2015-04-06 17:25:42 +02:00
|
|
|
cmd = command.Command(name=names[0], instance=self._instance,
|
|
|
|
handler=func, **self._kwargs)
|
2014-09-15 07:42:21 +02:00
|
|
|
for name in names:
|
|
|
|
cmd_dict[name] = cmd
|
|
|
|
aliases += names[1:]
|
|
|
|
return func
|