From 266204d1d17c9e2097a7b0e133871650506984e3 Mon Sep 17 00:00:00 2001 From: Christian Zandee Date: Fri, 26 Nov 2021 19:31:06 +0100 Subject: [PATCH] allow additional default configuration locations --- src/flake8/options/config.py | 38 ++++++++++++++++++++++++++---------- 1 file changed, 28 insertions(+), 10 deletions(-) diff --git a/src/flake8/options/config.py b/src/flake8/options/config.py index d7519df..e762f45 100644 --- a/src/flake8/options/config.py +++ b/src/flake8/options/config.py @@ -11,21 +11,31 @@ from typing import Tuple from flake8.options.manager import OptionManager LOG = logging.getLogger(__name__) +DEFAULT_CANDIDATES = ("setup.cfg", "tox.ini", ".flake8") +ADDITIONAL_CANDIDATES = ("flake8",) +ADDITIONAL_LOCATIONS = (os.path.expanduser(r'~'), + os.path.expanduser(r'~/.config')) + + +def _is_config(path: str) -> bool: + cfg = configparser.RawConfigParser() + try: + cfg.read(path) + except (UnicodeDecodeError, configparser.ParsingError) as e: + LOG.warning("ignoring unparseable config %s: %s", path, e) + else: + # only consider it a config if it contains flake8 sections + if "flake8" in cfg or "flake8:local-plugins" in cfg: + return True + return False def _find_config_file(path: str) -> Optional[str]: - cfg = configparser.RawConfigParser() while True: - for candidate in ("setup.cfg", "tox.ini", ".flake8"): + for candidate in DEFAULT_CANDIDATES: cfg_path = os.path.join(path, candidate) - try: - cfg.read(cfg_path) - except (UnicodeDecodeError, configparser.ParsingError) as e: - LOG.warning("ignoring unparseable config %s: %s", cfg_path, e) - else: - # only consider it a config if it contains flake8 sections - if "flake8" in cfg or "flake8:local-plugins" in cfg: - return cfg_path + if _is_config(cfg_path): + return cfg_path new_path = os.path.dirname(path) if new_path == path: @@ -33,6 +43,14 @@ def _find_config_file(path: str) -> Optional[str]: else: path = new_path + # try some additional locations with additional candidate names + candidates = DEFAULT_CANDIDATES + ADDITIONAL_CANDIDATES + for location in ADDITIONAL_LOCATIONS: + for candidate in candidates: + cfg_path = os.path.join(location, candidate) + if _is_config(cfg_path): + return cfg_path + # did not find any configuration file return None