made colors toggleable

Tue, 25 Aug 2020 22:08:30 +0300

author
Teemu Piippo <teemu@hecknology.net>
date
Tue, 25 Aug 2020 22:08:30 +0300
changeset 98
f9d4e59392f7
parent 97
7b24ff111cb6
child 99
01941a811b5a

made colors toggleable

ldcheck.py file | annotate | diff | comparison | revisions
testsuite.py file | annotate | diff | comparison | revisions
--- 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))
--- 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()

mercurial