Skip to content
This repository has been archived by the owner on Dec 3, 2022. It is now read-only.

Commit

Permalink
Feature/isort (#20)
Browse files Browse the repository at this point in the history
* add isort package

* black compatible isort settings

* add sort command

* apply sorting to project

* use command line arguments

cannot rely on isort configuration to be set when installed as a package

* print to stdout when running fmt

help to distinguish from sort task

* add fmt-only command

* add sort test param

* add isort to readme

* test _fmt_cmd
  • Loading branch information
Kilo59 committed Feb 16, 2020
1 parent 17085a7 commit 4246e51
Show file tree
Hide file tree
Showing 9 changed files with 90 additions and 19 deletions.
1 change: 1 addition & 0 deletions Pipfile
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ name = "pypi"

[packages]
black = "==19.10b0"
isort = "*"
invoke = "*"
prospector = {extras = ["with_pyroma"],version = "*"}

Expand Down
9 changes: 5 additions & 4 deletions Pipfile.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
# blue-chip
One click Python code quality package

`blue-chip` uses [`black`](https://github.com/ambv/black), [`invoke`](http://www.pyinvoke.org/) and [`prospector`](https://prospector.readthedocs.io/en/master/) configured to work well together out of the box.
`blue-chip` uses [`black`](https://github.com/ambv/black), [`isort`](https://github.com/timothycrosley/isort), [`invoke`](http://www.pyinvoke.org/) and [`prospector`](https://prospector.readthedocs.io/en/master/) configured to work well together out of the box.

-----------------------
## Install
Expand All @@ -30,6 +30,7 @@ Subcommands:
fmt Format python source code.
lint Run static analysis on python source code.
sort Sort module imports.
```

----------------------
Expand Down
4 changes: 2 additions & 2 deletions blue_chip/__main__.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@
~~~~~~~~~~~~~~~~~~~~~
"""
from invoke import Collection, Program # pylint: disable=import-error
from blue_chip import tasks
from blue_chip import __version__

from blue_chip import __version__, tasks

# pylint: disable=invalid-name
program = Program(
Expand Down
2 changes: 1 addition & 1 deletion blue_chip/tasks/__init__.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
# flake8: noqa
from .linting import *
from .formatting import *
from .linting import *
60 changes: 56 additions & 4 deletions blue_chip/tasks/formatting.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
blue_chip.tasks.formatting.py
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
"""
from typing import List, Union

from invoke import task

from blue_chip import constants
Expand All @@ -13,10 +15,60 @@
constants.LINE_LENGTH
),
"targets": "Paths/directories to format. [default: . ]",
}
},
)
def sort(ctx, line_length=constants.LINE_LENGTH, targets="."):
"""Sort module imports."""
print("sorting imports ...")
args = [
"isort",
"--use-parentheses",
"--trailing-comma",
"--force-grid-wrap",
"0",
"--multi-line",
"3",
"-l",
str(line_length),
"-rc",
"--atomic",
targets,
]
ctx.run(" ".join(args))


def _fmt_cmd(line_length: int, targets: Union[str, List[str]]) -> str:
args = ["black", "--line-length", str(line_length)]
if isinstance(targets, (list, tuple, set)):
args.extend(targets)
else:
args.append(targets)
return " ".join(args)


@task(
pre=[sort],
help={
"line-length": "How many characters per line to allow. [default: {}]".format(
constants.LINE_LENGTH
),
"targets": "Paths/directories to format. [default: . ]",
},
)
def fmt(ctx, line_length=constants.LINE_LENGTH, targets="."):
"""Format python source code & sort imports."""
print("formatting ...")
ctx.run(_fmt_cmd(line_length, targets))


@task(
help={
"line-length": "How many characters per line to allow. [default: {}]".format(
constants.LINE_LENGTH
),
"targets": "Paths/directories to format. [default: . ]",
},
)
def fmt_only(ctx, line_length=constants.LINE_LENGTH, targets="."):
"""Format python source code."""
if isinstance(targets, (list, tuple, set)):
targets = " ".join(targets)
ctx.run(f"black --line-length {line_length} {targets}")
ctx.run(_fmt_cmd(line_length, targets))
4 changes: 1 addition & 3 deletions blue_chip/tasks/linting.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,7 @@

from invoke import task

from blue_chip import constants
from blue_chip import config

from blue_chip import config, constants

# pylint:disable=protected-access

Expand Down
7 changes: 7 additions & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -26,3 +26,10 @@ classifiers = [

[tool.flit.scripts]
bc = "blue_chip.__main__:program.run"

[tool.isort]
multi_line_output = 3
include_trailing_comma = "True"
force_grid_wrap = 0
use_parentheses = "True"
line_length = 88
19 changes: 15 additions & 4 deletions tests/test_basic.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
"""tests/test_basic.py"""
import subprocess
import pathlib
import importlib
from setuptools import find_packages
import pathlib
import subprocess

import pytest
from setuptools import find_packages

ROOT = pathlib.Path(__file__).joinpath("..").resolve()

Expand All @@ -28,13 +28,24 @@ def test_for_fire():
raise ImportError(", ".join(on_fire.keys()))


@pytest.mark.parametrize("cmd", ["fmt", "lint"])
@pytest.mark.parametrize("cmd", ["fmt", "lint", "sort"])
def test_noarg_cli_task(cmd):
cmplt_process = subprocess.run(["bc", cmd])
print(cmplt_process.stdout)
assert cmplt_process.returncode is 0


@pytest.mark.parametrize(
"targets_arg",
["foo.py", ("foo.py", "bar.py"), ["foo.py", "bar.py"], {"foo.py", "bar.py"}],
)
def test_fmt_cmd(targets_arg):
from blue_chip.tasks.formatting import _fmt_cmd

cmd_string = _fmt_cmd(88, targets_arg)
assert isinstance(cmd_string, str)


if __name__ == "__main__":
print(ROOT)
pytest.main(args=[__file__, "-v"])

0 comments on commit 4246e51

Please sign in to comment.