Completed
Pull Request — master (#2077)
by Lasse
02:08
created

OpenEditorAction   A

Complexity

Total Complexity 11

Size/Duplication

Total Lines 43
Duplicated Lines 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
c 2
b 0
f 0
dl 0
loc 43
rs 10
wmc 11

2 Methods

Rating   Name   Duplication   Size   Complexity  
B is_applicable() 0 11 5
B apply() 0 27 6
1
import subprocess
2
from os.path import exists
3
4
from coalib.results.Diff import Diff
5
from coalib.results.Result import Result
6
from coalib.results.result_actions.ResultAction import ResultAction
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(ResultAction):
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable ResultAction does not seem to be defined.
Loading history...
19
20
    SUCCESS_MESSAGE = "Changes saved successfully."
21
22
    @staticmethod
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable staticmethod does not seem to be defined.
Loading history...
23
    def is_applicable(result, original_file_dict, file_diff_dict):
24
        """
25
        For being applicable, the result has to point to a number of files
26
        that have to exist i.e. have not been previously deleted.
27
        """
28
        if not isinstance(result, Result) or not len(result.affected_code) > 0:
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable result does not seem to be defined.
Loading history...
Comprehensibility Best Practice introduced by
The variable Result does not seem to be defined.
Loading history...
29
            return False
30
31
        filenames = set(src.file for src in result.affected_code)
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable src does not seem to be defined.
Loading history...
32
        return all(exists(filename) for filename in filenames)
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable filename does not seem to be defined.
Loading history...
Comprehensibility Best Practice introduced by
The variable filenames does not seem to be defined.
Loading history...
33
34
    def apply(self, result, original_file_dict, file_diff_dict, editor: str):
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable str does not seem to be defined.
Loading history...
35
        '''
36
        Open the affected file(s) in an editor.
37
38
        :param editor: The editor to open the file with.
39
        '''
40
        # Use set to remove duplicates
41
        filenames = set(src.file for src in result.affected_code)
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable src does not seem to be defined.
Loading history...
Comprehensibility Best Practice introduced by
The variable result does not seem to be defined.
Loading history...
42
43
        editor_args = [editor] + list(filenames)
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable editor does not seem to be defined.
Loading history...
Comprehensibility Best Practice introduced by
The variable filenames does not seem to be defined.
Loading history...
44
        arg = EDITOR_ARGS.get(editor.strip(), None)
45
        if arg:
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable arg does not seem to be defined.
Loading history...
46
            editor_args.append(arg)
47
48
        # Dear user, you wanted an editor, so you get it. But do you really
49
        # think you can do better than we?
50
        if editor in GUI_EDITORS:
51
            subprocess.call(editor_args, stdout=subprocess.PIPE)
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable editor_args does not seem to be defined.
Loading history...
Comprehensibility Best Practice introduced by
The variable subprocess does not seem to be defined.
Loading history...
52
        else:
53
            subprocess.call(editor_args)
54
55
        for filename in filenames:
56
            with open(filename, encoding='utf-8') as file:
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable filename does not seem to be defined.
Loading history...
Comprehensibility Best Practice introduced by
The variable file does not seem to be defined.
Loading history...
57
                file_diff_dict[filename] = Diff.from_string_arrays(
58
                    original_file_dict[filename], file.readlines())
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable original_file_dict does not seem to be defined.
Loading history...
59
60
        return file_diff_dict
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable file_diff_dict does not seem to be defined.
Loading history...
61