Skip to content
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 apply-single-action #4462

Merged
merged 1 commit into from Jul 20, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
64 changes: 63 additions & 1 deletion coalib/coala_main.py
Expand Up @@ -11,6 +11,15 @@
from coalib.output.Logging import CounterHandler
from coalib.processes.Processing import execute_section, simplify_section_result
from coalib.settings.ConfigurationGathering import gather_configuration
from coalib.results.result_actions.ShowPatchAction import ShowPatchAction
from coalib.results.result_actions.ApplyPatchAction import ApplyPatchAction
from coalib.results.result_actions.IgnoreResultAction import IgnoreResultAction
from coalib.results.result_actions.OpenEditorAction import OpenEditorAction
from coalib.results.result_actions.PrintAspectAction import PrintAspectAction
from coalib.results.result_actions.PrintMoreInfoAction import \
PrintMoreInfoAction
from coalib.results.result_actions.PrintDebugMessageAction import \
PrintDebugMessageAction
from coalib.misc.Caching import FileCache
from coalib.misc.CachingUtilities import (
settings_changed, update_settings_db, get_settings_hash)
Expand All @@ -20,6 +29,27 @@ def do_nothing(*args):
return True


STR_ENTER_NUMBER = 'Enter number (Ctrl-{} to exit): '.format(
'Z' if platform.system() == 'Windows' else 'D')


def provide_all_actions():
return ['Do (N)othing', ShowPatchAction().get_metadata().desc,
ApplyPatchAction().get_metadata().desc,
IgnoreResultAction().get_metadata().desc,
OpenEditorAction().get_metadata().desc,
PrintAspectAction().get_metadata().desc,
PrintDebugMessageAction().get_metadata().desc,
PrintMoreInfoAction().get_metadata().desc]


def format_lines(lines, symbol='', line_nr=''):
# type: (object, object, object) -> object
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'))


def run_coala(console_printer=None,
log_printer=None,
print_results=do_nothing,
Expand Down Expand Up @@ -69,6 +99,35 @@ def run_coala(console_printer=None,
:return: A dictionary containing a list of results
for all analyzed sections as key.
"""
all_actions_possible = provide_all_actions()
apply_single = None
if getattr(args, 'single_action', None) is not None:
while True:
for i, action in enumerate(all_actions_possible, 1):
console_printer.print(format_lines('{}'.format(
action), symbol='['))

line = format_lines(STR_ENTER_NUMBER, symbol='[')

choice = input(line)

if choice.isalpha():
choice = choice.upper()
choice = '(' + choice + ')'
if choice == '(N)':
apply_single = 'Do (N)othing'
break
for i, action in enumerate(all_actions_possible, 1):
if choice in action:
apply_single = action
break
if apply_single:
break
console_printer.print(format_lines(
'Please enter a valid letter.',
symbol='['))

args.apply_patch = False

log_printer = (
LogPrinter(ConsolePrinter(), LOG_LEVEL.DEBUG) if log_printer is None
Expand Down Expand Up @@ -118,7 +177,10 @@ def run_coala(console_printer=None,
cache=cache,
log_printer=log_printer,
console_printer=console_printer,
debug=debug or args and args.debug)
debug=debug or args and args.debug,
apply_single=(apply_single
if apply_single is not None else
False))
yielded, yielded_unfixed, results[section_name] = (
simplify_section_result(section_result))

Expand Down
121 changes: 75 additions & 46 deletions coalib/output/ConsoleInteraction.py
Expand Up @@ -142,7 +142,8 @@ def acquire_actions_and_apply(console_printer,
file_diff_dict,
result,
file_dict,
cli_actions=None):
cli_actions=None,
apply_single=False):
"""
Acquires applicable actions and applies them.

Expand Down Expand Up @@ -174,14 +175,26 @@ def acquire_actions_and_apply(console_printer,
metadata_list.append(metadata)

# User can always choose no action which is guaranteed to succeed
if not ask_for_action_and_apply(console_printer,
section,
metadata_list,
action_dict,
failed_actions,
result,
file_diff_dict,
file_dict):
if apply_single:
ask_for_action_and_apply(console_printer,
section,
metadata_list,
action_dict,
failed_actions,
result,
file_diff_dict,
file_dict,
apply_single=apply_single)
break
elif not ask_for_action_and_apply(console_printer,
section,
metadata_list,
action_dict,
failed_actions,
result,
file_diff_dict,
file_dict,
apply_single=apply_single):
break


Expand Down Expand Up @@ -241,7 +254,8 @@ def print_result(console_printer,
file_diff_dict,
result,
file_dict,
interactive=True):
interactive=True,
apply_single=False):
"""
Prints the result to console.

Expand Down Expand Up @@ -298,7 +312,8 @@ def print_result(console_printer,
file_diff_dict,
result,
file_dict,
cli_actions)
cli_actions,
apply_single=apply_single)


def print_diffs_info(diffs, printer):
Expand Down Expand Up @@ -411,7 +426,8 @@ def print_results_no_input(log_printer,
result_list,
file_dict,
file_diff_dict,
console_printer):
console_printer,
apply_single=False):
"""
Prints all non interactive results in a section

Expand All @@ -436,15 +452,17 @@ def print_results_no_input(log_printer,
file_diff_dict,
result,
file_dict,
interactive=False)
interactive=False,
apply_single=apply_single)


def print_results(log_printer,
section,
result_list,
file_dict,
file_diff_dict,
console_printer):
console_printer,
apply_single=False):
"""
Prints all the results in a section.

Expand All @@ -468,7 +486,8 @@ def print_results(log_printer,
section,
file_diff_dict,
result,
file_dict)
file_dict,
apply_single=apply_single)


def print_affected_lines(console_printer, file_dict, sourcerange):
Expand Down Expand Up @@ -580,7 +599,7 @@ def get_action_info(section, action, failed_actions):
return action.name, section


def choose_action(console_printer, actions):
def choose_action(console_printer, actions, apply_single=False):
"""
Presents the actions available to the user and takes as input the action
the user wants to choose.
Expand All @@ -589,37 +608,45 @@ def choose_action(console_printer, actions):
:param actions: Actions available to the user.
:return: Return choice of action of user.
"""
while True:
color_letter(console_printer, '[ ] *0. Do (N)othing')
for i, action in enumerate(actions, 1):
color_letter(console_printer, format_lines('{:>2}. {}'.format(
i, action.desc), symbol='['))

line = format_lines(STR_ENTER_NUMBER, symbol='[')

choice = input(line)
if not choice:
if apply_single:
if apply_single == 'Do (N)othing':
return 0
choice = str(choice)
if choice.isalpha():
choice = choice.upper()
choice = '(' + choice + ')'
if choice == '(N)':
return 0
for i, action in enumerate(actions, 1):
if apply_single == action.desc:
return i
return 0
else:
while True:
color_letter(console_printer, '[ ] *0. Do (N)othing')
for i, action in enumerate(actions, 1):
if choice in action.desc:
return i
elif choice.isnumeric():
choice = int(choice)
print(choice)
if 0 <= choice <= len(actions):
return choice

console_printer.print(format_lines(
'Please enter a valid letter.', symbol='['))
color_letter(console_printer, format_lines('{:>2}. {}'.format(
i, action.desc), symbol='['))

line = format_lines(STR_ENTER_NUMBER, symbol='[')

def print_actions(console_printer, section, actions, failed_actions):
choice = input(line)
if not choice:
return 0
choice = str(choice)
if choice.isalpha():
choice = choice.upper()
choice = '(' + choice + ')'
if choice == '(N)':
return 0
for i, action in enumerate(actions, 1):
if choice in action.desc:
return i
elif choice.isnumeric():
choice = int(choice)
if 0 <= choice <= len(actions):
return choice

console_printer.print(format_lines(
'Please enter a valid letter.', symbol='['))


def print_actions(console_printer, section, actions, failed_actions,
apply_single):
"""
Prints the given actions and lets the user choose.

Expand All @@ -634,7 +661,7 @@ def print_actions(console_printer, section, actions, failed_actions):
values for the action. If the user did
choose to do nothing, return (None, None).
"""
choice = choose_action(console_printer, actions)
choice = choose_action(console_printer, actions, apply_single)

if choice == 0:
return None, None
Expand Down Expand Up @@ -769,7 +796,8 @@ def ask_for_action_and_apply(console_printer,
failed_actions,
result,
file_diff_dict,
file_dict):
file_dict,
apply_single=False):
"""
Asks the user for an action and applies it.

Expand All @@ -792,7 +820,8 @@ def ask_for_action_and_apply(console_printer,
another action, False otherwise.
"""
action_name, section = print_actions(console_printer, section,
metadata_list, failed_actions)
metadata_list, failed_actions,
apply_single=apply_single)
if action_name is None:
return False

Expand Down
4 changes: 4 additions & 0 deletions coalib/parsing/DefaultArgParser.py
Expand Up @@ -230,6 +230,10 @@ def default_arg_parser(formatter_class=None):
'-n', '--no-orig', const=True, action='store_const',
help="don't create .orig backup files before patching")

misc_group.add_argument(
'-A', '--single-action', const=True, action='store_const',
help='apply a single action for all results')

misc_group.add_argument(
'--debug', const=True, action='store_const',
help='run coala in debug mode, starting ipdb, '
Expand Down