qutebrowser/qutebrowser/commands/argparser.py

176 lines
5.4 KiB
Python
Raw Normal View History

2014-09-02 21:54:07 +02:00
# vim: ft=python fileencoding=utf-8 sts=4 sw=4 et:
2016-01-04 07:12:39 +01:00
# Copyright 2014-2016 Florian Bruhin (The Compiler) <mail@qutebrowser.org>
2014-09-02 21:54:07 +02: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/>.
"""argparse.ArgumentParser subclass to parse qutebrowser commands."""
import argparse
2014-09-23 22:00:26 +02:00
from PyQt5.QtCore import QUrl
from qutebrowser.commands import cmdexc
2014-09-24 07:06:45 +02:00
from qutebrowser.utils import utils, objreg
2014-09-02 21:54:07 +02:00
SUPPRESS = argparse.SUPPRESS
2014-09-02 21:54:07 +02:00
class ArgumentParserError(Exception):
"""Exception raised when the ArgumentParser signals an error."""
2014-09-04 17:58:28 +02:00
class ArgumentParserExit(Exception):
2015-10-04 15:41:42 +02:00
"""Exception raised when the argument parser exited.
2014-09-24 22:22:02 +02:00
Attributes:
status: The exit status.
"""
2014-09-04 17:58:28 +02:00
def __init__(self, status, msg):
self.status = status
super().__init__(msg)
class HelpAction(argparse.Action):
2014-09-05 06:57:02 +02:00
"""Argparse action to open the help page in the browser.
This is horrible encapsulation, but I can't think of a good way to do this
better...
"""
def __call__(self, parser, _namespace, _values, _option_string=None):
2014-09-28 23:23:02 +02:00
tabbed_browser = objreg.get('tabbed-browser', scope='window',
window='last-focused')
2014-09-28 23:23:02 +02:00
tabbed_browser.tabopen(
2014-09-07 23:22:38 +02:00
QUrl('qute://help/commands.html#{}'.format(parser.name)))
parser.exit()
2014-09-02 21:54:07 +02:00
class ArgumentParser(argparse.ArgumentParser):
2014-09-24 22:22:02 +02:00
"""Subclass ArgumentParser to be more suitable for runtime parsing.
Attributes:
name: The command name.
"""
def __init__(self, name, *args, **kwargs):
self.name = name
super().__init__(*args, add_help=False, prog=name, **kwargs)
2014-09-02 21:54:07 +02:00
def exit(self, status=0, msg=None):
2014-09-04 17:58:28 +02:00
raise ArgumentParserExit(status, msg)
2014-09-02 21:54:07 +02:00
def error(self, msg):
2015-08-20 07:08:59 +02:00
raise ArgumentParserError(msg.capitalize())
def arg_name(name):
"""Get the name an argument should have based on its Python name."""
return name.rstrip('_').replace('_', '-')
def _enum_getter(enum):
"""Function factory to get an enum getter."""
def _get_enum_item(key):
"""Helper function to get an enum item.
Passes through existing items unmodified.
"""
assert not isinstance(key, enum)
return enum[key.replace('-', '_')]
return _get_enum_item
def _check_choices(param, value, choices):
if value not in choices:
expected_values = ', '.join(arg_name(val) for val in choices)
raise cmdexc.ArgumentTypeError("{}: Invalid value {} - expected "
"one of: {}".format(
param.name, value, expected_values))
def type_conv(param, typ, str_choices=None):
"""Function factory to get a type converter for a single type.
Args:
param: The argparse.Parameter we're checking
types: The allowed type
str_choices: The allowed choices if the type ends up being a string
"""
if isinstance(typ, str):
raise TypeError("{}: Legacy string type!".format(param.name))
def _convert(value):
if value is param.default:
return value
if utils.is_enum(typ):
if isinstance(value, typ):
return value
assert isinstance(value, str)
_check_choices(param, value, [arg_name(e.name) for e in typ])
getter = _enum_getter(typ)
return getter(value)
elif typ is str:
assert isinstance(value, str)
if str_choices is not None:
_check_choices(param, value, str_choices)
return value
elif callable(typ):
# int, float, etc.
if isinstance(value, typ):
return value
assert isinstance(value, str)
try:
return typ(value)
except (TypeError, ValueError):
msg = '{}: Invalid {} value {}'.format(
param.name, typ.__name__, value)
raise cmdexc.ArgumentTypeError(msg)
else:
raise ValueError("{}: Unknown type {!r}!".format(
param.name, typ))
return _convert
def multitype_conv(param, types, str_choices=None):
"""Function factory to get a type converter for a choice of types.
Args:
param: The inspect.Parameter we're checking
types: The allowed types ("overloads")
str_choices: The allowed choices if the type ends up being a string
"""
def _convert(value):
"""Convert a value according to an iterable of possible arg types."""
2015-08-20 07:08:59 +02:00
for typ in set(types):
try:
converter = type_conv(param, typ, str_choices)
return converter(value)
except cmdexc.ArgumentTypeError:
pass
raise cmdexc.ArgumentTypeError('{}: Invalid value {}'.format(
param.name, value))
return _convert