ldcheck.py

Sat, 08 Jun 2019 11:17:17 +0300

author
Teemu Piippo <teemu@hecknology.net>
date
Sat, 08 Jun 2019 11:17:17 +0300
changeset 65
f2dc17b830e0
parent 63
8949af6a4279
permissions
-rwxr-xr-x

cleanup

3
1dc58f44d556 Can now write dat files, added direct color handling
Santeri Piippo
parents:
diff changeset
1 #!/usr/bin/env python3
65
f2dc17b830e0 cleanup
Teemu Piippo <teemu@hecknology.net>
parents: 63
diff changeset
2 '''
f2dc17b830e0 cleanup
Teemu Piippo <teemu@hecknology.net>
parents: 63
diff changeset
3 Main LDCheck commandline program.
f2dc17b830e0 cleanup
Teemu Piippo <teemu@hecknology.net>
parents: 63
diff changeset
4 '''
8
303c51137cb2 Added ldconfig.ldr support
Santeri Piippo
parents: 7
diff changeset
5 from sys import version_info
303c51137cb2 Added ldconfig.ldr support
Santeri Piippo
parents: 7
diff changeset
6 if version_info < (3, 4):
303c51137cb2 Added ldconfig.ldr support
Santeri Piippo
parents: 7
diff changeset
7 raise RuntimeError('Python 3.4 or newer required')
303c51137cb2 Added ldconfig.ldr support
Santeri Piippo
parents: 7
diff changeset
8
7
0ab0d61ccee8 Smallest angles
Santeri Piippo
parents: 6
diff changeset
9 from geometry import *
13
12d4ddc4bfd8 Got the skew test working
Santeri Piippo
parents: 9
diff changeset
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
f2dc17b830e0 cleanup
Teemu Piippo <teemu@hecknology.net>
parents: 63
diff changeset
13 import config
f2dc17b830e0 cleanup
Teemu Piippo <teemu@hecknology.net>
parents: 63
diff changeset
14 import colours
9
fea8e9ae6f29 Added matrix code, moved library paths to ldcheck.cfg.
Santeri Piippo
parents: 8
diff changeset
15
65
f2dc17b830e0 cleanup
Teemu Piippo <teemu@hecknology.net>
parents: 63
diff changeset
16 def format_report(report, model):
f2dc17b830e0 cleanup
Teemu Piippo <teemu@hecknology.net>
parents: 63
diff changeset
17 '''
f2dc17b830e0 cleanup
Teemu Piippo <teemu@hecknology.net>
parents: 63
diff changeset
18 Formats the report from the test suite so as to be
f2dc17b830e0 cleanup
Teemu Piippo <teemu@hecknology.net>
parents: 63
diff changeset
19 printed in the console.
f2dc17b830e0 cleanup
Teemu Piippo <teemu@hecknology.net>
parents: 63
diff changeset
20 '''
f2dc17b830e0 cleanup
Teemu Piippo <teemu@hecknology.net>
parents: 63
diff changeset
21 import colorama
f2dc17b830e0 cleanup
Teemu Piippo <teemu@hecknology.net>
parents: 63
diff changeset
22 colorama.init()
f2dc17b830e0 cleanup
Teemu Piippo <teemu@hecknology.net>
parents: 63
diff changeset
23 messages = []
f2dc17b830e0 cleanup
Teemu Piippo <teemu@hecknology.net>
parents: 63
diff changeset
24 for problem in report['problems']:
f2dc17b830e0 cleanup
Teemu Piippo <teemu@hecknology.net>
parents: 63
diff changeset
25 if problem.severity == 'hold':
f2dc17b830e0 cleanup
Teemu Piippo <teemu@hecknology.net>
parents: 63
diff changeset
26 text_colour = colorama.Fore.LIGHTRED_EX
f2dc17b830e0 cleanup
Teemu Piippo <teemu@hecknology.net>
parents: 63
diff changeset
27 elif problem.severity == 'warning':
f2dc17b830e0 cleanup
Teemu Piippo <teemu@hecknology.net>
parents: 63
diff changeset
28 text_colour = colorama.Fore.LIGHTBLUE_EX
f2dc17b830e0 cleanup
Teemu Piippo <teemu@hecknology.net>
parents: 63
diff changeset
29 else:
f2dc17b830e0 cleanup
Teemu Piippo <teemu@hecknology.net>
parents: 63
diff changeset
30 text_colour = ''
f2dc17b830e0 cleanup
Teemu Piippo <teemu@hecknology.net>
parents: 63
diff changeset
31 ldraw_code = model.body[problem.body_index].textual_representation()
f2dc17b830e0 cleanup
Teemu Piippo <teemu@hecknology.net>
parents: 63
diff changeset
32 message = str.format(
f2dc17b830e0 cleanup
Teemu Piippo <teemu@hecknology.net>
parents: 63
diff changeset
33 '{text_colour}{model_name}:{line_number}: {problem_type}: {message}'
f2dc17b830e0 cleanup
Teemu Piippo <teemu@hecknology.net>
parents: 63
diff changeset
34 '{colour_reset}\n\t{ldraw_code}',
f2dc17b830e0 cleanup
Teemu Piippo <teemu@hecknology.net>
parents: 63
diff changeset
35 text_colour = text_colour,
f2dc17b830e0 cleanup
Teemu Piippo <teemu@hecknology.net>
parents: 63
diff changeset
36 model_name = model.name,
f2dc17b830e0 cleanup
Teemu Piippo <teemu@hecknology.net>
parents: 63
diff changeset
37 line_number = problem.line_number,
f2dc17b830e0 cleanup
Teemu Piippo <teemu@hecknology.net>
parents: 63
diff changeset
38 problem_type = problem.severity,
f2dc17b830e0 cleanup
Teemu Piippo <teemu@hecknology.net>
parents: 63
diff changeset
39 message = str(problem),
f2dc17b830e0 cleanup
Teemu Piippo <teemu@hecknology.net>
parents: 63
diff changeset
40 colour_reset = colorama.Fore.RESET,
f2dc17b830e0 cleanup
Teemu Piippo <teemu@hecknology.net>
parents: 63
diff changeset
41 ldraw_code = ldraw_code,
f2dc17b830e0 cleanup
Teemu Piippo <teemu@hecknology.net>
parents: 63
diff changeset
42 )
f2dc17b830e0 cleanup
Teemu Piippo <teemu@hecknology.net>
parents: 63
diff changeset
43 messages.append(message)
f2dc17b830e0 cleanup
Teemu Piippo <teemu@hecknology.net>
parents: 63
diff changeset
44 return '\n'.join(messages)
8
303c51137cb2 Added ldconfig.ldr support
Santeri Piippo
parents: 7
diff changeset
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
f2dc17b830e0 cleanup
Teemu Piippo <teemu@hecknology.net>
parents: 63
diff changeset
76 config_object = config.load_config('ldcheck.cfg')
f2dc17b830e0 cleanup
Teemu Piippo <teemu@hecknology.net>
parents: 63
diff changeset
77 for ldconfig_ldr_path in config.find_ldconfig_ldr_paths(config_object):
8
303c51137cb2 Added ldconfig.ldr support
Santeri Piippo
parents: 7
diff changeset
78 with ldconfig_ldr_path.open() as ldconfig_ldr:
65
f2dc17b830e0 cleanup
Teemu Piippo <teemu@hecknology.net>
parents: 63
diff changeset
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
f2dc17b830e0 cleanup
Teemu Piippo <teemu@hecknology.net>
parents: 63
diff changeset
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
f2dc17b830e0 cleanup
Teemu Piippo <teemu@hecknology.net>
parents: 63
diff changeset
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
f2dc17b830e0 cleanup
Teemu Piippo <teemu@hecknology.net>
parents: 63
diff changeset
114 import testsuite
f2dc17b830e0 cleanup
Teemu Piippo <teemu@hecknology.net>
parents: 63
diff changeset
115 test_suite = testsuite.load_tests()
f2dc17b830e0 cleanup
Teemu Piippo <teemu@hecknology.net>
parents: 63
diff changeset
116 report = testsuite.check_model(model, test_suite)
f2dc17b830e0 cleanup
Teemu Piippo <teemu@hecknology.net>
parents: 63
diff changeset
117 print(format_report(report, model))

mercurial