Wed, 31 Jan 2018 15:58:38 +0200
removed old code
16 | 1 | from warnings import warn |
2 | ||
3 | def report_element(bad_object, type, error_name, args): | |
12 | 4 | return { |
16 | 5 | 'type': type, |
6 | 'object': bad_object, | |
7 | 'name': error_name, | |
8 | 'args': args, | |
13 | 9 | } |
10 | ||
17 | 11 | def warning(bad_object, error_name, **args): |
16 | 12 | return report_element(bad_object, 'warning', error_name, args) |
13 | ||
17 | 14 | def error(bad_object, error_name, **args): |
16 | 15 | return report_element(bad_object, 'error', error_name, args) |
16 | ||
32
75f44d3063da
Reworked web front, problems are now sorted by category as well as line number
Santeri Piippo
parents:
29
diff
changeset
|
17 | def notice(bad_object, error_name, **args): |
75f44d3063da
Reworked web front, problems are now sorted by category as well as line number
Santeri Piippo
parents:
29
diff
changeset
|
18 | return report_element(bad_object, 'notice', error_name, args) |
75f44d3063da
Reworked web front, problems are now sorted by category as well as line number
Santeri Piippo
parents:
29
diff
changeset
|
19 | |
16 | 20 | def test_discovery(): |
21 | ''' | |
22 | Finds all test modules and yields their names. | |
23 | ''' | |
24 | from pkgutil import walk_packages | |
25 | import tests | |
26 | yield from sorted( | |
42 | 27 | 'tests.' + result[1] |
16 | 28 | for result in walk_packages(tests.__path__) |
29 | ) | |
30 | ||
31 | def do_manifest_integrity_checks(test_suite, module): | |
32 | ''' | |
33 | Runs integrity checks on a given module's manifest. | |
34 | ''' | |
35 | def check_for_extra_keys(): | |
36 | extra_keys = module.manifest.keys() - test_suite.keys() | |
37 | if extra_keys: | |
38 | warn(str.format( | |
39 | '{}: extra keys in manifest: {}', | |
40 | module.__name__, | |
41 | ', '.join(map(str, extra_keys)) | |
42 | )) | |
43 | def check_for_manifest_duplicates(): | |
44 | for key in test_suite.keys(): | |
45 | duplicates = module.manifest[key].keys() & test_suite[key].keys() | |
46 | if duplicates: | |
47 | warn(str.format( | |
48 | '{}: redefined {} in manifests: {}', | |
49 | module.__name__, | |
50 | key, | |
51 | duplicates, | |
52 | )) | |
53 | check_for_extra_keys() | |
54 | check_for_manifest_duplicates() | |
55 | ||
56 | def load_tests(): | |
57 | ''' | |
58 | Imports test modules and combines their manifests into a test suite. | |
59 | ''' | |
60 | test_suite = {'tests': {}, 'messages': {}} | |
61 | for module_name in test_discovery(): | |
62 | from importlib import import_module | |
63 | module = import_module(module_name) | |
64 | if hasattr(module, 'manifest'): | |
65 | do_manifest_integrity_checks(test_suite, module) | |
66 | # Merge the data from the manifest | |
67 | for key in module.manifest.keys() & test_suite.keys(): | |
68 | test_suite[key].update(module.manifest[key]) | |
69 | else: | |
70 | warn(str.format('Module {} does not have a manifest', module_name)) | |
71 | return test_suite | |
72 | ||
32
75f44d3063da
Reworked web front, problems are now sorted by category as well as line number
Santeri Piippo
parents:
29
diff
changeset
|
73 | def problem_key(problem): |
75f44d3063da
Reworked web front, problems are now sorted by category as well as line number
Santeri Piippo
parents:
29
diff
changeset
|
74 | problem_hierarchy = ['error', 'warning', 'notice'] |
75f44d3063da
Reworked web front, problems are now sorted by category as well as line number
Santeri Piippo
parents:
29
diff
changeset
|
75 | return (problem_hierarchy.index(problem['type']), problem['line-number']) |
75f44d3063da
Reworked web front, problems are now sorted by category as well as line number
Santeri Piippo
parents:
29
diff
changeset
|
76 | |
17 | 77 | def check_model(model, test_suite = None): |
78 | if not test_suite: | |
79 | test_suite = load_tests() | |
32
75f44d3063da
Reworked web front, problems are now sorted by category as well as line number
Santeri Piippo
parents:
29
diff
changeset
|
80 | problems = [] |
17 | 81 | line_numbers = { |
82 | element: (i, i + 1 + model.body_offset) | |
83 | for i, element in enumerate(model.body) | |
84 | } | |
85 | for test_name, test_function in test_suite['tests'].items(): | |
32
75f44d3063da
Reworked web front, problems are now sorted by category as well as line number
Santeri Piippo
parents:
29
diff
changeset
|
86 | for problem in test_function(model): |
17 | 87 | problem['body-index'], problem['line-number'] \ |
88 | = line_numbers[problem['object']] | |
89 | del problem['object'] | |
32
75f44d3063da
Reworked web front, problems are now sorted by category as well as line number
Santeri Piippo
parents:
29
diff
changeset
|
90 | problems.append(problem) |
75f44d3063da
Reworked web front, problems are now sorted by category as well as line number
Santeri Piippo
parents:
29
diff
changeset
|
91 | return { |
75f44d3063da
Reworked web front, problems are now sorted by category as well as line number
Santeri Piippo
parents:
29
diff
changeset
|
92 | 'passed': not any( |
75f44d3063da
Reworked web front, problems are now sorted by category as well as line number
Santeri Piippo
parents:
29
diff
changeset
|
93 | problem['type'] == 'error' |
75f44d3063da
Reworked web front, problems are now sorted by category as well as line number
Santeri Piippo
parents:
29
diff
changeset
|
94 | for problem in problems |
75f44d3063da
Reworked web front, problems are now sorted by category as well as line number
Santeri Piippo
parents:
29
diff
changeset
|
95 | ), |
75f44d3063da
Reworked web front, problems are now sorted by category as well as line number
Santeri Piippo
parents:
29
diff
changeset
|
96 | 'problems': sorted(problems, key = problem_key), |
75f44d3063da
Reworked web front, problems are now sorted by category as well as line number
Santeri Piippo
parents:
29
diff
changeset
|
97 | } |
17 | 98 | |
29 | 99 | def problem_text(problem, test_suite): |
100 | message = test_suite['messages'][problem['name']] | |
101 | if callable(message): | |
102 | message = message(**problem['args']) | |
103 | return message | |
104 | ||
17 | 105 | def format_report(report, model, test_suite): |
18 | 106 | import colorama |
107 | colorama.init() | |
32
75f44d3063da
Reworked web front, problems are now sorted by category as well as line number
Santeri Piippo
parents:
29
diff
changeset
|
108 | messages = [] |
75f44d3063da
Reworked web front, problems are now sorted by category as well as line number
Santeri Piippo
parents:
29
diff
changeset
|
109 | for problem in report['problems']: |
18 | 110 | if problem['type'] == 'error': |
111 | text_colour = colorama.Fore.LIGHTRED_EX | |
112 | elif problem['type'] == 'warning': | |
113 | text_colour = colorama.Fore.LIGHTYELLOW_EX | |
32
75f44d3063da
Reworked web front, problems are now sorted by category as well as line number
Santeri Piippo
parents:
29
diff
changeset
|
114 | elif problem['type'] == 'notice': |
75f44d3063da
Reworked web front, problems are now sorted by category as well as line number
Santeri Piippo
parents:
29
diff
changeset
|
115 | text_colour = colorama.Fore.LIGHTBLUE_EX |
18 | 116 | else: |
117 | text_colour = '' | |
19 | 118 | ldraw_code = model.body[problem['body-index']].textual_representation() |
17 | 119 | message = str.format( |
18 | 120 | '{text_colour}{model_name}:{line_number}: {problem_type}: {message}' |
19 | 121 | '{colour_reset}\n\t{ldraw_code}', |
18 | 122 | text_colour = text_colour, |
123 | model_name = model.name, | |
124 | line_number = problem['line-number'], | |
125 | problem_type = problem['type'], | |
29 | 126 | message = problem_text(problem, test_suite), |
18 | 127 | colour_reset = colorama.Fore.RESET, |
19 | 128 | ldraw_code = ldraw_code, |
17 | 129 | ) |
32
75f44d3063da
Reworked web front, problems are now sorted by category as well as line number
Santeri Piippo
parents:
29
diff
changeset
|
130 | messages.append(message) |
75f44d3063da
Reworked web front, problems are now sorted by category as well as line number
Santeri Piippo
parents:
29
diff
changeset
|
131 | return '\n'.join(messages) |
17 | 132 | |
16 | 133 | if __name__ == '__main__': |
134 | from pprint import pprint | |
135 | pprint(load_tests()) |