2014-02-06 14:01:23 +01:00
|
|
|
# Copyright 2014 Florian Bruhin (The Compiler) <mail@qutebrowser.org>
|
|
|
|
#
|
|
|
|
# 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-02-24 19:11:43 +01:00
|
|
|
"""Contains various command utils and a global command dict."""
|
2014-02-17 12:23:52 +01:00
|
|
|
|
|
|
|
import inspect
|
2014-04-10 07:02:24 +02:00
|
|
|
from collections import Iterable
|
2014-01-20 15:58:49 +01:00
|
|
|
|
2014-03-03 21:06:10 +01:00
|
|
|
from qutebrowser.commands.command import Command
|
2014-01-27 21:35:12 +01:00
|
|
|
|
2014-01-29 15:30:19 +01:00
|
|
|
# A mapping from command-strings to command objects.
|
2014-01-16 17:12:55 +01:00
|
|
|
cmd_dict = {}
|
|
|
|
|
2014-01-28 23:04:02 +01:00
|
|
|
|
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:
|
|
|
|
instance: The instance to be used as "self", as a dotted string.
|
|
|
|
name: The name (as string) or names (as list) of the command.
|
2014-04-10 07:02:24 +02:00
|
|
|
nargs: A (minargs, maxargs) tuple of valid argument counts, or an int.
|
2014-03-04 07:02:45 +01:00
|
|
|
split_args: Whether to split the arguments or not.
|
|
|
|
hide: Whether to hide the command or not.
|
2014-03-21 20:01:13 +01:00
|
|
|
completion: Which completion to use for arguments, as a list of
|
|
|
|
strings.
|
2014-02-28 17:55:17 +01:00
|
|
|
"""
|
2014-03-03 06:09:23 +01:00
|
|
|
|
2014-03-03 17:56:53 +01:00
|
|
|
def __init__(self, instance=None, name=None, nargs=None, split_args=True,
|
2014-03-21 20:01:13 +01:00
|
|
|
hide=False, completion=None):
|
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
|
|
|
|
|
|
|
Arguments:
|
|
|
|
See class attributes.
|
|
|
|
"""
|
2014-03-03 06:09:23 +01:00
|
|
|
self.name = name
|
|
|
|
self.split_args = split_args
|
|
|
|
self.hide = hide
|
2014-04-10 11:48:26 +02:00
|
|
|
self.nargs = nargs
|
2014-03-03 17:56:53 +01:00
|
|
|
self.instance = instance
|
2014-03-21 20:01:13 +01:00
|
|
|
self.completion = completion
|
2014-03-03 06:09:23 +01:00
|
|
|
|
|
|
|
def __call__(self, func):
|
2014-03-24 11:48:56 +01:00
|
|
|
"""Register the command before running the function.
|
|
|
|
|
|
|
|
Gets called when a function should be decorated.
|
2014-03-04 07:02:45 +01:00
|
|
|
|
|
|
|
Doesn't actually decorate anything, but creates a Command object and
|
|
|
|
registers it in the cmd_dict.
|
|
|
|
|
|
|
|
Arguments:
|
|
|
|
func: The function to be decorated.
|
|
|
|
|
|
|
|
Return:
|
|
|
|
The original function (unmodified).
|
|
|
|
"""
|
2014-03-24 11:48:56 +01:00
|
|
|
# FIXME: only register commands once
|
2014-03-03 06:09:23 +01:00
|
|
|
names = []
|
|
|
|
name = func.__name__.lower() if self.name is None else self.name
|
|
|
|
if isinstance(name, str):
|
|
|
|
mainname = name
|
|
|
|
names.append(name)
|
|
|
|
else:
|
|
|
|
mainname = name[0]
|
|
|
|
names += name
|
|
|
|
count, nargs = self._get_nargs_count(func)
|
|
|
|
desc = func.__doc__.splitlines()[0].strip().rstrip('.')
|
2014-03-03 21:06:10 +01:00
|
|
|
cmd = Command(name=mainname, split_args=self.split_args,
|
|
|
|
hide=self.hide, nargs=nargs, count=count, desc=desc,
|
2014-03-21 20:01:13 +01:00
|
|
|
instance=self.instance, handler=func,
|
|
|
|
completion=self.completion)
|
2014-03-03 06:09:23 +01:00
|
|
|
for name in names:
|
|
|
|
cmd_dict[name] = cmd
|
|
|
|
return func
|
|
|
|
|
|
|
|
def _get_nargs_count(self, func):
|
|
|
|
"""Get the number of command-arguments and count-support for a func.
|
|
|
|
|
|
|
|
Args:
|
|
|
|
func: The function to get the argcount for.
|
|
|
|
|
|
|
|
Return:
|
|
|
|
A (count, (minargs, maxargs)) tuple, with maxargs=None if there are
|
|
|
|
infinite args. count is True if the function supports count, else
|
|
|
|
False.
|
|
|
|
|
|
|
|
Mapping from old nargs format to (minargs, maxargs):
|
|
|
|
? (0, 1)
|
|
|
|
N (N, N)
|
|
|
|
+ (1, None)
|
|
|
|
* (0, None)
|
|
|
|
"""
|
2014-03-03 21:19:29 +01:00
|
|
|
# pylint: disable=no-member
|
|
|
|
# pylint: disable=unpacking-non-sequence
|
2014-03-03 06:09:23 +01:00
|
|
|
# We could use inspect.signature maybe, but that's python >= 3.3 only.
|
|
|
|
spec = inspect.getfullargspec(func)
|
|
|
|
count = 'count' in spec.args
|
|
|
|
# we assume count always has a default (and it should!)
|
|
|
|
if self.nargs is not None:
|
|
|
|
# If nargs is overriden, use that.
|
2014-04-10 12:24:41 +02:00
|
|
|
if isinstance(self.nargs, Iterable):
|
|
|
|
# Iterable (min, max)
|
|
|
|
minargs, maxargs = self.nargs
|
|
|
|
else:
|
2014-03-03 06:09:23 +01:00
|
|
|
# Single int
|
|
|
|
minargs, maxargs = self.nargs, self.nargs
|
|
|
|
else:
|
|
|
|
defaultcount = (len(spec.defaults) if spec.defaults is not None
|
2014-03-03 21:19:29 +01:00
|
|
|
else 0)
|
2014-03-03 06:09:23 +01:00
|
|
|
argcount = len(spec.args)
|
|
|
|
if 'self' in spec.args:
|
|
|
|
argcount -= 1
|
|
|
|
minargs = argcount - defaultcount
|
|
|
|
if spec.varargs is not None:
|
|
|
|
maxargs = None
|
|
|
|
else:
|
2014-04-10 11:48:26 +02:00
|
|
|
maxargs = argcount - int(count) # -1 if count is defined
|
2014-03-03 06:09:23 +01:00
|
|
|
return (count, (minargs, maxargs))
|