ldcheck.py

Fri, 24 May 2019 14:18:31 +0200

author
Teemu Piippo <teemu@hecknology.net>
date
Fri, 24 May 2019 14:18:31 +0200
changeset 35
865cd526e8b6
parent 34
7ed2e831acd4
child 36
2753aad79678
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
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
3
1dc58f44d556 Can now write dat files, added direct color handling
Santeri Piippo
parents:
diff changeset
6 from parse import parse_ldraw_code
8
303c51137cb2 Added ldconfig.ldr support
Santeri Piippo
parents: 7
diff changeset
7 from colours import load_colours
7
0ab0d61ccee8 Smallest angles
Santeri Piippo
parents: 6
diff changeset
8 from geometry import *
8
303c51137cb2 Added ldconfig.ldr support
Santeri Piippo
parents: 7
diff changeset
9 from pathlib import Path
13
12d4ddc4bfd8 Got the skew test working
Santeri Piippo
parents: 9
diff changeset
10 import linetypes
3
1dc58f44d556 Can now write dat files, added direct color handling
Santeri Piippo
parents:
diff changeset
11
9
fea8e9ae6f29 Added matrix code, moved library paths to ldcheck.cfg.
Santeri Piippo
parents: 8
diff changeset
12 from os.path import realpath
fea8e9ae6f29 Added matrix code, moved library paths to ldcheck.cfg.
Santeri Piippo
parents: 8
diff changeset
13 script_directory = Path(realpath(__file__)).parent
fea8e9ae6f29 Added matrix code, moved library paths to ldcheck.cfg.
Santeri Piippo
parents: 8
diff changeset
14
fea8e9ae6f29 Added matrix code, moved library paths to ldcheck.cfg.
Santeri Piippo
parents: 8
diff changeset
15 def load_config(filename):
fea8e9ae6f29 Added matrix code, moved library paths to ldcheck.cfg.
Santeri Piippo
parents: 8
diff changeset
16 from configobj import ConfigObj
fea8e9ae6f29 Added matrix code, moved library paths to ldcheck.cfg.
Santeri Piippo
parents: 8
diff changeset
17 from copy import deepcopy
fea8e9ae6f29 Added matrix code, moved library paths to ldcheck.cfg.
Santeri Piippo
parents: 8
diff changeset
18 config = ConfigObj(filename, encoding = 'UTF8')
fea8e9ae6f29 Added matrix code, moved library paths to ldcheck.cfg.
Santeri Piippo
parents: 8
diff changeset
19 read_config = deepcopy(config)
fea8e9ae6f29 Added matrix code, moved library paths to ldcheck.cfg.
Santeri Piippo
parents: 8
diff changeset
20 if 'libraries' not in config:
fea8e9ae6f29 Added matrix code, moved library paths to ldcheck.cfg.
Santeri Piippo
parents: 8
diff changeset
21 config['libraries'] = ['/path/to/ldraw']
fea8e9ae6f29 Added matrix code, moved library paths to ldcheck.cfg.
Santeri Piippo
parents: 8
diff changeset
22 if config != read_config:
fea8e9ae6f29 Added matrix code, moved library paths to ldcheck.cfg.
Santeri Piippo
parents: 8
diff changeset
23 config.write()
34
7ed2e831acd4 the program now checks that all ldraw paths are reachable
Teemu Piippo <teemu@hecknology.net>
parents: 32
diff changeset
24 check_library_paths(config)
9
fea8e9ae6f29 Added matrix code, moved library paths to ldcheck.cfg.
Santeri Piippo
parents: 8
diff changeset
25 return config
fea8e9ae6f29 Added matrix code, moved library paths to ldcheck.cfg.
Santeri Piippo
parents: 8
diff changeset
26
21
8006fb8cdb77 refactor
Santeri Piippo
parents: 18
diff changeset
27 def read_ldraw(file, *, name = '', config):
8006fb8cdb77 refactor
Santeri Piippo
parents: 18
diff changeset
28 model_body = [
8006fb8cdb77 refactor
Santeri Piippo
parents: 18
diff changeset
29 parse_ldraw_code(line)
8006fb8cdb77 refactor
Santeri Piippo
parents: 18
diff changeset
30 for line in file
8006fb8cdb77 refactor
Santeri Piippo
parents: 18
diff changeset
31 ]
8006fb8cdb77 refactor
Santeri Piippo
parents: 18
diff changeset
32 model = Model(body = model_body)
8006fb8cdb77 refactor
Santeri Piippo
parents: 18
diff changeset
33 model.name = name
8006fb8cdb77 refactor
Santeri Piippo
parents: 18
diff changeset
34 return model
3
1dc58f44d556 Can now write dat files, added direct color handling
Santeri Piippo
parents:
diff changeset
35
9
fea8e9ae6f29 Added matrix code, moved library paths to ldcheck.cfg.
Santeri Piippo
parents: 8
diff changeset
36 def library_paths(config):
fea8e9ae6f29 Added matrix code, moved library paths to ldcheck.cfg.
Santeri Piippo
parents: 8
diff changeset
37 for library_path_string in config['libraries']:
fea8e9ae6f29 Added matrix code, moved library paths to ldcheck.cfg.
Santeri Piippo
parents: 8
diff changeset
38 yield Path(library_path_string).expanduser()
fea8e9ae6f29 Added matrix code, moved library paths to ldcheck.cfg.
Santeri Piippo
parents: 8
diff changeset
39
34
7ed2e831acd4 the program now checks that all ldraw paths are reachable
Teemu Piippo <teemu@hecknology.net>
parents: 32
diff changeset
40 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
41 from sys import exit
7ed2e831acd4 the program now checks that all ldraw paths are reachable
Teemu Piippo <teemu@hecknology.net>
parents: 32
diff changeset
42 problems = False
7ed2e831acd4 the program now checks that all ldraw paths are reachable
Teemu Piippo <teemu@hecknology.net>
parents: 32
diff changeset
43 for library_path in library_paths(config):
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 ))
7ed2e831acd4 the program now checks that all ldraw paths are reachable
Teemu Piippo <teemu@hecknology.net>
parents: 32
diff changeset
56 if problems:
7ed2e831acd4 the program now checks that all ldraw paths are reachable
Teemu Piippo <teemu@hecknology.net>
parents: 32
diff changeset
57 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
58 exit(1)
7ed2e831acd4 the program now checks that all ldraw paths are reachable
Teemu Piippo <teemu@hecknology.net>
parents: 32
diff changeset
59
9
fea8e9ae6f29 Added matrix code, moved library paths to ldcheck.cfg.
Santeri Piippo
parents: 8
diff changeset
60 def find_ldconfig_ldr_paths(config):
fea8e9ae6f29 Added matrix code, moved library paths to ldcheck.cfg.
Santeri Piippo
parents: 8
diff changeset
61 for library_path in library_paths(config):
17
327da5d00360 Added code to run the test suite.
Santeri Piippo
parents: 13
diff changeset
62 yield from [
327da5d00360 Added code to run the test suite.
Santeri Piippo
parents: 13
diff changeset
63 library_path / path
327da5d00360 Added code to run the test suite.
Santeri Piippo
parents: 13
diff changeset
64 for path in ['LDConfig.ldr', 'ldconfig.ldr']
327da5d00360 Added code to run the test suite.
Santeri Piippo
parents: 13
diff changeset
65 if (library_path / path).is_file()
8
303c51137cb2 Added ldconfig.ldr support
Santeri Piippo
parents: 7
diff changeset
66 ]
303c51137cb2 Added ldconfig.ldr support
Santeri Piippo
parents: 7
diff changeset
67
13
12d4ddc4bfd8 Got the skew test working
Santeri Piippo
parents: 9
diff changeset
68 class Model:
17
327da5d00360 Added code to run the test suite.
Santeri Piippo
parents: 13
diff changeset
69 def __init__(self, body):
327da5d00360 Added code to run the test suite.
Santeri Piippo
parents: 13
diff changeset
70 self.body = body
327da5d00360 Added code to run the test suite.
Santeri Piippo
parents: 13
diff changeset
71 self.body_offset = 0
21
8006fb8cdb77 refactor
Santeri Piippo
parents: 18
diff changeset
72 def filter_by_type(self, type):
17
327da5d00360 Added code to run the test suite.
Santeri Piippo
parents: 13
diff changeset
73 yield from [
327da5d00360 Added code to run the test suite.
Santeri Piippo
parents: 13
diff changeset
74 element
327da5d00360 Added code to run the test suite.
Santeri Piippo
parents: 13
diff changeset
75 for element in self.body
21
8006fb8cdb77 refactor
Santeri Piippo
parents: 18
diff changeset
76 if isinstance(element, type)
17
327da5d00360 Added code to run the test suite.
Santeri Piippo
parents: 13
diff changeset
77 ]
21
8006fb8cdb77 refactor
Santeri Piippo
parents: 18
diff changeset
78 @property
8006fb8cdb77 refactor
Santeri Piippo
parents: 18
diff changeset
79 def subfile_references(self):
8006fb8cdb77 refactor
Santeri Piippo
parents: 18
diff changeset
80 yield from self.filter_by_type(linetypes.SubfileReference)
8006fb8cdb77 refactor
Santeri Piippo
parents: 18
diff changeset
81 @property
8006fb8cdb77 refactor
Santeri Piippo
parents: 18
diff changeset
82 def line_segments(self):
8006fb8cdb77 refactor
Santeri Piippo
parents: 18
diff changeset
83 yield from self.filter_by_type(linetypes.LineSegment)
8006fb8cdb77 refactor
Santeri Piippo
parents: 18
diff changeset
84 @property
8006fb8cdb77 refactor
Santeri Piippo
parents: 18
diff changeset
85 def triangles(self):
8006fb8cdb77 refactor
Santeri Piippo
parents: 18
diff changeset
86 yield from self.filter_by_type(linetypes.Triangle)
8006fb8cdb77 refactor
Santeri Piippo
parents: 18
diff changeset
87 @property
8006fb8cdb77 refactor
Santeri Piippo
parents: 18
diff changeset
88 def quadrilaterals(self):
8006fb8cdb77 refactor
Santeri Piippo
parents: 18
diff changeset
89 yield from self.filter_by_type(linetypes.Quadrilateral)
13
12d4ddc4bfd8 Got the skew test working
Santeri Piippo
parents: 9
diff changeset
90
26
7c263b864371 Added command line option to list all checks.
Santeri Piippo
parents: 21
diff changeset
91 import argparse
7c263b864371 Added command line option to list all checks.
Santeri Piippo
parents: 21
diff changeset
92 class ListTestSuiteAction(argparse.Action):
7c263b864371 Added command line option to list all checks.
Santeri Piippo
parents: 21
diff changeset
93 def __init__(self, option_strings, dest, nargs = None, **kwargs):
7c263b864371 Added command line option to list all checks.
Santeri Piippo
parents: 21
diff changeset
94 super().__init__(option_strings, dest, nargs = 0, **kwargs)
7c263b864371 Added command line option to list all checks.
Santeri Piippo
parents: 21
diff changeset
95 def __call__(self, *args, **kwargs):
7c263b864371 Added command line option to list all checks.
Santeri Piippo
parents: 21
diff changeset
96 from testsuite import load_tests
7c263b864371 Added command line option to list all checks.
Santeri Piippo
parents: 21
diff changeset
97 from sys import exit
7c263b864371 Added command line option to list all checks.
Santeri Piippo
parents: 21
diff changeset
98 from re import sub
7c263b864371 Added command line option to list all checks.
Santeri Piippo
parents: 21
diff changeset
99 test_suite = load_tests()
7c263b864371 Added command line option to list all checks.
Santeri Piippo
parents: 21
diff changeset
100 for test_name in sorted(test_suite['tests'].keys()):
7c263b864371 Added command line option to list all checks.
Santeri Piippo
parents: 21
diff changeset
101 test_function = test_suite['tests'][test_name]
7c263b864371 Added command line option to list all checks.
Santeri Piippo
parents: 21
diff changeset
102 help = sub(r'\s+', ' ', test_function.__doc__ or '').strip()
7c263b864371 Added command line option to list all checks.
Santeri Piippo
parents: 21
diff changeset
103 print(test_name + ': ' + help)
7c263b864371 Added command line option to list all checks.
Santeri Piippo
parents: 21
diff changeset
104 exit(0)
7c263b864371 Added command line option to list all checks.
Santeri Piippo
parents: 21
diff changeset
105
3
1dc58f44d556 Can now write dat files, added direct color handling
Santeri Piippo
parents:
diff changeset
106 if __name__ == '__main__':
1dc58f44d556 Can now write dat files, added direct color handling
Santeri Piippo
parents:
diff changeset
107 from sys import argv
26
7c263b864371 Added command line option to list all checks.
Santeri Piippo
parents: 21
diff changeset
108 parser = argparse.ArgumentParser()
7c263b864371 Added command line option to list all checks.
Santeri Piippo
parents: 21
diff changeset
109 parser.add_argument('filename')
7c263b864371 Added command line option to list all checks.
Santeri Piippo
parents: 21
diff changeset
110 parser.add_argument('--list',
7c263b864371 Added command line option to list all checks.
Santeri Piippo
parents: 21
diff changeset
111 action = ListTestSuiteAction,
7c263b864371 Added command line option to list all checks.
Santeri Piippo
parents: 21
diff changeset
112 help = 'Lists all possible checks and exit',
7c263b864371 Added command line option to list all checks.
Santeri Piippo
parents: 21
diff changeset
113 )
7c263b864371 Added command line option to list all checks.
Santeri Piippo
parents: 21
diff changeset
114 args = parser.parse_args()
9
fea8e9ae6f29 Added matrix code, moved library paths to ldcheck.cfg.
Santeri Piippo
parents: 8
diff changeset
115 config = load_config('ldcheck.cfg')
fea8e9ae6f29 Added matrix code, moved library paths to ldcheck.cfg.
Santeri Piippo
parents: 8
diff changeset
116 for ldconfig_ldr_path in find_ldconfig_ldr_paths(config):
8
303c51137cb2 Added ldconfig.ldr support
Santeri Piippo
parents: 7
diff changeset
117 with ldconfig_ldr_path.open() as ldconfig_ldr:
303c51137cb2 Added ldconfig.ldr support
Santeri Piippo
parents: 7
diff changeset
118 load_colours(ldconfig_ldr)
26
7c263b864371 Added command line option to list all checks.
Santeri Piippo
parents: 21
diff changeset
119 with open(args.filename) as file:
18
672ebc45685a made errors prettier
Santeri Piippo
parents: 17
diff changeset
120 from os.path import basename
26
7c263b864371 Added command line option to list all checks.
Santeri Piippo
parents: 21
diff changeset
121 model = read_ldraw(
7c263b864371 Added command line option to list all checks.
Santeri Piippo
parents: 21
diff changeset
122 file,
7c263b864371 Added command line option to list all checks.
Santeri Piippo
parents: 21
diff changeset
123 name = basename(args.filename),
7c263b864371 Added command line option to list all checks.
Santeri Piippo
parents: 21
diff changeset
124 config = config,
7c263b864371 Added command line option to list all checks.
Santeri Piippo
parents: 21
diff changeset
125 )
17
327da5d00360 Added code to run the test suite.
Santeri Piippo
parents: 13
diff changeset
126 from testsuite import load_tests, check_model, format_report
327da5d00360 Added code to run the test suite.
Santeri Piippo
parents: 13
diff changeset
127 test_suite = load_tests()
327da5d00360 Added code to run the test suite.
Santeri Piippo
parents: 13
diff changeset
128 report = check_model(model, test_suite)
327da5d00360 Added code to run the test suite.
Santeri Piippo
parents: 13
diff changeset
129 print(format_report(report, model, test_suite))

mercurial