From 6fe8953bf9738ccd2f9ee23af1762524ab93aad1 Mon Sep 17 00:00:00 2001 From: pwoolvett Date: Wed, 31 Mar 2021 15:58:45 -0300 Subject: [PATCH] refactor(config): search yaml using `pathlib.Path.parents` (#172) * refactor: search yaml using `pathlib.Path.parents` Signed-off-by: Pablo Woolvett * Update config.py --- fixit/common/config.py | 16 ++++++---------- 1 file changed, 6 insertions(+), 10 deletions(-) diff --git a/fixit/common/config.py b/fixit/common/config.py index fb086ab7..47521bfd 100644 --- a/fixit/common/config.py +++ b/fixit/common/config.py @@ -9,7 +9,7 @@ from dataclasses import asdict from functools import lru_cache from pathlib import Path -from typing import Any, Dict, Optional, Pattern, Set +from typing import Any, Dict, Pattern, Set import yaml @@ -114,23 +114,19 @@ def get_validated_settings( @lru_cache() def get_lint_config() -> LintConfig: config = {} - current_dir = Path.cwd() - previous_dir: Optional[Path] = None - while current_dir != previous_dir: + + cwd = Path.cwd() + for directory in (cwd, *cwd.parents): # Check for config file. - possible_config = current_dir / LINT_CONFIG_FILE_NAME + possible_config = directory / LINT_CONFIG_FILE_NAME if possible_config.is_file(): with open(possible_config, "r") as f: file_content = yaml.safe_load(f.read()) if isinstance(file_content, dict): - config = get_validated_settings(file_content, current_dir) + config = get_validated_settings(file_content, directory) break - # Try to go up a directory. - previous_dir = current_dir - current_dir = current_dir.parent - # Find formatter executable if there is one. formatter_args = config.get("formatter", DEFAULT_FORMATTER) exe = distutils.spawn.find_executable(formatter_args[0]) or formatter_args[0]