parse.py

Sun, 10 Dec 2017 15:46:47 +0200

author
Santeri Piippo
date
Sun, 10 Dec 2017 15:46:47 +0200
changeset 2
50d3086070df
child 3
1dc58f44d556
permissions
-rw-r--r--

Moved the parsing function into a new file

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 *
50d3086070df Moved the parsing function into a new file
Santeri Piippo
parents:
diff changeset
4
50d3086070df Moved the parsing function into a new file
Santeri Piippo
parents:
diff changeset
5 class BadLdrawLine(Exception):
50d3086070df Moved the parsing function into a new file
Santeri Piippo
parents:
diff changeset
6 pass
50d3086070df Moved the parsing function into a new file
Santeri Piippo
parents:
diff changeset
7
50d3086070df Moved the parsing function into a new file
Santeri Piippo
parents:
diff changeset
8 def parse_ldraw_code(line):
50d3086070df Moved the parsing function into a new file
Santeri Piippo
parents:
diff changeset
9 line = line.strip()
50d3086070df Moved the parsing function into a new file
Santeri Piippo
parents:
diff changeset
10 if not line:
50d3086070df Moved the parsing function into a new file
Santeri Piippo
parents:
diff changeset
11 return linetypes.EmptyLine()
50d3086070df Moved the parsing function into a new file
Santeri Piippo
parents:
diff changeset
12 elif line == '0':
50d3086070df Moved the parsing function into a new file
Santeri Piippo
parents:
diff changeset
13 return linetypes.Comment('')
50d3086070df Moved the parsing function into a new file
Santeri Piippo
parents:
diff changeset
14 elif line.startswith('0 '):
50d3086070df Moved the parsing function into a new file
Santeri Piippo
parents:
diff changeset
15 return linetypes.Comment(line[2:].strip())
50d3086070df Moved the parsing function into a new file
Santeri Piippo
parents:
diff changeset
16 elif line.startswith('1 '):
50d3086070df Moved the parsing function into a new file
Santeri Piippo
parents:
diff changeset
17 return parse_ldraw_subfile_reference(line)
50d3086070df Moved the parsing function into a new file
Santeri Piippo
parents:
diff changeset
18 elif line.startswith('2 '):
50d3086070df Moved the parsing function into a new file
Santeri Piippo
parents:
diff changeset
19 return parse_ldraw_line(line)
50d3086070df Moved the parsing function into a new file
Santeri Piippo
parents:
diff changeset
20 elif line.startswith('3 '):
50d3086070df Moved the parsing function into a new file
Santeri Piippo
parents:
diff changeset
21 return parse_ldraw_triangle(line)
50d3086070df Moved the parsing function into a new file
Santeri Piippo
parents:
diff changeset
22 elif line.startswith('4 '):
50d3086070df Moved the parsing function into a new file
Santeri Piippo
parents:
diff changeset
23 return parse_ldraw_quadrilateral(line)
50d3086070df Moved the parsing function into a new file
Santeri Piippo
parents:
diff changeset
24 elif line.startswith('5 '):
50d3086070df Moved the parsing function into a new file
Santeri Piippo
parents:
diff changeset
25 return parse_ldraw_contour(line)
50d3086070df Moved the parsing function into a new file
Santeri Piippo
parents:
diff changeset
26 else:
50d3086070df Moved the parsing function into a new file
Santeri Piippo
parents:
diff changeset
27 raise BadLdrawLine('unknown line type')
50d3086070df Moved the parsing function into a new file
Santeri Piippo
parents:
diff changeset
28
50d3086070df Moved the parsing function into a new file
Santeri Piippo
parents:
diff changeset
29 def parse_ldraw_subfile_reference(line):
50d3086070df Moved the parsing function into a new file
Santeri Piippo
parents:
diff changeset
30 pattern = r'^1\s+(\d+)' + r'\s+([^ ]+)' * (3 + 9 + 1) + r'\s*$'
50d3086070df Moved the parsing function into a new file
Santeri Piippo
parents:
diff changeset
31 match = re.search(pattern, line)
50d3086070df Moved the parsing function into a new file
Santeri Piippo
parents:
diff changeset
32 if not match:
50d3086070df Moved the parsing function into a new file
Santeri Piippo
parents:
diff changeset
33 raise BadLdrawLine('unable to parse')
50d3086070df Moved the parsing function into a new file
Santeri Piippo
parents:
diff changeset
34 groups = list(match.groups())
50d3086070df Moved the parsing function into a new file
Santeri Piippo
parents:
diff changeset
35 indices = {
50d3086070df Moved the parsing function into a new file
Santeri Piippo
parents:
diff changeset
36 'color': 0,
50d3086070df Moved the parsing function into a new file
Santeri Piippo
parents:
diff changeset
37 'anchor': slice(1, 4),
50d3086070df Moved the parsing function into a new file
Santeri Piippo
parents:
diff changeset
38 'matrix': slice(4, 13),
50d3086070df Moved the parsing function into a new file
Santeri Piippo
parents:
diff changeset
39 'subfile_path': 13
50d3086070df Moved the parsing function into a new file
Santeri Piippo
parents:
diff changeset
40 }
50d3086070df Moved the parsing function into a new file
Santeri Piippo
parents:
diff changeset
41 try:
50d3086070df Moved the parsing function into a new file
Santeri Piippo
parents:
diff changeset
42 color = int(groups[indices['color']])
50d3086070df Moved the parsing function into a new file
Santeri Piippo
parents:
diff changeset
43 vertex_values = [float(x) for x in groups[indices['anchor']]]
50d3086070df Moved the parsing function into a new file
Santeri Piippo
parents:
diff changeset
44 matrix_values = [float(x) for x in groups[indices['matrix']]]
50d3086070df Moved the parsing function into a new file
Santeri Piippo
parents:
diff changeset
45 except ValueError:
50d3086070df Moved the parsing function into a new file
Santeri Piippo
parents:
diff changeset
46 raise BadLdrawLine('bad numeric values')
50d3086070df Moved the parsing function into a new file
Santeri Piippo
parents:
diff changeset
47 return linetypes.SubfileReference(
50d3086070df Moved the parsing function into a new file
Santeri Piippo
parents:
diff changeset
48 color = color,
50d3086070df Moved the parsing function into a new file
Santeri Piippo
parents:
diff changeset
49 anchor = Vertex(*vertex_values),
50d3086070df Moved the parsing function into a new file
Santeri Piippo
parents:
diff changeset
50 matrix = TransformationMatrix(matrix_values),
50d3086070df Moved the parsing function into a new file
Santeri Piippo
parents:
diff changeset
51 subfile_path = groups[indices['subfile_path']]
50d3086070df Moved the parsing function into a new file
Santeri Piippo
parents:
diff changeset
52 )
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 def generic_parse_polygon(line, *, type_code, vertex_count):
50d3086070df Moved the parsing function into a new file
Santeri Piippo
parents:
diff changeset
55 pattern = r'^' \
50d3086070df Moved the parsing function into a new file
Santeri Piippo
parents:
diff changeset
56 + str(type_code) \
50d3086070df Moved the parsing function into a new file
Santeri Piippo
parents:
diff changeset
57 + '\s+(\d+)' \
50d3086070df Moved the parsing function into a new file
Santeri Piippo
parents:
diff changeset
58 + r'\s+([^ ]+)' * (vertex_count * 3) \
50d3086070df Moved the parsing function into a new file
Santeri Piippo
parents:
diff changeset
59 + r'\s*$'
50d3086070df Moved the parsing function into a new file
Santeri Piippo
parents:
diff changeset
60 match = re.search(pattern, line)
50d3086070df Moved the parsing function into a new file
Santeri Piippo
parents:
diff changeset
61 if not match:
50d3086070df Moved the parsing function into a new file
Santeri Piippo
parents:
diff changeset
62 raise BadLdrawLine(str.format('cannot parse type-{} line', type_code))
50d3086070df Moved the parsing function into a new file
Santeri Piippo
parents:
diff changeset
63 vertices = []
50d3086070df Moved the parsing function into a new file
Santeri Piippo
parents:
diff changeset
64 for vertex_index in range(vertex_count):
50d3086070df Moved the parsing function into a new file
Santeri Piippo
parents:
diff changeset
65 slice_begin = 1 + vertex_index * 3
50d3086070df Moved the parsing function into a new file
Santeri Piippo
parents:
diff changeset
66 slice_end = 1 + (vertex_index + 1) * 3
50d3086070df Moved the parsing function into a new file
Santeri Piippo
parents:
diff changeset
67 coordinates = match.groups()[slice_begin:slice_end]
50d3086070df Moved the parsing function into a new file
Santeri Piippo
parents:
diff changeset
68 assert(len(coordinates) == 3)
50d3086070df Moved the parsing function into a new file
Santeri Piippo
parents:
diff changeset
69 try:
50d3086070df Moved the parsing function into a new file
Santeri Piippo
parents:
diff changeset
70 coordinates = [float(x) for x in coordinates]
50d3086070df Moved the parsing function into a new file
Santeri Piippo
parents:
diff changeset
71 except ValueError:
50d3086070df Moved the parsing function into a new file
Santeri Piippo
parents:
diff changeset
72 raise BadLdrawLine('bad numeric values')
50d3086070df Moved the parsing function into a new file
Santeri Piippo
parents:
diff changeset
73 vertices.append(Vertex(*coordinates))
50d3086070df Moved the parsing function into a new file
Santeri Piippo
parents:
diff changeset
74 return {
50d3086070df Moved the parsing function into a new file
Santeri Piippo
parents:
diff changeset
75 'color': int(match.group(1)),
50d3086070df Moved the parsing function into a new file
Santeri Piippo
parents:
diff changeset
76 'vertices': vertices,
50d3086070df Moved the parsing function into a new file
Santeri Piippo
parents:
diff changeset
77 }
50d3086070df Moved the parsing function into a new file
Santeri Piippo
parents:
diff changeset
78
50d3086070df Moved the parsing function into a new file
Santeri Piippo
parents:
diff changeset
79 def parse_ldraw_line(line):
50d3086070df Moved the parsing function into a new file
Santeri Piippo
parents:
diff changeset
80 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
81 return linetypes.LineSegment(
50d3086070df Moved the parsing function into a new file
Santeri Piippo
parents:
diff changeset
82 color = parse_result['color'],
50d3086070df Moved the parsing function into a new file
Santeri Piippo
parents:
diff changeset
83 geometry = LineSegment(*parse_result['vertices']),
50d3086070df Moved the parsing function into a new file
Santeri Piippo
parents:
diff changeset
84 )
50d3086070df Moved the parsing function into a new file
Santeri Piippo
parents:
diff changeset
85
50d3086070df Moved the parsing function into a new file
Santeri Piippo
parents:
diff changeset
86 def parse_ldraw_triangle(line):
50d3086070df Moved the parsing function into a new file
Santeri Piippo
parents:
diff changeset
87 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
88 return linetypes.Triangle(
50d3086070df Moved the parsing function into a new file
Santeri Piippo
parents:
diff changeset
89 color = parse_result['color'],
50d3086070df Moved the parsing function into a new file
Santeri Piippo
parents:
diff changeset
90 geometry = Polygon(parse_result['vertices']),
50d3086070df Moved the parsing function into a new file
Santeri Piippo
parents:
diff changeset
91 )
50d3086070df Moved the parsing function into a new file
Santeri Piippo
parents:
diff changeset
92
50d3086070df Moved the parsing function into a new file
Santeri Piippo
parents:
diff changeset
93 def parse_ldraw_quadrilateral(line):
50d3086070df Moved the parsing function into a new file
Santeri Piippo
parents:
diff changeset
94 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
95 return linetypes.Quadrilateral(
50d3086070df Moved the parsing function into a new file
Santeri Piippo
parents:
diff changeset
96 color = parse_result['color'],
50d3086070df Moved the parsing function into a new file
Santeri Piippo
parents:
diff changeset
97 geometry = Polygon(parse_result['vertices']),
50d3086070df Moved the parsing function into a new file
Santeri Piippo
parents:
diff changeset
98 )
50d3086070df Moved the parsing function into a new file
Santeri Piippo
parents:
diff changeset
99
50d3086070df Moved the parsing function into a new file
Santeri Piippo
parents:
diff changeset
100 def parse_ldraw_contour(line):
50d3086070df Moved the parsing function into a new file
Santeri Piippo
parents:
diff changeset
101 parse_result = generic_parse_polygon(line, type_code = 5, vertex_count = 4)
50d3086070df Moved the parsing function into a new file
Santeri Piippo
parents:
diff changeset
102 return linetypes.Contour(
50d3086070df Moved the parsing function into a new file
Santeri Piippo
parents:
diff changeset
103 color = parse_result['color'],
50d3086070df Moved the parsing function into a new file
Santeri Piippo
parents:
diff changeset
104 geometry = LineSegment(*parse_result['vertices'][0:2]),
50d3086070df Moved the parsing function into a new file
Santeri Piippo
parents:
diff changeset
105 control_points = parse_result['vertices'][2:],
50d3086070df Moved the parsing function into a new file
Santeri Piippo
parents:
diff changeset
106 )

mercurial