Skip to content

Commit

Permalink
Merge e531517 into 6aa82a4
Browse files Browse the repository at this point in the history
  • Loading branch information
bnavigator committed Jul 13, 2020
2 parents 6aa82a4 + e531517 commit 539a387
Show file tree
Hide file tree
Showing 5 changed files with 340 additions and 312 deletions.
28 changes: 20 additions & 8 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -1,15 +1,27 @@
dist: xenial
language: python
matrix:
- python: '2.7'
- python: '3.5'
- python: '3.6'
- python: '3.7'
- python: '3.8'
- python: 'pypy'
- python: 'pypy3'
python:
- '2.7'
- '3.5'
- '3.6'
- '3.7'
- '3.8'
- 'pypy'
- 'pypy3'
env:
- ISORT_VER="4"
- ISORT_VER="5"
jobs:
exclude:
- python: "2.7"
env: ISORT_VER="5"
- python: "3.5"
env: ISORT_VER="5"
- python: "pypy"
env: ISORT_VER="5"
cache: pip
install:
- if [[ $ISORT_VER == "4" ]]; then pip install 'isort[pyproject] < 5'; fi
- pip install .[test]
script:
- flake8 *.py
Expand Down
4 changes: 2 additions & 2 deletions CHANGES.rst
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,10 @@
Changelog
=========

3.0.2 (unreleased)
4.0.0 (unreleased)
------------------

- Nothing changed yet.
- support isort >= 5 [bnavigator, pkolbus]


3.0.1 (2020-07-08)
Expand Down
106 changes: 101 additions & 5 deletions flake8_isort.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,20 @@
# -*- coding: utf-8 -*-
from difflib import Differ
from isort import SortImports
from testfixtures import OutputCapture

import isort

__version__ = '3.0.2.dev0'

if hasattr(isort, 'api'): # isort 5
from pathlib import Path

class Flake8Isort(object):
import warnings
else:
from difflib import Differ

__version__ = '4.0.0.dev0'


class Flake8IsortBase(object):
name = 'flake8_isort'
version = __version__
isort_unsorted = (
Expand Down Expand Up @@ -49,13 +56,17 @@ def parse_options(cls, options):
cls.stdin_display_name = options.stdin_display_name
cls.show_traceback = options.isort_show_traceback


class Flake8Isort4(Flake8IsortBase):
"""class for isort <5"""

def run(self):
if self.filename is not self.stdin_display_name:
file_path = self.filename
else:
file_path = None
with OutputCapture() as buffer:
sort_result = SortImports(
sort_result = isort.SortImports(
file_path=file_path,
file_contents=''.join(self.lines),
check=True,
Expand Down Expand Up @@ -168,3 +179,88 @@ def _fixup_sortimports_wrapped(sort_imports):
for new_idx, new_line in enumerate(
sort_imports.out_lines.pop(idx).splitlines()):
sort_imports.out_lines.insert(idx + new_idx, new_line)


class Flake8Isort5(Flake8IsortBase):
"""class for isort >=5"""

def run(self):
if self.filename is not self.stdin_display_name:
file_path = Path(self.filename)
isort_config = isort.settings.Config(
settings_path=file_path.parent)
else:
isort_config = isort.settings.Config(
settings_path=Path.cwd())
in_string = ''.join(self.lines)
traceback = ''
iscorrect = True
with OutputCapture() as buffer:
try:
if self.filename is not self.stdin_display_name:
iscorrect = isort.api.check_file(
filename=self.filename,
show_diff=True,
config=isort_config)
else:
iscorrect = isort.api.check_code_string(
code=in_string,
show_diff=True,
config=isort_config)
except isort.exceptions.FileSkipped:
pass
except isort.exceptions.ISortError as e:
warnings.warn(e)
isort_output = buffer.captured
print(isort_output)
if not iscorrect:
traceback += isort_output
for line_num, message in self.isort_linenum_msg(isort_output):
if self.show_traceback:
message += traceback
yield line_num, 0, message, type(self)

def isort_linenum_msg(self, isort_output):
"""Parse isort output for line number changes and message
Args
----
isort_diff : the stdout of the isort check when show_diff=True
Yields
------
tuple: A tuple of the specific isort line number and message.
"""
line_num = 0
additions = []
moves = []
for line in isort_output.splitlines():
if line.startswith('@@', 0, 2):
line_num = int(line[4:].split(' ')[0].split(',')[0])
continue
elif not line_num: # skip lines before first hunk
continue
if line.startswith(' ', 0, 1):
line_num += 1 # Ignore unchanged lines but increment line_num.
elif line.startswith('-', 0, 1):
if line.strip() == '-':
yield line_num, self.isort_blank_unexp
line_num += 1
else:
moves.append(line[1:])
yield line_num, self.isort_unsorted
line_num += 1
elif line.startswith('+', 0, 1):
if line.strip() == '+':
# Include newline additions but do not increment line_num.
yield line_num, self.isort_blank_req
else:
additions.append((line_num, line))

# return all additions that did not move
for line_num, line in additions:
if not line[1:] in moves:
yield line_num, self.isort_add_unexp


Flake8Isort = Flake8Isort5 if hasattr(isort, 'api') else Flake8Isort4
Loading

0 comments on commit 539a387

Please sign in to comment.