Sat, 08 Jun 2019 11:17:17 +0300
cleanup
65 | 1 | #!/usr/bin/env python3 |
2 | ''' | |
3 | Classes for formally storing LDraw objects | |
4 | ''' | |
5 | ||
3
1dc58f44d556
Can now write dat files, added direct color handling
Santeri Piippo
parents:
1
diff
changeset
|
6 | def ldraw_str(value): |
1dc58f44d556
Can now write dat files, added direct color handling
Santeri Piippo
parents:
1
diff
changeset
|
7 | ' Like str() except removes unneeded ".0"-suffixes ' |
1dc58f44d556
Can now write dat files, added direct color handling
Santeri Piippo
parents:
1
diff
changeset
|
8 | rep = str(value) |
1dc58f44d556
Can now write dat files, added direct color handling
Santeri Piippo
parents:
1
diff
changeset
|
9 | if isinstance(value, float): |
1dc58f44d556
Can now write dat files, added direct color handling
Santeri Piippo
parents:
1
diff
changeset
|
10 | if rep.endswith('.0'): |
1dc58f44d556
Can now write dat files, added direct color handling
Santeri Piippo
parents:
1
diff
changeset
|
11 | rep = rep[:-2] |
1dc58f44d556
Can now write dat files, added direct color handling
Santeri Piippo
parents:
1
diff
changeset
|
12 | if rep == '-0': |
1dc58f44d556
Can now write dat files, added direct color handling
Santeri Piippo
parents:
1
diff
changeset
|
13 | rep = '0' |
1dc58f44d556
Can now write dat files, added direct color handling
Santeri Piippo
parents:
1
diff
changeset
|
14 | return rep |
1dc58f44d556
Can now write dat files, added direct color handling
Santeri Piippo
parents:
1
diff
changeset
|
15 | |
0 | 16 | class EmptyLine: |
17 | def __repr__(self): | |
18 | return 'linetypes.EmptyLine()' | |
3
1dc58f44d556
Can now write dat files, added direct color handling
Santeri Piippo
parents:
1
diff
changeset
|
19 | def textual_representation(self): |
1dc58f44d556
Can now write dat files, added direct color handling
Santeri Piippo
parents:
1
diff
changeset
|
20 | return '' |
7 | 21 | def typename(self): |
22 | return 'empty line' | |
0 | 23 | |
38
66c9591b733d
added proper handling of syntax errors
Teemu Piippo <teemu@hecknology.net>
parents:
35
diff
changeset
|
24 | class MetaCommand: |
66c9591b733d
added proper handling of syntax errors
Teemu Piippo <teemu@hecknology.net>
parents:
35
diff
changeset
|
25 | def __init__(self, text, style = 'old'): |
66c9591b733d
added proper handling of syntax errors
Teemu Piippo <teemu@hecknology.net>
parents:
35
diff
changeset
|
26 | self.text = text |
66c9591b733d
added proper handling of syntax errors
Teemu Piippo <teemu@hecknology.net>
parents:
35
diff
changeset
|
27 | def __repr__(self): |
66c9591b733d
added proper handling of syntax errors
Teemu Piippo <teemu@hecknology.net>
parents:
35
diff
changeset
|
28 | return str.format('linetypes.MetaCommand({text!r})', |
66c9591b733d
added proper handling of syntax errors
Teemu Piippo <teemu@hecknology.net>
parents:
35
diff
changeset
|
29 | text = self.text, |
66c9591b733d
added proper handling of syntax errors
Teemu Piippo <teemu@hecknology.net>
parents:
35
diff
changeset
|
30 | ) |
66c9591b733d
added proper handling of syntax errors
Teemu Piippo <teemu@hecknology.net>
parents:
35
diff
changeset
|
31 | def textual_representation(self): |
66c9591b733d
added proper handling of syntax errors
Teemu Piippo <teemu@hecknology.net>
parents:
35
diff
changeset
|
32 | return ('0 ' + self.text).strip() |
66c9591b733d
added proper handling of syntax errors
Teemu Piippo <teemu@hecknology.net>
parents:
35
diff
changeset
|
33 | def typename(self): |
66c9591b733d
added proper handling of syntax errors
Teemu Piippo <teemu@hecknology.net>
parents:
35
diff
changeset
|
34 | return 'metacommand' |
66c9591b733d
added proper handling of syntax errors
Teemu Piippo <teemu@hecknology.net>
parents:
35
diff
changeset
|
35 | |
0 | 36 | class Comment: |
37 | def __init__(self, text, style = 'old'): | |
38
66c9591b733d
added proper handling of syntax errors
Teemu Piippo <teemu@hecknology.net>
parents:
35
diff
changeset
|
38 | self.text = text |
0 | 39 | def __repr__(self): |
38
66c9591b733d
added proper handling of syntax errors
Teemu Piippo <teemu@hecknology.net>
parents:
35
diff
changeset
|
40 | return str.format('linetypes.Comment({text!r})', |
0 | 41 | text = self.text, |
42 | ) | |
3
1dc58f44d556
Can now write dat files, added direct color handling
Santeri Piippo
parents:
1
diff
changeset
|
43 | def textual_representation(self): |
38
66c9591b733d
added proper handling of syntax errors
Teemu Piippo <teemu@hecknology.net>
parents:
35
diff
changeset
|
44 | return '0 //' + self.text |
7 | 45 | def typename(self): |
46 | return 'comment' | |
0 | 47 | |
48 | class SubfileReference: | |
4 | 49 | def __init__(self, *, colour, subfile_path, anchor, matrix): |
50 | self.colour, self.subfile_path, = colour, subfile_path | |
0 | 51 | self.anchor, self.matrix = anchor, matrix |
52 | def __repr__(self): | |
53 | return str.format('linetypes.SubfileReference(' \ | |
4 | 54 | 'colour = {colour!r}, ' \ |
0 | 55 | 'subfile_path = {subfile_path!r}, ' \ |
56 | 'anchor = {anchor!r}, ' \ | |
57 | 'matrix = {matrix!r})', **self.__dict__) | |
3
1dc58f44d556
Can now write dat files, added direct color handling
Santeri Piippo
parents:
1
diff
changeset
|
58 | def textual_representation(self): |
1dc58f44d556
Can now write dat files, added direct color handling
Santeri Piippo
parents:
1
diff
changeset
|
59 | args = [ |
1dc58f44d556
Can now write dat files, added direct color handling
Santeri Piippo
parents:
1
diff
changeset
|
60 | 1, |
4 | 61 | self.colour, |
3
1dc58f44d556
Can now write dat files, added direct color handling
Santeri Piippo
parents:
1
diff
changeset
|
62 | *self.anchor.coordinates, |
1dc58f44d556
Can now write dat files, added direct color handling
Santeri Piippo
parents:
1
diff
changeset
|
63 | *self.matrix.values, |
1dc58f44d556
Can now write dat files, added direct color handling
Santeri Piippo
parents:
1
diff
changeset
|
64 | self.subfile_path, |
1dc58f44d556
Can now write dat files, added direct color handling
Santeri Piippo
parents:
1
diff
changeset
|
65 | ] |
1dc58f44d556
Can now write dat files, added direct color handling
Santeri Piippo
parents:
1
diff
changeset
|
66 | return ' '.join(ldraw_str(arg) for arg in args) |
7 | 67 | def typename(self): |
68 | return 'subfile reference' | |
0 | 69 | |
3
1dc58f44d556
Can now write dat files, added direct color handling
Santeri Piippo
parents:
1
diff
changeset
|
70 | class BasePolygon: |
4 | 71 | def __init__(self, *, colour, geometry): |
72 | self.colour, self.geometry = colour, geometry | |
0 | 73 | def __repr__(self): |
74 | return str.format('linetypes.{typename}(' \ | |
4 | 75 | 'colour = {colour!r}, ' \ |
0 | 76 | 'geometry = {geometry!r})', |
77 | typename = type(self).__name__, | |
4 | 78 | colour = self.colour, |
0 | 79 | geometry = self.geometry, |
80 | ) | |
3
1dc58f44d556
Can now write dat files, added direct color handling
Santeri Piippo
parents:
1
diff
changeset
|
81 | def base_textual_representation(self): |
4 | 82 | args = [self.colour] |
3
1dc58f44d556
Can now write dat files, added direct color handling
Santeri Piippo
parents:
1
diff
changeset
|
83 | for vertex in self.geometry.vertices: |
1dc58f44d556
Can now write dat files, added direct color handling
Santeri Piippo
parents:
1
diff
changeset
|
84 | args += vertex.coordinates |
1dc58f44d556
Can now write dat files, added direct color handling
Santeri Piippo
parents:
1
diff
changeset
|
85 | return ' '.join(ldraw_str(arg) for arg in args) |
0 | 86 | |
3
1dc58f44d556
Can now write dat files, added direct color handling
Santeri Piippo
parents:
1
diff
changeset
|
87 | class LineSegment(BasePolygon): |
1dc58f44d556
Can now write dat files, added direct color handling
Santeri Piippo
parents:
1
diff
changeset
|
88 | def textual_representation(self): |
1dc58f44d556
Can now write dat files, added direct color handling
Santeri Piippo
parents:
1
diff
changeset
|
89 | return '2 ' + self.base_textual_representation() |
7 | 90 | def typename(self): |
91 | return 'line segment' | |
0 | 92 | |
3
1dc58f44d556
Can now write dat files, added direct color handling
Santeri Piippo
parents:
1
diff
changeset
|
93 | class Triangle(BasePolygon): |
1dc58f44d556
Can now write dat files, added direct color handling
Santeri Piippo
parents:
1
diff
changeset
|
94 | def textual_representation(self): |
1dc58f44d556
Can now write dat files, added direct color handling
Santeri Piippo
parents:
1
diff
changeset
|
95 | return '3 ' + self.base_textual_representation() |
7 | 96 | def typename(self): |
97 | return 'triangle' | |
3
1dc58f44d556
Can now write dat files, added direct color handling
Santeri Piippo
parents:
1
diff
changeset
|
98 | |
1dc58f44d556
Can now write dat files, added direct color handling
Santeri Piippo
parents:
1
diff
changeset
|
99 | class Quadrilateral(BasePolygon): |
1dc58f44d556
Can now write dat files, added direct color handling
Santeri Piippo
parents:
1
diff
changeset
|
100 | def textual_representation(self): |
1dc58f44d556
Can now write dat files, added direct color handling
Santeri Piippo
parents:
1
diff
changeset
|
101 | return '4 ' + self.base_textual_representation() |
7 | 102 | def typename(self): |
103 | return 'quadrilateral' | |
1 | 104 | |
35 | 105 | class ConditionalLine(LineSegment): |
4 | 106 | def __init__(self, *, colour, geometry, control_points): |
107 | super().__init__(colour = colour, geometry = geometry) | |
1 | 108 | self.control_points = control_points |
109 | assert(len(self.control_points) == 2) | |
110 | def __repr__(self): | |
35 | 111 | return str.format('linetypes.ConditionalLine(' \ |
4 | 112 | 'colour = {colour!r}, ' \ |
1 | 113 | 'geometry = {geometry!r}, ' \ |
114 | 'control_points = {control_points!r})', **self.__dict__) | |
3
1dc58f44d556
Can now write dat files, added direct color handling
Santeri Piippo
parents:
1
diff
changeset
|
115 | def textual_representation(self): |
1dc58f44d556
Can now write dat files, added direct color handling
Santeri Piippo
parents:
1
diff
changeset
|
116 | result = '5 ' + self.base_textual_representation() |
1dc58f44d556
Can now write dat files, added direct color handling
Santeri Piippo
parents:
1
diff
changeset
|
117 | for control_point in self.control_points: |
1dc58f44d556
Can now write dat files, added direct color handling
Santeri Piippo
parents:
1
diff
changeset
|
118 | strings = (ldraw_str(value) for value in control_point.coordinates) |
1dc58f44d556
Can now write dat files, added direct color handling
Santeri Piippo
parents:
1
diff
changeset
|
119 | result += ' ' |
1dc58f44d556
Can now write dat files, added direct color handling
Santeri Piippo
parents:
1
diff
changeset
|
120 | result += ' '.join(strings) |
1dc58f44d556
Can now write dat files, added direct color handling
Santeri Piippo
parents:
1
diff
changeset
|
121 | return result |
7 | 122 | def typename(self): |
123 | return 'contour line segment' | |
38
66c9591b733d
added proper handling of syntax errors
Teemu Piippo <teemu@hecknology.net>
parents:
35
diff
changeset
|
124 | |
66c9591b733d
added proper handling of syntax errors
Teemu Piippo <teemu@hecknology.net>
parents:
35
diff
changeset
|
125 | class Error: |
66c9591b733d
added proper handling of syntax errors
Teemu Piippo <teemu@hecknology.net>
parents:
35
diff
changeset
|
126 | def __init__(self, line, reason): |
66c9591b733d
added proper handling of syntax errors
Teemu Piippo <teemu@hecknology.net>
parents:
35
diff
changeset
|
127 | self.line = line |
66c9591b733d
added proper handling of syntax errors
Teemu Piippo <teemu@hecknology.net>
parents:
35
diff
changeset
|
128 | self.reason = reason |
66c9591b733d
added proper handling of syntax errors
Teemu Piippo <teemu@hecknology.net>
parents:
35
diff
changeset
|
129 | def __repr__(self): |
66c9591b733d
added proper handling of syntax errors
Teemu Piippo <teemu@hecknology.net>
parents:
35
diff
changeset
|
130 | return str.format( |
66c9591b733d
added proper handling of syntax errors
Teemu Piippo <teemu@hecknology.net>
parents:
35
diff
changeset
|
131 | 'linetypes.Error(line = {line!r}, reason = {reason!r})', |
66c9591b733d
added proper handling of syntax errors
Teemu Piippo <teemu@hecknology.net>
parents:
35
diff
changeset
|
132 | line = self.line, |
66c9591b733d
added proper handling of syntax errors
Teemu Piippo <teemu@hecknology.net>
parents:
35
diff
changeset
|
133 | reason = self.reason, |
66c9591b733d
added proper handling of syntax errors
Teemu Piippo <teemu@hecknology.net>
parents:
35
diff
changeset
|
134 | ) |
66c9591b733d
added proper handling of syntax errors
Teemu Piippo <teemu@hecknology.net>
parents:
35
diff
changeset
|
135 | def textual_representation(self): |
66c9591b733d
added proper handling of syntax errors
Teemu Piippo <teemu@hecknology.net>
parents:
35
diff
changeset
|
136 | return self.line |