This repository has been archived by the owner on Oct 4, 2022. It is now read-only.
forked from gforcada/flake8-isort
-
Notifications
You must be signed in to change notification settings - Fork 0
/
flake8_isort.py
86 lines (70 loc) · 2.67 KB
/
flake8_isort.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
# -*- coding: utf-8 -*-
from isort import SortImports
from testfixtures import OutputCapture
import os
import pep8
try:
from configparser import ConfigParser
except ImportError:
from ConfigParser import ConfigParser
class Flake8Isort(object):
name = 'flake8_isort'
version = '0.1'
isort_error_msg = 'I001 isort found changes, run it on the file'
no_config_msg = 'I002 no configuration found (.isort.cfg or [isort] on ' \
'setup.cfg)'
config_file = None
def __init__(self, tree, filename):
self.filename = filename
@classmethod
def add_options(cls, parser):
parser.add_option(
'--no-isort-config',
action='store_true',
help='Do not require explicit configuration to be found'
)
parser.config_options.append('no-isort-config')
@classmethod
def parse_options(cls, options):
if options.no_isort_config is None:
cls.config_file = True
else:
cls.config_file = False
def run(self):
if self.config_file and not self.search_isort_config():
yield 0, 0, self.no_config_msg, type(self)
else:
with OutputCapture():
if self.filename == 'stdin':
sort_result = SortImports(
file_contents=pep8.stdin_get_value(),
check=True,
)
else:
sort_result = SortImports(self.filename, check=True)
if sort_result.incorrectly_sorted:
yield 0, 0, self.isort_error_msg, type(self)
def search_isort_config(self):
"""Search for isort configuration all the way up to the root folder
Either on ``.isort.cfg`` file or an ``[isort]`` section on
``setup.cfg``.
"""
full_path = os.path.abspath(self.filename)
path_parts = full_path.split(os.path.sep)
dirs_missing = len(path_parts)
while dirs_missing > 0:
dirs_missing -= 1
partial_parts = path_parts[:dirs_missing]
partial_path = os.sep.join(partial_parts)
isort_file = '{0}{1}.isort.cfg'.format(partial_path, os.sep)
if os.path.exists(isort_file):
return True
# If the setup file exists and has an "isort" section,
# then we've found the configuration.
setup_file = '{0}{1}setup.cfg'.format(partial_path, os.sep)
if os.path.exists(setup_file):
config = ConfigParser()
config.read(setup_file)
if 'isort' in config.sections():
return True
return False