summaryrefslogtreecommitdiff
path: root/.config/qutebrowser/scripts/dev/pylint_checkers/qute_pylint
diff options
context:
space:
mode:
Diffstat (limited to '.config/qutebrowser/scripts/dev/pylint_checkers/qute_pylint')
-rw-r--r--.config/qutebrowser/scripts/dev/pylint_checkers/qute_pylint/__init__.py1
-rw-r--r--.config/qutebrowser/scripts/dev/pylint_checkers/qute_pylint/config.py84
-rw-r--r--.config/qutebrowser/scripts/dev/pylint_checkers/qute_pylint/modeline.py63
-rw-r--r--.config/qutebrowser/scripts/dev/pylint_checkers/qute_pylint/openencoding.py83
-rw-r--r--.config/qutebrowser/scripts/dev/pylint_checkers/qute_pylint/settrace.py49
5 files changed, 280 insertions, 0 deletions
diff --git a/.config/qutebrowser/scripts/dev/pylint_checkers/qute_pylint/__init__.py b/.config/qutebrowser/scripts/dev/pylint_checkers/qute_pylint/__init__.py
new file mode 100644
index 0000000..1341a93
--- /dev/null
+++ b/.config/qutebrowser/scripts/dev/pylint_checkers/qute_pylint/__init__.py
@@ -0,0 +1 @@
+"""Custom pylint checkers."""
diff --git a/.config/qutebrowser/scripts/dev/pylint_checkers/qute_pylint/config.py b/.config/qutebrowser/scripts/dev/pylint_checkers/qute_pylint/config.py
new file mode 100644
index 0000000..5aa5250
--- /dev/null
+++ b/.config/qutebrowser/scripts/dev/pylint_checkers/qute_pylint/config.py
@@ -0,0 +1,84 @@
+# vim: ft=python fileencoding=utf-8 sts=4 sw=4 et:
+
+# Copyright 2014-2018 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 sys
+import pathlib
+
+import yaml
+import astroid
+from pylint import interfaces, checkers
+from pylint.checkers import utils
+
+
+OPTIONS = None
+FAILED_LOAD = False
+
+
+class ConfigChecker(checkers.BaseChecker):
+
+ """Custom astroid checker for config calls."""
+
+ __implements__ = interfaces.IAstroidChecker
+ name = 'config'
+ msgs = {
+ 'E9998': ('%s is no valid config option.', # flake8: disable=S001
+ 'bad-config-option',
+ None),
+ }
+ priority = -1
+ printed_warning = False
+
+ @utils.check_messages('bad-config-option')
+ 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...
+ node_str = node.as_string()
+ prefix = 'config.val.'
+ if node_str.startswith(prefix):
+ self._check_config(node, node_str[len(prefix):])
+
+ 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
+ if name not in OPTIONS:
+ self.add_message('bad-config-option', node=node, args=name)
+
+
+def register(linter):
+ """Register this checker."""
+ linter.register_checker(ConfigChecker(linter))
+ 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:
+ OPTIONS = list(yaml.load(f))
diff --git a/.config/qutebrowser/scripts/dev/pylint_checkers/qute_pylint/modeline.py b/.config/qutebrowser/scripts/dev/pylint_checkers/qute_pylint/modeline.py
new file mode 100644
index 0000000..429974c
--- /dev/null
+++ b/.config/qutebrowser/scripts/dev/pylint_checkers/qute_pylint/modeline.py
@@ -0,0 +1,63 @@
+# Copyright 2014-2018 Florian Bruhin (The Compiler) <mail@qutebrowser.org>
+# vim: ft=python fileencoding=utf-8 sts=4 sw=4 et:
+
+# 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/>.
+
+"""Checker for vim modelines in files."""
+
+import os.path
+import contextlib
+
+from pylint import interfaces, checkers
+
+
+class ModelineChecker(checkers.BaseChecker):
+
+ """Check for vim modelines in files."""
+
+ __implements__ = interfaces.IRawChecker
+
+ name = 'modeline'
+ msgs = {'W9002': ('Does not have vim modeline', 'modeline-missing', None),
+ 'W9003': ('Modeline is invalid', 'invalid-modeline', None),
+ 'W9004': ('Modeline position is wrong', 'modeline-position', None)}
+ options = ()
+ priority = -1
+
+ def process_module(self, node):
+ """Process the module."""
+ if os.path.basename(os.path.splitext(node.file)[0]) == '__init__':
+ return
+ max_lineno = 1
+ with contextlib.closing(node.stream()) as stream:
+ for (lineno, line) in enumerate(stream):
+ if lineno == 1 and line.startswith(b'#!'):
+ max_lineno += 1
+ continue
+ elif line.startswith(b'# vim:'):
+ if lineno > max_lineno:
+ self.add_message('modeline-position', line=lineno)
+ if (line.rstrip() != b'# vim: ft=python '
+ b'fileencoding=utf-8 sts=4 sw=4 et:'):
+ self.add_message('invalid-modeline', line=lineno)
+ break
+ else:
+ self.add_message('modeline-missing', line=1)
+
+
+def register(linter):
+ """Register the checker."""
+ linter.register_checker(ModelineChecker(linter))
diff --git a/.config/qutebrowser/scripts/dev/pylint_checkers/qute_pylint/openencoding.py b/.config/qutebrowser/scripts/dev/pylint_checkers/qute_pylint/openencoding.py
new file mode 100644
index 0000000..f577011
--- /dev/null
+++ b/.config/qutebrowser/scripts/dev/pylint_checkers/qute_pylint/openencoding.py
@@ -0,0 +1,83 @@
+# vim: ft=python fileencoding=utf-8 sts=4 sw=4 et:
+
+# Copyright 2014-2018 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/>.
+
+"""Make sure open() has an encoding set."""
+
+import astroid
+from pylint import interfaces, checkers
+from pylint.checkers import utils
+
+
+class OpenEncodingChecker(checkers.BaseChecker):
+
+ """Checker to check open() has an encoding set."""
+
+ __implements__ = interfaces.IAstroidChecker
+ name = 'open-encoding'
+
+ msgs = {
+ 'W9400': ('open() called without encoding', 'open-without-encoding',
+ None),
+ }
+
+ @utils.check_messages('open-without-encoding')
+ def visit_call(self, node):
+ """Visit a Call node."""
+ if hasattr(node, 'func'):
+ infer = utils.safe_infer(node.func)
+ if infer and infer.root().name == '_io':
+ if getattr(node.func, 'name', None) in ['open', 'file']:
+ self._check_open_encoding(node)
+
+ def _check_open_encoding(self, node):
+ """Check that an open() call always has an encoding set."""
+ try:
+ mode_arg = utils.get_argument_from_call(node, position=1,
+ keyword='mode')
+ except utils.NoSuchArgumentError:
+ mode_arg = None
+ _encoding = None
+ try:
+ _encoding = utils.get_argument_from_call(node, position=2)
+ except utils.NoSuchArgumentError:
+ try:
+ _encoding = utils.get_argument_from_call(node,
+ keyword='encoding')
+ except utils.NoSuchArgumentError:
+ pass
+ if _encoding is None:
+ if mode_arg is None:
+ mode = None
+ else:
+ mode = utils.safe_infer(mode_arg)
+ if mode is not None and not isinstance(mode, astroid.Const):
+ # We can't say what mode is exactly.
+ return
+ if mode is None:
+ self.add_message('open-without-encoding', node=node)
+ elif 'b' in getattr(mode, 'value', ''):
+ # Files opened as binary don't need an encoding.
+ return
+ else:
+ self.add_message('open-without-encoding', node=node)
+
+
+def register(linter):
+ """Register this checker."""
+ linter.register_checker(OpenEncodingChecker(linter))
diff --git a/.config/qutebrowser/scripts/dev/pylint_checkers/qute_pylint/settrace.py b/.config/qutebrowser/scripts/dev/pylint_checkers/qute_pylint/settrace.py
new file mode 100644
index 0000000..c82d646
--- /dev/null
+++ b/.config/qutebrowser/scripts/dev/pylint_checkers/qute_pylint/settrace.py
@@ -0,0 +1,49 @@
+# vim: ft=python fileencoding=utf-8 sts=4 sw=4 et:
+
+# Copyright 2014-2018 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 set_trace calls."""
+
+from pylint.interfaces import IAstroidChecker
+from pylint.checkers import BaseChecker, utils
+
+
+class SetTraceChecker(BaseChecker):
+
+ """Custom astroid checker for set_trace calls."""
+
+ __implements__ = IAstroidChecker
+ name = 'settrace'
+ msgs = {
+ 'E9101': ('set_trace call found', 'set-trace', None),
+ }
+ priority = -1
+
+ @utils.check_messages('set-trace')
+ def visit_call(self, node):
+ """Visit a Call node."""
+ if hasattr(node, 'func'):
+ infer = utils.safe_infer(node.func)
+ if infer:
+ if getattr(node.func, 'name', None) == 'set_trace':
+ self.add_message('set-trace', node=node)
+
+
+def register(linter):
+ """Register this checker."""
+ linter.register_checker(SetTraceChecker(linter))