ldcheck.py

Fri, 18 Sep 2020 20:26:19 +0300

author
Teemu Piippo <teemu@hecknology.net>
date
Fri, 18 Sep 2020 20:26:19 +0300
changeset 121
35b95d1806c9
parent 116
60cac583b5df
child 122
116a81996832
permissions
-rwxr-xr-x

remove old collinearity reference

3
1dc58f44d556 Can now write dat files, added direct color handling
Santeri Piippo
parents:
diff changeset
1 #!/usr/bin/env python3
8
303c51137cb2 Added ldconfig.ldr support
Santeri Piippo
parents: 7
diff changeset
2 from sys import version_info
303c51137cb2 Added ldconfig.ldr support
Santeri Piippo
parents: 7
diff changeset
3 if version_info < (3, 4):
303c51137cb2 Added ldconfig.ldr support
Santeri Piippo
parents: 7
diff changeset
4 raise RuntimeError('Python 3.4 or newer required')
303c51137cb2 Added ldconfig.ldr support
Santeri Piippo
parents: 7
diff changeset
5
100
62759e5c4554 add some basic versioning
Teemu Piippo <teemu@hecknology.net>
parents: 98
diff changeset
6 appname = 'ldcheck'
114
9ea2549e6171 changed version to 1.0.9999
Teemu Piippo <teemu@hecknology.net>
parents: 113
diff changeset
7 version = (1, 0, 9999)
100
62759e5c4554 add some basic versioning
Teemu Piippo <teemu@hecknology.net>
parents: 98
diff changeset
8 version_string = str.join('.', map(str, version))
62759e5c4554 add some basic versioning
Teemu Piippo <teemu@hecknology.net>
parents: 98
diff changeset
9
8
303c51137cb2 Added ldconfig.ldr support
Santeri Piippo
parents: 7
diff changeset
10 from colours import load_colours
7
0ab0d61ccee8 Smallest angles
Santeri Piippo
parents: 6
diff changeset
11 from geometry import *
8
303c51137cb2 Added ldconfig.ldr support
Santeri Piippo
parents: 7
diff changeset
12 from pathlib import Path
13
12d4ddc4bfd8 Got the skew test working
Santeri Piippo
parents: 9
diff changeset
13 import linetypes
47
4da025d0b283 added work on header check
Teemu Piippo <teemu@hecknology.net>
parents: 38
diff changeset
14 import header
54
0c686d10eb49 added tests for moved-to files and scaling in flat dimensions
Teemu Piippo <teemu@hecknology.net>
parents: 47
diff changeset
15 import parse
3
1dc58f44d556 Can now write dat files, added direct color handling
Santeri Piippo
parents:
diff changeset
16
9
fea8e9ae6f29 Added matrix code, moved library paths to ldcheck.cfg.
Santeri Piippo
parents: 8
diff changeset
17 from os.path import realpath
fea8e9ae6f29 Added matrix code, moved library paths to ldcheck.cfg.
Santeri Piippo
parents: 8
diff changeset
18 script_directory = Path(realpath(__file__)).parent
fea8e9ae6f29 Added matrix code, moved library paths to ldcheck.cfg.
Santeri Piippo
parents: 8
diff changeset
19
116
60cac583b5df fixed config not being properly loaded from script directory
Teemu Piippo <teemu@hecknology.net>
parents: 114
diff changeset
20 def load_config(filename = None):
60cac583b5df fixed config not being properly loaded from script directory
Teemu Piippo <teemu@hecknology.net>
parents: 114
diff changeset
21 if filename is None:
60cac583b5df fixed config not being properly loaded from script directory
Teemu Piippo <teemu@hecknology.net>
parents: 114
diff changeset
22 filename = script_directory / 'ldcheck.cfg'
9
fea8e9ae6f29 Added matrix code, moved library paths to ldcheck.cfg.
Santeri Piippo
parents: 8
diff changeset
23 from configobj import ConfigObj
fea8e9ae6f29 Added matrix code, moved library paths to ldcheck.cfg.
Santeri Piippo
parents: 8
diff changeset
24 from copy import deepcopy
116
60cac583b5df fixed config not being properly loaded from script directory
Teemu Piippo <teemu@hecknology.net>
parents: 114
diff changeset
25 config = ConfigObj(str(filename), encoding = 'UTF8')
9
fea8e9ae6f29 Added matrix code, moved library paths to ldcheck.cfg.
Santeri Piippo
parents: 8
diff changeset
26 read_config = deepcopy(config)
fea8e9ae6f29 Added matrix code, moved library paths to ldcheck.cfg.
Santeri Piippo
parents: 8
diff changeset
27 if 'libraries' not in config:
fea8e9ae6f29 Added matrix code, moved library paths to ldcheck.cfg.
Santeri Piippo
parents: 8
diff changeset
28 config['libraries'] = ['/path/to/ldraw']
fea8e9ae6f29 Added matrix code, moved library paths to ldcheck.cfg.
Santeri Piippo
parents: 8
diff changeset
29 if config != read_config:
fea8e9ae6f29 Added matrix code, moved library paths to ldcheck.cfg.
Santeri Piippo
parents: 8
diff changeset
30 config.write()
34
7ed2e831acd4 the program now checks that all ldraw paths are reachable
Teemu Piippo <teemu@hecknology.net>
parents: 32
diff changeset
31 check_library_paths(config)
9
fea8e9ae6f29 Added matrix code, moved library paths to ldcheck.cfg.
Santeri Piippo
parents: 8
diff changeset
32 return config
fea8e9ae6f29 Added matrix code, moved library paths to ldcheck.cfg.
Santeri Piippo
parents: 8
diff changeset
33
fea8e9ae6f29 Added matrix code, moved library paths to ldcheck.cfg.
Santeri Piippo
parents: 8
diff changeset
34 def library_paths(config):
fea8e9ae6f29 Added matrix code, moved library paths to ldcheck.cfg.
Santeri Piippo
parents: 8
diff changeset
35 for library_path_string in config['libraries']:
fea8e9ae6f29 Added matrix code, moved library paths to ldcheck.cfg.
Santeri Piippo
parents: 8
diff changeset
36 yield Path(library_path_string).expanduser()
fea8e9ae6f29 Added matrix code, moved library paths to ldcheck.cfg.
Santeri Piippo
parents: 8
diff changeset
37
34
7ed2e831acd4 the program now checks that all ldraw paths are reachable
Teemu Piippo <teemu@hecknology.net>
parents: 32
diff changeset
38 def check_library_paths(config):
7ed2e831acd4 the program now checks that all ldraw paths are reachable
Teemu Piippo <teemu@hecknology.net>
parents: 32
diff changeset
39 from sys import exit
7ed2e831acd4 the program now checks that all ldraw paths are reachable
Teemu Piippo <teemu@hecknology.net>
parents: 32
diff changeset
40 problems = False
36
2753aad79678 now checks that paths are specified
Teemu Piippo <teemu@hecknology.net>
parents: 34
diff changeset
41 have_paths = False
34
7ed2e831acd4 the program now checks that all ldraw paths are reachable
Teemu Piippo <teemu@hecknology.net>
parents: 32
diff changeset
42 for library_path in library_paths(config):
36
2753aad79678 now checks that paths are specified
Teemu Piippo <teemu@hecknology.net>
parents: 34
diff changeset
43 have_paths = True
34
7ed2e831acd4 the program now checks that all ldraw paths are reachable
Teemu Piippo <teemu@hecknology.net>
parents: 32
diff changeset
44 if not library_path.exists():
7ed2e831acd4 the program now checks that all ldraw paths are reachable
Teemu Piippo <teemu@hecknology.net>
parents: 32
diff changeset
45 problems = True
7ed2e831acd4 the program now checks that all ldraw paths are reachable
Teemu Piippo <teemu@hecknology.net>
parents: 32
diff changeset
46 print(str.format(
7ed2e831acd4 the program now checks that all ldraw paths are reachable
Teemu Piippo <teemu@hecknology.net>
parents: 32
diff changeset
47 'Library path {} does not exist',
7ed2e831acd4 the program now checks that all ldraw paths are reachable
Teemu Piippo <teemu@hecknology.net>
parents: 32
diff changeset
48 library_path,
7ed2e831acd4 the program now checks that all ldraw paths are reachable
Teemu Piippo <teemu@hecknology.net>
parents: 32
diff changeset
49 ))
7ed2e831acd4 the program now checks that all ldraw paths are reachable
Teemu Piippo <teemu@hecknology.net>
parents: 32
diff changeset
50 elif not library_path.exists():
7ed2e831acd4 the program now checks that all ldraw paths are reachable
Teemu Piippo <teemu@hecknology.net>
parents: 32
diff changeset
51 problems = True
7ed2e831acd4 the program now checks that all ldraw paths are reachable
Teemu Piippo <teemu@hecknology.net>
parents: 32
diff changeset
52 print(str.format(
7ed2e831acd4 the program now checks that all ldraw paths are reachable
Teemu Piippo <teemu@hecknology.net>
parents: 32
diff changeset
53 'Library path {} is not a directory',
7ed2e831acd4 the program now checks that all ldraw paths are reachable
Teemu Piippo <teemu@hecknology.net>
parents: 32
diff changeset
54 library_path,
7ed2e831acd4 the program now checks that all ldraw paths are reachable
Teemu Piippo <teemu@hecknology.net>
parents: 32
diff changeset
55 ))
36
2753aad79678 now checks that paths are specified
Teemu Piippo <teemu@hecknology.net>
parents: 34
diff changeset
56 if not have_paths:
2753aad79678 now checks that paths are specified
Teemu Piippo <teemu@hecknology.net>
parents: 34
diff changeset
57 print('No LDraw path specified')
2753aad79678 now checks that paths are specified
Teemu Piippo <teemu@hecknology.net>
parents: 34
diff changeset
58 problems = True
34
7ed2e831acd4 the program now checks that all ldraw paths are reachable
Teemu Piippo <teemu@hecknology.net>
parents: 32
diff changeset
59 if problems:
7ed2e831acd4 the program now checks that all ldraw paths are reachable
Teemu Piippo <teemu@hecknology.net>
parents: 32
diff changeset
60 print('Please fix ldcheck.cfg')
7ed2e831acd4 the program now checks that all ldraw paths are reachable
Teemu Piippo <teemu@hecknology.net>
parents: 32
diff changeset
61 exit(1)
7ed2e831acd4 the program now checks that all ldraw paths are reachable
Teemu Piippo <teemu@hecknology.net>
parents: 32
diff changeset
62
9
fea8e9ae6f29 Added matrix code, moved library paths to ldcheck.cfg.
Santeri Piippo
parents: 8
diff changeset
63 def find_ldconfig_ldr_paths(config):
fea8e9ae6f29 Added matrix code, moved library paths to ldcheck.cfg.
Santeri Piippo
parents: 8
diff changeset
64 for library_path in library_paths(config):
17
327da5d00360 Added code to run the test suite.
Santeri Piippo
parents: 13
diff changeset
65 yield from [
327da5d00360 Added code to run the test suite.
Santeri Piippo
parents: 13
diff changeset
66 library_path / path
327da5d00360 Added code to run the test suite.
Santeri Piippo
parents: 13
diff changeset
67 for path in ['LDConfig.ldr', 'ldconfig.ldr']
327da5d00360 Added code to run the test suite.
Santeri Piippo
parents: 13
diff changeset
68 if (library_path / path).is_file()
8
303c51137cb2 Added ldconfig.ldr support
Santeri Piippo
parents: 7
diff changeset
69 ]
303c51137cb2 Added ldconfig.ldr support
Santeri Piippo
parents: 7
diff changeset
70
26
7c263b864371 Added command line option to list all checks.
Santeri Piippo
parents: 21
diff changeset
71 import argparse
62
f0a6bf48b05e Problem reporting revamp, program is now aware of its problem types
Teemu Piippo <teemu@hecknology.net>
parents: 54
diff changeset
72
81
e65d82501a38 added better help entries to command line parameters
Teemu Piippo <teemu@hecknology.net>
parents: 77
diff changeset
73 class ListProblemsAction(argparse.Action):
26
7c263b864371 Added command line option to list all checks.
Santeri Piippo
parents: 21
diff changeset
74 def __init__(self, option_strings, dest, nargs = None, **kwargs):
7c263b864371 Added command line option to list all checks.
Santeri Piippo
parents: 21
diff changeset
75 super().__init__(option_strings, dest, nargs = 0, **kwargs)
7c263b864371 Added command line option to list all checks.
Santeri Piippo
parents: 21
diff changeset
76 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
77 import testsuite
26
7c263b864371 Added command line option to list all checks.
Santeri Piippo
parents: 21
diff changeset
78 from sys import exit
7c263b864371 Added command line option to list all checks.
Santeri Piippo
parents: 21
diff changeset
79 from re import sub
63
8949af6a4279 added the list of issues onto the web frontend
Teemu Piippo <teemu@hecknology.net>
parents: 62
diff changeset
80 test_suite = testsuite.load_tests()
8949af6a4279 added the list of issues onto the web frontend
Teemu Piippo <teemu@hecknology.net>
parents: 62
diff changeset
81 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
82 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
83 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
84 severity = warning_type.severity,
63
8949af6a4279 added the list of issues onto the web frontend
Teemu Piippo <teemu@hecknology.net>
parents: 62
diff changeset
85 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
86 ))
26
7c263b864371 Added command line option to list all checks.
Santeri Piippo
parents: 21
diff changeset
87 exit(0)
7c263b864371 Added command line option to list all checks.
Santeri Piippo
parents: 21
diff changeset
88
98
f9d4e59392f7 made colors toggleable
Teemu Piippo <teemu@hecknology.net>
parents: 94
diff changeset
89 def format_report(report, model, test_suite, *, use_colors = True):
f9d4e59392f7 made colors toggleable
Teemu Piippo <teemu@hecknology.net>
parents: 94
diff changeset
90 from testsuite import problem_text
f9d4e59392f7 made colors toggleable
Teemu Piippo <teemu@hecknology.net>
parents: 94
diff changeset
91 messages = []
f9d4e59392f7 made colors toggleable
Teemu Piippo <teemu@hecknology.net>
parents: 94
diff changeset
92 for problem in report['problems']:
f9d4e59392f7 made colors toggleable
Teemu Piippo <teemu@hecknology.net>
parents: 94
diff changeset
93 text_colour = ''
f9d4e59392f7 made colors toggleable
Teemu Piippo <teemu@hecknology.net>
parents: 94
diff changeset
94 if use_colors:
f9d4e59392f7 made colors toggleable
Teemu Piippo <teemu@hecknology.net>
parents: 94
diff changeset
95 if problem.severity == 'hold':
f9d4e59392f7 made colors toggleable
Teemu Piippo <teemu@hecknology.net>
parents: 94
diff changeset
96 text_colour = colorama.Fore.LIGHTRED_EX
f9d4e59392f7 made colors toggleable
Teemu Piippo <teemu@hecknology.net>
parents: 94
diff changeset
97 elif problem.severity == 'warning':
f9d4e59392f7 made colors toggleable
Teemu Piippo <teemu@hecknology.net>
parents: 94
diff changeset
98 text_colour = colorama.Fore.LIGHTBLUE_EX
f9d4e59392f7 made colors toggleable
Teemu Piippo <teemu@hecknology.net>
parents: 94
diff changeset
99 ldraw_code = model.body[problem.body_index].textual_representation()
f9d4e59392f7 made colors toggleable
Teemu Piippo <teemu@hecknology.net>
parents: 94
diff changeset
100 message = str.format(
f9d4e59392f7 made colors toggleable
Teemu Piippo <teemu@hecknology.net>
parents: 94
diff changeset
101 '{text_colour}{model_name}:{line_number}: {problem_type}: {message}'
f9d4e59392f7 made colors toggleable
Teemu Piippo <teemu@hecknology.net>
parents: 94
diff changeset
102 '{colour_reset}\n\t{ldraw_code}',
f9d4e59392f7 made colors toggleable
Teemu Piippo <teemu@hecknology.net>
parents: 94
diff changeset
103 text_colour = text_colour,
f9d4e59392f7 made colors toggleable
Teemu Piippo <teemu@hecknology.net>
parents: 94
diff changeset
104 model_name = model.name,
f9d4e59392f7 made colors toggleable
Teemu Piippo <teemu@hecknology.net>
parents: 94
diff changeset
105 line_number = problem.line_number,
f9d4e59392f7 made colors toggleable
Teemu Piippo <teemu@hecknology.net>
parents: 94
diff changeset
106 problem_type = problem.severity,
f9d4e59392f7 made colors toggleable
Teemu Piippo <teemu@hecknology.net>
parents: 94
diff changeset
107 message = problem_text(problem, test_suite),
f9d4e59392f7 made colors toggleable
Teemu Piippo <teemu@hecknology.net>
parents: 94
diff changeset
108 colour_reset = use_colors and colorama.Fore.RESET or '',
f9d4e59392f7 made colors toggleable
Teemu Piippo <teemu@hecknology.net>
parents: 94
diff changeset
109 ldraw_code = ldraw_code,
f9d4e59392f7 made colors toggleable
Teemu Piippo <teemu@hecknology.net>
parents: 94
diff changeset
110 )
f9d4e59392f7 made colors toggleable
Teemu Piippo <teemu@hecknology.net>
parents: 94
diff changeset
111 messages.append(message)
f9d4e59392f7 made colors toggleable
Teemu Piippo <teemu@hecknology.net>
parents: 94
diff changeset
112 return '\n'.join(messages)
f9d4e59392f7 made colors toggleable
Teemu Piippo <teemu@hecknology.net>
parents: 94
diff changeset
113
3
1dc58f44d556 Can now write dat files, added direct color handling
Santeri Piippo
parents:
diff changeset
114 if __name__ == '__main__':
109
b627f8963a84 handle file not found more cleanly
Teemu Piippo <teemu@hecknology.net>
parents: 100
diff changeset
115 from sys import argv, stderr, exit
26
7c263b864371 Added command line option to list all checks.
Santeri Piippo
parents: 21
diff changeset
116 parser = argparse.ArgumentParser()
7c263b864371 Added command line option to list all checks.
Santeri Piippo
parents: 21
diff changeset
117 parser.add_argument('filename')
7c263b864371 Added command line option to list all checks.
Santeri Piippo
parents: 21
diff changeset
118 parser.add_argument('--list',
81
e65d82501a38 added better help entries to command line parameters
Teemu Piippo <teemu@hecknology.net>
parents: 77
diff changeset
119 action = ListProblemsAction,
e65d82501a38 added better help entries to command line parameters
Teemu Piippo <teemu@hecknology.net>
parents: 77
diff changeset
120 help = 'lists all possible problem types and exit',
e65d82501a38 added better help entries to command line parameters
Teemu Piippo <teemu@hecknology.net>
parents: 77
diff changeset
121 )
e65d82501a38 added better help entries to command line parameters
Teemu Piippo <teemu@hecknology.net>
parents: 77
diff changeset
122 parser.add_argument('--dump',
e65d82501a38 added better help entries to command line parameters
Teemu Piippo <teemu@hecknology.net>
parents: 77
diff changeset
123 action = 'store_true',
e65d82501a38 added better help entries to command line parameters
Teemu Piippo <teemu@hecknology.net>
parents: 77
diff changeset
124 help = 'dumps the internal parsed structure of the part file',
26
7c263b864371 Added command line option to list all checks.
Santeri Piippo
parents: 21
diff changeset
125 )
81
e65d82501a38 added better help entries to command line parameters
Teemu Piippo <teemu@hecknology.net>
parents: 77
diff changeset
126 parser.add_argument('--rebuild',
e65d82501a38 added better help entries to command line parameters
Teemu Piippo <teemu@hecknology.net>
parents: 77
diff changeset
127 action = 'store_true',
e65d82501a38 added better help entries to command line parameters
Teemu Piippo <teemu@hecknology.net>
parents: 77
diff changeset
128 help = 'parses the part file and prints it back out, used for '
e65d82501a38 added better help entries to command line parameters
Teemu Piippo <teemu@hecknology.net>
parents: 77
diff changeset
129 'testing whether the program interprets part files correctly',
e65d82501a38 added better help entries to command line parameters
Teemu Piippo <teemu@hecknology.net>
parents: 77
diff changeset
130 )
92
b8d72909d593 improved the mirrored stud check to catch cases where a subfile that contains studs is mirrored
Teemu Piippo <teemu@hecknology.net>
parents: 85
diff changeset
131 parser.add_argument('--subfile',
81
e65d82501a38 added better help entries to command line parameters
Teemu Piippo <teemu@hecknology.net>
parents: 77
diff changeset
132 action = 'store_true',
92
b8d72909d593 improved the mirrored stud check to catch cases where a subfile that contains studs is mirrored
Teemu Piippo <teemu@hecknology.net>
parents: 85
diff changeset
133 help = 'finds a subfile by name and prints out information about it'
85
4438502fd3e0 fixed the use of moved-to-files check not working if there were non-alphanumerics in the filename
Teemu Piippo <teemu@hecknology.net>
parents: 81
diff changeset
134 )
98
f9d4e59392f7 made colors toggleable
Teemu Piippo <teemu@hecknology.net>
parents: 94
diff changeset
135 parser.add_argument('--color',
f9d4e59392f7 made colors toggleable
Teemu Piippo <teemu@hecknology.net>
parents: 94
diff changeset
136 action = 'store_true',
f9d4e59392f7 made colors toggleable
Teemu Piippo <teemu@hecknology.net>
parents: 94
diff changeset
137 help = 'use colors'
f9d4e59392f7 made colors toggleable
Teemu Piippo <teemu@hecknology.net>
parents: 94
diff changeset
138 )
100
62759e5c4554 add some basic versioning
Teemu Piippo <teemu@hecknology.net>
parents: 98
diff changeset
139 parser.add_argument('-v', '--version',
62759e5c4554 add some basic versioning
Teemu Piippo <teemu@hecknology.net>
parents: 98
diff changeset
140 action = 'version',
62759e5c4554 add some basic versioning
Teemu Piippo <teemu@hecknology.net>
parents: 98
diff changeset
141 version = str.format('{appname} {version}',
62759e5c4554 add some basic versioning
Teemu Piippo <teemu@hecknology.net>
parents: 98
diff changeset
142 appname = appname,
62759e5c4554 add some basic versioning
Teemu Piippo <teemu@hecknology.net>
parents: 98
diff changeset
143 version = version_string,
62759e5c4554 add some basic versioning
Teemu Piippo <teemu@hecknology.net>
parents: 98
diff changeset
144 ),
62759e5c4554 add some basic versioning
Teemu Piippo <teemu@hecknology.net>
parents: 98
diff changeset
145 )
26
7c263b864371 Added command line option to list all checks.
Santeri Piippo
parents: 21
diff changeset
146 args = parser.parse_args()
116
60cac583b5df fixed config not being properly loaded from script directory
Teemu Piippo <teemu@hecknology.net>
parents: 114
diff changeset
147 config = load_config()
9
fea8e9ae6f29 Added matrix code, moved library paths to ldcheck.cfg.
Santeri Piippo
parents: 8
diff changeset
148 for ldconfig_ldr_path in find_ldconfig_ldr_paths(config):
8
303c51137cb2 Added ldconfig.ldr support
Santeri Piippo
parents: 7
diff changeset
149 with ldconfig_ldr_path.open() as ldconfig_ldr:
303c51137cb2 Added ldconfig.ldr support
Santeri Piippo
parents: 7
diff changeset
150 load_colours(ldconfig_ldr)
98
f9d4e59392f7 made colors toggleable
Teemu Piippo <teemu@hecknology.net>
parents: 94
diff changeset
151 if args.color:
f9d4e59392f7 made colors toggleable
Teemu Piippo <teemu@hecknology.net>
parents: 94
diff changeset
152 try:
f9d4e59392f7 made colors toggleable
Teemu Piippo <teemu@hecknology.net>
parents: 94
diff changeset
153 import colorama
f9d4e59392f7 made colors toggleable
Teemu Piippo <teemu@hecknology.net>
parents: 94
diff changeset
154 colorama.init()
f9d4e59392f7 made colors toggleable
Teemu Piippo <teemu@hecknology.net>
parents: 94
diff changeset
155 except ImportError:
f9d4e59392f7 made colors toggleable
Teemu Piippo <teemu@hecknology.net>
parents: 94
diff changeset
156 print('Use of --color requires the colorama module, disabling colours', file = stderr)
f9d4e59392f7 made colors toggleable
Teemu Piippo <teemu@hecknology.net>
parents: 94
diff changeset
157 args.color = False
92
b8d72909d593 improved the mirrored stud check to catch cases where a subfile that contains studs is mirrored
Teemu Piippo <teemu@hecknology.net>
parents: 85
diff changeset
158 if args.subfile:
54
0c686d10eb49 added tests for moved-to files and scaling in flat dimensions
Teemu Piippo <teemu@hecknology.net>
parents: 47
diff changeset
159 import filecache
0c686d10eb49 added tests for moved-to files and scaling in flat dimensions
Teemu Piippo <teemu@hecknology.net>
parents: 47
diff changeset
160 cache = filecache.SubfileCache(
0c686d10eb49 added tests for moved-to files and scaling in flat dimensions
Teemu Piippo <teemu@hecknology.net>
parents: 47
diff changeset
161 ldraw_directories = config['libraries'],
26
7c263b864371 Added command line option to list all checks.
Santeri Piippo
parents: 21
diff changeset
162 )
54
0c686d10eb49 added tests for moved-to files and scaling in flat dimensions
Teemu Piippo <teemu@hecknology.net>
parents: 47
diff changeset
163 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
164 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
165 print(subfile.problem)
92
b8d72909d593 improved the mirrored stud check to catch cases where a subfile that contains studs is mirrored
Teemu Piippo <teemu@hecknology.net>
parents: 85
diff changeset
166 else:
b8d72909d593 improved the mirrored stud check to catch cases where a subfile that contains studs is mirrored
Teemu Piippo <teemu@hecknology.net>
parents: 85
diff changeset
167 print('Flat dimensions:', repr(subfile.flatness))
85
4438502fd3e0 fixed the use of moved-to-files check not working if there were non-alphanumerics in the filename
Teemu Piippo <teemu@hecknology.net>
parents: 81
diff changeset
168 print('Description:', repr(subfile.description))
92
b8d72909d593 improved the mirrored stud check to catch cases where a subfile that contains studs is mirrored
Teemu Piippo <teemu@hecknology.net>
parents: 85
diff changeset
169 print('Contains studs:', repr(subfile.has_studs))
54
0c686d10eb49 added tests for moved-to files and scaling in flat dimensions
Teemu Piippo <teemu@hecknology.net>
parents: 47
diff changeset
170 else:
109
b627f8963a84 handle file not found more cleanly
Teemu Piippo <teemu@hecknology.net>
parents: 100
diff changeset
171 try:
b627f8963a84 handle file not found more cleanly
Teemu Piippo <teemu@hecknology.net>
parents: 100
diff changeset
172 with open(args.filename, 'rb') as file:
b627f8963a84 handle file not found more cleanly
Teemu Piippo <teemu@hecknology.net>
parents: 100
diff changeset
173 from os.path import basename
b627f8963a84 handle file not found more cleanly
Teemu Piippo <teemu@hecknology.net>
parents: 100
diff changeset
174 model = parse.read_ldraw(
b627f8963a84 handle file not found more cleanly
Teemu Piippo <teemu@hecknology.net>
parents: 100
diff changeset
175 file,
b627f8963a84 handle file not found more cleanly
Teemu Piippo <teemu@hecknology.net>
parents: 100
diff changeset
176 name = basename(args.filename),
b627f8963a84 handle file not found more cleanly
Teemu Piippo <teemu@hecknology.net>
parents: 100
diff changeset
177 ldraw_directories = config['libraries'])
b627f8963a84 handle file not found more cleanly
Teemu Piippo <teemu@hecknology.net>
parents: 100
diff changeset
178 if args.dump:
b627f8963a84 handle file not found more cleanly
Teemu Piippo <teemu@hecknology.net>
parents: 100
diff changeset
179 print('header: ' + type(model.header).__name__)
b627f8963a84 handle file not found more cleanly
Teemu Piippo <teemu@hecknology.net>
parents: 100
diff changeset
180 for key in sorted(dir(model.header)):
b627f8963a84 handle file not found more cleanly
Teemu Piippo <teemu@hecknology.net>
parents: 100
diff changeset
181 if not key.startswith('__'):
b627f8963a84 handle file not found more cleanly
Teemu Piippo <teemu@hecknology.net>
parents: 100
diff changeset
182 print('\t' + key + ': ' + repr(getattr(model.header, key)))
b627f8963a84 handle file not found more cleanly
Teemu Piippo <teemu@hecknology.net>
parents: 100
diff changeset
183 for i, entry in enumerate(model.body):
b627f8963a84 handle file not found more cleanly
Teemu Piippo <teemu@hecknology.net>
parents: 100
diff changeset
184 if model.header.valid and i == model.header_size:
b627f8963a84 handle file not found more cleanly
Teemu Piippo <teemu@hecknology.net>
parents: 100
diff changeset
185 print('--------- End of header')
b627f8963a84 handle file not found more cleanly
Teemu Piippo <teemu@hecknology.net>
parents: 100
diff changeset
186 print(entry)
b627f8963a84 handle file not found more cleanly
Teemu Piippo <teemu@hecknology.net>
parents: 100
diff changeset
187 elif args.rebuild:
b627f8963a84 handle file not found more cleanly
Teemu Piippo <teemu@hecknology.net>
parents: 100
diff changeset
188 for entry in model.body:
b627f8963a84 handle file not found more cleanly
Teemu Piippo <teemu@hecknology.net>
parents: 100
diff changeset
189 print(entry.textual_representation(), end = '\r\n')
b627f8963a84 handle file not found more cleanly
Teemu Piippo <teemu@hecknology.net>
parents: 100
diff changeset
190 else:
b627f8963a84 handle file not found more cleanly
Teemu Piippo <teemu@hecknology.net>
parents: 100
diff changeset
191 from testsuite import load_tests, check_model
b627f8963a84 handle file not found more cleanly
Teemu Piippo <teemu@hecknology.net>
parents: 100
diff changeset
192 test_suite = load_tests()
b627f8963a84 handle file not found more cleanly
Teemu Piippo <teemu@hecknology.net>
parents: 100
diff changeset
193 report = check_model(model, test_suite)
b627f8963a84 handle file not found more cleanly
Teemu Piippo <teemu@hecknology.net>
parents: 100
diff changeset
194 print(format_report(
b627f8963a84 handle file not found more cleanly
Teemu Piippo <teemu@hecknology.net>
parents: 100
diff changeset
195 report,
b627f8963a84 handle file not found more cleanly
Teemu Piippo <teemu@hecknology.net>
parents: 100
diff changeset
196 model,
b627f8963a84 handle file not found more cleanly
Teemu Piippo <teemu@hecknology.net>
parents: 100
diff changeset
197 test_suite,
b627f8963a84 handle file not found more cleanly
Teemu Piippo <teemu@hecknology.net>
parents: 100
diff changeset
198 use_colors = args.color
b627f8963a84 handle file not found more cleanly
Teemu Piippo <teemu@hecknology.net>
parents: 100
diff changeset
199 ))
b627f8963a84 handle file not found more cleanly
Teemu Piippo <teemu@hecknology.net>
parents: 100
diff changeset
200 except FileNotFoundError:
b627f8963a84 handle file not found more cleanly
Teemu Piippo <teemu@hecknology.net>
parents: 100
diff changeset
201 print(str.format(
b627f8963a84 handle file not found more cleanly
Teemu Piippo <teemu@hecknology.net>
parents: 100
diff changeset
202 'no such file: {filename!r}',
b627f8963a84 handle file not found more cleanly
Teemu Piippo <teemu@hecknology.net>
parents: 100
diff changeset
203 filename = args.filename
b627f8963a84 handle file not found more cleanly
Teemu Piippo <teemu@hecknology.net>
parents: 100
diff changeset
204 ), file = stderr)
b627f8963a84 handle file not found more cleanly
Teemu Piippo <teemu@hecknology.net>
parents: 100
diff changeset
205 exit(1)

mercurial