File: flake8_conf.py

package info (click to toggle)
python-lsp-server 1.12.0-3
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 796 kB
  • sloc: python: 7,791; sh: 12; makefile: 4
file content (64 lines) | stat: -rw-r--r-- 2,370 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
# Copyright 2017-2020 Palantir Technologies, Inc.
# Copyright 2021- Python Language Server Contributors.

import logging
import os

from pylsp._utils import find_parents

from .source import ConfigSource

log = logging.getLogger(__name__)

CONFIG_KEY = "flake8"
PROJECT_CONFIGS = [".flake8", "setup.cfg", "tox.ini"]

OPTIONS = [
    # mccabe
    ("max-complexity", "plugins.mccabe.threshold", int),
    # pycodestyle
    ("exclude", "plugins.pycodestyle.exclude", list),
    ("filename", "plugins.pycodestyle.filename", list),
    ("hang-closing", "plugins.pycodestyle.hangClosing", bool),
    ("ignore", "plugins.pycodestyle.ignore", list),
    ("max-line-length", "plugins.pycodestyle.maxLineLength", int),
    ("indent-size", "plugins.pycodestyle.indentSize", int),
    ("select", "plugins.pycodestyle.select", list),
    # flake8
    ("exclude", "plugins.flake8.exclude", list),
    ("extend-ignore", "plugins.flake8.extendIgnore", list),
    ("extend-select", "plugins.flake8.extendSelect", list),
    ("filename", "plugins.flake8.filename", list),
    ("hang-closing", "plugins.flake8.hangClosing", bool),
    ("ignore", "plugins.flake8.ignore", list),
    ("max-complexity", "plugins.flake8.maxComplexity", int),
    ("max-line-length", "plugins.flake8.maxLineLength", int),
    ("indent-size", "plugins.flake8.indentSize", int),
    ("select", "plugins.flake8.select", list),
    ("per-file-ignores", "plugins.flake8.perFileIgnores", list),
]


class Flake8Config(ConfigSource):
    """Parse flake8 configurations."""

    def user_config(self):
        config_file = self._user_config_file()
        config = self.read_config_from_files([config_file])
        return self.parse_config(config, CONFIG_KEY, OPTIONS)

    def _user_config_file(self):
        if self.is_windows:
            return os.path.expanduser("~\\.flake8")
        return os.path.join(self.xdg_home, "flake8")

    def project_config(self, document_path):
        files = find_parents(self.root_path, document_path, PROJECT_CONFIGS)
        config = self.read_config_from_files(files)
        return self.parse_config(config, CONFIG_KEY, OPTIONS)

    @classmethod
    def _parse_list_opt(cls, string):
        if string.startswith("\n"):
            return [s.strip().rstrip(",") for s in string.split("\n") if s.strip()]
        return [s.strip() for s in string.split(",") if s.strip()]