ldcheck.py

Sat, 01 Jun 2019 10:47:58 +0300

author
Teemu Piippo <teemu@hecknology.net>
date
Sat, 01 Jun 2019 10:47:58 +0300
changeset 56
ed6d39c59e56
parent 54
0c686d10eb49
child 62
f0a6bf48b05e
permissions
-rwxr-xr-x

fixed BFC INVERTNEXT being interpreted as a header command

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
303c51137cb2 Added ldconfig.ldr support
Santeri Piippo
parents: 7
diff changeset
6 from colours import load_colours
7
0ab0d61ccee8 Smallest angles
Santeri Piippo
parents: 6
diff changeset
7 from geometry import *
8
303c51137cb2 Added ldconfig.ldr support
Santeri Piippo
parents: 7
diff changeset
8 from pathlib import Path
13
12d4ddc4bfd8 Got the skew test working
Santeri Piippo
parents: 9
diff changeset
9 import linetypes
47
4da025d0b283 added work on header check
Teemu Piippo <teemu@hecknology.net>
parents: 38
diff changeset
10 import header
54
0c686d10eb49 added tests for moved-to files and scaling in flat dimensions
Teemu Piippo <teemu@hecknology.net>
parents: 47
diff changeset
11 import parse
3
1dc58f44d556 Can now write dat files, added direct color handling
Santeri Piippo
parents:
diff changeset
12
9
fea8e9ae6f29 Added matrix code, moved library paths to ldcheck.cfg.
Santeri Piippo
parents: 8
diff changeset
13 from os.path import realpath
fea8e9ae6f29 Added matrix code, moved library paths to ldcheck.cfg.
Santeri Piippo
parents: 8
diff changeset
14 script_directory = Path(realpath(__file__)).parent
fea8e9ae6f29 Added matrix code, moved library paths to ldcheck.cfg.
Santeri Piippo
parents: 8
diff changeset
15
fea8e9ae6f29 Added matrix code, moved library paths to ldcheck.cfg.
Santeri Piippo
parents: 8
diff changeset
16 def load_config(filename):
fea8e9ae6f29 Added matrix code, moved library paths to ldcheck.cfg.
Santeri Piippo
parents: 8
diff changeset
17 from configobj import ConfigObj
fea8e9ae6f29 Added matrix code, moved library paths to ldcheck.cfg.
Santeri Piippo
parents: 8
diff changeset
18 from copy import deepcopy
fea8e9ae6f29 Added matrix code, moved library paths to ldcheck.cfg.
Santeri Piippo
parents: 8
diff changeset
19 config = ConfigObj(filename, encoding = 'UTF8')
fea8e9ae6f29 Added matrix code, moved library paths to ldcheck.cfg.
Santeri Piippo
parents: 8
diff changeset
20 read_config = deepcopy(config)
fea8e9ae6f29 Added matrix code, moved library paths to ldcheck.cfg.
Santeri Piippo
parents: 8
diff changeset
21 if 'libraries' not in config:
fea8e9ae6f29 Added matrix code, moved library paths to ldcheck.cfg.
Santeri Piippo
parents: 8
diff changeset
22 config['libraries'] = ['/path/to/ldraw']
fea8e9ae6f29 Added matrix code, moved library paths to ldcheck.cfg.
Santeri Piippo
parents: 8
diff changeset
23 if config != read_config:
fea8e9ae6f29 Added matrix code, moved library paths to ldcheck.cfg.
Santeri Piippo
parents: 8
diff changeset
24 config.write()
34
7ed2e831acd4 the program now checks that all ldraw paths are reachable
Teemu Piippo <teemu@hecknology.net>
parents: 32
diff changeset
25 check_library_paths(config)
9
fea8e9ae6f29 Added matrix code, moved library paths to ldcheck.cfg.
Santeri Piippo
parents: 8
diff changeset
26 return config
fea8e9ae6f29 Added matrix code, moved library paths to ldcheck.cfg.
Santeri Piippo
parents: 8
diff changeset
27
fea8e9ae6f29 Added matrix code, moved library paths to ldcheck.cfg.
Santeri Piippo
parents: 8
diff changeset
28 def library_paths(config):
fea8e9ae6f29 Added matrix code, moved library paths to ldcheck.cfg.
Santeri Piippo
parents: 8
diff changeset
29 for library_path_string in config['libraries']:
fea8e9ae6f29 Added matrix code, moved library paths to ldcheck.cfg.
Santeri Piippo
parents: 8
diff changeset
30 yield Path(library_path_string).expanduser()
fea8e9ae6f29 Added matrix code, moved library paths to ldcheck.cfg.
Santeri Piippo
parents: 8
diff changeset
31
34
7ed2e831acd4 the program now checks that all ldraw paths are reachable
Teemu Piippo <teemu@hecknology.net>
parents: 32
diff changeset
32 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
33 from sys import exit
7ed2e831acd4 the program now checks that all ldraw paths are reachable
Teemu Piippo <teemu@hecknology.net>
parents: 32
diff changeset
34 problems = False
36
2753aad79678 now checks that paths are specified
Teemu Piippo <teemu@hecknology.net>
parents: 34
diff changeset
35 have_paths = False
34
7ed2e831acd4 the program now checks that all ldraw paths are reachable
Teemu Piippo <teemu@hecknology.net>
parents: 32
diff changeset
36 for library_path in library_paths(config):
36
2753aad79678 now checks that paths are specified
Teemu Piippo <teemu@hecknology.net>
parents: 34
diff changeset
37 have_paths = True
34
7ed2e831acd4 the program now checks that all ldraw paths are reachable
Teemu Piippo <teemu@hecknology.net>
parents: 32
diff changeset
38 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
39 problems = True
7ed2e831acd4 the program now checks that all ldraw paths are reachable
Teemu Piippo <teemu@hecknology.net>
parents: 32
diff changeset
40 print(str.format(
7ed2e831acd4 the program now checks that all ldraw paths are reachable
Teemu Piippo <teemu@hecknology.net>
parents: 32
diff changeset
41 '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
42 library_path,
7ed2e831acd4 the program now checks that all ldraw paths are reachable
Teemu Piippo <teemu@hecknology.net>
parents: 32
diff changeset
43 ))
7ed2e831acd4 the program now checks that all ldraw paths are reachable
Teemu Piippo <teemu@hecknology.net>
parents: 32
diff changeset
44 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
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 {} is not a directory',
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 ))
36
2753aad79678 now checks that paths are specified
Teemu Piippo <teemu@hecknology.net>
parents: 34
diff changeset
50 if not have_paths:
2753aad79678 now checks that paths are specified
Teemu Piippo <teemu@hecknology.net>
parents: 34
diff changeset
51 print('No LDraw path specified')
2753aad79678 now checks that paths are specified
Teemu Piippo <teemu@hecknology.net>
parents: 34
diff changeset
52 problems = True
34
7ed2e831acd4 the program now checks that all ldraw paths are reachable
Teemu Piippo <teemu@hecknology.net>
parents: 32
diff changeset
53 if problems:
7ed2e831acd4 the program now checks that all ldraw paths are reachable
Teemu Piippo <teemu@hecknology.net>
parents: 32
diff changeset
54 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
55 exit(1)
7ed2e831acd4 the program now checks that all ldraw paths are reachable
Teemu Piippo <teemu@hecknology.net>
parents: 32
diff changeset
56
9
fea8e9ae6f29 Added matrix code, moved library paths to ldcheck.cfg.
Santeri Piippo
parents: 8
diff changeset
57 def find_ldconfig_ldr_paths(config):
fea8e9ae6f29 Added matrix code, moved library paths to ldcheck.cfg.
Santeri Piippo
parents: 8
diff changeset
58 for library_path in library_paths(config):
17
327da5d00360 Added code to run the test suite.
Santeri Piippo
parents: 13
diff changeset
59 yield from [
327da5d00360 Added code to run the test suite.
Santeri Piippo
parents: 13
diff changeset
60 library_path / path
327da5d00360 Added code to run the test suite.
Santeri Piippo
parents: 13
diff changeset
61 for path in ['LDConfig.ldr', 'ldconfig.ldr']
327da5d00360 Added code to run the test suite.
Santeri Piippo
parents: 13
diff changeset
62 if (library_path / path).is_file()
8
303c51137cb2 Added ldconfig.ldr support
Santeri Piippo
parents: 7
diff changeset
63 ]
303c51137cb2 Added ldconfig.ldr support
Santeri Piippo
parents: 7
diff changeset
64
26
7c263b864371 Added command line option to list all checks.
Santeri Piippo
parents: 21
diff changeset
65 import argparse
7c263b864371 Added command line option to list all checks.
Santeri Piippo
parents: 21
diff changeset
66 class ListTestSuiteAction(argparse.Action):
7c263b864371 Added command line option to list all checks.
Santeri Piippo
parents: 21
diff changeset
67 def __init__(self, option_strings, dest, nargs = None, **kwargs):
7c263b864371 Added command line option to list all checks.
Santeri Piippo
parents: 21
diff changeset
68 super().__init__(option_strings, dest, nargs = 0, **kwargs)
7c263b864371 Added command line option to list all checks.
Santeri Piippo
parents: 21
diff changeset
69 def __call__(self, *args, **kwargs):
7c263b864371 Added command line option to list all checks.
Santeri Piippo
parents: 21
diff changeset
70 from testsuite import load_tests
7c263b864371 Added command line option to list all checks.
Santeri Piippo
parents: 21
diff changeset
71 from sys import exit
7c263b864371 Added command line option to list all checks.
Santeri Piippo
parents: 21
diff changeset
72 from re import sub
7c263b864371 Added command line option to list all checks.
Santeri Piippo
parents: 21
diff changeset
73 test_suite = load_tests()
7c263b864371 Added command line option to list all checks.
Santeri Piippo
parents: 21
diff changeset
74 for test_name in sorted(test_suite['tests'].keys()):
7c263b864371 Added command line option to list all checks.
Santeri Piippo
parents: 21
diff changeset
75 test_function = test_suite['tests'][test_name]
7c263b864371 Added command line option to list all checks.
Santeri Piippo
parents: 21
diff changeset
76 help = sub(r'\s+', ' ', test_function.__doc__ or '').strip()
7c263b864371 Added command line option to list all checks.
Santeri Piippo
parents: 21
diff changeset
77 print(test_name + ': ' + help)
7c263b864371 Added command line option to list all checks.
Santeri Piippo
parents: 21
diff changeset
78 exit(0)
7c263b864371 Added command line option to list all checks.
Santeri Piippo
parents: 21
diff changeset
79
3
1dc58f44d556 Can now write dat files, added direct color handling
Santeri Piippo
parents:
diff changeset
80 if __name__ == '__main__':
1dc58f44d556 Can now write dat files, added direct color handling
Santeri Piippo
parents:
diff changeset
81 from sys import argv
26
7c263b864371 Added command line option to list all checks.
Santeri Piippo
parents: 21
diff changeset
82 parser = argparse.ArgumentParser()
7c263b864371 Added command line option to list all checks.
Santeri Piippo
parents: 21
diff changeset
83 parser.add_argument('filename')
7c263b864371 Added command line option to list all checks.
Santeri Piippo
parents: 21
diff changeset
84 parser.add_argument('--list',
7c263b864371 Added command line option to list all checks.
Santeri Piippo
parents: 21
diff changeset
85 action = ListTestSuiteAction,
7c263b864371 Added command line option to list all checks.
Santeri Piippo
parents: 21
diff changeset
86 help = 'Lists all possible checks and exit',
7c263b864371 Added command line option to list all checks.
Santeri Piippo
parents: 21
diff changeset
87 )
38
66c9591b733d added proper handling of syntax errors
Teemu Piippo <teemu@hecknology.net>
parents: 36
diff changeset
88 parser.add_argument('--dump-structure', action = 'store_true')
66c9591b733d added proper handling of syntax errors
Teemu Piippo <teemu@hecknology.net>
parents: 36
diff changeset
89 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
90 parser.add_argument('--flatness', action = 'store_true')
26
7c263b864371 Added command line option to list all checks.
Santeri Piippo
parents: 21
diff changeset
91 args = parser.parse_args()
9
fea8e9ae6f29 Added matrix code, moved library paths to ldcheck.cfg.
Santeri Piippo
parents: 8
diff changeset
92 config = load_config('ldcheck.cfg')
fea8e9ae6f29 Added matrix code, moved library paths to ldcheck.cfg.
Santeri Piippo
parents: 8
diff changeset
93 for ldconfig_ldr_path in find_ldconfig_ldr_paths(config):
8
303c51137cb2 Added ldconfig.ldr support
Santeri Piippo
parents: 7
diff changeset
94 with ldconfig_ldr_path.open() as ldconfig_ldr:
303c51137cb2 Added ldconfig.ldr support
Santeri Piippo
parents: 7
diff changeset
95 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
96 if args.flatness:
0c686d10eb49 added tests for moved-to files and scaling in flat dimensions
Teemu Piippo <teemu@hecknology.net>
parents: 47
diff changeset
97 import filecache
0c686d10eb49 added tests for moved-to files and scaling in flat dimensions
Teemu Piippo <teemu@hecknology.net>
parents: 47
diff changeset
98 cache = filecache.SubfileCache(
0c686d10eb49 added tests for moved-to files and scaling in flat dimensions
Teemu Piippo <teemu@hecknology.net>
parents: 47
diff changeset
99 ldraw_directories = config['libraries'],
26
7c263b864371 Added command line option to list all checks.
Santeri Piippo
parents: 21
diff changeset
100 )
54
0c686d10eb49 added tests for moved-to files and scaling in flat dimensions
Teemu Piippo <teemu@hecknology.net>
parents: 47
diff changeset
101 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
102 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
103 print(subfile.problem)
38
66c9591b733d added proper handling of syntax errors
Teemu Piippo <teemu@hecknology.net>
parents: 36
diff changeset
104 else:
54
0c686d10eb49 added tests for moved-to files and scaling in flat dimensions
Teemu Piippo <teemu@hecknology.net>
parents: 47
diff changeset
105 if subfile.flatness:
0c686d10eb49 added tests for moved-to files and scaling in flat dimensions
Teemu Piippo <teemu@hecknology.net>
parents: 47
diff changeset
106 print(str.format(
0c686d10eb49 added tests for moved-to files and scaling in flat dimensions
Teemu Piippo <teemu@hecknology.net>
parents: 47
diff changeset
107 'Flatness: {}',
0c686d10eb49 added tests for moved-to files and scaling in flat dimensions
Teemu Piippo <teemu@hecknology.net>
parents: 47
diff changeset
108 ', '.join(subfile.flatness),
0c686d10eb49 added tests for moved-to files and scaling in flat dimensions
Teemu Piippo <teemu@hecknology.net>
parents: 47
diff changeset
109 ))
0c686d10eb49 added tests for moved-to files and scaling in flat dimensions
Teemu Piippo <teemu@hecknology.net>
parents: 47
diff changeset
110 else:
0c686d10eb49 added tests for moved-to files and scaling in flat dimensions
Teemu Piippo <teemu@hecknology.net>
parents: 47
diff changeset
111 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
112 else:
0c686d10eb49 added tests for moved-to files and scaling in flat dimensions
Teemu Piippo <teemu@hecknology.net>
parents: 47
diff changeset
113 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
114 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
115 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
116 file,
0c686d10eb49 added tests for moved-to files and scaling in flat dimensions
Teemu Piippo <teemu@hecknology.net>
parents: 47
diff changeset
117 name = basename(args.filename),
0c686d10eb49 added tests for moved-to files and scaling in flat dimensions
Teemu Piippo <teemu@hecknology.net>
parents: 47
diff changeset
118 ldraw_directories = config['libraries'])
0c686d10eb49 added tests for moved-to files and scaling in flat dimensions
Teemu Piippo <teemu@hecknology.net>
parents: 47
diff changeset
119 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
120 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
121 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
122 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
123 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
124 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
125 print(entry)
0c686d10eb49 added tests for moved-to files and scaling in flat dimensions
Teemu Piippo <teemu@hecknology.net>
parents: 47
diff changeset
126 elif args.rebuild:
0c686d10eb49 added tests for moved-to files and scaling in flat dimensions
Teemu Piippo <teemu@hecknology.net>
parents: 47
diff changeset
127 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
128 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
129 else:
0c686d10eb49 added tests for moved-to files and scaling in flat dimensions
Teemu Piippo <teemu@hecknology.net>
parents: 47
diff changeset
130 from testsuite import load_tests, check_model, format_report
0c686d10eb49 added tests for moved-to files and scaling in flat dimensions
Teemu Piippo <teemu@hecknology.net>
parents: 47
diff changeset
131 test_suite = load_tests()
0c686d10eb49 added tests for moved-to files and scaling in flat dimensions
Teemu Piippo <teemu@hecknology.net>
parents: 47
diff changeset
132 report = check_model(model, test_suite)
0c686d10eb49 added tests for moved-to files and scaling in flat dimensions
Teemu Piippo <teemu@hecknology.net>
parents: 47
diff changeset
133 print(format_report(report, model, test_suite))

mercurial