ldcheck.py

Mon, 22 Jan 2018 17:42:57 +0200

author
Santeri Piippo
date
Mon, 22 Jan 2018 17:42:57 +0200
changeset 18
672ebc45685a
parent 17
327da5d00360
child 21
8006fb8cdb77
permissions
-rwxr-xr-x

made errors prettier

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()
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
fea8e9ae6f29 Added matrix code, moved library paths to ldcheck.cfg.
Santeri Piippo
parents: 8
diff changeset
26 def read_ldraw(file, *, config):
3
1dc58f44d556 Can now write dat files, added direct color handling
Santeri Piippo
parents:
diff changeset
27 result = list()
1dc58f44d556 Can now write dat files, added direct color handling
Santeri Piippo
parents:
diff changeset
28 for line in file:
1dc58f44d556 Can now write dat files, added direct color handling
Santeri Piippo
parents:
diff changeset
29 result.append(parse_ldraw_code(line))
1dc58f44d556 Can now write dat files, added direct color handling
Santeri Piippo
parents:
diff changeset
30 return result
1dc58f44d556 Can now write dat files, added direct color handling
Santeri Piippo
parents:
diff changeset
31
9
fea8e9ae6f29 Added matrix code, moved library paths to ldcheck.cfg.
Santeri Piippo
parents: 8
diff changeset
32 def library_paths(config):
fea8e9ae6f29 Added matrix code, moved library paths to ldcheck.cfg.
Santeri Piippo
parents: 8
diff changeset
33 for library_path_string in config['libraries']:
fea8e9ae6f29 Added matrix code, moved library paths to ldcheck.cfg.
Santeri Piippo
parents: 8
diff changeset
34 yield Path(library_path_string).expanduser()
fea8e9ae6f29 Added matrix code, moved library paths to ldcheck.cfg.
Santeri Piippo
parents: 8
diff changeset
35
fea8e9ae6f29 Added matrix code, moved library paths to ldcheck.cfg.
Santeri Piippo
parents: 8
diff changeset
36 def find_ldconfig_ldr_paths(config):
fea8e9ae6f29 Added matrix code, moved library paths to ldcheck.cfg.
Santeri Piippo
parents: 8
diff changeset
37 for library_path in library_paths(config):
17
327da5d00360 Added code to run the test suite.
Santeri Piippo
parents: 13
diff changeset
38 yield from [
327da5d00360 Added code to run the test suite.
Santeri Piippo
parents: 13
diff changeset
39 library_path / path
327da5d00360 Added code to run the test suite.
Santeri Piippo
parents: 13
diff changeset
40 for path in ['LDConfig.ldr', 'ldconfig.ldr']
327da5d00360 Added code to run the test suite.
Santeri Piippo
parents: 13
diff changeset
41 if (library_path / path).is_file()
8
303c51137cb2 Added ldconfig.ldr support
Santeri Piippo
parents: 7
diff changeset
42 ]
303c51137cb2 Added ldconfig.ldr support
Santeri Piippo
parents: 7
diff changeset
43
7
0ab0d61ccee8 Smallest angles
Santeri Piippo
parents: 6
diff changeset
44 def hairline_score(smallest_angle):
0ab0d61ccee8 Smallest angles
Santeri Piippo
parents: 6
diff changeset
45 from math import log10
0ab0d61ccee8 Smallest angles
Santeri Piippo
parents: 6
diff changeset
46 return max(0, -log10(smallest_angle))
0ab0d61ccee8 Smallest angles
Santeri Piippo
parents: 6
diff changeset
47
13
12d4ddc4bfd8 Got the skew test working
Santeri Piippo
parents: 9
diff changeset
48 class Model:
17
327da5d00360 Added code to run the test suite.
Santeri Piippo
parents: 13
diff changeset
49 def __init__(self, body):
327da5d00360 Added code to run the test suite.
Santeri Piippo
parents: 13
diff changeset
50 self.body = body
327da5d00360 Added code to run the test suite.
Santeri Piippo
parents: 13
diff changeset
51 self.body_offset = 0
327da5d00360 Added code to run the test suite.
Santeri Piippo
parents: 13
diff changeset
52 @property
327da5d00360 Added code to run the test suite.
Santeri Piippo
parents: 13
diff changeset
53 def quadrilaterals(self):
327da5d00360 Added code to run the test suite.
Santeri Piippo
parents: 13
diff changeset
54 yield from [
327da5d00360 Added code to run the test suite.
Santeri Piippo
parents: 13
diff changeset
55 element
327da5d00360 Added code to run the test suite.
Santeri Piippo
parents: 13
diff changeset
56 for element in self.body
327da5d00360 Added code to run the test suite.
Santeri Piippo
parents: 13
diff changeset
57 if isinstance(element, linetypes.Quadrilateral)
327da5d00360 Added code to run the test suite.
Santeri Piippo
parents: 13
diff changeset
58 ]
13
12d4ddc4bfd8 Got the skew test working
Santeri Piippo
parents: 9
diff changeset
59
3
1dc58f44d556 Can now write dat files, added direct color handling
Santeri Piippo
parents:
diff changeset
60 if __name__ == '__main__':
1dc58f44d556 Can now write dat files, added direct color handling
Santeri Piippo
parents:
diff changeset
61 from sys import argv
9
fea8e9ae6f29 Added matrix code, moved library paths to ldcheck.cfg.
Santeri Piippo
parents: 8
diff changeset
62 config = load_config('ldcheck.cfg')
fea8e9ae6f29 Added matrix code, moved library paths to ldcheck.cfg.
Santeri Piippo
parents: 8
diff changeset
63 for ldconfig_ldr_path in find_ldconfig_ldr_paths(config):
8
303c51137cb2 Added ldconfig.ldr support
Santeri Piippo
parents: 7
diff changeset
64 with ldconfig_ldr_path.open() as ldconfig_ldr:
303c51137cb2 Added ldconfig.ldr support
Santeri Piippo
parents: 7
diff changeset
65 load_colours(ldconfig_ldr)
3
1dc58f44d556 Can now write dat files, added direct color handling
Santeri Piippo
parents:
diff changeset
66 with open(argv[1], 'r') as file:
13
12d4ddc4bfd8 Got the skew test working
Santeri Piippo
parents: 9
diff changeset
67 model_body = read_ldraw(file, config = config)
12d4ddc4bfd8 Got the skew test working
Santeri Piippo
parents: 9
diff changeset
68 model = Model(body = model_body)
18
672ebc45685a made errors prettier
Santeri Piippo
parents: 17
diff changeset
69 from os.path import basename
672ebc45685a made errors prettier
Santeri Piippo
parents: 17
diff changeset
70 model.name = basename(argv[1])
17
327da5d00360 Added code to run the test suite.
Santeri Piippo
parents: 13
diff changeset
71 from testsuite import load_tests, check_model, format_report
327da5d00360 Added code to run the test suite.
Santeri Piippo
parents: 13
diff changeset
72 test_suite = load_tests()
327da5d00360 Added code to run the test suite.
Santeri Piippo
parents: 13
diff changeset
73 report = check_model(model, test_suite)
327da5d00360 Added code to run the test suite.
Santeri Piippo
parents: 13
diff changeset
74 print(format_report(report, model, test_suite))

mercurial