Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: remove flake8 dependency #96

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
68 changes: 46 additions & 22 deletions blue/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@
import re
import sys

from configparser import ConfigParser

from importlib import machinery

__version__ = '0.9.1'
Expand Down Expand Up @@ -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 (
Expand All @@ -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
Expand Down Expand Up @@ -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(
Expand All @@ -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 {})
Expand Down
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand Down