1
|
|
|
""" |
2
|
|
|
A ResultAction is an action that is applicable to at least some results. This |
3
|
|
|
file serves the base class for all result actions, thus providing a unified |
4
|
|
|
interface for all actions. |
5
|
|
|
""" |
6
|
|
|
from coala_utils.decorators import enforce_signature |
7
|
|
|
from coalib.settings.FunctionMetadata import FunctionMetadata |
8
|
|
|
from coalib.settings.Section import Section |
9
|
|
|
|
10
|
|
|
|
11
|
|
|
class ResultAction: |
12
|
|
|
|
13
|
|
|
SUCCESS_MESSAGE = "The action was executed successfully." |
14
|
|
|
|
15
|
|
|
@staticmethod |
16
|
|
|
def is_applicable(result, original_file_dict, file_diff_dict): |
17
|
|
|
""" |
18
|
|
|
Checks whether the Action is valid for the result type. |
19
|
|
|
|
20
|
|
|
Returns ``True`` by default. |
21
|
|
|
|
22
|
|
|
:param result: The result from the coala run to check if an |
23
|
|
|
Action is applicable. |
24
|
|
|
:param original_file_dict: A dictionary containing the files in the |
25
|
|
|
state where the result was generated. |
26
|
|
|
:param file_diff_dict: A dictionary containing a diff for every |
27
|
|
|
file from the state in the |
28
|
|
|
original_file_dict to the current state. |
29
|
|
|
This dict will be altered so you do not |
30
|
|
|
need to use the return value. |
31
|
|
|
""" |
32
|
|
|
return True |
33
|
|
|
|
34
|
|
|
def apply(self, result, original_file_dict, file_diff_dict, **kwargs): |
35
|
|
|
""" |
36
|
|
|
This action has no description although it should. Probably something |
37
|
|
|
went wrong. |
38
|
|
|
""" |
39
|
|
|
raise NotImplementedError |
40
|
|
|
|
41
|
|
|
@enforce_signature |
42
|
|
|
def apply_from_section(self, |
43
|
|
|
result, |
44
|
|
|
original_file_dict: dict, |
45
|
|
|
file_diff_dict: dict, |
46
|
|
|
section: Section): |
47
|
|
|
""" |
48
|
|
|
Applies this action to the given results with all additional options |
49
|
|
|
given as a section. The file dictionaries |
50
|
|
|
are needed for differential results. |
51
|
|
|
|
52
|
|
|
:param result: The result to apply. |
53
|
|
|
:param original_file_dict: A dictionary containing the files in the |
54
|
|
|
state where the result was generated. |
55
|
|
|
:param file_diff_dict: A dictionary containing a diff for every |
56
|
|
|
file from the state in the |
57
|
|
|
original_file_dict to the current state. |
58
|
|
|
This dict will be altered so you do not |
59
|
|
|
need to use the return value. |
60
|
|
|
:param section: The section where to retrieve the additional |
61
|
|
|
information. |
62
|
|
|
:return The modified file_diff_dict. |
63
|
|
|
""" |
64
|
|
|
params = self.get_metadata().create_params_from_section(section) |
65
|
|
|
return self.apply(result, original_file_dict, file_diff_dict, **params) |
66
|
|
|
|
67
|
|
|
@classmethod |
68
|
|
|
def get_metadata(cls): |
69
|
|
|
""" |
70
|
|
|
Retrieves metadata for the apply function. The description may be used |
71
|
|
|
to advertise this action to the user. The parameters and their help |
72
|
|
|
texts are additional information that are needed from the user. You can |
73
|
|
|
create a section out of the inputs from the user and use |
74
|
|
|
apply_from_section to apply |
75
|
|
|
|
76
|
|
|
:return A FunctionMetadata object. |
77
|
|
|
""" |
78
|
|
|
data = FunctionMetadata.from_function( |
79
|
|
|
cls.apply, |
80
|
|
|
omit={"self", "result", "original_file_dict", "file_diff_dict"}) |
81
|
|
|
data.name = cls.__name__ |
82
|
|
|
|
83
|
|
|
return data |
84
|
|
|
|