qutebrowser/qutebrowser/commands/command.py

123 lines
4.2 KiB
Python
Raw Normal View History

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."""
2014-01-29 06:28:21 +01:00
import logging
from qutebrowser.commands.exceptions import ArgumentCountError
2014-03-03 21:06:10 +01:00
from PyQt5.QtCore import pyqtSignal, QObject
2014-01-29 06:28:21 +01:00
2014-03-03 21:06:10 +01:00
class Command(QObject):
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.
maxsplit: Maximum count of splits to be made.
-1: Split everything (default)
0: Don't split.
n: Split a maximum of n times.
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-03-04 07:02:45 +01:00
Signals:
signal: Gets emitted when something should be called via handle_command
from the app.py context.
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
2014-03-04 07:02:45 +01:00
signal = pyqtSignal(tuple)
def __init__(self, name, maxsplit, hide, nargs, count, desc, instance,
2014-03-21 20:01:13 +01:00
handler, completion):
2014-01-29 06:28:21 +01:00
super().__init__()
2014-02-28 17:00:25 +01:00
self.name = name
self.maxsplit = maxsplit
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
2014-01-29 06:28:21 +01:00
def check(self, args):
2014-02-07 20:21:50 +01:00
"""Check if the argument count is valid.
Raise ArgumentCountError if not.
2014-02-19 10:58:32 +01:00
Args:
args: The supplied arguments
Raise:
ArgumentCountError if the argument count is wrong.
2014-01-29 06:28:21 +01:00
"""
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(
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-02-19 10:58:32 +01:00
Args:
args: Arguments to the command.
count: Command repetition count.
2014-03-04 07:02:45 +01:00
Emit:
signal: When the command has an instance and should be handled from
the app.py context.
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))
logging.debug(' '.join(dbgout))
2014-03-03 21:06:10 +01:00
if self.instance is not None and self.count and count is not None:
self.signal.emit((self.instance, self.handler.__name__, count,
args))
elif self.instance is not None:
self.signal.emit((self.instance, self.handler.__name__, None,
args))
elif count is not None and self.count:
2014-04-17 17:44:27 +02:00
self.handler(*args, count=count)
2014-03-03 06:09:23 +01:00
else:
2014-04-17 17:44:27 +02:00
self.handler(*args)