ldverify.py

Thu, 21 Dec 2017 12:27:16 +0200

author
Santeri Piippo
date
Thu, 21 Dec 2017 12:27:16 +0200
changeset 8
303c51137cb2
parent 7
0ab0d61ccee8
permissions
-rwxr-xr-x

Added ldconfig.ldr support

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
1dc58f44d556 Can now write dat files, added direct color handling
Santeri Piippo
parents:
diff changeset
11 def read_ldraw(file, *, libraries):
1dc58f44d556 Can now write dat files, added direct color handling
Santeri Piippo
parents:
diff changeset
12 result = list()
1dc58f44d556 Can now write dat files, added direct color handling
Santeri Piippo
parents:
diff changeset
13 for line in file:
1dc58f44d556 Can now write dat files, added direct color handling
Santeri Piippo
parents:
diff changeset
14 result.append(parse_ldraw_code(line))
1dc58f44d556 Can now write dat files, added direct color handling
Santeri Piippo
parents:
diff changeset
15 return result
1dc58f44d556 Can now write dat files, added direct color handling
Santeri Piippo
parents:
diff changeset
16
8
303c51137cb2 Added ldconfig.ldr support
Santeri Piippo
parents: 7
diff changeset
17 def find_ldconfig_ldr_paths(libraries):
303c51137cb2 Added ldconfig.ldr support
Santeri Piippo
parents: 7
diff changeset
18 for library in libraries:
303c51137cb2 Added ldconfig.ldr support
Santeri Piippo
parents: 7
diff changeset
19 ldconfig_paths = [
303c51137cb2 Added ldconfig.ldr support
Santeri Piippo
parents: 7
diff changeset
20 library['path'] / 'LDConfig.ldr',
303c51137cb2 Added ldconfig.ldr support
Santeri Piippo
parents: 7
diff changeset
21 library['path'] / 'ldconfig.ldr',
303c51137cb2 Added ldconfig.ldr support
Santeri Piippo
parents: 7
diff changeset
22 ]
303c51137cb2 Added ldconfig.ldr support
Santeri Piippo
parents: 7
diff changeset
23 for path in ldconfig_paths:
303c51137cb2 Added ldconfig.ldr support
Santeri Piippo
parents: 7
diff changeset
24 if path.is_file():
303c51137cb2 Added ldconfig.ldr support
Santeri Piippo
parents: 7
diff changeset
25 yield path
303c51137cb2 Added ldconfig.ldr support
Santeri Piippo
parents: 7
diff changeset
26
7
0ab0d61ccee8 Smallest angles
Santeri Piippo
parents: 6
diff changeset
27 def hairline_score(smallest_angle):
0ab0d61ccee8 Smallest angles
Santeri Piippo
parents: 6
diff changeset
28 from math import log10
0ab0d61ccee8 Smallest angles
Santeri Piippo
parents: 6
diff changeset
29 return max(0, -log10(smallest_angle))
0ab0d61ccee8 Smallest angles
Santeri Piippo
parents: 6
diff changeset
30
3
1dc58f44d556 Can now write dat files, added direct color handling
Santeri Piippo
parents:
diff changeset
31 if __name__ == '__main__':
1dc58f44d556 Can now write dat files, added direct color handling
Santeri Piippo
parents:
diff changeset
32 from sys import argv
8
303c51137cb2 Added ldconfig.ldr support
Santeri Piippo
parents: 7
diff changeset
33 libraries = [{'path': Path('~/ldraw').expanduser(), 'role': 'official'}]
303c51137cb2 Added ldconfig.ldr support
Santeri Piippo
parents: 7
diff changeset
34 for ldconfig_ldr_path in find_ldconfig_ldr_paths(libraries):
303c51137cb2 Added ldconfig.ldr support
Santeri Piippo
parents: 7
diff changeset
35 with ldconfig_ldr_path.open() as ldconfig_ldr:
303c51137cb2 Added ldconfig.ldr support
Santeri Piippo
parents: 7
diff changeset
36 load_colours(ldconfig_ldr)
3
1dc58f44d556 Can now write dat files, added direct color handling
Santeri Piippo
parents:
diff changeset
37 with open(argv[1], 'r') as file:
1dc58f44d556 Can now write dat files, added direct color handling
Santeri Piippo
parents:
diff changeset
38 model = read_ldraw(file, libraries = libraries)
7
0ab0d61ccee8 Smallest angles
Santeri Piippo
parents: 6
diff changeset
39 for line_number, entry in enumerate(model, 1):
8
303c51137cb2 Added ldconfig.ldr support
Santeri Piippo
parents: 7
diff changeset
40 if hasattr(entry, 'colour'):
303c51137cb2 Added ldconfig.ldr support
Santeri Piippo
parents: 7
diff changeset
41 print(repr(entry.colour))
6
6da1e81c5652 Added code to compute areas of triangles and quadrilaterals
Santeri Piippo
parents: 3
diff changeset
42 if hasattr(entry, 'geometry') and len(entry.geometry) >= 3:
7
0ab0d61ccee8 Smallest angles
Santeri Piippo
parents: 6
diff changeset
43 if hairline_score(entry.geometry.smallest_angle) >= 2.0:
0ab0d61ccee8 Smallest angles
Santeri Piippo
parents: 6
diff changeset
44 print(str.format(
0ab0d61ccee8 Smallest angles
Santeri Piippo
parents: 6
diff changeset
45 'Hairline {type} at line {line_number}',
0ab0d61ccee8 Smallest angles
Santeri Piippo
parents: 6
diff changeset
46 type = entry.typename(),
0ab0d61ccee8 Smallest angles
Santeri Piippo
parents: 6
diff changeset
47 line_number = line_number,
0ab0d61ccee8 Smallest angles
Santeri Piippo
parents: 6
diff changeset
48 ))
0ab0d61ccee8 Smallest angles
Santeri Piippo
parents: 6
diff changeset
49 print(entry.textual_representation())
0ab0d61ccee8 Smallest angles
Santeri Piippo
parents: 6
diff changeset
50 print('-' * 25)

mercurial