|
| 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 |
0 commit comments