diff options
author | Pierre Sassoulas <pierre.sassoulas@gmail.com> | 2020-05-03 11:09:23 +0200 |
---|---|---|
committer | Pierre Sassoulas <pierre.sassoulas@gmail.com> | 2020-05-03 14:40:55 +0200 |
commit | 02a402e22b8479437067cac52339d4dc0ae56321 (patch) | |
tree | fa4bedf40357cddb2d749db3ed174d1e222dd343 | |
parent | 7802e7dd6882af9e2e64762aa1f018415dd25f3f (diff) | |
download | pylint-git-02a402e22b8479437067cac52339d4dc0ae56321.tar.gz |
Create a file for find_default_config_files
-rw-r--r-- | .isort.cfg | 2 | ||||
-rw-r--r-- | pylint/config/__init__.py | 57 | ||||
-rw-r--r-- | pylint/config/find_default_config_files.py | 63 |
3 files changed, 65 insertions, 57 deletions
diff --git a/.isort.cfg b/.isort.cfg index 32dfac06e..c4ae79c5e 100644 --- a/.isort.cfg +++ b/.isort.cfg @@ -1,7 +1,7 @@ [settings] multi_line_output=3 line_length=88 -known_third_party=astroid, sphinx, isort, pytest, mccabe, six, +known_third_party=astroid, sphinx, isort, pytest, mccabe, six, toml include_trailing_comma=True skip_glob=tests/functional/**,tests/input/**,tests/extensions/data/**,tests/regrtest_data/**,tests/data/**,astroid/**,venv/** project=pylint diff --git a/pylint/config/__init__.py b/pylint/config/__init__.py index 452ec3dfc..6bb118cac 100644 --- a/pylint/config/__init__.py +++ b/pylint/config/__init__.py @@ -54,6 +54,7 @@ from typing import Any, Dict, Tuple import toml from pylint import utils +from pylint.config.find_default_config_files import find_default_config_files USER_HOME = os.path.expanduser("~") if "PYLINTHOME" in os.environ: @@ -94,62 +95,6 @@ def save_results(results, base): print("Unable to create file %s: %s" % (data_file, ex), file=sys.stderr) -def _toml_has_config(path): - with open(path) as toml_handle: - content = toml.load(toml_handle) - try: - content["tool"]["pylint"] - except KeyError: - return False - - return True - - -def _cfg_has_config(path): - parser = configparser.ConfigParser() - parser.read(path) - return any(section.startswith("pylint.") for section in parser.sections()) - - -def find_default_config_files(): - """Find all possible config files.""" - rc_names = ("pylintrc", ".pylintrc") - config_names = rc_names + ("pyproject.toml", "setup.cfg") - for config_name in config_names: - if os.path.isfile(config_name): - if config_name.endswith(".toml") and not _toml_has_config(config_name): - continue - if config_name.endswith(".cfg") and not _cfg_has_config(config_name): - continue - - yield os.path.abspath(config_name) - - if os.path.isfile("__init__.py"): - curdir = os.path.abspath(os.getcwd()) - while os.path.isfile(os.path.join(curdir, "__init__.py")): - curdir = os.path.abspath(os.path.join(curdir, "..")) - for rc_name in rc_names: - rc_path = os.path.join(curdir, rc_name) - if os.path.isfile(rc_path): - yield rc_path - - if "PYLINTRC" in os.environ and os.path.exists(os.environ["PYLINTRC"]): - if os.path.isfile(os.environ["PYLINTRC"]): - yield os.environ["PYLINTRC"] - else: - user_home = os.path.expanduser("~") - if user_home not in ("~", "/root"): - home_rc = os.path.join(user_home, ".pylintrc") - if os.path.isfile(home_rc): - yield home_rc - home_rc = os.path.join(user_home, ".config", "pylintrc") - if os.path.isfile(home_rc): - yield home_rc - - if os.path.isfile("/etc/pylintrc"): - yield "/etc/pylintrc" - - def find_pylintrc(): """search the pylint rc file and return its path if it find it, else None """ diff --git a/pylint/config/find_default_config_files.py b/pylint/config/find_default_config_files.py new file mode 100644 index 000000000..92d333f0f --- /dev/null +++ b/pylint/config/find_default_config_files.py @@ -0,0 +1,63 @@ +# Licensed under the GPL: https://www.gnu.org/licenses/old-licenses/gpl-2.0.html +# For details: https://github.com/PyCQA/pylint/blob/master/COPYING + +import configparser +import os + +import toml + + +def _toml_has_config(path): + with open(path) as toml_handle: + content = toml.load(toml_handle) + try: + content["tool"]["pylint"] + except KeyError: + return False + + return True + + +def _cfg_has_config(path): + parser = configparser.ConfigParser() + parser.read(path) + return any(section.startswith("pylint.") for section in parser.sections()) + + +def find_default_config_files(): + """Find all possible config files.""" + rc_names = ("pylintrc", ".pylintrc") + config_names = rc_names + ("pyproject.toml", "setup.cfg") + for config_name in config_names: + if os.path.isfile(config_name): + if config_name.endswith(".toml") and not _toml_has_config(config_name): + continue + if config_name.endswith(".cfg") and not _cfg_has_config(config_name): + continue + + yield os.path.abspath(config_name) + + if os.path.isfile("__init__.py"): + curdir = os.path.abspath(os.getcwd()) + while os.path.isfile(os.path.join(curdir, "__init__.py")): + curdir = os.path.abspath(os.path.join(curdir, "..")) + for rc_name in rc_names: + rc_path = os.path.join(curdir, rc_name) + if os.path.isfile(rc_path): + yield rc_path + + if "PYLINTRC" in os.environ and os.path.exists(os.environ["PYLINTRC"]): + if os.path.isfile(os.environ["PYLINTRC"]): + yield os.environ["PYLINTRC"] + else: + user_home = os.path.expanduser("~") + if user_home not in ("~", "/root"): + home_rc = os.path.join(user_home, ".pylintrc") + if os.path.isfile(home_rc): + yield home_rc + home_rc = os.path.join(user_home, ".config", "pylintrc") + if os.path.isfile(home_rc): + yield home_rc + + if os.path.isfile("/etc/pylintrc"): + yield "/etc/pylintrc" |