parse.py

Tue, 28 May 2019 19:10:52 +0300

author
Teemu Piippo <teemu@hecknology.net>
date
Tue, 28 May 2019 19:10:52 +0300
changeset 49
a1f5c12fa45c
parent 47
4da025d0b283
child 54
0c686d10eb49
permissions
-rw-r--r--

fix LDRAW_ORG parsing

2
50d3086070df Moved the parsing function into a new file
Santeri Piippo
parents:
diff changeset
1 import linetypes
50d3086070df Moved the parsing function into a new file
Santeri Piippo
parents:
diff changeset
2 import re
50d3086070df Moved the parsing function into a new file
Santeri Piippo
parents:
diff changeset
3 from geometry import *
8
303c51137cb2 Added ldconfig.ldr support
Santeri Piippo
parents: 6
diff changeset
4 from colours import Colour
47
4da025d0b283 added work on header check
Teemu Piippo <teemu@hecknology.net>
parents: 38
diff changeset
5 from testsuite import error
2
50d3086070df Moved the parsing function into a new file
Santeri Piippo
parents:
diff changeset
6
50d3086070df Moved the parsing function into a new file
Santeri Piippo
parents:
diff changeset
7 class BadLdrawLine(Exception):
50d3086070df Moved the parsing function into a new file
Santeri Piippo
parents:
diff changeset
8 pass
50d3086070df Moved the parsing function into a new file
Santeri Piippo
parents:
diff changeset
9
50d3086070df Moved the parsing function into a new file
Santeri Piippo
parents:
diff changeset
10 def parse_ldraw_code(line):
38
66c9591b733d added proper handling of syntax errors
Teemu Piippo <teemu@hecknology.net>
parents: 35
diff changeset
11 try:
66c9591b733d added proper handling of syntax errors
Teemu Piippo <teemu@hecknology.net>
parents: 35
diff changeset
12 if isinstance(line, bytes):
66c9591b733d added proper handling of syntax errors
Teemu Piippo <teemu@hecknology.net>
parents: 35
diff changeset
13 line = line.decode()
66c9591b733d added proper handling of syntax errors
Teemu Piippo <teemu@hecknology.net>
parents: 35
diff changeset
14 line = line.strip()
66c9591b733d added proper handling of syntax errors
Teemu Piippo <teemu@hecknology.net>
parents: 35
diff changeset
15 if not line:
66c9591b733d added proper handling of syntax errors
Teemu Piippo <teemu@hecknology.net>
parents: 35
diff changeset
16 return linetypes.EmptyLine()
66c9591b733d added proper handling of syntax errors
Teemu Piippo <teemu@hecknology.net>
parents: 35
diff changeset
17 elif line == '0':
66c9591b733d added proper handling of syntax errors
Teemu Piippo <teemu@hecknology.net>
parents: 35
diff changeset
18 return linetypes.MetaCommand('')
66c9591b733d added proper handling of syntax errors
Teemu Piippo <teemu@hecknology.net>
parents: 35
diff changeset
19 elif line.startswith('0 '):
66c9591b733d added proper handling of syntax errors
Teemu Piippo <teemu@hecknology.net>
parents: 35
diff changeset
20 return parse_ldraw_meta_line(line)
66c9591b733d added proper handling of syntax errors
Teemu Piippo <teemu@hecknology.net>
parents: 35
diff changeset
21 elif line.startswith('1 '):
66c9591b733d added proper handling of syntax errors
Teemu Piippo <teemu@hecknology.net>
parents: 35
diff changeset
22 return parse_ldraw_subfile_reference(line)
66c9591b733d added proper handling of syntax errors
Teemu Piippo <teemu@hecknology.net>
parents: 35
diff changeset
23 elif line.startswith('2 '):
66c9591b733d added proper handling of syntax errors
Teemu Piippo <teemu@hecknology.net>
parents: 35
diff changeset
24 return parse_ldraw_line(line)
66c9591b733d added proper handling of syntax errors
Teemu Piippo <teemu@hecknology.net>
parents: 35
diff changeset
25 elif line.startswith('3 '):
66c9591b733d added proper handling of syntax errors
Teemu Piippo <teemu@hecknology.net>
parents: 35
diff changeset
26 return parse_ldraw_triangle(line)
66c9591b733d added proper handling of syntax errors
Teemu Piippo <teemu@hecknology.net>
parents: 35
diff changeset
27 elif line.startswith('4 '):
66c9591b733d added proper handling of syntax errors
Teemu Piippo <teemu@hecknology.net>
parents: 35
diff changeset
28 return parse_ldraw_quadrilateral(line)
66c9591b733d added proper handling of syntax errors
Teemu Piippo <teemu@hecknology.net>
parents: 35
diff changeset
29 elif line.startswith('5 '):
66c9591b733d added proper handling of syntax errors
Teemu Piippo <teemu@hecknology.net>
parents: 35
diff changeset
30 return parse_ldraw_conditional_line(line)
66c9591b733d added proper handling of syntax errors
Teemu Piippo <teemu@hecknology.net>
parents: 35
diff changeset
31 else:
66c9591b733d added proper handling of syntax errors
Teemu Piippo <teemu@hecknology.net>
parents: 35
diff changeset
32 raise BadLdrawLine('unknown line type')
66c9591b733d added proper handling of syntax errors
Teemu Piippo <teemu@hecknology.net>
parents: 35
diff changeset
33 except BadLdrawLine as error:
66c9591b733d added proper handling of syntax errors
Teemu Piippo <teemu@hecknology.net>
parents: 35
diff changeset
34 return linetypes.Error(line, str(error))
66c9591b733d added proper handling of syntax errors
Teemu Piippo <teemu@hecknology.net>
parents: 35
diff changeset
35
66c9591b733d added proper handling of syntax errors
Teemu Piippo <teemu@hecknology.net>
parents: 35
diff changeset
36 def parse_ldraw_meta_line(line):
66c9591b733d added proper handling of syntax errors
Teemu Piippo <teemu@hecknology.net>
parents: 35
diff changeset
37 if line.startswith('0 //'):
66c9591b733d added proper handling of syntax errors
Teemu Piippo <teemu@hecknology.net>
parents: 35
diff changeset
38 return linetypes.Comment(line[4:])
2
50d3086070df Moved the parsing function into a new file
Santeri Piippo
parents:
diff changeset
39 else:
38
66c9591b733d added proper handling of syntax errors
Teemu Piippo <teemu@hecknology.net>
parents: 35
diff changeset
40 return linetypes.MetaCommand(line[2:])
2
50d3086070df Moved the parsing function into a new file
Santeri Piippo
parents:
diff changeset
41
50d3086070df Moved the parsing function into a new file
Santeri Piippo
parents:
diff changeset
42 def parse_ldraw_subfile_reference(line):
3
1dc58f44d556 Can now write dat files, added direct color handling
Santeri Piippo
parents: 2
diff changeset
43 pattern = r'^1\s+([^ ]+)' + r'\s+([^ ]+)' * (3 + 9 + 1) + r'\s*$'
2
50d3086070df Moved the parsing function into a new file
Santeri Piippo
parents:
diff changeset
44 match = re.search(pattern, line)
50d3086070df Moved the parsing function into a new file
Santeri Piippo
parents:
diff changeset
45 if not match:
50d3086070df Moved the parsing function into a new file
Santeri Piippo
parents:
diff changeset
46 raise BadLdrawLine('unable to parse')
50d3086070df Moved the parsing function into a new file
Santeri Piippo
parents:
diff changeset
47 groups = list(match.groups())
50d3086070df Moved the parsing function into a new file
Santeri Piippo
parents:
diff changeset
48 indices = {
4
8eb83f200486 color -> colour
Santeri Piippo
parents: 3
diff changeset
49 'colour_index': 0,
2
50d3086070df Moved the parsing function into a new file
Santeri Piippo
parents:
diff changeset
50 'anchor': slice(1, 4),
50d3086070df Moved the parsing function into a new file
Santeri Piippo
parents:
diff changeset
51 'matrix': slice(4, 13),
50d3086070df Moved the parsing function into a new file
Santeri Piippo
parents:
diff changeset
52 'subfile_path': 13
50d3086070df Moved the parsing function into a new file
Santeri Piippo
parents:
diff changeset
53 }
50d3086070df Moved the parsing function into a new file
Santeri Piippo
parents:
diff changeset
54 try:
4
8eb83f200486 color -> colour
Santeri Piippo
parents: 3
diff changeset
55 colour = Colour(groups[indices['colour_index']])
2
50d3086070df Moved the parsing function into a new file
Santeri Piippo
parents:
diff changeset
56 vertex_values = [float(x) for x in groups[indices['anchor']]]
50d3086070df Moved the parsing function into a new file
Santeri Piippo
parents:
diff changeset
57 matrix_values = [float(x) for x in groups[indices['matrix']]]
50d3086070df Moved the parsing function into a new file
Santeri Piippo
parents:
diff changeset
58 except ValueError:
50d3086070df Moved the parsing function into a new file
Santeri Piippo
parents:
diff changeset
59 raise BadLdrawLine('bad numeric values')
50d3086070df Moved the parsing function into a new file
Santeri Piippo
parents:
diff changeset
60 return linetypes.SubfileReference(
4
8eb83f200486 color -> colour
Santeri Piippo
parents: 3
diff changeset
61 colour = colour,
2
50d3086070df Moved the parsing function into a new file
Santeri Piippo
parents:
diff changeset
62 anchor = Vertex(*vertex_values),
6
6da1e81c5652 Added code to compute areas of triangles and quadrilaterals
Santeri Piippo
parents: 4
diff changeset
63 matrix = Matrix3x3(matrix_values),
2
50d3086070df Moved the parsing function into a new file
Santeri Piippo
parents:
diff changeset
64 subfile_path = groups[indices['subfile_path']]
50d3086070df Moved the parsing function into a new file
Santeri Piippo
parents:
diff changeset
65 )
50d3086070df Moved the parsing function into a new file
Santeri Piippo
parents:
diff changeset
66
50d3086070df Moved the parsing function into a new file
Santeri Piippo
parents:
diff changeset
67 def generic_parse_polygon(line, *, type_code, vertex_count):
35
865cd526e8b6 cleanup
Teemu Piippo <teemu@hecknology.net>
parents: 29
diff changeset
68 pattern = r'^' # matches the start of line
865cd526e8b6 cleanup
Teemu Piippo <teemu@hecknology.net>
parents: 29
diff changeset
69 pattern += str(type_code) # matches the type code
865cd526e8b6 cleanup
Teemu Piippo <teemu@hecknology.net>
parents: 29
diff changeset
70 pattern += '\s+([^ ]+)' # matches the colour
865cd526e8b6 cleanup
Teemu Piippo <teemu@hecknology.net>
parents: 29
diff changeset
71 pattern += r'\s+([^ ]+)' * (vertex_count * 3) # matches the vertices
865cd526e8b6 cleanup
Teemu Piippo <teemu@hecknology.net>
parents: 29
diff changeset
72 pattern += r'\s*$' # matches any trailing space
2
50d3086070df Moved the parsing function into a new file
Santeri Piippo
parents:
diff changeset
73 match = re.search(pattern, line)
50d3086070df Moved the parsing function into a new file
Santeri Piippo
parents:
diff changeset
74 if not match:
50d3086070df Moved the parsing function into a new file
Santeri Piippo
parents:
diff changeset
75 raise BadLdrawLine(str.format('cannot parse type-{} line', type_code))
50d3086070df Moved the parsing function into a new file
Santeri Piippo
parents:
diff changeset
76 vertices = []
50d3086070df Moved the parsing function into a new file
Santeri Piippo
parents:
diff changeset
77 for vertex_index in range(vertex_count):
50d3086070df Moved the parsing function into a new file
Santeri Piippo
parents:
diff changeset
78 slice_begin = 1 + vertex_index * 3
50d3086070df Moved the parsing function into a new file
Santeri Piippo
parents:
diff changeset
79 slice_end = 1 + (vertex_index + 1) * 3
50d3086070df Moved the parsing function into a new file
Santeri Piippo
parents:
diff changeset
80 coordinates = match.groups()[slice_begin:slice_end]
50d3086070df Moved the parsing function into a new file
Santeri Piippo
parents:
diff changeset
81 assert(len(coordinates) == 3)
50d3086070df Moved the parsing function into a new file
Santeri Piippo
parents:
diff changeset
82 try:
50d3086070df Moved the parsing function into a new file
Santeri Piippo
parents:
diff changeset
83 coordinates = [float(x) for x in coordinates]
50d3086070df Moved the parsing function into a new file
Santeri Piippo
parents:
diff changeset
84 except ValueError:
50d3086070df Moved the parsing function into a new file
Santeri Piippo
parents:
diff changeset
85 raise BadLdrawLine('bad numeric values')
50d3086070df Moved the parsing function into a new file
Santeri Piippo
parents:
diff changeset
86 vertices.append(Vertex(*coordinates))
38
66c9591b733d added proper handling of syntax errors
Teemu Piippo <teemu@hecknology.net>
parents: 35
diff changeset
87 try:
66c9591b733d added proper handling of syntax errors
Teemu Piippo <teemu@hecknology.net>
parents: 35
diff changeset
88 colour = int(match.group(1), 0)
66c9591b733d added proper handling of syntax errors
Teemu Piippo <teemu@hecknology.net>
parents: 35
diff changeset
89 except ValueError:
66c9591b733d added proper handling of syntax errors
Teemu Piippo <teemu@hecknology.net>
parents: 35
diff changeset
90 raise BadLdrawLine('invalid syntax for colour: ' + repr(match.group(1)))
2
50d3086070df Moved the parsing function into a new file
Santeri Piippo
parents:
diff changeset
91 return {
38
66c9591b733d added proper handling of syntax errors
Teemu Piippo <teemu@hecknology.net>
parents: 35
diff changeset
92 'colour': Colour(colour),
2
50d3086070df Moved the parsing function into a new file
Santeri Piippo
parents:
diff changeset
93 'vertices': vertices,
50d3086070df Moved the parsing function into a new file
Santeri Piippo
parents:
diff changeset
94 }
50d3086070df Moved the parsing function into a new file
Santeri Piippo
parents:
diff changeset
95
50d3086070df Moved the parsing function into a new file
Santeri Piippo
parents:
diff changeset
96 def parse_ldraw_line(line):
50d3086070df Moved the parsing function into a new file
Santeri Piippo
parents:
diff changeset
97 parse_result = generic_parse_polygon(line, type_code = 2, vertex_count = 2)
50d3086070df Moved the parsing function into a new file
Santeri Piippo
parents:
diff changeset
98 return linetypes.LineSegment(
4
8eb83f200486 color -> colour
Santeri Piippo
parents: 3
diff changeset
99 colour = parse_result['colour'],
2
50d3086070df Moved the parsing function into a new file
Santeri Piippo
parents:
diff changeset
100 geometry = LineSegment(*parse_result['vertices']),
50d3086070df Moved the parsing function into a new file
Santeri Piippo
parents:
diff changeset
101 )
50d3086070df Moved the parsing function into a new file
Santeri Piippo
parents:
diff changeset
102
50d3086070df Moved the parsing function into a new file
Santeri Piippo
parents:
diff changeset
103 def parse_ldraw_triangle(line):
50d3086070df Moved the parsing function into a new file
Santeri Piippo
parents:
diff changeset
104 parse_result = generic_parse_polygon(line, type_code = 3, vertex_count = 3)
50d3086070df Moved the parsing function into a new file
Santeri Piippo
parents:
diff changeset
105 return linetypes.Triangle(
4
8eb83f200486 color -> colour
Santeri Piippo
parents: 3
diff changeset
106 colour = parse_result['colour'],
2
50d3086070df Moved the parsing function into a new file
Santeri Piippo
parents:
diff changeset
107 geometry = Polygon(parse_result['vertices']),
50d3086070df Moved the parsing function into a new file
Santeri Piippo
parents:
diff changeset
108 )
50d3086070df Moved the parsing function into a new file
Santeri Piippo
parents:
diff changeset
109
50d3086070df Moved the parsing function into a new file
Santeri Piippo
parents:
diff changeset
110 def parse_ldraw_quadrilateral(line):
50d3086070df Moved the parsing function into a new file
Santeri Piippo
parents:
diff changeset
111 parse_result = generic_parse_polygon(line, type_code = 4, vertex_count = 4)
50d3086070df Moved the parsing function into a new file
Santeri Piippo
parents:
diff changeset
112 return linetypes.Quadrilateral(
4
8eb83f200486 color -> colour
Santeri Piippo
parents: 3
diff changeset
113 colour = parse_result['colour'],
2
50d3086070df Moved the parsing function into a new file
Santeri Piippo
parents:
diff changeset
114 geometry = Polygon(parse_result['vertices']),
50d3086070df Moved the parsing function into a new file
Santeri Piippo
parents:
diff changeset
115 )
50d3086070df Moved the parsing function into a new file
Santeri Piippo
parents:
diff changeset
116
35
865cd526e8b6 cleanup
Teemu Piippo <teemu@hecknology.net>
parents: 29
diff changeset
117 def parse_ldraw_conditional_line(line):
2
50d3086070df Moved the parsing function into a new file
Santeri Piippo
parents:
diff changeset
118 parse_result = generic_parse_polygon(line, type_code = 5, vertex_count = 4)
35
865cd526e8b6 cleanup
Teemu Piippo <teemu@hecknology.net>
parents: 29
diff changeset
119 return linetypes.ConditionalLine(
4
8eb83f200486 color -> colour
Santeri Piippo
parents: 3
diff changeset
120 colour = parse_result['colour'],
2
50d3086070df Moved the parsing function into a new file
Santeri Piippo
parents:
diff changeset
121 geometry = LineSegment(*parse_result['vertices'][0:2]),
50d3086070df Moved the parsing function into a new file
Santeri Piippo
parents:
diff changeset
122 control_points = parse_result['vertices'][2:],
50d3086070df Moved the parsing function into a new file
Santeri Piippo
parents:
diff changeset
123 )

mercurial