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 Chain Action #4429

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

Always

Just for now

Add (C)hain Actions

Add class ChainPatchAction
  • Loading branch information...
Nosferatul committed Jun 30, 2017
commit 45a7369dfcc7af78c0844f0203a1deb1a17a3c75
@@ -15,6 +15,7 @@
from coalib.results.result_actions.ApplyPatchAction import ApplyPatchAction
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.PrintDebugMessageAction import (
PrintDebugMessageAction)
from coalib.results.result_actions.PrintMoreInfoAction import (
@@ -82,7 +83,8 @@ def highlight_text(no_color, text, lexer=TextLexer(), style=None):
PrintDebugMessageAction(),
PrintMoreInfoAction(),
ShowPatchAction(),
IgnoreResultAction())
IgnoreResultAction(),
ChainPatchAction())
DIFF_EXCERPT_MAX_SIZE = 4
@@ -607,6 +609,126 @@ def print_actions(console_printer, section, actions, failed_actions):
return get_action_info(section, actions[choice - 1], failed_actions)
def try_to_apply_action(action_name,
chosen_action,
console_printer,
section,
metadata_list,
action_dict,
failed_actions,
result,
file_diff_dict,
file_dict):
"""
Try to apply the given action.
:param action_name: The name of the action.
:param choose_action: The action object that will be applied.
:param console_printer: Object to print messages on the console.
:param section: Currently active section.
:param metadata_list: Contains metadata for all the actions.
:param action_dict: Contains the action names as keys and their
references as values.
:param failed_actions: A set of all actions that have failed. A failed
action remains in the list until it is successfully
executed.
:param result: Result corresponding to the actions.
:param file_diff_dict: If it is an action which applies a patch, this

This comment has been minimized.

@Adrianzatreanu

Adrianzatreanu Jul 6, 2017

Contributor

this description makes no sense to me

@Adrianzatreanu

Adrianzatreanu Jul 6, 2017

Contributor

this description makes no sense to me

This comment has been minimized.

@Adrianzatreanu

Adrianzatreanu Jul 6, 2017

Contributor

what does this exactly do?

@Adrianzatreanu

Adrianzatreanu Jul 6, 2017

Contributor

what does this exactly do?

This comment has been minimized.

@Nosferatul

Nosferatul Jul 6, 2017

Member

it's a dictionary with differences for a file. This is the description copied from other function already implemented.

@Nosferatul

Nosferatul Jul 6, 2017

Member

it's a dictionary with differences for a file. This is the description copied from other function already implemented.

contains the diff of the patch to be applied to
the file with filename as keys.
:param file_dict: Dictionary with filename as keys and its contents
as values.
"""
try:
chosen_action.apply_from_section(result,
file_dict,
file_diff_dict,
section)
console_printer.print(
format_lines(chosen_action.SUCCESS_MESSAGE, symbol='['),
color=SUCCESS_COLOR)
failed_actions.discard(action_name)
except Exception as exception: # pylint: disable=broad-except

This comment has been minimized.

@Adrianzatreanu

Adrianzatreanu Jul 6, 2017

Contributor

what does this comment do?

@Adrianzatreanu

Adrianzatreanu Jul 6, 2017

Contributor

what does this comment do?

This comment has been minimized.

@Nosferatul

Nosferatul Jul 6, 2017

Member

the comment was before, i just made a new function to not duplicate code

@Nosferatul

Nosferatul Jul 6, 2017

Member

the comment was before, i just made a new function to not duplicate code

logging.error('Failed to execute the action {} with error: {}.'
.format(action_name, exception))
failed_actions.add(action_name)
def apply_chain_action(console_printer,
section,
metadata_list,
action_dict,
failed_actions,
result,
file_diff_dict,
file_dict):
"""
Asks the user for an action and applies it.
:param console_printer: Object to print messages on the console.
:param section: Currently active section.
:param metadata_list: Contains metadata for all the actions.
:param action_dict: Contains the action names as keys and their
references as values.
:param failed_actions: A set of all actions that have failed. A failed
action remains in the list until it is successfully
executed.
:param result: Result corresponding to the actions.
: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 file_dict: Dictionary with filename as keys and its contents
as values.
:return: Return False if the action that is applied is Do
(N)othing or True otherwise.
"""
flag = False
action_list = str(section['actions'])
action_list = list(action_list)
for action_choice in action_list:
if action_choice.isalpha():
action_choice = action_choice.upper()
action_choice = '(' + action_choice + ')'
if action_choice == '(N)':
return False
flag = False
for action_n in metadata_list:
if action_choice in action_n.desc:
chosen_action = action_dict[action_n.name]
action_choice_made = action_choice
flag = True
break
if flag:
for index, action_details in enumerate(metadata_list, 1):
if action_choice_made in action_details.desc:
action_name, section = get_action_info(
section, metadata_list[index-1], failed_actions)
try_to_apply_action(action_name,
chosen_action,
console_printer,
section,
metadata_list,
action_dict,
failed_actions,
result,
file_diff_dict,
file_dict)
else:
console_printer.print(
format_lines('Couldn\'t apply \'{}\''.format(
action_choice), symbol='['),
color=HIGHLIGHTED_CODE_COLOR)
else:
console_printer.print(
format_lines('Please enter a letter'.format(
action_choice), symbol='['),
color=HIGHLIGHTED_CODE_COLOR)
section.delete_setting('actions')
return True
def ask_for_action_and_apply(console_printer,
section,
metadata_list,
@@ -641,21 +763,29 @@ def ask_for_action_and_apply(console_printer,
if action_name is None:
return False
chosen_action = action_dict[action_name]
try:
chosen_action.apply_from_section(result,
file_dict,
file_diff_dict,
section)
console_printer.print(
format_lines(chosen_action.SUCCESS_MESSAGE, symbol='['),
color=SUCCESS_COLOR)
failed_actions.discard(action_name)
except Exception as exception: # pylint: disable=broad-except
logging.error('Failed to execute the action {} with error: {}.'.format(
action_name, exception))
failed_actions.add(action_name)
return True
if action_name == 'ChainPatchAction':
ret = apply_chain_action(console_printer,
section,
metadata_list,
action_dict,
failed_actions,
result,
file_diff_dict,
file_dict)
return ret
else:
chosen_action = action_dict[action_name]
try_to_apply_action(action_name,
chosen_action,
console_printer,
section,
metadata_list,
action_dict,
failed_actions,
result,
file_diff_dict,
file_dict)
return True
def show_enumeration(console_printer,
@@ -13,6 +13,7 @@
from coalib.results.Result import Result
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.PrintDebugMessageAction import (
PrintDebugMessageAction)
from coalib.results.result_actions.ShowPatchAction import ShowPatchAction
@@ -25,7 +26,8 @@
ACTIONS = [ApplyPatchAction,
PrintDebugMessageAction,
ShowPatchAction,
IgnoreResultAction]
IgnoreResultAction,
ChainPatchAction]
def get_cpu_count():
@@ -0,0 +1,20 @@
from coalib.results.result_actions.ResultAction import ResultAction
from coalib.results.result_actions.ShowPatchAction import ShowPatchAction
class ChainPatchAction(ResultAction):
SUCCESS_MESSAGE = 'All actions have been applied'
is_applicable = staticmethod(ShowPatchAction.is_applicable)
def apply(self,
result,
original_file_dict,
file_diff_dict,
actions: str):
"""
(C)hain actions
:param actions: A string of all actions.
"""
@@ -25,6 +25,7 @@
from coalib.results.Result import Result
from coalib.results.result_actions.ApplyPatchAction import ApplyPatchAction
from coalib.results.result_actions.OpenEditorAction import OpenEditorAction
from coalib.results.result_actions.ChainPatchAction import ChainPatchAction
from coalib.results.result_actions.ResultAction import ResultAction
from coalib.results.SourceRange import SourceRange
from coalib.settings.Section import Section
@@ -421,6 +422,77 @@ def test_ask_for_actions_and_apply(self):
self.assertEqual(generator.last_input, 3)
self.assertNotIn('TestAction', failed_actions)
def test_ask_for_actions_and_apply2(self):
failed_actions = set()
action = ApplyPatchAction()
action2 = ChainPatchAction()
args = [self.console_printer, Section(''),
[action.get_metadata(), action2.get_metadata()],
{'ApplyPatchAction': action, 'ChainPatchAction': action2},
failed_actions, Result('origin', 'message'), {}, {}]
with simulate_console_inputs('c', 'a') as generator:
ask_for_action_and_apply(*args)
self.assertEqual(generator.last_input, 1)
self.assertIn('ApplyPatchAction', failed_actions)
with simulate_console_inputs('c', 'n') as generator:
ask_for_action_and_apply(*args)
self.assertEqual(generator.last_input, 1)
self.assertNotIn('ChainPatchAction', failed_actions)
with simulate_console_inputs('c', 'x') as generator:
ask_for_action_and_apply(*args)
self.assertEqual(generator.last_input, 0)
self.assertNotIn('ChainPatchAction', failed_actions)
with simulate_console_inputs('c', 'o') as generator:
ask_for_action_and_apply(*args)
self.assertEqual(generator.last_input, 0)
self.assertNotIn('ChainPatchAction', failed_actions)
def test_ask_for_actions_and_apply3(self):
failed_actions = set()
action = ApplyPatchAction()
action2 = ChainPatchAction()
args = [self.console_printer, Section(''),
[action.get_metadata(), action2.get_metadata()],
{'ApplyPatchAction': action, 'ChainPatchAction': action2},
failed_actions, Result('origin', 'message'), {}, {}]
with simulate_console_inputs('c', 'x') as generator:
ask_for_action_and_apply(*args)
self.assertEqual(generator.last_input, 1)
self.assertNotIn('ChainPatchAction', failed_actions)
def test_ask_for_actions_and_apply4(self):
failed_actions = set()
action = ApplyPatchAction()
action2 = ChainPatchAction()
args = [self.console_printer, Section(''),
[action.get_metadata(), action2.get_metadata()],
{'ApplyPatchAction': action, 'ChainPatchAction': action2},
failed_actions, Result('origin', 'message'), {}, {}]
with simulate_console_inputs('c', 1) as generator:
ask_for_action_and_apply(*args)
self.assertEqual(generator.last_input, 1)
self.assertNotIn('ChainPatchAction', failed_actions)
def test_ask_for_actions_and_apply5(self):
failed_actions = set()
action = ApplyPatchAction()
action2 = ChainPatchAction()
args = [self.console_printer, Section(''),
[action.get_metadata(), action2.get_metadata()],
{'ApplyPatchAction': action, 'ChainPatchAction': action2},
failed_actions, Result('origin', 'message'), {}, {}]
with simulate_console_inputs('c', 'i') as generator:
ask_for_action_and_apply(*args)
self.assertEqual(generator.last_input, 1)
self.assertNotIn('ChainPatchAction', failed_actions)
def test_default_input(self):
action = TestAction()
args = [self.console_printer, Section(''),
ProTip! Use n and p to navigate between commits in a pull request.