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

feature/BLACK_CONFIG: Black configuration file configurable #11

Merged
merged 2 commits into from Aug 6, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
3 changes: 3 additions & 0 deletions README.rst
Expand Up @@ -144,6 +144,9 @@ Version History
======= ============ ===========================================================
Version Release date Changes
------- ------------ -----------------------------------------------------------
v0.1.1 2019-08-06 - Option to use a (global) black configuration file,
contribution from
`Tomasz Grining <https://github.com/098799>`_.
v0.1.0 2019-06-03 - Uses main ``black`` settings from ``pyproject.toml``,
contribution from `Alex <https://github.com/ADKosm>`_.
- WARNING: Now ignores ``flake8`` max-line-length setting.
Expand Down
46 changes: 41 additions & 5 deletions flake8_black.py
Expand Up @@ -3,6 +3,8 @@
This is a plugin for the tool flake8 tool for checking Python
soucre code using the tool black.
"""
from functools import lru_cache
from os import path
from pathlib import Path

import black
Expand All @@ -11,7 +13,7 @@
from flake8 import utils as stdin_utils


__version__ = "0.1.0"
__version__ = "0.1.1"

black_prefix = "BLK"

Expand Down Expand Up @@ -52,17 +54,30 @@ def __init__(self, tree, filename="(none)", builtins=None):
# see property self._file_mode for new black versions:
self.file_mode = 0 # was: black.FileMode.AUTO_DETECT

def _load_black_config(self):
@property
@lru_cache()
peterjc marked this conversation as resolved.
Show resolved Hide resolved
def config_file(self):
"""File path to the black configuration file."""
if self.flake8_black_config:
flake8_black_path = Path(self.flake8_black_config)

if self.flake8_config:
flake8_config_path = path.dirname(path.abspath(self.flake8_config))
return Path(flake8_config_path) / flake8_black_path

return flake8_black_path

source_path = (
self.filename
if self.filename not in self.STDIN_NAMES
else Path.cwd().as_posix()
)
project_root = black.find_project_root((Path(source_path),))
path = project_root / "pyproject.toml"
return project_root / "pyproject.toml"

if path.is_file():
pyproject_toml = toml.load(str(path))
def _load_black_config(self):
if self.config_file.is_file():
pyproject_toml = toml.load(str(self.config_file))
config = pyproject_toml.get("tool", {}).get("black", {})
return {k.replace("--", "").replace("-", "_"): v for k, v in config.items()}
return None
Expand Down Expand Up @@ -98,6 +113,27 @@ def _file_mode(self):
assert "got an unexpected keyword argument" in str(e), e
return None

@classmethod
def add_options(cls, parser):
"""Adding black-config option."""
parser.add_option(
"--black-config",
default=None,
action="store",
type="string",
parse_from_config=True,
help=(
"Path to black configuration file"
"(overrides the default pyproject.toml)",
),
)

@classmethod
def parse_options(cls, options):
"""Adding black-config option."""
cls.flake8_black_config = options.black_config
cls.flake8_config = options.config

def run(self):
"""Use black to check code style."""
msg = None
Expand Down
2 changes: 2 additions & 0 deletions tests/flake8_config/flake8
@@ -0,0 +1,2 @@
[flake8]
black-config = ../with_pyproject_toml/pyproject.toml
1 change: 1 addition & 0 deletions tests/run_tests.sh
Expand Up @@ -9,6 +9,7 @@ flake8 --select BLK test_cases/*.py
flake8 --select BLK --max-line-length 50 test_cases/*.py
flake8 --select BLK --max-line-length 90 test_cases/*.py
flake8 --select BLK with_pyproject_toml/*.py
flake8 --select BLK without_pyproject_toml/*.py --config=flake8_config/flake8
flake8 --select BLK --max-line-length 88 with_pyproject_toml/
flake8 --select BLK non_conflicting_configurations/*.py
flake8 --select BLK conflicting_configurations/*.py
Expand Down
10 changes: 10 additions & 0 deletions tests/without_pyproject_toml/ordinary_quotes.py
@@ -0,0 +1,10 @@
#!/usr/bin/env python
# -*- coding: utf-8 -*-
"""Print 'Hello world' to the terminal.

This is a simple test script using a hashbang line
and a PEP263 encoding line.
We use ordinary quotes in this test.
"""

print('Hello world')