made errors prettier

Mon, 22 Jan 2018 17:42:57 +0200

author
Santeri Piippo
date
Mon, 22 Jan 2018 17:42:57 +0200
changeset 18
672ebc45685a
parent 17
327da5d00360
child 19
9169bad392c4

made errors prettier

ldcheck.py file | annotate | diff | comparison | revisions
tests/concave.py file | annotate | diff | comparison | revisions
testsuite.py file | annotate | diff | comparison | revisions
--- a/ldcheck.py	Mon Jan 22 17:05:10 2018 +0200
+++ b/ldcheck.py	Mon Jan 22 17:42:57 2018 +0200
@@ -66,6 +66,8 @@
     with open(argv[1], 'r') as file:
         model_body = read_ldraw(file, config = config)
         model = Model(body = model_body)
+        from os.path import basename
+        model.name = basename(argv[1])
         from testsuite import load_tests, check_model, format_report
         test_suite = load_tests()
         report = check_model(model, test_suite)
--- a/tests/concave.py	Mon Jan 22 17:05:10 2018 +0200
+++ b/tests/concave.py	Mon Jan 22 17:42:57 2018 +0200
@@ -36,9 +36,9 @@
     },
     'messages': {
         'skew-error': lambda skew_angle:
-            str.format('Skew quadrilateral (plane angle {}°)',
+            str.format('skew quadrilateral (plane angle {}°)',
                 '%.2f' % degrees(skew_angle),
             ),
-        'concave-error': 'Concave quadrilateral',
+        'concave-error': 'concave quadrilateral',
     },
 }
--- a/testsuite.py	Mon Jan 22 17:05:10 2018 +0200
+++ b/testsuite.py	Mon Jan 22 17:42:57 2018 +0200
@@ -85,16 +85,29 @@
     return report
 
 def format_report(report, model, test_suite):
+    import colorama
+    colorama.init()
     result = []
     for problem in report:
         problem_text = test_suite['messages'][problem['name']]
         if callable(problem_text):
             problem_text = problem_text(**problem['args'])
+        if problem['type'] == 'error':
+            text_colour = colorama.Fore.LIGHTRED_EX
+        elif problem['type'] == 'warning':
+            text_colour = colorama.Fore.LIGHTYELLOW_EX
+        else:
+            text_colour = ''
         message = str.format(
-            'Line {}: {}\n\t{}',
-            problem['line-number'],
-            problem_text,
-            model.body[problem['body-index']].original_code
+            '{text_colour}{model_name}:{line_number}: {problem_type}: {message}'
+            '{colour_reset}\n\t{trouble_source}',
+            text_colour = text_colour,
+            model_name = model.name,
+            line_number = problem['line-number'],
+            problem_type = problem['type'],
+            message = problem_text,
+            colour_reset = colorama.Fore.RESET,
+            trouble_source = model.body[problem['body-index']].original_code,
         )
         result.append((problem['line-number'], message))
     return '\n'.join(

mercurial