Failed Conditions
Pull Request — master (#1152)
by Lasse
03:36
created

tion.apply()   C

Complexity

Conditions 7

Size

Total Lines 35

Duplication

Lines 0
Ratio 0 %
Metric Value
cc 7
dl 0
loc 35
rs 5.5

1 Method

Rating   Name   Duplication   Size   Complexity  
A coalib.results.result_actions.ResultAction.apply() 0 6 1
1
import subprocess
2
3
from coalib.results.Diff import Diff
4
from coalib.results.result_actions.ApplyPatchAction import ApplyPatchAction
5
from coalib.results.Result import Result
6
7
8
EDITOR_ARGS = {
9
    "subl": "--wait",
10
    "gedit": "-s",
11
    "atom": "--wait"
12
}
13
14
15
GUI_EDITORS = ["kate", "gedit", "subl", "atom"]
16
17
18
class OpenEditorAction(ApplyPatchAction):
19
    @staticmethod
20
    def is_applicable(result, original_file_dict, file_diff_dict):
21
        return isinstance(result, Result) and len(result.affected_code) > 0
22
23
    def apply(self, result, original_file_dict, file_diff_dict, editor: str):
24
        '''
25
        Open the affected file(s) in an editor.
26
27
        :param editor: The editor to open the file with.
28
        '''
29
        # Use set to remove duplicates
30
        filenames = set(src.file for src in result.affected_code)
31
32
        editor_args = [editor] + list(filenames)
33
        arg = EDITOR_ARGS.get(editor.strip(), None)
34
        if arg:
35
            editor_args.append(arg)
36
37
        # Dear user, you wanted an editor, so you get it. But do you really
38
        # think you can do better than we?
39
        if editor in GUI_EDITORS:
40
            subprocess.call(editor_args, stdout=subprocess.PIPE)
41
        else:
42
            subprocess.call(editor_args)
43
44
        for filename in filenames:
45
            with open(filename) as filehandle:
46
                new_file = filehandle.readlines()
47
48
            original_file = original_file_dict[filename]
49
            try:
50
                current_file = file_diff_dict[filename].modified
51
            except KeyError:
52
                current_file = original_file
53
54
            file_diff_dict[filename] = Diff.from_string_arrays(original_file,
55
                                                               new_file)
56
57
        return file_diff_dict
58