Skip to content

Commit c0444a7

Browse files
committed
Add support for pylint config files
1 parent fecffb2 commit c0444a7

File tree

4 files changed

+83
-4
lines changed

4 files changed

+83
-4
lines changed

pyls/config/config.py

+6-1
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
log = logging.getLogger(__name__)
1414

1515
# Sources of config, first source overrides next source
16-
DEFAULT_CONFIG_SOURCES = ['pycodestyle']
16+
DEFAULT_CONFIG_SOURCES = ['pycodestyle', 'pylint']
1717

1818

1919
class Config(object):
@@ -39,6 +39,11 @@ def __init__(self, root_uri, init_opts, process_id, capabilities):
3939
self._config_sources['pycodestyle'] = PyCodeStyleConfig(self._root_path)
4040
except ImportError:
4141
pass
42+
try:
43+
from .pylint_conf import PylintConfig
44+
self._config_sources['pylint'] = PylintConfig(self._root_path)
45+
except ImportError:
46+
pass
4247

4348
self._pm = pluggy.PluginManager(PYLS)
4449
self._pm.trace.root.setwriter(log.debug)

pyls/config/pylint_conf.py

+54
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
# Copyright 2019 Palantir Technologies, Inc.
2+
import logging
3+
import os
4+
from pyls._utils import find_parents
5+
from .source import ConfigSource, _get_opt, _set_opt
6+
7+
log = logging.getLogger(__name__)
8+
9+
PROJECT_CONFIGS = ['.pylintrc', 'pylintrc']
10+
11+
CONFIG_KEYS = { # 'option': 'section key'
12+
'disable': 'MESSAGES CONTROL',
13+
'ignore': 'MASTER',
14+
'max-line-length': 'FORMAT',
15+
}
16+
17+
OPTIONS = [
18+
('disable', 'plugins.pylint.disable', list),
19+
('ignore', 'plugins.pylint.ignore', list),
20+
('max-line-length', 'plugins.pylint.maxLineLength', int),
21+
]
22+
23+
24+
class PylintConfig(ConfigSource):
25+
"""Parse pylint configurations."""
26+
27+
def user_config(self):
28+
config_file = self._user_config_file()
29+
config = self.read_config_from_files([config_file])
30+
return self.parse_config(config, CONFIG_KEYS, OPTIONS)
31+
32+
def _user_config_file(self):
33+
if self.is_windows:
34+
return os.path.expanduser('~\\.pylintrc')
35+
return os.path.expanduser('~/.pylintrc')
36+
37+
def project_config(self, document_path):
38+
files = find_parents(self.root_path, document_path, PROJECT_CONFIGS)
39+
config = self.read_config_from_files(files)
40+
return self.parse_config(config, CONFIG_KEYS, OPTIONS)
41+
42+
@staticmethod
43+
def parse_config(config, keys, options):
44+
"""Parse the config with the given options.
45+
This method override its parent to use multiple keys depending
46+
on the value we want to get.
47+
"""
48+
conf = {}
49+
for source, destination, opt_type in options:
50+
key = keys[source]
51+
opt_value = _get_opt(config, key, source, opt_type)
52+
if opt_value is not None:
53+
_set_opt(conf, destination, opt_value)
54+
return conf

pyls/plugins/pylint_lint.py

+22-2
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,12 @@
1111

1212
log = logging.getLogger(__name__)
1313

14+
ARGS = { # 'argument_name': 'name_under_plugin_conf'
15+
'disable': 'disable',
16+
'ignore': 'ignore',
17+
'max-line-length': 'maxLineLength',
18+
}
19+
1420

1521
class PylintLinter(object):
1622
last_diags = collections.defaultdict(list)
@@ -140,10 +146,24 @@ def lint(cls, document, is_saved, flags=''):
140146

141147

142148
def _build_pylint_flags(settings):
143-
"""Build arguments for calling pylint."""
149+
"""Build arguments for calling pylint.
150+
If args is found then it's the arguments used, otherwise,
151+
we build arguments from the plugin config.
152+
"""
144153
pylint_args = settings.get('args')
145154
if pylint_args is None:
146-
return ''
155+
# Build args from plugin config
156+
pylint_args = list()
157+
for arg_name in ARGS:
158+
arg_val = settings.get(ARGS[arg_name])
159+
arg = None
160+
if isinstance(arg_val, list):
161+
arg = '--{}={}'.format(arg_name, ','.join(arg_val))
162+
elif isinstance(arg_val, int):
163+
arg = '--{}={}'.format(arg_name, arg_val)
164+
if arg:
165+
pylint_args.append(arg)
166+
147167
return ' '.join(pylint_args)
148168

149169

vscode-client/package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@
2727
},
2828
"pyls.configurationSources": {
2929
"type": "array",
30-
"default": ["pycodestyle"],
30+
"default": ["pycodestyle", "pylint"],
3131
"description": "List of configuration sources to use.",
3232
"items": {
3333
"type": "string",

0 commit comments

Comments
 (0)