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