2014-08-06 14:42:05 +02:00
|
|
|
# vim: ft=python fileencoding=utf-8 sts=4 sw=4 et:
|
|
|
|
|
|
|
|
# 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/>.
|
|
|
|
|
|
|
|
"""Custom astroid checker for config calls."""
|
|
|
|
|
|
|
|
import astroid
|
2014-08-26 19:10:14 +02:00
|
|
|
from pylint import interfaces, checkers
|
2014-08-06 14:42:05 +02:00
|
|
|
from pylint.checkers import utils
|
|
|
|
|
2014-08-26 19:10:14 +02:00
|
|
|
from qutebrowser.config import configdata
|
2014-08-06 14:42:05 +02:00
|
|
|
|
|
|
|
|
2014-08-26 19:10:14 +02:00
|
|
|
class ConfigChecker(checkers.BaseChecker):
|
2014-08-06 14:42:05 +02:00
|
|
|
|
|
|
|
"""Custom astroid checker for config calls."""
|
|
|
|
|
2014-08-26 19:10:14 +02:00
|
|
|
__implements__ = interfaces.IAstroidChecker
|
2014-08-06 14:42:05 +02:00
|
|
|
name = 'config'
|
|
|
|
msgs = {
|
|
|
|
'E0000': ('"%s -> %s" is no valid config option.', 'bad-config-call',
|
|
|
|
None),
|
|
|
|
}
|
|
|
|
priority = -1
|
|
|
|
|
|
|
|
@utils.check_messages('bad-config-call')
|
|
|
|
def visit_callfunc(self, node):
|
|
|
|
"""Visit a CallFunc node."""
|
|
|
|
if hasattr(node, 'func'):
|
|
|
|
infer = utils.safe_infer(node.func)
|
|
|
|
if infer and infer.root().name == 'qutebrowser.config.config':
|
|
|
|
if getattr(node.func, 'attrname', None) in ('get', 'set'):
|
|
|
|
self._check_config(node)
|
|
|
|
|
|
|
|
def _check_config(self, node):
|
|
|
|
"""Check that the arguments to config.get(...) are valid.
|
|
|
|
|
|
|
|
FIXME: We should check all ConfigManager calls.
|
|
|
|
"""
|
|
|
|
try:
|
|
|
|
sect_arg = utils.get_argument_from_call(node, position=0,
|
|
|
|
keyword='sectname')
|
|
|
|
opt_arg = utils.get_argument_from_call(node, position=1,
|
|
|
|
keyword='optname')
|
|
|
|
except utils.NoSuchArgumentError:
|
|
|
|
return
|
|
|
|
sect_arg = utils.safe_infer(sect_arg)
|
|
|
|
opt_arg = utils.safe_infer(opt_arg)
|
|
|
|
if not (isinstance(sect_arg, astroid.Const) and
|
|
|
|
isinstance(opt_arg, astroid.Const)):
|
|
|
|
return
|
|
|
|
try:
|
|
|
|
configdata.DATA[sect_arg.value][opt_arg.value]
|
|
|
|
except KeyError:
|
|
|
|
self.add_message('bad-config-call', node=node,
|
|
|
|
args=(sect_arg.value, opt_arg.value))
|
|
|
|
|
|
|
|
|
|
|
|
def register(linter):
|
|
|
|
"""Register this checker."""
|
|
|
|
linter.register_checker(ConfigChecker(linter))
|