New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

gsoc: Add Show Applied Patches #4481

Merged
merged 1 commit into from Jul 24, 2017
Jump to file or symbol
Failed to load files and symbols.
+172 −28
Diff settings

Always

Just for now

Add Show Applied Patches

A new action that will show a history of actions in a single run of
"coala".

Closes #4553
  • Loading branch information...
Nosferatul committed Jul 19, 2017
commit 149d59d28318954d4035c7e8f639cc344af061c0
@@ -1,3 +1,4 @@
import copy
import logging
import platform
import os
@@ -17,6 +18,8 @@
from coalib.results.result_actions.OpenEditorAction import OpenEditorAction
from coalib.results.result_actions.IgnoreResultAction import IgnoreResultAction
from coalib.results.result_actions.ChainPatchAction import ChainPatchAction
from coalib.results.result_actions.ShowAppliedPatchesAction import (
ShowAppliedPatchesAction)
from coalib.results.result_actions.PrintDebugMessageAction import (
PrintDebugMessageAction)
from coalib.results.result_actions.PrintMoreInfoAction import (
@@ -85,7 +88,8 @@ def highlight_text(no_color, text, lexer=TextLexer(), style=None):
PrintMoreInfoAction(),
ShowPatchAction(),
IgnoreResultAction(),
ChainPatchAction())
ChainPatchAction(),
ShowAppliedPatchesAction())
DIFF_EXCERPT_MAX_SIZE = 4
@@ -160,6 +164,7 @@ def acquire_actions_and_apply(console_printer,
"""
cli_actions = CLI_ACTIONS if cli_actions is None else cli_actions
failed_actions = set()
applied_actions = {}
while True:
actions = []
for action in cli_actions:
@@ -186,6 +191,7 @@ def acquire_actions_and_apply(console_printer,
result,
file_diff_dict,
file_dict,
applied_actions,
apply_single=apply_single)
break
elif not ask_for_action_and_apply(console_printer,
@@ -196,6 +202,7 @@ def acquire_actions_and_apply(console_printer,
result,
file_diff_dict,
file_dict,
applied_actions,
apply_single=apply_single):
break
@@ -379,7 +386,6 @@ def print_results_formatted(log_printer,
for range in result.affected_code:
format_args['affected_code'] = range
format_args['source_lines'] = range.affected_source(file_dict)
print(format_str.format(file=range.start.file,
line=range.start.line,
end_line=range.end.line,
@@ -690,7 +696,8 @@ def try_to_apply_action(action_name,
failed_actions,
result,
file_diff_dict,
file_dict):
file_dict,
applied_actions):
"""
Try to apply the given action.
@@ -708,6 +715,8 @@ def try_to_apply_action(action_name,
:param file_diff_dict: If it is an action which applies a patch, this
contains the diff of the patch to be applied to
the file with filename as keys.
:param applied_actions: A dictionary that contains the result, file_dict,
file_diff_dict and the section for an action.
:param file_dict: Dictionary with filename as keys and its contents
as values.
"""
@@ -719,6 +728,11 @@ def try_to_apply_action(action_name,
console_printer.print(
format_lines(chosen_action.SUCCESS_MESSAGE, symbol='['),
color=SUCCESS_COLOR)
applied_actions[action_name] = [copy.copy(result), copy.copy(
file_dict),
copy.copy(file_diff_dict),
copy.copy(section)]
result.set_applied_actions(applied_actions)
failed_actions.discard(action_name)
except Exception as exception: # pylint: disable=broad-except
logging.error('Failed to execute the action {} with error: {}.'
@@ -733,7 +747,8 @@ def apply_chain_action(console_printer,
failed_actions,
result,
file_diff_dict,
file_dict):
file_dict,
applied_actions):
"""
Asks the user for an action and applies it.
@@ -751,6 +766,8 @@ def apply_chain_action(console_printer,
the file with filename as keys.
:param file_dict: Dictionary with filename as keys and its contents
as values.
:param applied_actions: A dictionary that contains the result, file_dict,
file_diff_dict and the section for an action.
:return: Return False if the action that is applied is Do
(N)othing or True otherwise.
"""
@@ -785,7 +802,8 @@ def apply_chain_action(console_printer,
failed_actions,
result,
file_diff_dict,
file_dict)
file_dict,
applied_actions)
else:
console_printer.print(
format_lines('Couldn\'t apply \'{}\''.format(
@@ -809,6 +827,7 @@ def ask_for_action_and_apply(console_printer,
result,
file_diff_dict,
file_dict,
applied_actions,
apply_single=False):
"""
Asks the user for an action and applies it.
@@ -829,6 +848,8 @@ def ask_for_action_and_apply(console_printer,
as values.
:param apply_single: The action that should be applied for all results.
If it's not selected, has a value of False.
:param applied_actions: A dictionary that contains the result, file_dict,
file_diff_dict and the section for an action.
:return: Returns a boolean value. True will be returned, if
it makes sense that the user may choose to execute
another action, False otherwise.
@@ -847,7 +868,8 @@ def ask_for_action_and_apply(console_printer,
failed_actions,
result,
file_diff_dict,
file_dict)
file_dict,
applied_actions)
return ret
else:
chosen_action = action_dict[action_name]
@@ -860,7 +882,8 @@ def ask_for_action_and_apply(console_printer,
failed_actions,
result,
file_diff_dict,
file_dict)
file_dict,
applied_actions)
return True
@@ -16,6 +16,8 @@
from coalib.results.result_actions.ApplyPatchAction import ApplyPatchAction
from coalib.results.result_actions.IgnoreResultAction import IgnoreResultAction
from coalib.results.result_actions.ChainPatchAction import ChainPatchAction
from coalib.results.result_actions.ShowAppliedPatchesAction \
import ShowAppliedPatchesAction
from coalib.results.result_actions.PrintDebugMessageAction import (
PrintDebugMessageAction)
from coalib.results.result_actions.ShowPatchAction import ShowPatchAction
@@ -29,7 +31,8 @@
PrintDebugMessageAction,
ShowPatchAction,
IgnoreResultAction,
ChainPatchAction]
ChainPatchAction,
ShowAppliedPatchesAction]
def get_cpu_count():
View
@@ -15,7 +15,8 @@
('severity', RESULT_SEVERITY.reverse.get),
'confidence',
'message',
('aspect', lambda aspect: type(aspect).__qualname__))
('aspect', lambda aspect: type(aspect).__qualname__),
'applied_actions')
@generate_ordering('affected_code',
'severity',
'confidence',
@@ -24,7 +25,8 @@
'message_arguments',
'aspect',
'additional_info',
'debug_msg')
'debug_msg',
'applied_actions')
class Result:
"""
A result is anything that has an origin and a message.
@@ -59,7 +61,8 @@ def __init__(self,
diffs: (dict, None)=None,
confidence: int=100,
aspect: (aspectbase, None)=None,
message_arguments: dict={}):
message_arguments: dict={},
applied_actions: dict={}):
"""
:param origin:
Class name or creator object of this object.
@@ -90,6 +93,9 @@ def __init__(self,
the leafs exactly your result belongs to.)
:param message_arguments:
Arguments to be provided to the base message.
:param applied_actions:
A dictionary that contains the result, file_dict, file_diff_dict and
the section for an action.
:raises ValueError:
Raised when confidence is not between 0 and 100.
:raises KeyError:
@@ -105,6 +111,7 @@ def __init__(self,
self.origin = origin
self.message_base = message
self.message_arguments = message_arguments
self.applied_actions = applied_actions
if message_arguments:
self.message_base.format(**self.message_arguments)
self.debug_msg = debug_msg
@@ -129,6 +136,12 @@ def message(self):
def message(self, value: str):
self.message_base = value
def set_applied_actions(self, applied_actions):
self.applied_actions = applied_actions
def get_applied_actions(self):
return self.applied_actions
@classmethod
@enforce_signature
def from_values(cls,
@@ -0,0 +1,54 @@
from coalib.results.result_actions.ResultAction import ResultAction
from coalib.results.result_actions.ShowPatchAction import ShowPatchAction
from coalib.results.RESULT_SEVERITY import (
RESULT_SEVERITY, RESULT_SEVERITY_COLORS)
from pyprint.ConsolePrinter import ConsolePrinter
def format_lines(lines, symbol='', line_nr=''):
def sym(x): return ']' if x is '[' else x
return '\n'.join('{}{:>5}{} {}'.format(symbol, sym(symbol), line_nr, line)
for line in lines.rstrip('\n').split('\n'))
class ShowAppliedPatchesAction(ResultAction):
SUCCESS_MESSAGE = 'All actions have been applied'
is_applicable = staticmethod(ShowPatchAction.is_applicable)
def apply(self,
result,
original_file_dict,
file_diff_dict):
"""
Show Applied (P)atches

This comment has been minimized.

@yukiisbored

yukiisbored Jul 20, 2017

Member

I think (S) is a better option than (P) because it's more obvious since the verb is "Show".

@yukiisbored

yukiisbored Jul 20, 2017

Member

I think (S) is a better option than (P) because it's more obvious since the verb is "Show".

This comment has been minimized.

@Nosferatul

Nosferatul Jul 21, 2017

Member

We use at (S)how Patch

@Nosferatul

Nosferatul Jul 21, 2017

Member

We use at (S)how Patch

"""
console_printer = ConsolePrinter()
applied_actions = result.get_applied_actions()
show_patch_action = ShowPatchAction()
RESULT_INDEX = 0
FILE_DICT_INDEX = 1
FILE_DIFF_DICT_INDEX = 2
SECTION_INDEX = 3
for key, val in applied_actions.items():
this_result = val[RESULT_INDEX]
this_section = val[SECTION_INDEX]
color_res = RESULT_SEVERITY_COLORS[this_result.severity]
console_printer.print('\n**** {bear} [Section: {section}] ***'
'*\n**** Action Applied: {action} ****\n'
.format(bear=this_result.origin,
section=this_section.name,
action=key),
color=color_res)
console_printer.print(format_lines('[Severity: {sev}]'.format(
sev=RESULT_SEVERITY.__str__(this_result.severity)), '!'),
color=color_res)
show_patch_action.apply_from_section(val[RESULT_INDEX],
val[FILE_DICT_INDEX],
val[FILE_DIFF_DICT_INDEX],
val[SECTION_INDEX])
console_printer.print(
'\n**************\n', color=color_res)
return True
Oops, something went wrong.
ProTip! Use n and p to navigate between commits in a pull request.