ldverify.py

changeset 9
fea8e9ae6f29
parent 8
303c51137cb2
child 10
ccc33237949e
equal deleted inserted replaced
8:303c51137cb2 9:fea8e9ae6f29
1 #!/usr/bin/env python3
2 from sys import version_info
3 if version_info < (3, 4):
4 raise RuntimeError('Python 3.4 or newer required')
5
6 from parse import parse_ldraw_code
7 from colours import load_colours
8 from geometry import *
9 from pathlib import Path
10
11 def read_ldraw(file, *, libraries):
12 result = list()
13 for line in file:
14 result.append(parse_ldraw_code(line))
15 return result
16
17 def find_ldconfig_ldr_paths(libraries):
18 for library in libraries:
19 ldconfig_paths = [
20 library['path'] / 'LDConfig.ldr',
21 library['path'] / 'ldconfig.ldr',
22 ]
23 for path in ldconfig_paths:
24 if path.is_file():
25 yield path
26
27 def hairline_score(smallest_angle):
28 from math import log10
29 return max(0, -log10(smallest_angle))
30
31 if __name__ == '__main__':
32 from sys import argv
33 libraries = [{'path': Path('~/ldraw').expanduser(), 'role': 'official'}]
34 for ldconfig_ldr_path in find_ldconfig_ldr_paths(libraries):
35 with ldconfig_ldr_path.open() as ldconfig_ldr:
36 load_colours(ldconfig_ldr)
37 with open(argv[1], 'r') as file:
38 model = read_ldraw(file, libraries = libraries)
39 for line_number, entry in enumerate(model, 1):
40 if hasattr(entry, 'colour'):
41 print(repr(entry.colour))
42 if hasattr(entry, 'geometry') and len(entry.geometry) >= 3:
43 if hairline_score(entry.geometry.smallest_angle) >= 2.0:
44 print(str.format(
45 'Hairline {type} at line {line_number}',
46 type = entry.typename(),
47 line_number = line_number,
48 ))
49 print(entry.textual_representation())
50 print('-' * 25)

mercurial