diff --git a/blue/__init__.py b/blue/__init__.py index c6b60ea..4a6c27a 100644 --- a/blue/__init__.py +++ b/blue/__init__.py @@ -7,6 +7,8 @@ import re import sys +from configparser import ConfigParser + from importlib import machinery __version__ = '0.9.1' @@ -70,7 +72,7 @@ def path_hook(cls): from black import Leaf, Path, click, token from black.cache import user_cache_dir from black.comments import ProtoComment, make_comment -from black.files import tomli +from black.files import tomli, find_user_pyproject_toml from black.linegen import LineGenerator as BlackLineGenerator from black.lines import Line from black.nodes import ( @@ -86,9 +88,6 @@ def path_hook(cls): sub_twice, ) -from flake8.options import config as flake8_config -from flake8.options import manager as flake8_manager - from enum import Enum from functools import lru_cache from typing import Any, Dict, Iterator, List, Optional, Pattern @@ -393,21 +392,49 @@ def format_file_in_place(*args, **kws): return black_format_file_in_place(*args, **kws) -try: - BaseConfigParser = flake8_config.ConfigParser # flake8 v4 -except AttributeError: - BaseConfigParser = flake8_config.MergedConfigParser # flake8 v3 +def load_configs_from_file() -> Dict[str, Any]: + """Parses supported config files using configparser""" + supported_config_files = ('setup.cfg', 'tox.ini', '.blue') + config_dict = {} + pwd = Path.cwd() + cfg = ConfigParser() + + config_file_found = False + + # search config files from pwd and its parents + for dir in (pwd, *pwd.parents): + filenames = [ + (dir / config_file) for config_file in supported_config_files + ] + files_read = cfg.read(filenames) + + # if config file was read, stop search + if len(files_read) > 0: + config_file_found = True + break + + if not config_file_found: + # config file not found yet + # last try using top-level user configuration for black + try: + top_level_full_path = find_user_pyproject_toml() + + top_level_dir = top_level_full_path.parent + + filenames = [ + (top_level_dir / config_file) + for config_file in supported_config_files + ] + + cfg.read(filenames) + except PermissionError: + # ignore user level config directory if no access permission was given + pass + if cfg.has_section('blue'): + config_dict.update(cfg.items('blue')) -class MergedConfigParser(BaseConfigParser): - def _parse_config(self, config_parser, parent=None): - """Skip option parsing in flake8's config parsing.""" - config_dict = {} - for option_name in config_parser.options(self.program_name): - value = config_parser.get(self.program_name, option_name) - LOG.debug('Option "%s" has value: %r', option_name, value) - config_dict[option_name] = value - return config_dict + return config_dict def read_configs( @@ -416,12 +443,9 @@ def read_configs( """Read configs through the config param's callback hook.""" # Use black's `read_pyproject_toml` for the default result = black.read_pyproject_toml(ctx, param, value) - # Use flake8's config file parsing to load setup.cfg, tox.ini, and .blue + # parses setup.cfg, tox.ini, and .blue config files # The parsing looks both in the project and user directories. - finder = flake8_config.ConfigFileFinder('blue') - manager = flake8_manager.OptionManager('blue', '0') - parser = MergedConfigParser(manager, finder) - config = parser.parse() + config = load_configs_from_file() # Merge the configs into Click's `default_map`. default_map: Dict[str, Any] = {} default_map.update(ctx.default_map or {}) diff --git a/setup.py b/setup.py index adbd833..93be9c8 100644 --- a/setup.py +++ b/setup.py @@ -37,7 +37,7 @@ def run_tests(self): packages=['blue'], tests_require=['tox'], cmdclass={'test': Tox}, - install_requires=['black==22.1.0', 'flake8>=3.8,<5.0.0'], + install_requires=['black==22.1.0'], project_urls={ 'Documentation': 'https://blue.readthedocs.io/en/latest', 'Source': 'https://github.com/grantjenks/blue.git',