-
Notifications
You must be signed in to change notification settings - Fork 10
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
110 additions
and
16 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
from typing import Any, Dict, List | ||
import configparser | ||
|
||
from flake8p.util import monkeypatch | ||
|
||
import flake8.options.config | ||
|
||
class FlakeConfigToml: | ||
def __init__(self, tree): | ||
pass | ||
|
||
def __iter__(self): | ||
raise StopIteration() | ||
|
||
def run(self, *args) -> List[Any]: | ||
return [] | ||
|
||
def add_options(self, *args): | ||
monkeypatch() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,80 @@ | ||
from typing import Tuple, Any, Dict, List, Optional | ||
from pathlib import Path | ||
import configparser | ||
|
||
if sys.version_info >= (3, 11): | ||
import tomllib as toml | ||
else: | ||
import tomli as toml | ||
|
||
import flake8.options.config | ||
|
||
def _stat_key(p: Path) -> Tuple[int, int]: | ||
st = p.stat() | ||
return st.st_ino, st.st_dev | ||
|
||
def load_flake8_from_toml(filename: Path) -> Dict[str, Any]: | ||
try: | ||
with filename.open('rb') as f: | ||
settings = toml.load(f) | ||
if 'tool' in settings and 'flake8' in settings['tool']: | ||
return {"flake8": settings["tool"]["flake8"]} | ||
else: | ||
return {} | ||
except Exception: | ||
return {} | ||
|
||
def normalize_from_toml(settings: Dict[str, Any]) -> Dict[str, Any]: | ||
output = {} | ||
for key, value in settings["flake8"].items(): | ||
if isinstance(value, (bool, int, float)): | ||
value = str(value) | ||
elif isinstance(value, list): | ||
value = ",".join(str(x) for x in value) | ||
output[key] = value | ||
return {"flake8": output} | ||
|
||
def find_and_load_toml_file(_path: Optional[str]) -> Tuple[Optional[Path], Dict[str, Any]]: | ||
if _path: | ||
path = Path(_path).resolve() | ||
else: | ||
path = Path(".").resolve() | ||
try: | ||
# if the homedir isn't detected expanduser() raises RuntimeError | ||
home_stat = _stat_key(Path("~").expanduser()) | ||
except RuntimeError: | ||
home_stat = None | ||
|
||
dir_stat = _stat_key(path) | ||
while True: | ||
cfg_path = path / "pyproject.toml" | ||
if cfg_path.exists(): | ||
cfg = load_flake8_from_toml(cfg_path) | ||
if cfg: | ||
return cfg_path, cfg | ||
new_path = path.parent | ||
new_dir_stat = _stat_key(new_path) | ||
if new_dir_stat == dir_stat or new_dir_stat == home_stat: | ||
break | ||
else: | ||
path = new_path | ||
dir_stat = new_dir_stat | ||
|
||
# did not find any configuration file | ||
return None, {} | ||
|
||
def monkeypatch(): | ||
flake8_parse_config = flake8.options.config.parse_config | ||
flake8_option_manager = flake8.options.config.OptionManager | ||
|
||
def parse_config( | ||
option_manager: flake8_option_manager, | ||
cfg: configparser.RawConfigParser, | ||
cfg_dir: str, | ||
) -> Dict[str, Any]: | ||
loaded_toml = find_and_load_toml_file() | ||
normalized = normalize_from_toml(loaded_toml) | ||
cfg.read_dict(normalized) | ||
return flake8_parse_config(option_manager, cfg, cfg_dir) | ||
|
||
flake8.options.config.parse_config = parse_config |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters