|
| 1 | +import logging |
| 2 | +import os |
| 3 | +from pyls._utils import find_parents, merge_dicts |
| 4 | +from .source import ConfigSource |
| 5 | + |
| 6 | +log = logging.getLogger(__name__) |
| 7 | + |
| 8 | +CONFIG_KEY = 'pyls' |
| 9 | +PROJECT_CONFIGS = ['setup.cfg', 'tox.ini'] |
| 10 | + |
| 11 | +OPTIONS = [ |
| 12 | +] |
| 13 | + |
| 14 | + |
| 15 | +class PylsConfig(ConfigSource): |
| 16 | + """Parse pyls configuration""" |
| 17 | + |
| 18 | + def __init__(self, root_path): |
| 19 | + super(PylsConfig, self).__init__(root_path) |
| 20 | + |
| 21 | + # If workspace URI has trailing '/', root_path does, too. |
| 22 | + # (e.g. "lsp" on Emacs sends such URI). To avoid normpath at |
| 23 | + # each normalization, os.path.normpath()-ed root_path is |
| 24 | + # cached here. |
| 25 | + self._norm_root_path = os.path.normpath(self.root_path) |
| 26 | + |
| 27 | + def user_config(self): |
| 28 | + # pyls specific configuration mainly focuses on per-project |
| 29 | + # configuration |
| 30 | + return {} |
| 31 | + |
| 32 | + def project_config(self, document_path): |
| 33 | + settings = {} |
| 34 | + seen = set() |
| 35 | + |
| 36 | + # To read config files in root_path even if any config file is |
| 37 | + # found by find_parents() in the directory other than |
| 38 | + # root_path, root_path is listed below as one of targets. |
| 39 | + # |
| 40 | + # On the other hand, "seen" is used to manage name of already |
| 41 | + # evaluated files, in order to avoid multiple evaluation of |
| 42 | + # config files in root_path. |
| 43 | + for target in self.root_path, document_path: |
| 44 | + # os.path.normpath is needed to treat that |
| 45 | + # "root_path/./foobar" and "root_path/foobar" are |
| 46 | + # identical. |
| 47 | + sources = map(os.path.normpath, find_parents(self.root_path, target, PROJECT_CONFIGS)) |
| 48 | + files = [] |
| 49 | + for source in sources: |
| 50 | + if source not in seen: |
| 51 | + files.append(source) |
| 52 | + seen.add(source) |
| 53 | + if not files: |
| 54 | + continue # there is no file to be read in |
| 55 | + |
| 56 | + parsed = self.parse_config(self.read_config_from_files(files), |
| 57 | + CONFIG_KEY, OPTIONS) |
| 58 | + if not parsed: |
| 59 | + continue # no pyls specific configuration |
| 60 | + |
| 61 | + settings = merge_dicts(settings, parsed) |
| 62 | + |
| 63 | + return settings |
0 commit comments