|
46 | 46 | import time
|
47 | 47 | from typing import Any, Dict, Tuple
|
48 | 48 |
|
| 49 | +import toml |
| 50 | + |
49 | 51 | from pylint import utils
|
50 | 52 |
|
51 | 53 | USER_HOME = os.path.expanduser("~")
|
@@ -87,38 +89,70 @@ def save_results(results, base):
|
87 | 89 | print("Unable to create file %s: %s" % (data_file, ex), file=sys.stderr)
|
88 | 90 |
|
89 | 91 |
|
90 |
| -def find_pylintrc(): |
91 |
| - """search the pylint rc file and return its path if it find it, else None |
92 |
| - """ |
93 |
| - # is there a pylint rc file in the current directory ? |
94 |
| - if os.path.exists("pylintrc"): |
95 |
| - return os.path.abspath("pylintrc") |
96 |
| - if os.path.exists(".pylintrc"): |
97 |
| - return os.path.abspath(".pylintrc") |
| 92 | +def _toml_has_config(path): |
| 93 | + with open(path, "r") as toml_handle: |
| 94 | + content = toml.load(toml_handle) |
| 95 | + try: |
| 96 | + content["tool"]["pylint"] |
| 97 | + except KeyError: |
| 98 | + return False |
| 99 | + |
| 100 | + return True |
| 101 | + |
| 102 | + |
| 103 | +def _cfg_has_config(path): |
| 104 | + parser = configparser.ConfigParser() |
| 105 | + parser.read(path) |
| 106 | + return any(section.startswith("pylint.") for section in parser.sections()) |
| 107 | + |
| 108 | + |
| 109 | +def find_default_config_files(): |
| 110 | + """Find all possible config files.""" |
| 111 | + rc_names = ("pylintrc", ".pylintrc") |
| 112 | + config_names = rc_names + ("pyproject.toml", "setup.cfg") |
| 113 | + for config_name in config_names: |
| 114 | + if os.path.isfile(config_name): |
| 115 | + if config_name.endswith(".toml") and not _toml_has_config(config_name): |
| 116 | + continue |
| 117 | + if config_name.endswith(".cfg") and not _cfg_has_config(config_name): |
| 118 | + continue |
| 119 | + |
| 120 | + yield os.path.abspath(config_name) |
| 121 | + |
98 | 122 | if os.path.isfile("__init__.py"):
|
99 | 123 | curdir = os.path.abspath(os.getcwd())
|
100 | 124 | while os.path.isfile(os.path.join(curdir, "__init__.py")):
|
101 | 125 | curdir = os.path.abspath(os.path.join(curdir, ".."))
|
102 |
| - if os.path.isfile(os.path.join(curdir, "pylintrc")): |
103 |
| - return os.path.join(curdir, "pylintrc") |
104 |
| - if os.path.isfile(os.path.join(curdir, ".pylintrc")): |
105 |
| - return os.path.join(curdir, ".pylintrc") |
| 126 | + for rc_name in rc_names: |
| 127 | + rc_path = os.path.join(curdir, rc_name) |
| 128 | + if os.path.isfile(rc_path): |
| 129 | + yield rc_path |
| 130 | + |
106 | 131 | if "PYLINTRC" in os.environ and os.path.exists(os.environ["PYLINTRC"]):
|
107 |
| - pylintrc = os.environ["PYLINTRC"] |
| 132 | + if os.path.isfile(os.environ["PYLINTRC"]): |
| 133 | + yield os.environ["PYLINTRC"] |
108 | 134 | else:
|
109 | 135 | user_home = os.path.expanduser("~")
|
110 |
| - if user_home in ("~", "/root"): |
111 |
| - pylintrc = ".pylintrc" |
112 |
| - else: |
113 |
| - pylintrc = os.path.join(user_home, ".pylintrc") |
114 |
| - if not os.path.isfile(pylintrc): |
115 |
| - pylintrc = os.path.join(user_home, ".config", "pylintrc") |
116 |
| - if not os.path.isfile(pylintrc): |
117 |
| - if os.path.isfile("/etc/pylintrc"): |
118 |
| - pylintrc = "/etc/pylintrc" |
119 |
| - else: |
120 |
| - pylintrc = None |
121 |
| - return pylintrc |
| 136 | + if user_home not in ("~", "/root"): |
| 137 | + home_rc = os.path.join(user_home, ".pylintrc") |
| 138 | + if os.path.isfile(home_rc): |
| 139 | + yield home_rc |
| 140 | + home_rc = os.path.join(user_home, ".config", "pylintrc") |
| 141 | + if os.path.isfile(home_rc): |
| 142 | + yield home_rc |
| 143 | + |
| 144 | + if os.path.isfile("/etc/pylintrc"): |
| 145 | + yield "/etc/pylintrc" |
| 146 | + |
| 147 | + |
| 148 | +def find_pylintrc(): |
| 149 | + """search the pylint rc file and return its path if it find it, else None |
| 150 | + """ |
| 151 | + for config_file in find_default_config_files(): |
| 152 | + if config_file.endswith("pylintrc"): |
| 153 | + return config_file |
| 154 | + |
| 155 | + return None |
122 | 156 |
|
123 | 157 |
|
124 | 158 | PYLINTRC = find_pylintrc()
|
@@ -707,14 +741,28 @@ def helpfunc(option, opt, val, p, level=helplevel):
|
707 | 741 | if use_config_file:
|
708 | 742 | parser = self.cfgfile_parser
|
709 | 743 |
|
710 |
| - # Use this encoding in order to strip the BOM marker, if any. |
711 |
| - with io.open(config_file, "r", encoding="utf_8_sig") as fp: |
712 |
| - parser.read_file(fp) |
| 744 | + if config_file.endswith(".toml"): |
| 745 | + with open(config_file, "r") as fp: |
| 746 | + content = toml.load(fp) |
713 | 747 |
|
714 |
| - # normalize sections'title |
715 |
| - for sect, values in list(parser._sections.items()): |
716 |
| - if not sect.isupper() and values: |
717 |
| - parser._sections[sect.upper()] = values |
| 748 | + try: |
| 749 | + sections_values = content["tool"]["pylint"] |
| 750 | + except KeyError: |
| 751 | + pass |
| 752 | + else: |
| 753 | + for section, values in sections_values.items(): |
| 754 | + parser._sections[section.upper()] = values |
| 755 | + else: |
| 756 | + # Use this encoding in order to strip the BOM marker, if any. |
| 757 | + with io.open(config_file, "r", encoding="utf_8_sig") as fp: |
| 758 | + parser.read_file(fp) |
| 759 | + |
| 760 | + # normalize sections'title |
| 761 | + for sect, values in list(parser._sections.items()): |
| 762 | + if sect.startswith("pylint."): |
| 763 | + sect = sect[len("pylint."):] |
| 764 | + if not sect.isupper() and values: |
| 765 | + parser._sections[sect.upper()] = values |
718 | 766 |
|
719 | 767 | if not verbose:
|
720 | 768 | return
|
|
0 commit comments