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

coala-quickstart.py: Change the printing of bears #227

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 23 additions & 0 deletions coala_quickstart/Strings.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,3 +50,26 @@

You can see all of them here: {}
""".format(BEAR_DOCS_URL)

PRINT_UNUSABLE = """
The following bears have been dropped because '-C'/'--ci' (non
interactive mode) option has been selected. Remove the option or
check '--allow-incomplete-sections' option for more information
on how to include them:
"""

PRINT_USABLE = """
Based on the languages used in project the following bears have been
identified as usable:
"""

PRINT_BEARS = {
'unusable': {
'msg': PRINT_UNUSABLE,
'colors': ('green', 'red')
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

add trailing comma

},
'usable': {
'msg': PRINT_USABLE,
'colors': ('green', 'cyan')
}
}
24 changes: 17 additions & 7 deletions coala_quickstart/coala_quickstart.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
import logging
import os
import sys
from collections import OrderedDict
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't see the use of ordered dict over here

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I used a OrderedDict there because I wanted to make sure that each time coala-quickstart is run, fristly the unused bears are printed and then the used bears. (I wanted a deterministic execution, and, for the user, if he doesn't want to check the full log he'll only get a glimpse at the ones that were chosen - I feel this is the most common case). Sounds ok? Or should I change it?


from pyprint.ConsolePrinter import ConsolePrinter

Expand All @@ -17,7 +18,7 @@
filter_relevant_bears,
print_relevant_bears,
get_non_optional_settings_bears,
remove_unusable_bears,
filter_unusable_bears,
)
from coala_quickstart.generation.Settings import (
generate_settings, write_coafile)
Expand Down Expand Up @@ -103,18 +104,27 @@ def main():

extracted_information = collect_info(project_dir)

relevant_bears = filter_relevant_bears(
selected_bears = filter_relevant_bears(
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

unnecessary .. see elsewhere

used_languages, printer, arg_parser, extracted_information)

if args.green_mode:
collect_bear_settings(relevant_bears)
collect_bear_settings(selected_bears)

print_relevant_bears(printer, relevant_bears)
# OrderedDict used for print_relevant_bears to first print unusable bears
relevant_bears = OrderedDict(
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

labelled_bears = {'usable': selected_bears}
print_labels = ['usable']

[('unusable', {}),
('usable',
selected_bears)])

if args.non_interactive and not args.incomplete_sections:
unusable_bears = get_non_optional_settings_bears(relevant_bears)
remove_unusable_bears(relevant_bears, unusable_bears)
print_relevant_bears(printer, relevant_bears, 'usable')
unusable_bears = get_non_optional_settings_bears(
relevant_bears['usable'])
filter_unusable_bears(relevant_bears, unusable_bears)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

labelled_bears['unusable'] = remove_unusable_bears(relevant_bears, unusable_bears)
print_labels = ['unusable', 'usable']


print_relevant_bears(printer, relevant_bears)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

print_labelled_bears(print, labelled_bears, print_labels)


# Drop unusable bears
relevant_bears = relevant_bears['usable']
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Isn't the remove_unusable_bears method doing this?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I changed remove_unusable_bears to also add the unusable bears to the OrderdDict, but I'm using that only for printing purposes. Here the printing has ended, so I drop unusable bears from the dict.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I changed remove_unusable_bears to also add the unusable bears to the OrderdDict

doesn't that contradict with the function name? remove_unusable_bears is adding unusable bears to OrdereDict


settings = generate_settings(
project_dir,
Expand Down
48 changes: 30 additions & 18 deletions coala_quickstart/generation/Bears.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -201,40 +201,52 @@ def get_non_optional_settings_bears(bears):
return non_optional_settings


def remove_unusable_bears(bears, unusable_bears):
def filter_unusable_bears(bears, unusable_bears):
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This can stay as remove_unusable_bears , and return the removed bears grouped by language.

"""
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():
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

not needed

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(
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

return a new dict with the unusable bears grouped by language.

language, ()) + (bear, )


def print_relevant_bears(printer, relevant_bears, label='relevant'):
def print_relevant_bears(printer, relevant_bears):
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

change this to:

def print_bears(printer, labelled_bears, labels=['usable']):

The order of the labels is then used to determine which groups to show.

"""
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)

nonempty_label_bears = [
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nonempty -> non_empty

label for label in relevant_bears if len(relevant_bears[label]) > 0]

if not nonempty_label_bears:
printer.print('No relevant bears were found.')
else:
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

you can use return here if there are no bears to print

then the following can be dedented

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):
Expand Down
31 changes: 27 additions & 4 deletions tests/generation/Bears.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
import sys
import unittest
from copy import deepcopy
from collections import OrderedDict


from pyprint.ConsolePrinter import ConsolePrinter
Expand Down Expand Up @@ -304,10 +305,27 @@ 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, {}))
languages = [('Python', 70), ('Unknown', 30)]
bears_filtered = filter_relevant_bears(languages,
self.printer,
self.arg_parser, {})
relevant_bears = OrderedDict([('unusable', {}),
('usable', bears_filtered)])

print_relevant_bears(self.printer, relevant_bears)
self.assertIn("PycodestyleBear", custom_stdout.getvalue())
# Should print only the usable bears
self.assertNotIn("dropped",
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', {})]))
# No bears to print
self.assertNotIn("usable", custom_stdout.getvalue())
self.assertNotIn("dropped", custom_stdout.getvalue())
self.assertIn("No relevant", custom_stdout.getvalue())

def test_bears_allow_incomplete_sections_mode(self):
sys.argv.append('--ci')
Expand All @@ -317,7 +335,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("dropped",
custom_stdout.getvalue())
os.remove('.coafile')
os.chdir(orig_cwd)
Expand All @@ -329,8 +350,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("dropped", custom_stdout.getvalue())
os.remove('.coafile')
os.chdir(orig_cwd)

Expand Down