From 52438410096ec69c66726ddeb5ae19a5ca3a65f5 Mon Sep 17 00:00:00 2001 From: Mihai Tiganus Date: Sun, 20 May 2018 20:19:30 +0300 Subject: [PATCH] coala-quickstart.py: Change the printing of bears Remove duplicate printing of `relevant_bears` and instead change `print_relevant_bears` to inform the user both of the usable and unusable bears. Closes https://github.com/coala/coala-quickstart/issues/220 --- coala_quickstart/Strings.py | 14 +++++++++ coala_quickstart/coala_quickstart.py | 22 ++++++++++---- coala_quickstart/generation/Bears.py | 44 +++++++++++++++++----------- tests/generation/Bears.py | 26 +++++++++++++--- 4 files changed, 79 insertions(+), 27 deletions(-) diff --git a/coala_quickstart/Strings.py b/coala_quickstart/Strings.py index d944dcb..4d16f09 100644 --- a/coala_quickstart/Strings.py +++ b/coala_quickstart/Strings.py @@ -50,3 +50,17 @@ You can see all of them here: {} """.format(BEAR_DOCS_URL) + +PRINT_BEARS = {'unusable': + {'msg': """ +Based on the configuration options the following bears have been identified +as unusable ('-C' or '--ci' set non-interactive mode, making the bears that +need to be configured by the user unusable: check '--allow-incomplete-sections' +for more information): +""", + 'colors': ('green', 'red')}, + 'usable': {'msg': """ +Based on the languages used in project the following bears have been identified +as usable: +""", + 'colors': ('green', 'cyan')}} diff --git a/coala_quickstart/coala_quickstart.py b/coala_quickstart/coala_quickstart.py index 913b1f3..baa7404 100644 --- a/coala_quickstart/coala_quickstart.py +++ b/coala_quickstart/coala_quickstart.py @@ -2,6 +2,7 @@ import logging import os import sys +from collections import OrderedDict from pyprint.ConsolePrinter import ConsolePrinter @@ -90,15 +91,24 @@ def main(): extracted_information = collect_info(project_dir) - relevant_bears = filter_relevant_bears( - used_languages, printer, arg_parser, extracted_information) - - print_relevant_bears(printer, relevant_bears) + # OrderedDict used for print_relevant_bears to first print unusable bears + relevant_bears = OrderedDict( + [('unusable', {}), + ('usable', + filter_relevant_bears(used_languages, + printer, + arg_parser, + extracted_information))]) if args.non_interactive and not args.incomplete_sections: - unusable_bears = get_non_optional_settings_bears(relevant_bears) + unusable_bears = get_non_optional_settings_bears( + relevant_bears['usable']) remove_unusable_bears(relevant_bears, unusable_bears) - print_relevant_bears(printer, relevant_bears, 'usable') + + print_relevant_bears(printer, relevant_bears) + + # Drop unusable bears + relevant_bears = relevant_bears['usable'] settings = generate_settings( project_dir, diff --git a/coala_quickstart/generation/Bears.py b/coala_quickstart/generation/Bears.py index f01ac22..914e23a 100644 --- a/coala_quickstart/generation/Bears.py +++ b/coala_quickstart/generation/Bears.py @@ -6,7 +6,7 @@ from coala_quickstart.Constants import ( IMPORTANT_BEAR_LIST, ALL_CAPABILITIES, DEFAULT_CAPABILTIES) -from coala_quickstart.Strings import BEAR_HELP +from coala_quickstart.Strings import PRINT_BEARS, BEAR_HELP from coala_quickstart.generation.SettingsFilling import is_autofill_possible from coalib.bearlib.abstractions.LinterClass import LinterClass from coalib.settings.ConfigurationGathering import get_filtered_bears @@ -200,38 +200,48 @@ def get_non_optional_settings_bears(bears): def remove_unusable_bears(bears, unusable_bears): """ - From the bears dict, filter the bears appearing in unusable_bears. + From the bears dict, filter the bears appearing in unusable_bears + and save them under “unusable” key for printing later. :param bears: A dict with language name as key and bear classes as value. :param unusable_bears: A collection of Bear classes. """ - for language, language_bears in bears.items(): + for language, language_bears in bears['usable'].items(): for bear in tuple(language_bears): if bear in unusable_bears: - bears[language].remove(bear) + bears['usable'][language].remove(bear) + bears['unusable'][language] = bears['unusable'].get( + language, ()) + (bear, ) -def print_relevant_bears(printer, relevant_bears, label='relevant'): +def print_relevant_bears(printer, relevant_bears): """ - Prints the relevant bears in sections separated by language. + Prints both the usable and unusable, relevant bears + in sections indexed by language. :param printer: A ``ConsolePrinter`` object used for console interactions. :param relevant_bears: - A dict with language name as key and bear classes as value. + An ``OrderedDict`` indexed by “usable” and “unusable” bears stored in + dictionaries that use language as key and bear classes as value. """ - if label == 'relevant': - printer.print(BEAR_HELP) - - printer.print('\nBased on the languages used in project the following ' - 'bears have been identified to be %s:' % label) - for language in relevant_bears: - printer.print(' [' + language + ']', color='green') - for bear in relevant_bears[language]: - printer.print(' ' + bear.name, color='cyan') - printer.print('') + printer.print(BEAR_HELP) + + # Don't print anything for empty bear sets + nonempty_label_bears = ( + label for label in relevant_bears if len(relevant_bears[label]) > 0) + + for label_bears in nonempty_label_bears: + printer.print(PRINT_BEARS[label_bears]['msg']) + for language in relevant_bears[label_bears]: + printer.print(" [" + language + "]", + color=PRINT_BEARS[label_bears]["colors"][0]) + for bear in relevant_bears[label_bears][language]: + printer.print(" " + bear.name, + color=PRINT_BEARS[label_bears]["colors"][1]) + printer.print("") def generate_requirements_map(bears): diff --git a/tests/generation/Bears.py b/tests/generation/Bears.py index 1152336..aaea6cc 100644 --- a/tests/generation/Bears.py +++ b/tests/generation/Bears.py @@ -2,6 +2,7 @@ import sys import unittest from copy import deepcopy +from collections import OrderedDict from pyprint.ConsolePrinter import ConsolePrinter @@ -286,10 +287,22 @@ def test_filter_relevant_bears_gruntfile_present(self): def test_print_relevant_bears(self): with retrieve_stdout() as custom_stdout: - print_relevant_bears(self.printer, filter_relevant_bears( - [('Python', 70), ('Unknown', 30)], self.printer, - self.arg_parser, {})) + print_relevant_bears(self.printer, OrderedDict([('unusable', {}), + ('usable', filter_relevant_bears([('Python', 70), ('Unknown', 30)], + self.printer, self.arg_parser, {}))])) self.assertIn("PycodestyleBear", custom_stdout.getvalue()) + # Should print only the usable bears + self.assertNotIn("unusable", + custom_stdout.getvalue()) + + def test_print_relevant_bears_no_bears(self): + with retrieve_stdout() as custom_stdout: + print_relevant_bears(self.printer, OrderedDict([('unusable', {}), + ('usable', {})])) + self.assertNotIn("usable", custom_stdout.getvalue()) + # Should print only the usable bears + self.assertNotIn("unusable", + custom_stdout.getvalue()) def test_bears_allow_incomplete_sections_mode(self): sys.argv.append('--ci') @@ -299,7 +312,10 @@ def test_bears_allow_incomplete_sections_mode(self): os.chdir("bears_ci_testfiles") with retrieve_stdout() as custom_stdout: main() - self.assertNotIn("usable", + # Should print only the usable bears + self.assertIn("usable", + custom_stdout.getvalue()) + self.assertNotIn("unusable", custom_stdout.getvalue()) os.remove('.coafile') os.chdir(orig_cwd) @@ -311,8 +327,10 @@ def test_bears_ci_mode(self): os.chdir("bears_ci_testfiles") with retrieve_stdout() as custom_stdout: main() + # Should print both the usable and unusable bears self.assertIn("usable", custom_stdout.getvalue()) + self.assertIn("unusable", custom_stdout.getvalue()) os.remove('.coafile') os.chdir(orig_cwd)