ldcheck.py

Fri, 19 Jan 2018 13:41:23 +0200

author
Santeri Piippo
date
Fri, 19 Jan 2018 13:41:23 +0200
changeset 12
eb74680a5e43
parent 9
fea8e9ae6f29
child 13
12d4ddc4bfd8
permissions
-rwxr-xr-x

commit work done on test suites...

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
3
1dc58f44d556 Can now write dat files, added direct color handling
Santeri Piippo
parents:
diff changeset
10
9
fea8e9ae6f29 Added matrix code, moved library paths to ldcheck.cfg.
Santeri Piippo
parents: 8
diff changeset
11 from os.path import realpath
fea8e9ae6f29 Added matrix code, moved library paths to ldcheck.cfg.
Santeri Piippo
parents: 8
diff changeset
12 script_directory = Path(realpath(__file__)).parent
fea8e9ae6f29 Added matrix code, moved library paths to ldcheck.cfg.
Santeri Piippo
parents: 8
diff changeset
13
fea8e9ae6f29 Added matrix code, moved library paths to ldcheck.cfg.
Santeri Piippo
parents: 8
diff changeset
14 def load_config(filename):
fea8e9ae6f29 Added matrix code, moved library paths to ldcheck.cfg.
Santeri Piippo
parents: 8
diff changeset
15 from configobj import ConfigObj
fea8e9ae6f29 Added matrix code, moved library paths to ldcheck.cfg.
Santeri Piippo
parents: 8
diff changeset
16 from copy import deepcopy
fea8e9ae6f29 Added matrix code, moved library paths to ldcheck.cfg.
Santeri Piippo
parents: 8
diff changeset
17 config = ConfigObj(filename, encoding = 'UTF8')
fea8e9ae6f29 Added matrix code, moved library paths to ldcheck.cfg.
Santeri Piippo
parents: 8
diff changeset
18 read_config = deepcopy(config)
fea8e9ae6f29 Added matrix code, moved library paths to ldcheck.cfg.
Santeri Piippo
parents: 8
diff changeset
19 if 'libraries' not in config:
fea8e9ae6f29 Added matrix code, moved library paths to ldcheck.cfg.
Santeri Piippo
parents: 8
diff changeset
20 config['libraries'] = ['/path/to/ldraw']
fea8e9ae6f29 Added matrix code, moved library paths to ldcheck.cfg.
Santeri Piippo
parents: 8
diff changeset
21 if config != read_config:
fea8e9ae6f29 Added matrix code, moved library paths to ldcheck.cfg.
Santeri Piippo
parents: 8
diff changeset
22 config.write()
fea8e9ae6f29 Added matrix code, moved library paths to ldcheck.cfg.
Santeri Piippo
parents: 8
diff changeset
23 return config
fea8e9ae6f29 Added matrix code, moved library paths to ldcheck.cfg.
Santeri Piippo
parents: 8
diff changeset
24
fea8e9ae6f29 Added matrix code, moved library paths to ldcheck.cfg.
Santeri Piippo
parents: 8
diff changeset
25 def read_ldraw(file, *, config):
3
1dc58f44d556 Can now write dat files, added direct color handling
Santeri Piippo
parents:
diff changeset
26 result = list()
1dc58f44d556 Can now write dat files, added direct color handling
Santeri Piippo
parents:
diff changeset
27 for line in file:
1dc58f44d556 Can now write dat files, added direct color handling
Santeri Piippo
parents:
diff changeset
28 result.append(parse_ldraw_code(line))
1dc58f44d556 Can now write dat files, added direct color handling
Santeri Piippo
parents:
diff changeset
29 return result
1dc58f44d556 Can now write dat files, added direct color handling
Santeri Piippo
parents:
diff changeset
30
9
fea8e9ae6f29 Added matrix code, moved library paths to ldcheck.cfg.
Santeri Piippo
parents: 8
diff changeset
31 def library_paths(config):
fea8e9ae6f29 Added matrix code, moved library paths to ldcheck.cfg.
Santeri Piippo
parents: 8
diff changeset
32 for library_path_string in config['libraries']:
fea8e9ae6f29 Added matrix code, moved library paths to ldcheck.cfg.
Santeri Piippo
parents: 8
diff changeset
33 yield Path(library_path_string).expanduser()
fea8e9ae6f29 Added matrix code, moved library paths to ldcheck.cfg.
Santeri Piippo
parents: 8
diff changeset
34
fea8e9ae6f29 Added matrix code, moved library paths to ldcheck.cfg.
Santeri Piippo
parents: 8
diff changeset
35 def find_ldconfig_ldr_paths(config):
fea8e9ae6f29 Added matrix code, moved library paths to ldcheck.cfg.
Santeri Piippo
parents: 8
diff changeset
36 for library_path in library_paths(config):
8
303c51137cb2 Added ldconfig.ldr support
Santeri Piippo
parents: 7
diff changeset
37 ldconfig_paths = [
9
fea8e9ae6f29 Added matrix code, moved library paths to ldcheck.cfg.
Santeri Piippo
parents: 8
diff changeset
38 library_path / 'LDConfig.ldr',
fea8e9ae6f29 Added matrix code, moved library paths to ldcheck.cfg.
Santeri Piippo
parents: 8
diff changeset
39 library_path / 'ldconfig.ldr',
8
303c51137cb2 Added ldconfig.ldr support
Santeri Piippo
parents: 7
diff changeset
40 ]
303c51137cb2 Added ldconfig.ldr support
Santeri Piippo
parents: 7
diff changeset
41 for path in ldconfig_paths:
9
fea8e9ae6f29 Added matrix code, moved library paths to ldcheck.cfg.
Santeri Piippo
parents: 8
diff changeset
42 print(path)
8
303c51137cb2 Added ldconfig.ldr support
Santeri Piippo
parents: 7
diff changeset
43 if path.is_file():
303c51137cb2 Added ldconfig.ldr support
Santeri Piippo
parents: 7
diff changeset
44 yield path
303c51137cb2 Added ldconfig.ldr support
Santeri Piippo
parents: 7
diff changeset
45
7
0ab0d61ccee8 Smallest angles
Santeri Piippo
parents: 6
diff changeset
46 def hairline_score(smallest_angle):
0ab0d61ccee8 Smallest angles
Santeri Piippo
parents: 6
diff changeset
47 from math import log10
0ab0d61ccee8 Smallest angles
Santeri Piippo
parents: 6
diff changeset
48 return max(0, -log10(smallest_angle))
0ab0d61ccee8 Smallest angles
Santeri Piippo
parents: 6
diff changeset
49
3
1dc58f44d556 Can now write dat files, added direct color handling
Santeri Piippo
parents:
diff changeset
50 if __name__ == '__main__':
1dc58f44d556 Can now write dat files, added direct color handling
Santeri Piippo
parents:
diff changeset
51 from sys import argv
9
fea8e9ae6f29 Added matrix code, moved library paths to ldcheck.cfg.
Santeri Piippo
parents: 8
diff changeset
52 config = load_config('ldcheck.cfg')
fea8e9ae6f29 Added matrix code, moved library paths to ldcheck.cfg.
Santeri Piippo
parents: 8
diff changeset
53 for ldconfig_ldr_path in find_ldconfig_ldr_paths(config):
8
303c51137cb2 Added ldconfig.ldr support
Santeri Piippo
parents: 7
diff changeset
54 with ldconfig_ldr_path.open() as ldconfig_ldr:
303c51137cb2 Added ldconfig.ldr support
Santeri Piippo
parents: 7
diff changeset
55 load_colours(ldconfig_ldr)
3
1dc58f44d556 Can now write dat files, added direct color handling
Santeri Piippo
parents:
diff changeset
56 with open(argv[1], 'r') as file:
9
fea8e9ae6f29 Added matrix code, moved library paths to ldcheck.cfg.
Santeri Piippo
parents: 8
diff changeset
57 model = read_ldraw(file, config = config)
7
0ab0d61ccee8 Smallest angles
Santeri Piippo
parents: 6
diff changeset
58 for line_number, entry in enumerate(model, 1):
8
303c51137cb2 Added ldconfig.ldr support
Santeri Piippo
parents: 7
diff changeset
59 if hasattr(entry, 'colour'):
303c51137cb2 Added ldconfig.ldr support
Santeri Piippo
parents: 7
diff changeset
60 print(repr(entry.colour))
6
6da1e81c5652 Added code to compute areas of triangles and quadrilaterals
Santeri Piippo
parents: 3
diff changeset
61 if hasattr(entry, 'geometry') and len(entry.geometry) >= 3:
7
0ab0d61ccee8 Smallest angles
Santeri Piippo
parents: 6
diff changeset
62 if hairline_score(entry.geometry.smallest_angle) >= 2.0:
0ab0d61ccee8 Smallest angles
Santeri Piippo
parents: 6
diff changeset
63 print(str.format(
0ab0d61ccee8 Smallest angles
Santeri Piippo
parents: 6
diff changeset
64 'Hairline {type} at line {line_number}',
0ab0d61ccee8 Smallest angles
Santeri Piippo
parents: 6
diff changeset
65 type = entry.typename(),
0ab0d61ccee8 Smallest angles
Santeri Piippo
parents: 6
diff changeset
66 line_number = line_number,
0ab0d61ccee8 Smallest angles
Santeri Piippo
parents: 6
diff changeset
67 ))
0ab0d61ccee8 Smallest angles
Santeri Piippo
parents: 6
diff changeset
68 print(entry.textual_representation())
0ab0d61ccee8 Smallest angles
Santeri Piippo
parents: 6
diff changeset
69 print('-' * 25)

mercurial