parse.py

Wed, 31 Jan 2018 14:50:19 +0200

author
Santeri Piippo
date
Wed, 31 Jan 2018 14:50:19 +0200
changeset 41
4d87bc126368
parent 29
db6ca177c6c4
permissions
-rw-r--r--

don't crash and burn if someone sends something that's not LDraw

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
2
50d3086070df Moved the parsing function into a new file
Santeri Piippo
parents:
diff changeset
5
50d3086070df Moved the parsing function into a new file
Santeri Piippo
parents:
diff changeset
6 class BadLdrawLine(Exception):
50d3086070df Moved the parsing function into a new file
Santeri Piippo
parents:
diff changeset
7 pass
50d3086070df Moved the parsing function into a new file
Santeri Piippo
parents:
diff changeset
8
50d3086070df Moved the parsing function into a new file
Santeri Piippo
parents:
diff changeset
9 def parse_ldraw_code(line):
29
db6ca177c6c4 added a simple web frontend
Santeri Piippo
parents: 19
diff changeset
10 if isinstance(line, bytes):
41
4d87bc126368 don't crash and burn if someone sends something that's not LDraw
Santeri Piippo
parents: 29
diff changeset
11 try:
4d87bc126368 don't crash and burn if someone sends something that's not LDraw
Santeri Piippo
parents: 29
diff changeset
12 line = line.decode()
4d87bc126368 don't crash and burn if someone sends something that's not LDraw
Santeri Piippo
parents: 29
diff changeset
13 except UnicodeDecodeError:
4d87bc126368 don't crash and burn if someone sends something that's not LDraw
Santeri Piippo
parents: 29
diff changeset
14 raise BadLdrawLine("bad unicode")
2
50d3086070df Moved the parsing function into a new file
Santeri Piippo
parents:
diff changeset
15 line = line.strip()
50d3086070df Moved the parsing function into a new file
Santeri Piippo
parents:
diff changeset
16 if not line:
19
9169bad392c4 better handling of ldraw code
Santeri Piippo
parents: 17
diff changeset
17 return linetypes.EmptyLine()
2
50d3086070df Moved the parsing function into a new file
Santeri Piippo
parents:
diff changeset
18 elif line == '0':
19
9169bad392c4 better handling of ldraw code
Santeri Piippo
parents: 17
diff changeset
19 return linetypes.Comment('')
2
50d3086070df Moved the parsing function into a new file
Santeri Piippo
parents:
diff changeset
20 elif line.startswith('0 '):
19
9169bad392c4 better handling of ldraw code
Santeri Piippo
parents: 17
diff changeset
21 return linetypes.Comment(line[2:].strip())
2
50d3086070df Moved the parsing function into a new file
Santeri Piippo
parents:
diff changeset
22 elif line.startswith('1 '):
19
9169bad392c4 better handling of ldraw code
Santeri Piippo
parents: 17
diff changeset
23 return parse_ldraw_subfile_reference(line)
2
50d3086070df Moved the parsing function into a new file
Santeri Piippo
parents:
diff changeset
24 elif line.startswith('2 '):
19
9169bad392c4 better handling of ldraw code
Santeri Piippo
parents: 17
diff changeset
25 return parse_ldraw_line(line)
2
50d3086070df Moved the parsing function into a new file
Santeri Piippo
parents:
diff changeset
26 elif line.startswith('3 '):
19
9169bad392c4 better handling of ldraw code
Santeri Piippo
parents: 17
diff changeset
27 return parse_ldraw_triangle(line)
2
50d3086070df Moved the parsing function into a new file
Santeri Piippo
parents:
diff changeset
28 elif line.startswith('4 '):
19
9169bad392c4 better handling of ldraw code
Santeri Piippo
parents: 17
diff changeset
29 return parse_ldraw_quadrilateral(line)
2
50d3086070df Moved the parsing function into a new file
Santeri Piippo
parents:
diff changeset
30 elif line.startswith('5 '):
19
9169bad392c4 better handling of ldraw code
Santeri Piippo
parents: 17
diff changeset
31 return parse_ldraw_contour(line)
2
50d3086070df Moved the parsing function into a new file
Santeri Piippo
parents:
diff changeset
32 else:
50d3086070df Moved the parsing function into a new file
Santeri Piippo
parents:
diff changeset
33 raise BadLdrawLine('unknown line type')
50d3086070df Moved the parsing function into a new file
Santeri Piippo
parents:
diff changeset
34
50d3086070df Moved the parsing function into a new file
Santeri Piippo
parents:
diff changeset
35 def parse_ldraw_subfile_reference(line):
3
1dc58f44d556 Can now write dat files, added direct color handling
Santeri Piippo
parents: 2
diff changeset
36 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
37 match = re.search(pattern, line)
50d3086070df Moved the parsing function into a new file
Santeri Piippo
parents:
diff changeset
38 if not match:
50d3086070df Moved the parsing function into a new file
Santeri Piippo
parents:
diff changeset
39 raise BadLdrawLine('unable to parse')
50d3086070df Moved the parsing function into a new file
Santeri Piippo
parents:
diff changeset
40 groups = list(match.groups())
50d3086070df Moved the parsing function into a new file
Santeri Piippo
parents:
diff changeset
41 indices = {
4
8eb83f200486 color -> colour
Santeri Piippo
parents: 3
diff changeset
42 'colour_index': 0,
2
50d3086070df Moved the parsing function into a new file
Santeri Piippo
parents:
diff changeset
43 'anchor': slice(1, 4),
50d3086070df Moved the parsing function into a new file
Santeri Piippo
parents:
diff changeset
44 'matrix': slice(4, 13),
50d3086070df Moved the parsing function into a new file
Santeri Piippo
parents:
diff changeset
45 'subfile_path': 13
50d3086070df Moved the parsing function into a new file
Santeri Piippo
parents:
diff changeset
46 }
50d3086070df Moved the parsing function into a new file
Santeri Piippo
parents:
diff changeset
47 try:
4
8eb83f200486 color -> colour
Santeri Piippo
parents: 3
diff changeset
48 colour = Colour(groups[indices['colour_index']])
2
50d3086070df Moved the parsing function into a new file
Santeri Piippo
parents:
diff changeset
49 vertex_values = [float(x) for x in groups[indices['anchor']]]
50d3086070df Moved the parsing function into a new file
Santeri Piippo
parents:
diff changeset
50 matrix_values = [float(x) for x in groups[indices['matrix']]]
50d3086070df Moved the parsing function into a new file
Santeri Piippo
parents:
diff changeset
51 except ValueError:
50d3086070df Moved the parsing function into a new file
Santeri Piippo
parents:
diff changeset
52 raise BadLdrawLine('bad numeric values')
50d3086070df Moved the parsing function into a new file
Santeri Piippo
parents:
diff changeset
53 return linetypes.SubfileReference(
4
8eb83f200486 color -> colour
Santeri Piippo
parents: 3
diff changeset
54 colour = colour,
2
50d3086070df Moved the parsing function into a new file
Santeri Piippo
parents:
diff changeset
55 anchor = Vertex(*vertex_values),
6
6da1e81c5652 Added code to compute areas of triangles and quadrilaterals
Santeri Piippo
parents: 4
diff changeset
56 matrix = Matrix3x3(matrix_values),
2
50d3086070df Moved the parsing function into a new file
Santeri Piippo
parents:
diff changeset
57 subfile_path = groups[indices['subfile_path']]
50d3086070df Moved the parsing function into a new file
Santeri Piippo
parents:
diff changeset
58 )
50d3086070df Moved the parsing function into a new file
Santeri Piippo
parents:
diff changeset
59
50d3086070df Moved the parsing function into a new file
Santeri Piippo
parents:
diff changeset
60 def generic_parse_polygon(line, *, type_code, vertex_count):
50d3086070df Moved the parsing function into a new file
Santeri Piippo
parents:
diff changeset
61 pattern = r'^' \
50d3086070df Moved the parsing function into a new file
Santeri Piippo
parents:
diff changeset
62 + str(type_code) \
3
1dc58f44d556 Can now write dat files, added direct color handling
Santeri Piippo
parents: 2
diff changeset
63 + '\s+([^ ]+)' \
2
50d3086070df Moved the parsing function into a new file
Santeri Piippo
parents:
diff changeset
64 + r'\s+([^ ]+)' * (vertex_count * 3) \
50d3086070df Moved the parsing function into a new file
Santeri Piippo
parents:
diff changeset
65 + r'\s*$'
50d3086070df Moved the parsing function into a new file
Santeri Piippo
parents:
diff changeset
66 match = re.search(pattern, line)
50d3086070df Moved the parsing function into a new file
Santeri Piippo
parents:
diff changeset
67 if not match:
50d3086070df Moved the parsing function into a new file
Santeri Piippo
parents:
diff changeset
68 raise BadLdrawLine(str.format('cannot parse type-{} line', type_code))
50d3086070df Moved the parsing function into a new file
Santeri Piippo
parents:
diff changeset
69 vertices = []
50d3086070df Moved the parsing function into a new file
Santeri Piippo
parents:
diff changeset
70 for vertex_index in range(vertex_count):
50d3086070df Moved the parsing function into a new file
Santeri Piippo
parents:
diff changeset
71 slice_begin = 1 + vertex_index * 3
50d3086070df Moved the parsing function into a new file
Santeri Piippo
parents:
diff changeset
72 slice_end = 1 + (vertex_index + 1) * 3
50d3086070df Moved the parsing function into a new file
Santeri Piippo
parents:
diff changeset
73 coordinates = match.groups()[slice_begin:slice_end]
50d3086070df Moved the parsing function into a new file
Santeri Piippo
parents:
diff changeset
74 assert(len(coordinates) == 3)
50d3086070df Moved the parsing function into a new file
Santeri Piippo
parents:
diff changeset
75 try:
50d3086070df Moved the parsing function into a new file
Santeri Piippo
parents:
diff changeset
76 coordinates = [float(x) for x in coordinates]
50d3086070df Moved the parsing function into a new file
Santeri Piippo
parents:
diff changeset
77 except ValueError:
50d3086070df Moved the parsing function into a new file
Santeri Piippo
parents:
diff changeset
78 raise BadLdrawLine('bad numeric values')
50d3086070df Moved the parsing function into a new file
Santeri Piippo
parents:
diff changeset
79 vertices.append(Vertex(*coordinates))
50d3086070df Moved the parsing function into a new file
Santeri Piippo
parents:
diff changeset
80 return {
4
8eb83f200486 color -> colour
Santeri Piippo
parents: 3
diff changeset
81 'colour': Colour(match.group(1)),
2
50d3086070df Moved the parsing function into a new file
Santeri Piippo
parents:
diff changeset
82 'vertices': vertices,
50d3086070df Moved the parsing function into a new file
Santeri Piippo
parents:
diff changeset
83 }
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 def parse_ldraw_line(line):
50d3086070df Moved the parsing function into a new file
Santeri Piippo
parents:
diff changeset
86 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
87 return linetypes.LineSegment(
4
8eb83f200486 color -> colour
Santeri Piippo
parents: 3
diff changeset
88 colour = parse_result['colour'],
2
50d3086070df Moved the parsing function into a new file
Santeri Piippo
parents:
diff changeset
89 geometry = LineSegment(*parse_result['vertices']),
50d3086070df Moved the parsing function into a new file
Santeri Piippo
parents:
diff changeset
90 )
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 def parse_ldraw_triangle(line):
50d3086070df Moved the parsing function into a new file
Santeri Piippo
parents:
diff changeset
93 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
94 return linetypes.Triangle(
4
8eb83f200486 color -> colour
Santeri Piippo
parents: 3
diff changeset
95 colour = parse_result['colour'],
2
50d3086070df Moved the parsing function into a new file
Santeri Piippo
parents:
diff changeset
96 geometry = Polygon(parse_result['vertices']),
50d3086070df Moved the parsing function into a new file
Santeri Piippo
parents:
diff changeset
97 )
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 def parse_ldraw_quadrilateral(line):
50d3086070df Moved the parsing function into a new file
Santeri Piippo
parents:
diff changeset
100 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
101 return linetypes.Quadrilateral(
4
8eb83f200486 color -> colour
Santeri Piippo
parents: 3
diff changeset
102 colour = parse_result['colour'],
2
50d3086070df Moved the parsing function into a new file
Santeri Piippo
parents:
diff changeset
103 geometry = Polygon(parse_result['vertices']),
50d3086070df Moved the parsing function into a new file
Santeri Piippo
parents:
diff changeset
104 )
50d3086070df Moved the parsing function into a new file
Santeri Piippo
parents:
diff changeset
105
50d3086070df Moved the parsing function into a new file
Santeri Piippo
parents:
diff changeset
106 def parse_ldraw_contour(line):
50d3086070df Moved the parsing function into a new file
Santeri Piippo
parents:
diff changeset
107 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
108 return linetypes.Contour(
4
8eb83f200486 color -> colour
Santeri Piippo
parents: 3
diff changeset
109 colour = parse_result['colour'],
2
50d3086070df Moved the parsing function into a new file
Santeri Piippo
parents:
diff changeset
110 geometry = LineSegment(*parse_result['vertices'][0:2]),
50d3086070df Moved the parsing function into a new file
Santeri Piippo
parents:
diff changeset
111 control_points = parse_result['vertices'][2:],
50d3086070df Moved the parsing function into a new file
Santeri Piippo
parents:
diff changeset
112 )

mercurial