Failed Conditions
Pull Request — master (#1401)
by Abdeali
01:32
created

bears.python.PyImportSortBear.run_isort()   A

Complexity

Conditions 2

Size

Total Lines 14

Duplication

Lines 0
Ratio 0 %
Metric Value
cc 2
dl 0
loc 14
rs 9.4285
1
from isort import SortImports
2
3
from coalib.bearlib.abstractions.CorrectionBasedBear import CorrectionBasedBear
4
from coalib.bearlib.spacing.SpacingHelper import SpacingHelper
5
from coalib.bears.LocalBear import LocalBear
0 ignored issues
show
Unused Code introduced by
Unused LocalBear imported from coalib.bears.LocalBear
Loading history...
6
from coalib.results.Diff import Diff
0 ignored issues
show
Unused Code introduced by
Unused Diff imported from coalib.results.Diff
Loading history...
7
from coalib.results.Result import Result
0 ignored issues
show
Unused Code introduced by
Unused Result imported from coalib.results.Result
Loading history...
8
9
10
class PyImportSortBear(CorrectionBasedBear):
11
    RESULT_MESSAGE = "Imports can be sorted."
12
13
    @staticmethod
14
    def run_isort(file,
15
                  use_spaces,
16
                  max_line_length,
17
                  indent_size,
18
                  use_parentheses_in_import,
19
                  isort_multi_line_output):
20
        indent = "Tab" if use_spaces == False else indent_size
21
        new_file = SortImports(file_contents=''.join(file),
22
                               line_length=max_line_length,
23
                               indent=indent,
24
                               multi_line_output=isort_multi_line_output,
25
                               use_parentheses=use_parentheses_in_import).output
26
        return new_file.splitlines(True), []
27
28
    GET_REPLACEMENT = run_isort
29
30
    def run(self,
31
            filename,
32
            file,
33
            use_spaces: bool=True,
34
            tab_width: int=SpacingHelper.DEFAULT_TAB_WIDTH,
35
            max_line_length: int=80,
36
            use_parentheses_in_import: bool=True,
37
            isort_multi_line_output: int=4):
38
        """
39
        Sorts imports for python.
40
41
        :param use_spaces:                True if spaces are to be used
42
                                          instead of tabs.
43
        :param tab_width:                 Number of spaces per indent level.
44
        :param max_line_length:           Maximum number of characters for
45
                                          a line.
46
        :param use_parentheses_in_import: True if parenthesis are to be used
47
                                          in import statements.
48
        :param isort_multi_line_output:   The type of formatting to be used by
49
                                          isort when indenting imports. This
50
                                          value if passed to isort as the
51
                                          `multi_line_output` setting.
52
        """
53
        for result in self.retrieve_results(
54
                  filename,
55
                  file,
56
                  use_spaces=use_spaces,
57
                  indent_size=tab_width,
58
                  max_line_length=max_line_length,
59
                  use_parentheses_in_import=use_parentheses_in_import,
60
                  isort_multi_line_output=isort_multi_line_output):
61
            yield result
62