Make it possible to limit commands to backends

This commit is contained in:
Florian Bruhin 2016-07-08 18:21:20 +02:00
parent 11cd5f8653
commit 0ab601aaf3
3 changed files with 35 additions and 8 deletions

View File

@ -452,7 +452,8 @@ class CommandDispatcher:
url.setPath(new_path) url.setPath(new_path)
self._open(url, tab, background, window) self._open(url, tab, background, window)
@cmdutils.register(instance='command-dispatcher', scope='window') @cmdutils.register(instance='command-dispatcher', scope='window',
backend=tabmod.Backend.QtWebKit)
@cmdutils.argument('where', choices=['prev', 'next', 'up', 'increment', @cmdutils.argument('where', choices=['prev', 'next', 'up', 'increment',
'decrement']) 'decrement'])
def navigate(self, where: str, tab=False, bg=False, window=False): def navigate(self, where: str, tab=False, bg=False, window=False):
@ -485,6 +486,9 @@ class CommandDispatcher:
if where in ['prev', 'next']: if where in ['prev', 'next']:
# FIXME:qtwebengine have a proper API for this # FIXME:qtwebengine have a proper API for this
if widget.backend == tabmod.Backend.QtWebEngine:
raise cmdexc.CommandError(":navigate prev/next is not "
"supported yet with QtWebEngine")
page = widget._widget.page() # pylint: disable=protected-access page = widget._widget.page() # pylint: disable=protected-access
frame = page.currentFrame() frame = page.currentFrame()
if frame is None: if frame is None:
@ -1122,7 +1126,7 @@ class CommandDispatcher:
raise cmdexc.CommandError(str(e)) raise cmdexc.CommandError(str(e))
@cmdutils.register(instance='command-dispatcher', name='inspector', @cmdutils.register(instance='command-dispatcher', name='inspector',
scope='window') scope='window', backend=tabmod.Backend.QtWebKit)
def toggle_inspector(self): def toggle_inspector(self):
"""Toggle the web inspector. """Toggle the web inspector.
@ -1150,7 +1154,8 @@ class CommandDispatcher:
else: else:
tab.data.inspector.show() tab.data.inspector.show()
@cmdutils.register(instance='command-dispatcher', scope='window') @cmdutils.register(instance='command-dispatcher', scope='window',
backend=tabmod.Backend.QtWebKit)
@cmdutils.argument('dest_old', hide=True) @cmdutils.argument('dest_old', hide=True)
def download(self, url=None, dest_old=None, *, mhtml_=False, dest=None): def download(self, url=None, dest_old=None, *, mhtml_=False, dest=None):
"""Download a given URL, or current page if no URL given. """Download a given URL, or current page if no URL given.
@ -1323,7 +1328,8 @@ class CommandDispatcher:
self._open(url, tab, bg, window) self._open(url, tab, bg, window)
@cmdutils.register(instance='command-dispatcher', @cmdutils.register(instance='command-dispatcher',
modes=[KeyMode.insert], hide=True, scope='window') modes=[KeyMode.insert], hide=True, scope='window',
backend=tabmod.Backend.QtWebKit)
def open_editor(self): def open_editor(self):
"""Open an external editor with the currently selected form field. """Open an external editor with the currently selected form field.
@ -1372,7 +1378,7 @@ class CommandDispatcher:
@cmdutils.register(instance='command-dispatcher', @cmdutils.register(instance='command-dispatcher',
modes=[KeyMode.insert], hide=True, scope='window', modes=[KeyMode.insert], hide=True, scope='window',
needs_js=True) needs_js=True, backend=tabmod.Backend.QtWebKit)
def paste_primary(self): def paste_primary(self):
"""Paste the primary selection at cursor position.""" """Paste the primary selection at cursor position."""
# FIXME:qtwebengine have a proper API for this # FIXME:qtwebengine have a proper API for this

View File

@ -31,6 +31,7 @@ from PyQt5.QtGui import QMouseEvent
from PyQt5.QtWebKit import QWebElement from PyQt5.QtWebKit import QWebElement
from PyQt5.QtWebKitWidgets import QWebPage from PyQt5.QtWebKitWidgets import QWebPage
from qutebrowser.browser import tab as tabmod
from qutebrowser.config import config from qutebrowser.config import config
from qutebrowser.keyinput import modeman, modeparsers from qutebrowser.keyinput import modeman, modeparsers
from qutebrowser.browser.webkit import webelem from qutebrowser.browser.webkit import webelem
@ -762,7 +763,8 @@ class HintManager(QObject):
tab.openurl(url) tab.openurl(url)
@cmdutils.register(instance='hintmanager', scope='tab', name='hint', @cmdutils.register(instance='hintmanager', scope='tab', name='hint',
star_args_optional=True, maxsplit=2) star_args_optional=True, maxsplit=2,
backend=tabmod.Backend.QtWebKit)
@cmdutils.argument('win_id', win_id=True) @cmdutils.argument('win_id', win_id=True)
def start(self, rapid=False, group=webelem.Group.all, target=Target.normal, def start(self, rapid=False, group=webelem.Group.all, target=Target.normal,
*args, win_id): *args, win_id):

View File

@ -29,6 +29,7 @@ from qutebrowser.commands import cmdexc, argparser
from qutebrowser.utils import (log, utils, message, docutils, objreg, from qutebrowser.utils import (log, utils, message, docutils, objreg,
usertypes, typing) usertypes, typing)
from qutebrowser.utils import debug as debug_utils from qutebrowser.utils import debug as debug_utils
from qutebrowser.browser import tab as tabmod
class ArgInfo: class ArgInfo:
@ -80,6 +81,8 @@ class Command:
parser: The ArgumentParser to use to parse this command. parser: The ArgumentParser to use to parse this command.
flags_with_args: A list of flags which take an argument. flags_with_args: A list of flags which take an argument.
no_cmd_split: If true, ';;' to split sub-commands is ignored. no_cmd_split: If true, ';;' to split sub-commands is ignored.
backend: Which backend the command works with (or None if it works with
both)
_qute_args: The saved data from @cmdutils.argument _qute_args: The saved data from @cmdutils.argument
_needs_js: Whether the command needs javascript enabled _needs_js: Whether the command needs javascript enabled
_modes: The modes the command can be executed in. _modes: The modes the command can be executed in.
@ -92,7 +95,8 @@ class Command:
def __init__(self, *, handler, name, instance=None, maxsplit=None, def __init__(self, *, handler, name, instance=None, maxsplit=None,
hide=False, modes=None, not_modes=None, needs_js=False, hide=False, modes=None, not_modes=None, needs_js=False,
debug=False, ignore_args=False, deprecated=False, debug=False, ignore_args=False, deprecated=False,
no_cmd_split=False, star_args_optional=False, scope='global'): no_cmd_split=False, star_args_optional=False, scope='global',
backend=None):
# I really don't know how to solve this in a better way, I tried. # I really don't know how to solve this in a better way, I tried.
# pylint: disable=too-many-locals # pylint: disable=too-many-locals
if modes is not None and not_modes is not None: if modes is not None and not_modes is not None:
@ -123,6 +127,8 @@ class Command:
self.ignore_args = ignore_args self.ignore_args = ignore_args
self.handler = handler self.handler = handler
self.no_cmd_split = no_cmd_split self.no_cmd_split = no_cmd_split
self.backend = backend
self.docparser = docutils.DocstringParser(handler) self.docparser = docutils.DocstringParser(handler)
self.parser = argparser.ArgumentParser( self.parser = argparser.ArgumentParser(
name, description=self.docparser.short_desc, name, description=self.docparser.short_desc,
@ -170,10 +176,22 @@ class Command:
raise cmdexc.PrerequisitesError( raise cmdexc.PrerequisitesError(
"{}: This command is not allowed in {} mode.".format( "{}: This command is not allowed in {} mode.".format(
self.name, mode_names)) self.name, mode_names))
if self._needs_js and not QWebSettings.globalSettings().testAttribute( if self._needs_js and not QWebSettings.globalSettings().testAttribute(
QWebSettings.JavascriptEnabled): QWebSettings.JavascriptEnabled):
raise cmdexc.PrerequisitesError( raise cmdexc.PrerequisitesError(
"{}: This command needs javascript enabled.".format(self.name)) "{}: This command needs javascript enabled.".format(self.name))
backend_mapping = {
'webkit': tabmod.Backend.QtWebKit,
'webengine': tabmod.Backend.QtWebEngine,
}
used_backend = backend_mapping[objreg.get('args').backend]
if self.backend is not None and used_backend != self.backend:
raise cmdexc.PrerequisitesError(
"{}: Only available with {} "
"backend.".format(self.name, self.backend.name))
if self.deprecated: if self.deprecated:
message.warning(win_id, '{} is deprecated - {}'.format( message.warning(win_id, '{} is deprecated - {}'.format(
self.name, self.deprecated)) self.name, self.deprecated))
@ -497,8 +515,9 @@ class Command:
e.status, e)) e.status, e))
return return
self._count = count self._count = count
posargs, kwargs = self._get_call_args(win_id) # FIXME add tests for the _check_prerequisites move!
self._check_prerequisites(win_id) self._check_prerequisites(win_id)
posargs, kwargs = self._get_call_args(win_id)
log.commands.debug('Calling {}'.format( log.commands.debug('Calling {}'.format(
debug_utils.format_call(self.handler, posargs, kwargs))) debug_utils.format_call(self.handler, posargs, kwargs)))
self.handler(*posargs, **kwargs) self.handler(*posargs, **kwargs)