qutebrowser/scripts/dev/pylint_checkers/qute_pylint/config.py

85 lines
2.8 KiB
Python
Raw Normal View History

2014-08-06 14:42:05 +02:00
# vim: ft=python fileencoding=utf-8 sts=4 sw=4 et:
2018-02-05 12:19:50 +01:00
# Copyright 2014-2018 Florian Bruhin (The Compiler) <mail@qutebrowser.org>
2014-08-06 14:42:05 +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/>.
"""Custom astroid checker for config calls."""
import sys
import pathlib
2017-10-17 21:11:14 +02:00
2017-06-09 17:21:36 +02:00
import yaml
2014-08-06 14:42:05 +02:00
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-12-05 11:54:39 +01:00
2017-06-14 11:44:18 +02:00
OPTIONS = None
FAILED_LOAD = False
2017-06-14 11:44:18 +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 = {
2017-06-14 11:44:18 +02:00
'E9998': ('%s is no valid config option.', # flake8: disable=S001
'bad-config-option',
2017-06-09 19:54:25 +02:00
None),
2014-08-06 14:42:05 +02:00
}
priority = -1
printed_warning = False
2014-08-06 14:42:05 +02:00
@utils.check_messages('bad-config-option')
2017-06-14 11:44:18 +02:00
def visit_attribute(self, node):
"""Visit a getattr node."""
# At the end of a config.val.foo.bar chain
if not isinstance(node.parent, astroid.Attribute):
# FIXME:conf do some proper check for this...
2017-06-14 11:44:18 +02:00
node_str = node.as_string()
prefix = 'config.val.'
if node_str.startswith(prefix):
self._check_config(node, node_str[len(prefix):])
2017-06-09 19:54:25 +02:00
2017-06-14 11:44:18 +02:00
def _check_config(self, node, name):
"""Check that we're accessing proper config options."""
if FAILED_LOAD:
if not ConfigChecker.printed_warning:
print("[WARN] Could not find configdata.yml. Please run "
"pylint from qutebrowser root.", file=sys.stderr)
print("Skipping some checks...", file=sys.stderr)
ConfigChecker.printed_warning = True
return
2017-06-14 11:44:18 +02:00
if name not in OPTIONS:
self.add_message('bad-config-option', node=node, args=name)
2014-08-06 14:42:05 +02:00
def register(linter):
"""Register this checker."""
linter.register_checker(ConfigChecker(linter))
2017-06-14 11:44:18 +02:00
global OPTIONS
global FAILED_LOAD
yaml_file = pathlib.Path('qutebrowser') / 'config' / 'configdata.yml'
if not yaml_file.exists():
OPTIONS = None
FAILED_LOAD = True
return
with yaml_file.open(mode='r', encoding='utf-8') as f:
2017-06-14 11:44:18 +02:00
OPTIONS = list(yaml.load(f))