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

bears.python.PyImportSortBear   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 50
Duplicated Lines 0 %
Metric Value
dl 0
loc 50
rs 10
wmc 4

2 Methods

Rating   Name   Duplication   Size   Complexity  
B run() 0 30 2
A run_isort() 0 14 2
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_parantheses_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_parantheses_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_parantheses_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_parantheses_in_import: True is paranthesis 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.
50
        """
51
        for result in self.retrieve_results(
52
                  filename,
53
                  file,
54
                  use_spaces=use_spaces,
55
                  indent_size=tab_width,
56
                  max_line_length=max_line_length,
57
                  use_parantheses_in_import=use_parantheses_in_import,
58
                  isort_multi_line_output=isort_multi_line_output):
59
            yield result
60