qutebrowser/qutebrowser/commands/command.py

152 lines
5.8 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:
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-29 15:30:19 +01:00
2014-02-17 12:23:52 +01:00
"""Contains the Command class, a skeleton for a command."""
from PyQt5.QtCore import QCoreApplication, QUrl
2014-05-23 16:11:55 +02:00
from PyQt5.QtWebKit import QWebSettings
2014-01-29 06:28:21 +01:00
2014-05-14 18:00:40 +02:00
from qutebrowser.commands.exceptions import (ArgumentCountError,
PrerequisitesError)
from qutebrowser.utils.misc import dotted_getattr
2014-05-23 16:11:55 +02:00
from qutebrowser.utils.log import commands as logger
2014-01-29 06:28:21 +01:00
2014-03-03 21:06:10 +01:00
class Command:
2014-02-07 20:21:50 +01:00
2014-01-29 15:30:19 +01:00
"""Base skeleton for a command.
2014-03-03 06:09:23 +01:00
Attributes:
2014-03-04 07:02:45 +01:00
name: The main name of the command.
split: Whether to split the arguments.
2014-03-04 07:02:45 +01:00
hide: Whether to hide the arguments or not.
nargs: A (minargs, maxargs) tuple, maxargs = None if there's no limit.
count: Whether the command supports a count, or not.
desc: The description of the command.
instance: How to get to the "self" argument of the handler.
A dotted string as viewed from app.py, or None.
handler: The handler function to call.
2014-03-21 20:01:13 +01:00
completion: Completions to use for arguments, as a list of strings.
2014-04-29 23:06:07 +02:00
needs_js: Whether the command needs javascript enabled
2014-06-16 09:44:11 +02:00
debug: Whether this is a debugging command (only shown with --debug).
2014-01-29 06:28:21 +01:00
"""
2014-04-22 14:45:24 +02:00
# TODO:
2014-01-29 06:28:21 +01:00
# we should probably have some kind of typing / argument casting for args
# this might be combined with help texts or so as well
def __init__(self, name, split, hide, nargs, count, desc, instance,
2014-06-16 09:44:11 +02:00
handler, completion, modes, not_modes, needs_js, debug):
2014-04-25 11:21:00 +02:00
# I really don't know how to solve this in a better way, I tried.
# pylint: disable=too-many-arguments
2014-02-28 17:00:25 +01:00
self.name = name
self.split = split
2014-02-28 17:00:25 +01:00
self.hide = hide
self.nargs = nargs
self.count = count
2014-03-03 06:09:23 +01:00
self.desc = desc
2014-03-03 21:06:10 +01:00
self.instance = instance
2014-02-28 17:00:25 +01:00
self.handler = handler
2014-03-21 20:01:13 +01:00
self.completion = completion
self.modes = modes
self.not_modes = not_modes
2014-04-29 23:06:07 +02:00
self.needs_js = needs_js
2014-06-16 09:44:11 +02:00
self.debug = debug
2014-01-29 06:28:21 +01:00
def check(self, args):
"""Check if the argument count is valid and the command is permitted.
2014-02-07 20:21:50 +01:00
2014-02-19 10:58:32 +01:00
Args:
args: The supplied arguments
Raise:
ArgumentCountError if the argument count is wrong.
2014-04-30 10:41:25 +02:00
PrerequisitesError if the command can't be called currently.
2014-01-29 06:28:21 +01:00
"""
2014-05-05 17:56:14 +02:00
# We don't use modeman.instance() here to avoid a circular import
# of qutebrowser.keyinput.modeman.
2014-05-09 17:01:05 +02:00
curmode = QCoreApplication.instance().modeman.mode
2014-05-05 17:56:14 +02:00
if self.modes is not None and curmode not in self.modes:
mode_names = '/'.join(mode.name for mode in self.modes)
2014-04-30 10:41:25 +02:00
raise PrerequisitesError("{}: This command is only allowed in {} "
"mode.".format(self.name, mode_names))
2014-05-05 17:56:14 +02:00
elif self.not_modes is not None and curmode in self.not_modes:
mode_names = '/'.join(mode.name for mode in self.not_modes)
2014-04-30 10:41:25 +02:00
raise PrerequisitesError("{}: This command is not allowed in {} "
"mode.".format(self.name, mode_names))
2014-04-29 23:06:07 +02:00
if self.needs_js and not QWebSettings.globalSettings().testAttribute(
QWebSettings.JavascriptEnabled):
2014-04-30 10:41:25 +02:00
raise PrerequisitesError("{}: This command needs javascript "
"enabled.".format(self.name))
2014-04-10 11:48:26 +02:00
if self.nargs[1] is None and self.nargs[0] <= len(args):
pass
elif self.nargs[0] <= len(args) <= self.nargs[1]:
2014-03-03 06:09:23 +01:00
pass
else:
if self.nargs[0] == self.nargs[1]:
argcnt = str(self.nargs[0])
elif self.nargs[1] is None:
argcnt = '{}-inf'.format(self.nargs[0])
else:
argcnt = '{}-{}'.format(self.nargs[0], self.nargs[1])
raise ArgumentCountError("{}: {} args expected, but got {}".format(
self.name, argcnt, len(args)))
2014-01-29 06:28:21 +01:00
def run(self, args=None, count=None):
2014-02-07 20:21:50 +01:00
"""Run the command.
2014-01-29 06:28:21 +01:00
2014-05-14 18:00:40 +02:00
Note we don't catch CommandError here as it might happen async.
2014-02-19 10:58:32 +01:00
Args:
args: Arguments to the command.
count: Command repetition count.
2014-01-29 06:28:21 +01:00
"""
2014-03-03 06:09:23 +01:00
dbgout = ["command called:", self.name]
2014-01-29 06:28:21 +01:00
if args:
dbgout += args
if count is not None:
dbgout.append("(count={})".format(count))
2014-05-23 16:11:55 +02:00
logger.debug(' '.join(dbgout))
2014-01-29 06:28:21 +01:00
kwargs = {}
app = QCoreApplication.instance()
# Replace variables (currently only {url})
new_args = []
for arg in args:
if arg == '{url}':
urlstr = app.mainwindow.tabs.current_url().toString(
QUrl.FullyEncoded | QUrl.RemovePassword)
new_args.append(urlstr)
else:
new_args.append(arg)
if self.instance is not None:
# Add the 'self' parameter.
if self.instance == '':
obj = app
else:
obj = dotted_getattr(app, self.instance)
new_args.insert(0, obj)
if count is not None and self.count:
kwargs = {'count': count}
self.handler(*new_args, **kwargs)