git-linter/git_lint/reporters.py

83 lines
2.7 KiB
Python
Raw Normal View History

from __future__ import print_function
2016-09-30 19:10:52 +00:00
from .git_lint import load_config, run_linters, git_base
import operator
2016-09-28 21:26:06 +00:00
import gettext
_ = gettext.gettext
2016-09-28 22:37:23 +00:00
2016-09-30 19:10:52 +00:00
def base_file_cleaner(files):
return [file.replace(git_base + '/', '', 1) for file in files]
2016-09-28 21:57:02 +00:00
# ICK. Mutation, references, and hidden assignment.
def group_by(iterable, field_id):
results = []
keys = {}
for obj in iterable:
key = obj[field_id]
if key in keys:
keys[key].append(obj)
continue
keys[key] = [obj]
results.append((key, keys[key]))
return results
2016-09-28 22:37:23 +00:00
2016-09-28 21:26:06 +00:00
def print_report(results, unlintable_filenames, cant_lint_filenames,
2016-09-28 22:37:23 +00:00
broken_linter_names, unfindable_filenames, options={'bylinter': True}):
2016-09-28 21:26:06 +00:00
sort_position = 1
grouping = _('Linter: {}')
if 'byfile' in options:
sort_position = 0
grouping = _('Filename: {}')
grouped_results = group_by(results, sort_position)
2016-09-28 21:26:06 +00:00
for group in grouped_results:
messages = reduce(operator.add, [item[3] for item in group[1]], [])
if len(messages) == 0:
continue
2016-09-28 21:26:06 +00:00
print(grouping.format(group[0]))
for (filename, lintername, returncode, text) in group[1]:
if text:
print('\n'.join(base_file_cleaner(text)))
print('')
print ('')
if len(broken_linter_names) and (len(cant_lint_filenames) or ('verbose' in options)):
2016-10-04 17:38:53 +00:00
print(_('Linters not found:'), ','.join(broken_linter_names))
2016-09-28 21:26:06 +00:00
if len(cant_lint_filenames):
2016-10-04 17:38:53 +00:00
print(' ' + _('Files not linted:'))
2016-09-28 21:26:06 +00:00
print('\n'.join([' {}'.format(f) for f in cant_lint_filenames]))
print('')
if len(unlintable_filenames) and ('verbose' in options):
print(_('No recognizeable linters for:'))
2016-09-28 21:26:06 +00:00
print('\n'.join([' {}'.format(f) for f in unlintable_filenames]))
print('')
2016-09-28 21:26:06 +00:00
if len(unfindable_filenames):
2016-10-04 17:38:53 +00:00
print(_('Files not found:'))
2016-09-28 21:26:06 +00:00
print('\n'.join([' {}'.format(f) for f in unfindable_filenames]))
print('')
2016-09-28 21:26:06 +00:00
def print_help(options, name):
print(_('Usage: {} [options] [filenames]').format(name))
for item in options:
print(' -{:<1} --{:<12} {}'.format(item[0], item[1], item[3]))
def print_version(name, version):
2016-09-28 22:37:23 +00:00
print(_('{} {} Copyright (c) 2009, 2016 Kennth M. "Elf" Sternberg').format(name, version))
2016-09-28 21:26:06 +00:00
2016-09-28 21:57:02 +00:00
def print_linters(config, broken_linter_names):
2016-09-28 21:26:06 +00:00
print(_('Currently supported linters:'))
for linter in config:
print('{:<14} {}'.format(linter.name,
((linter.name in broken_linter_names and
_('(WARNING: executable not found)') or
linter.linter.get('comment', '')))))