Sat, 08 Jun 2019 11:17:17 +0300
cleanup
3
1dc58f44d556
Can now write dat files, added direct color handling
Santeri Piippo
parents:
diff
changeset
|
1 | #!/usr/bin/env python3 |
65 | 2 | ''' |
3 | Main LDCheck commandline program. | |
4 | ''' | |
8 | 5 | from sys import version_info |
6 | if version_info < (3, 4): | |
7 | raise RuntimeError('Python 3.4 or newer required') | |
8 | ||
7 | 9 | from geometry import * |
13 | 10 | import linetypes |
47
4da025d0b283
added work on header check
Teemu Piippo <teemu@hecknology.net>
parents:
38
diff
changeset
|
11 | import header |
54
0c686d10eb49
added tests for moved-to files and scaling in flat dimensions
Teemu Piippo <teemu@hecknology.net>
parents:
47
diff
changeset
|
12 | import parse |
65 | 13 | import config |
14 | import colours | |
9
fea8e9ae6f29
Added matrix code, moved library paths to ldcheck.cfg.
Santeri Piippo
parents:
8
diff
changeset
|
15 | |
65 | 16 | def format_report(report, model): |
17 | ''' | |
18 | Formats the report from the test suite so as to be | |
19 | printed in the console. | |
20 | ''' | |
21 | import colorama | |
22 | colorama.init() | |
23 | messages = [] | |
24 | for problem in report['problems']: | |
25 | if problem.severity == 'hold': | |
26 | text_colour = colorama.Fore.LIGHTRED_EX | |
27 | elif problem.severity == 'warning': | |
28 | text_colour = colorama.Fore.LIGHTBLUE_EX | |
29 | else: | |
30 | text_colour = '' | |
31 | ldraw_code = model.body[problem.body_index].textual_representation() | |
32 | message = str.format( | |
33 | '{text_colour}{model_name}:{line_number}: {problem_type}: {message}' | |
34 | '{colour_reset}\n\t{ldraw_code}', | |
35 | text_colour = text_colour, | |
36 | model_name = model.name, | |
37 | line_number = problem.line_number, | |
38 | problem_type = problem.severity, | |
39 | message = str(problem), | |
40 | colour_reset = colorama.Fore.RESET, | |
41 | ldraw_code = ldraw_code, | |
42 | ) | |
43 | messages.append(message) | |
44 | return '\n'.join(messages) | |
8 | 45 | |
26
7c263b864371
Added command line option to list all checks.
Santeri Piippo
parents:
21
diff
changeset
|
46 | import argparse |
62
f0a6bf48b05e
Problem reporting revamp, program is now aware of its problem types
Teemu Piippo <teemu@hecknology.net>
parents:
54
diff
changeset
|
47 | |
26
7c263b864371
Added command line option to list all checks.
Santeri Piippo
parents:
21
diff
changeset
|
48 | class ListTestSuiteAction(argparse.Action): |
7c263b864371
Added command line option to list all checks.
Santeri Piippo
parents:
21
diff
changeset
|
49 | def __init__(self, option_strings, dest, nargs = None, **kwargs): |
7c263b864371
Added command line option to list all checks.
Santeri Piippo
parents:
21
diff
changeset
|
50 | super().__init__(option_strings, dest, nargs = 0, **kwargs) |
7c263b864371
Added command line option to list all checks.
Santeri Piippo
parents:
21
diff
changeset
|
51 | def __call__(self, *args, **kwargs): |
63
8949af6a4279
added the list of issues onto the web frontend
Teemu Piippo <teemu@hecknology.net>
parents:
62
diff
changeset
|
52 | import testsuite |
26
7c263b864371
Added command line option to list all checks.
Santeri Piippo
parents:
21
diff
changeset
|
53 | from sys import exit |
7c263b864371
Added command line option to list all checks.
Santeri Piippo
parents:
21
diff
changeset
|
54 | from re import sub |
63
8949af6a4279
added the list of issues onto the web frontend
Teemu Piippo <teemu@hecknology.net>
parents:
62
diff
changeset
|
55 | test_suite = testsuite.load_tests() |
8949af6a4279
added the list of issues onto the web frontend
Teemu Piippo <teemu@hecknology.net>
parents:
62
diff
changeset
|
56 | for warning_type in testsuite.all_problem_types(test_suite): |
62
f0a6bf48b05e
Problem reporting revamp, program is now aware of its problem types
Teemu Piippo <teemu@hecknology.net>
parents:
54
diff
changeset
|
57 | print(str.format('{name}: {severity}: "{message}"', |
f0a6bf48b05e
Problem reporting revamp, program is now aware of its problem types
Teemu Piippo <teemu@hecknology.net>
parents:
54
diff
changeset
|
58 | name = warning_type.name, |
f0a6bf48b05e
Problem reporting revamp, program is now aware of its problem types
Teemu Piippo <teemu@hecknology.net>
parents:
54
diff
changeset
|
59 | severity = warning_type.severity, |
63
8949af6a4279
added the list of issues onto the web frontend
Teemu Piippo <teemu@hecknology.net>
parents:
62
diff
changeset
|
60 | message = warning_type.placeholder_message(), |
62
f0a6bf48b05e
Problem reporting revamp, program is now aware of its problem types
Teemu Piippo <teemu@hecknology.net>
parents:
54
diff
changeset
|
61 | )) |
26
7c263b864371
Added command line option to list all checks.
Santeri Piippo
parents:
21
diff
changeset
|
62 | exit(0) |
7c263b864371
Added command line option to list all checks.
Santeri Piippo
parents:
21
diff
changeset
|
63 | |
3
1dc58f44d556
Can now write dat files, added direct color handling
Santeri Piippo
parents:
diff
changeset
|
64 | if __name__ == '__main__': |
1dc58f44d556
Can now write dat files, added direct color handling
Santeri Piippo
parents:
diff
changeset
|
65 | from sys import argv |
26
7c263b864371
Added command line option to list all checks.
Santeri Piippo
parents:
21
diff
changeset
|
66 | parser = argparse.ArgumentParser() |
7c263b864371
Added command line option to list all checks.
Santeri Piippo
parents:
21
diff
changeset
|
67 | parser.add_argument('filename') |
7c263b864371
Added command line option to list all checks.
Santeri Piippo
parents:
21
diff
changeset
|
68 | parser.add_argument('--list', |
7c263b864371
Added command line option to list all checks.
Santeri Piippo
parents:
21
diff
changeset
|
69 | action = ListTestSuiteAction, |
7c263b864371
Added command line option to list all checks.
Santeri Piippo
parents:
21
diff
changeset
|
70 | help = 'Lists all possible checks and exit', |
7c263b864371
Added command line option to list all checks.
Santeri Piippo
parents:
21
diff
changeset
|
71 | ) |
38
66c9591b733d
added proper handling of syntax errors
Teemu Piippo <teemu@hecknology.net>
parents:
36
diff
changeset
|
72 | parser.add_argument('--dump-structure', action = 'store_true') |
66c9591b733d
added proper handling of syntax errors
Teemu Piippo <teemu@hecknology.net>
parents:
36
diff
changeset
|
73 | parser.add_argument('--rebuild', action = 'store_true') |
54
0c686d10eb49
added tests for moved-to files and scaling in flat dimensions
Teemu Piippo <teemu@hecknology.net>
parents:
47
diff
changeset
|
74 | parser.add_argument('--flatness', action = 'store_true') |
26
7c263b864371
Added command line option to list all checks.
Santeri Piippo
parents:
21
diff
changeset
|
75 | args = parser.parse_args() |
65 | 76 | config_object = config.load_config('ldcheck.cfg') |
77 | for ldconfig_ldr_path in config.find_ldconfig_ldr_paths(config_object): | |
8 | 78 | with ldconfig_ldr_path.open() as ldconfig_ldr: |
65 | 79 | colours.load_colours(ldconfig_ldr) |
54
0c686d10eb49
added tests for moved-to files and scaling in flat dimensions
Teemu Piippo <teemu@hecknology.net>
parents:
47
diff
changeset
|
80 | if args.flatness: |
0c686d10eb49
added tests for moved-to files and scaling in flat dimensions
Teemu Piippo <teemu@hecknology.net>
parents:
47
diff
changeset
|
81 | import filecache |
0c686d10eb49
added tests for moved-to files and scaling in flat dimensions
Teemu Piippo <teemu@hecknology.net>
parents:
47
diff
changeset
|
82 | cache = filecache.SubfileCache( |
65 | 83 | ldraw_directories = config_object['libraries'], |
26
7c263b864371
Added command line option to list all checks.
Santeri Piippo
parents:
21
diff
changeset
|
84 | ) |
54
0c686d10eb49
added tests for moved-to files and scaling in flat dimensions
Teemu Piippo <teemu@hecknology.net>
parents:
47
diff
changeset
|
85 | subfile = cache.prepare_file(args.filename) |
0c686d10eb49
added tests for moved-to files and scaling in flat dimensions
Teemu Piippo <teemu@hecknology.net>
parents:
47
diff
changeset
|
86 | if not subfile.valid: |
0c686d10eb49
added tests for moved-to files and scaling in flat dimensions
Teemu Piippo <teemu@hecknology.net>
parents:
47
diff
changeset
|
87 | print(subfile.problem) |
38
66c9591b733d
added proper handling of syntax errors
Teemu Piippo <teemu@hecknology.net>
parents:
36
diff
changeset
|
88 | else: |
54
0c686d10eb49
added tests for moved-to files and scaling in flat dimensions
Teemu Piippo <teemu@hecknology.net>
parents:
47
diff
changeset
|
89 | if subfile.flatness: |
0c686d10eb49
added tests for moved-to files and scaling in flat dimensions
Teemu Piippo <teemu@hecknology.net>
parents:
47
diff
changeset
|
90 | print(str.format( |
0c686d10eb49
added tests for moved-to files and scaling in flat dimensions
Teemu Piippo <teemu@hecknology.net>
parents:
47
diff
changeset
|
91 | 'Flatness: {}', |
0c686d10eb49
added tests for moved-to files and scaling in flat dimensions
Teemu Piippo <teemu@hecknology.net>
parents:
47
diff
changeset
|
92 | ', '.join(subfile.flatness), |
0c686d10eb49
added tests for moved-to files and scaling in flat dimensions
Teemu Piippo <teemu@hecknology.net>
parents:
47
diff
changeset
|
93 | )) |
0c686d10eb49
added tests for moved-to files and scaling in flat dimensions
Teemu Piippo <teemu@hecknology.net>
parents:
47
diff
changeset
|
94 | else: |
0c686d10eb49
added tests for moved-to files and scaling in flat dimensions
Teemu Piippo <teemu@hecknology.net>
parents:
47
diff
changeset
|
95 | print('File is not flat in any dimensions') |
0c686d10eb49
added tests for moved-to files and scaling in flat dimensions
Teemu Piippo <teemu@hecknology.net>
parents:
47
diff
changeset
|
96 | else: |
0c686d10eb49
added tests for moved-to files and scaling in flat dimensions
Teemu Piippo <teemu@hecknology.net>
parents:
47
diff
changeset
|
97 | with open(args.filename) as file: |
0c686d10eb49
added tests for moved-to files and scaling in flat dimensions
Teemu Piippo <teemu@hecknology.net>
parents:
47
diff
changeset
|
98 | from os.path import basename |
0c686d10eb49
added tests for moved-to files and scaling in flat dimensions
Teemu Piippo <teemu@hecknology.net>
parents:
47
diff
changeset
|
99 | model = parse.read_ldraw( |
0c686d10eb49
added tests for moved-to files and scaling in flat dimensions
Teemu Piippo <teemu@hecknology.net>
parents:
47
diff
changeset
|
100 | file, |
0c686d10eb49
added tests for moved-to files and scaling in flat dimensions
Teemu Piippo <teemu@hecknology.net>
parents:
47
diff
changeset
|
101 | name = basename(args.filename), |
65 | 102 | ldraw_directories = config_object['libraries']) |
54
0c686d10eb49
added tests for moved-to files and scaling in flat dimensions
Teemu Piippo <teemu@hecknology.net>
parents:
47
diff
changeset
|
103 | if args.dump_structure: |
0c686d10eb49
added tests for moved-to files and scaling in flat dimensions
Teemu Piippo <teemu@hecknology.net>
parents:
47
diff
changeset
|
104 | print('header: ' + type(model.header).__name__) |
0c686d10eb49
added tests for moved-to files and scaling in flat dimensions
Teemu Piippo <teemu@hecknology.net>
parents:
47
diff
changeset
|
105 | for key in sorted(dir(model.header)): |
0c686d10eb49
added tests for moved-to files and scaling in flat dimensions
Teemu Piippo <teemu@hecknology.net>
parents:
47
diff
changeset
|
106 | if not key.startswith('__'): |
0c686d10eb49
added tests for moved-to files and scaling in flat dimensions
Teemu Piippo <teemu@hecknology.net>
parents:
47
diff
changeset
|
107 | print('\t' + key + ': ' + repr(getattr(model.header, key))) |
0c686d10eb49
added tests for moved-to files and scaling in flat dimensions
Teemu Piippo <teemu@hecknology.net>
parents:
47
diff
changeset
|
108 | for entry in model.body: |
0c686d10eb49
added tests for moved-to files and scaling in flat dimensions
Teemu Piippo <teemu@hecknology.net>
parents:
47
diff
changeset
|
109 | print(entry) |
0c686d10eb49
added tests for moved-to files and scaling in flat dimensions
Teemu Piippo <teemu@hecknology.net>
parents:
47
diff
changeset
|
110 | elif args.rebuild: |
0c686d10eb49
added tests for moved-to files and scaling in flat dimensions
Teemu Piippo <teemu@hecknology.net>
parents:
47
diff
changeset
|
111 | for entry in model.body: |
0c686d10eb49
added tests for moved-to files and scaling in flat dimensions
Teemu Piippo <teemu@hecknology.net>
parents:
47
diff
changeset
|
112 | print(entry.textual_representation(), end = '\r\n') |
0c686d10eb49
added tests for moved-to files and scaling in flat dimensions
Teemu Piippo <teemu@hecknology.net>
parents:
47
diff
changeset
|
113 | else: |
65 | 114 | import testsuite |
115 | test_suite = testsuite.load_tests() | |
116 | report = testsuite.check_model(model, test_suite) | |
117 | print(format_report(report, model)) |