Skip to content

Commit

Permalink
WIP
Browse files Browse the repository at this point in the history
  • Loading branch information
gforcada committed Dec 15, 2015
1 parent 27af1f2 commit 113f82a
Showing 1 changed file with 38 additions and 3 deletions.
41 changes: 38 additions & 3 deletions flake8_isort.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
# -*- coding: utf-8 -*-
from isort import SortImports

import os


class Flake8Isort(object):
name = 'flake8_isort'
Expand All @@ -10,7 +12,40 @@ class Flake8Isort(object):
def __init__(self, tree, filename):
self.filename = filename

@classmethod
def add_options(cls, parser):
parser.add_option(
'--force-isort-config',
action='store_true',
help='Require an .isort.cfg file to be found'
)
parser.config_options.append('force-isort-config')

@classmethod
def parse_options(cls, options):
cls.isort_config = options.force_isort_config

def run(self):
sort_result = SortImports(self.filename, check=True)
if sort_result.incorrectly_sorted:
yield 0, 0, self.message, type(self)
if self.isort_config and not self.search_isort_config():
yield 0, 0, 'I002 no .isort.cfg file found', type(self)
else:
sort_result = SortImports(self.filename, check=True)
if sort_result.incorrectly_sorted:
yield 0, 0, self.message, type(self)

def search_isort_config(self):
"""Search for a .isort.cfg all the way up to the root folder"""
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

return False

0 comments on commit 113f82a

Please sign in to comment.