Fixit icon indicating copy to clipboard operation
Fixit copied to clipboard

Feature Request: configuration using `pyproject.toml`

Open pwoolvett opened this issue 4 years ago • 0 comments
trafficstars

Would you accept a PR to configure fixit using pyproject.toml?

something like this:

diff --git a/fixit/common/config.py b/fixit/common/config.py
index 9f723e0..e361f6a 100644
--- a/fixit/common/config.py
+++ b/fixit/common/config.py
@@ -9,16 +9,19 @@ import re
 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, List, Pattern, Set
 
 import yaml
+import toml
 
 from fixit.common.base import LintConfig
 from fixit.common.utils import LintRuleCollectionT, import_distinct_rules_from_package
 
 
-LINT_CONFIG_FILE_NAME: Path = Path(".fixit.config.yaml")
-
+LINT_CONFIG_FILE_NAMES: List[str] = [
+    ".fixit.config.yaml",
+    "pyproject.toml",
+]
 # https://gitlab.com/pycqa/flake8/blob/9631dac52aa6ed8a3de9d0983c/src/flake8/defaults.py
 NOQA_INLINE_REGEXP: Pattern[str] = re.compile(
     # TODO: Deprecate
@@ -115,17 +118,21 @@ def get_validated_settings(
 def get_lint_config() -> LintConfig:
     config = {}
 
-    cwd = Path.cwd()
-    for directory in (cwd, *cwd.parents):
-        # Check for config file.
-        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, directory)
-                break
+    current_dir = Path.cwd()
+    for directory in (current_dir, *current_dir.parents):
+        for lint_config_file_name in LINT_CONFIG_FILE_NAMES:
+            possible_config = directory / lint_config_file_name
+            if possible_config.is_file():
+                with open(possible_config, "r") as f:
+                    raw = f.read()
+                file_content = {
+                    ".yaml": yaml.safe_load,
+                    ".toml": lambda text: toml.loads(text)["tool.fixit"],
+                }[possible_config.suffix](raw)
+
+                if isinstance(file_content, dict):
+                    config = get_validated_settings(file_content, directory)
+                    break
 
     # Find formatter executable if there is one.
     formatter_args = config.get("formatter", DEFAULT_FORMATTER)
@@ -139,7 +146,7 @@ def get_lint_config() -> LintConfig:
 
 def gen_config_file() -> None:
     # Generates a `.fixit.config.yaml` file with defaults in the current working dir.
-    config_file = LINT_CONFIG_FILE_NAME.resolve()
+    config_file = Path(LINT_CONFIG_FILE_NAMES[0]).resolve()
     default_config_dict = asdict(LintConfig())
     with open(config_file, "w") as cf:
         yaml.dump(default_config_dict, cf)

pwoolvett avatar Feb 02 '21 02:02 pwoolvett