# HG changeset patch # User Teemu Piippo # Date 1598382510 -10800 # Node ID f9d4e59392f7e93e0b60d1001b45927b2e06ac45 # Parent 7b24ff111cb6f36f48226f6a20631fd509db8440 made colors toggleable diff -r 7b24ff111cb6 -r f9d4e59392f7 ldcheck.py --- a/ldcheck.py Tue Aug 25 21:58:09 2020 +0300 +++ b/ldcheck.py Tue Aug 25 22:08:30 2020 +0300 @@ -80,6 +80,31 @@ )) exit(0) +def format_report(report, model, test_suite, *, use_colors = True): + from testsuite import problem_text + messages = [] + for problem in report['problems']: + text_colour = '' + if use_colors: + if problem.severity == 'hold': + text_colour = colorama.Fore.LIGHTRED_EX + elif problem.severity == 'warning': + text_colour = colorama.Fore.LIGHTBLUE_EX + ldraw_code = model.body[problem.body_index].textual_representation() + message = str.format( + '{text_colour}{model_name}:{line_number}: {problem_type}: {message}' + '{colour_reset}\n\t{ldraw_code}', + text_colour = text_colour, + model_name = model.name, + line_number = problem.line_number, + problem_type = problem.severity, + message = problem_text(problem, test_suite), + colour_reset = use_colors and colorama.Fore.RESET or '', + ldraw_code = ldraw_code, + ) + messages.append(message) + return '\n'.join(messages) + if __name__ == '__main__': from sys import argv parser = argparse.ArgumentParser() @@ -101,11 +126,23 @@ action = 'store_true', help = 'finds a subfile by name and prints out information about it' ) + parser.add_argument('--color', + action = 'store_true', + help = 'use colors' + ) args = parser.parse_args() config = load_config('ldcheck.cfg') for ldconfig_ldr_path in find_ldconfig_ldr_paths(config): with ldconfig_ldr_path.open() as ldconfig_ldr: load_colours(ldconfig_ldr) + if args.color: + try: + import colorama + colorama.init() + except ImportError: + from sys import stderr + print('Use of --color requires the colorama module, disabling colours', file = stderr) + args.color = False if args.subfile: import filecache cache = filecache.SubfileCache( @@ -138,7 +175,7 @@ for entry in model.body: print(entry.textual_representation(), end = '\r\n') else: - from testsuite import load_tests, check_model, format_report + from testsuite import load_tests, check_model test_suite = load_tests() report = check_model(model, test_suite) - print(format_report(report, model, test_suite)) + print(format_report(report, model, test_suite, use_colors = args.color)) diff -r 7b24ff111cb6 -r f9d4e59392f7 testsuite.py --- a/testsuite.py Tue Aug 25 21:58:09 2020 +0300 +++ b/testsuite.py Tue Aug 25 22:08:30 2020 +0300 @@ -145,32 +145,6 @@ messages.append(message) return '\n'.join(messages) -def format_report(report, model, test_suite): - import colorama - colorama.init() - messages = [] - for problem in report['problems']: - if problem.severity == 'hold': - text_colour = colorama.Fore.LIGHTRED_EX - elif problem.severity == 'warning': - text_colour = colorama.Fore.LIGHTBLUE_EX - else: - text_colour = '' - ldraw_code = model.body[problem.body_index].textual_representation() - message = str.format( - '{text_colour}{model_name}:{line_number}: {problem_type}: {message}' - '{colour_reset}\n\t{ldraw_code}', - text_colour = text_colour, - model_name = model.name, - line_number = problem.line_number, - problem_type = problem.severity, - message = problem_text(problem, test_suite), - colour_reset = colorama.Fore.RESET, - ldraw_code = ldraw_code, - ) - messages.append(message) - return '\n'.join(messages) - def iterate_problems(test_suite): for test_function in test_suite['tests']: yield from test_function.ldcheck_problem_types.values()