Tue, 23 Jan 2018 14:09:44 +0200
Added bowtie check
3
1dc58f44d556
Can now write dat files, added direct color handling
Santeri Piippo
parents:
diff
changeset
|
1 | #!/usr/bin/env python3 |
8 | 2 | from sys import version_info |
3 | if version_info < (3, 4): | |
4 | raise RuntimeError('Python 3.4 or newer required') | |
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 | 7 | from colours import load_colours |
7 | 8 | from geometry import * |
8 | 9 | from pathlib import Path |
13 | 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() |
fea8e9ae6f29
Added matrix code, moved library paths to ldcheck.cfg.
Santeri Piippo
parents:
8
diff
changeset
|
24 | return config |
fea8e9ae6f29
Added matrix code, moved library paths to ldcheck.cfg.
Santeri Piippo
parents:
8
diff
changeset
|
25 | |
21 | 26 | def read_ldraw(file, *, name = '', config): |
27 | model_body = [ | |
28 | parse_ldraw_code(line) | |
29 | for line in file | |
30 | ] | |
31 | model = Model(body = model_body) | |
32 | model.name = name | |
33 | return model | |
3
1dc58f44d556
Can now write dat files, added direct color handling
Santeri Piippo
parents:
diff
changeset
|
34 | |
9
fea8e9ae6f29
Added matrix code, moved library paths to ldcheck.cfg.
Santeri Piippo
parents:
8
diff
changeset
|
35 | def library_paths(config): |
fea8e9ae6f29
Added matrix code, moved library paths to ldcheck.cfg.
Santeri Piippo
parents:
8
diff
changeset
|
36 | for library_path_string in config['libraries']: |
fea8e9ae6f29
Added matrix code, moved library paths to ldcheck.cfg.
Santeri Piippo
parents:
8
diff
changeset
|
37 | yield Path(library_path_string).expanduser() |
fea8e9ae6f29
Added matrix code, moved library paths to ldcheck.cfg.
Santeri Piippo
parents:
8
diff
changeset
|
38 | |
fea8e9ae6f29
Added matrix code, moved library paths to ldcheck.cfg.
Santeri Piippo
parents:
8
diff
changeset
|
39 | def find_ldconfig_ldr_paths(config): |
fea8e9ae6f29
Added matrix code, moved library paths to ldcheck.cfg.
Santeri Piippo
parents:
8
diff
changeset
|
40 | for library_path in library_paths(config): |
17 | 41 | yield from [ |
42 | library_path / path | |
43 | for path in ['LDConfig.ldr', 'ldconfig.ldr'] | |
44 | if (library_path / path).is_file() | |
8 | 45 | ] |
46 | ||
7 | 47 | def hairline_score(smallest_angle): |
48 | from math import log10 | |
49 | return max(0, -log10(smallest_angle)) | |
50 | ||
13 | 51 | class Model: |
17 | 52 | def __init__(self, body): |
53 | self.body = body | |
54 | self.body_offset = 0 | |
21 | 55 | def filter_by_type(self, type): |
17 | 56 | yield from [ |
57 | element | |
58 | for element in self.body | |
21 | 59 | if isinstance(element, type) |
17 | 60 | ] |
21 | 61 | @property |
62 | def subfile_references(self): | |
63 | yield from self.filter_by_type(linetypes.SubfileReference) | |
64 | @property | |
65 | def line_segments(self): | |
66 | yield from self.filter_by_type(linetypes.LineSegment) | |
67 | @property | |
68 | def triangles(self): | |
69 | yield from self.filter_by_type(linetypes.Triangle) | |
70 | @property | |
71 | def quadrilaterals(self): | |
72 | yield from self.filter_by_type(linetypes.Quadrilateral) | |
13 | 73 | |
26
7c263b864371
Added command line option to list all checks.
Santeri Piippo
parents:
21
diff
changeset
|
74 | import argparse |
7c263b864371
Added command line option to list all checks.
Santeri Piippo
parents:
21
diff
changeset
|
75 | class ListTestSuiteAction(argparse.Action): |
7c263b864371
Added command line option to list all checks.
Santeri Piippo
parents:
21
diff
changeset
|
76 | def __init__(self, option_strings, dest, nargs = None, **kwargs): |
7c263b864371
Added command line option to list all checks.
Santeri Piippo
parents:
21
diff
changeset
|
77 | super().__init__(option_strings, dest, nargs = 0, **kwargs) |
7c263b864371
Added command line option to list all checks.
Santeri Piippo
parents:
21
diff
changeset
|
78 | def __call__(self, *args, **kwargs): |
7c263b864371
Added command line option to list all checks.
Santeri Piippo
parents:
21
diff
changeset
|
79 | from testsuite import load_tests |
7c263b864371
Added command line option to list all checks.
Santeri Piippo
parents:
21
diff
changeset
|
80 | from sys import exit |
7c263b864371
Added command line option to list all checks.
Santeri Piippo
parents:
21
diff
changeset
|
81 | from re import sub |
7c263b864371
Added command line option to list all checks.
Santeri Piippo
parents:
21
diff
changeset
|
82 | test_suite = load_tests() |
7c263b864371
Added command line option to list all checks.
Santeri Piippo
parents:
21
diff
changeset
|
83 | for test_name in sorted(test_suite['tests'].keys()): |
7c263b864371
Added command line option to list all checks.
Santeri Piippo
parents:
21
diff
changeset
|
84 | test_function = test_suite['tests'][test_name] |
7c263b864371
Added command line option to list all checks.
Santeri Piippo
parents:
21
diff
changeset
|
85 | help = sub(r'\s+', ' ', test_function.__doc__ or '').strip() |
7c263b864371
Added command line option to list all checks.
Santeri Piippo
parents:
21
diff
changeset
|
86 | print(test_name + ': ' + help) |
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 | |
3
1dc58f44d556
Can now write dat files, added direct color handling
Santeri Piippo
parents:
diff
changeset
|
89 | if __name__ == '__main__': |
1dc58f44d556
Can now write dat files, added direct color handling
Santeri Piippo
parents:
diff
changeset
|
90 | from sys import argv |
26
7c263b864371
Added command line option to list all checks.
Santeri Piippo
parents:
21
diff
changeset
|
91 | parser = argparse.ArgumentParser() |
7c263b864371
Added command line option to list all checks.
Santeri Piippo
parents:
21
diff
changeset
|
92 | parser.add_argument('filename') |
7c263b864371
Added command line option to list all checks.
Santeri Piippo
parents:
21
diff
changeset
|
93 | parser.add_argument('--list', |
7c263b864371
Added command line option to list all checks.
Santeri Piippo
parents:
21
diff
changeset
|
94 | action = ListTestSuiteAction, |
7c263b864371
Added command line option to list all checks.
Santeri Piippo
parents:
21
diff
changeset
|
95 | help = 'Lists all possible checks and exit', |
7c263b864371
Added command line option to list all checks.
Santeri Piippo
parents:
21
diff
changeset
|
96 | ) |
7c263b864371
Added command line option to list all checks.
Santeri Piippo
parents:
21
diff
changeset
|
97 | args = parser.parse_args() |
9
fea8e9ae6f29
Added matrix code, moved library paths to ldcheck.cfg.
Santeri Piippo
parents:
8
diff
changeset
|
98 | config = load_config('ldcheck.cfg') |
fea8e9ae6f29
Added matrix code, moved library paths to ldcheck.cfg.
Santeri Piippo
parents:
8
diff
changeset
|
99 | for ldconfig_ldr_path in find_ldconfig_ldr_paths(config): |
8 | 100 | with ldconfig_ldr_path.open() as ldconfig_ldr: |
101 | load_colours(ldconfig_ldr) | |
26
7c263b864371
Added command line option to list all checks.
Santeri Piippo
parents:
21
diff
changeset
|
102 | with open(args.filename) as file: |
18 | 103 | from os.path import basename |
26
7c263b864371
Added command line option to list all checks.
Santeri Piippo
parents:
21
diff
changeset
|
104 | model = read_ldraw( |
7c263b864371
Added command line option to list all checks.
Santeri Piippo
parents:
21
diff
changeset
|
105 | file, |
7c263b864371
Added command line option to list all checks.
Santeri Piippo
parents:
21
diff
changeset
|
106 | name = basename(args.filename), |
7c263b864371
Added command line option to list all checks.
Santeri Piippo
parents:
21
diff
changeset
|
107 | config = config, |
7c263b864371
Added command line option to list all checks.
Santeri Piippo
parents:
21
diff
changeset
|
108 | ) |
17 | 109 | from testsuite import load_tests, check_model, format_report |
110 | test_suite = load_tests() | |
111 | report = check_model(model, test_suite) | |
112 | print(format_report(report, model, test_suite)) |