Thu, 27 Aug 2020 00:29:31 +0300
..., part two
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 '' |
0 | 16 | |
38
66c9591b733d
added proper handling of syntax errors
Teemu Piippo <teemu@hecknology.net>
parents:
35
diff
changeset
|
17 | class MetaCommand: |
66c9591b733d
added proper handling of syntax errors
Teemu Piippo <teemu@hecknology.net>
parents:
35
diff
changeset
|
18 | def __init__(self, text, style = 'old'): |
66c9591b733d
added proper handling of syntax errors
Teemu Piippo <teemu@hecknology.net>
parents:
35
diff
changeset
|
19 | self.text = text |
66c9591b733d
added proper handling of syntax errors
Teemu Piippo <teemu@hecknology.net>
parents:
35
diff
changeset
|
20 | def __repr__(self): |
66c9591b733d
added proper handling of syntax errors
Teemu Piippo <teemu@hecknology.net>
parents:
35
diff
changeset
|
21 | return str.format('linetypes.MetaCommand({text!r})', |
66c9591b733d
added proper handling of syntax errors
Teemu Piippo <teemu@hecknology.net>
parents:
35
diff
changeset
|
22 | text = self.text, |
66c9591b733d
added proper handling of syntax errors
Teemu Piippo <teemu@hecknology.net>
parents:
35
diff
changeset
|
23 | ) |
66c9591b733d
added proper handling of syntax errors
Teemu Piippo <teemu@hecknology.net>
parents:
35
diff
changeset
|
24 | def textual_representation(self): |
66c9591b733d
added proper handling of syntax errors
Teemu Piippo <teemu@hecknology.net>
parents:
35
diff
changeset
|
25 | return ('0 ' + self.text).strip() |
66c9591b733d
added proper handling of syntax errors
Teemu Piippo <teemu@hecknology.net>
parents:
35
diff
changeset
|
26 | |
0 | 27 | class Comment: |
28 | def __init__(self, text, style = 'old'): | |
38
66c9591b733d
added proper handling of syntax errors
Teemu Piippo <teemu@hecknology.net>
parents:
35
diff
changeset
|
29 | self.text = text |
0 | 30 | def __repr__(self): |
38
66c9591b733d
added proper handling of syntax errors
Teemu Piippo <teemu@hecknology.net>
parents:
35
diff
changeset
|
31 | return str.format('linetypes.Comment({text!r})', |
0 | 32 | text = self.text, |
33 | ) | |
3
1dc58f44d556
Can now write dat files, added direct color handling
Santeri Piippo
parents:
1
diff
changeset
|
34 | def textual_representation(self): |
38
66c9591b733d
added proper handling of syntax errors
Teemu Piippo <teemu@hecknology.net>
parents:
35
diff
changeset
|
35 | return '0 //' + self.text |
0 | 36 | |
37 | class SubfileReference: | |
4 | 38 | def __init__(self, *, colour, subfile_path, anchor, matrix): |
39 | self.colour, self.subfile_path, = colour, subfile_path | |
0 | 40 | self.anchor, self.matrix = anchor, matrix |
41 | def __repr__(self): | |
42 | return str.format('linetypes.SubfileReference(' \ | |
4 | 43 | 'colour = {colour!r}, ' \ |
0 | 44 | 'subfile_path = {subfile_path!r}, ' \ |
45 | 'anchor = {anchor!r}, ' \ | |
46 | 'matrix = {matrix!r})', **self.__dict__) | |
3
1dc58f44d556
Can now write dat files, added direct color handling
Santeri Piippo
parents:
1
diff
changeset
|
47 | def textual_representation(self): |
1dc58f44d556
Can now write dat files, added direct color handling
Santeri Piippo
parents:
1
diff
changeset
|
48 | args = [ |
1dc58f44d556
Can now write dat files, added direct color handling
Santeri Piippo
parents:
1
diff
changeset
|
49 | 1, |
4 | 50 | self.colour, |
3
1dc58f44d556
Can now write dat files, added direct color handling
Santeri Piippo
parents:
1
diff
changeset
|
51 | *self.anchor.coordinates, |
1dc58f44d556
Can now write dat files, added direct color handling
Santeri Piippo
parents:
1
diff
changeset
|
52 | *self.matrix.values, |
1dc58f44d556
Can now write dat files, added direct color handling
Santeri Piippo
parents:
1
diff
changeset
|
53 | self.subfile_path, |
1dc58f44d556
Can now write dat files, added direct color handling
Santeri Piippo
parents:
1
diff
changeset
|
54 | ] |
1dc58f44d556
Can now write dat files, added direct color handling
Santeri Piippo
parents:
1
diff
changeset
|
55 | return ' '.join(ldraw_str(arg) for arg in args) |
0 | 56 | |
3
1dc58f44d556
Can now write dat files, added direct color handling
Santeri Piippo
parents:
1
diff
changeset
|
57 | class BasePolygon: |
4 | 58 | def __init__(self, *, colour, geometry): |
59 | self.colour, self.geometry = colour, geometry | |
0 | 60 | def __repr__(self): |
61 | return str.format('linetypes.{typename}(' \ | |
4 | 62 | 'colour = {colour!r}, ' \ |
0 | 63 | 'geometry = {geometry!r})', |
64 | typename = type(self).__name__, | |
4 | 65 | colour = self.colour, |
0 | 66 | geometry = self.geometry, |
67 | ) | |
3
1dc58f44d556
Can now write dat files, added direct color handling
Santeri Piippo
parents:
1
diff
changeset
|
68 | def base_textual_representation(self): |
4 | 69 | args = [self.colour] |
3
1dc58f44d556
Can now write dat files, added direct color handling
Santeri Piippo
parents:
1
diff
changeset
|
70 | for vertex in self.geometry.vertices: |
1dc58f44d556
Can now write dat files, added direct color handling
Santeri Piippo
parents:
1
diff
changeset
|
71 | args += vertex.coordinates |
1dc58f44d556
Can now write dat files, added direct color handling
Santeri Piippo
parents:
1
diff
changeset
|
72 | return ' '.join(ldraw_str(arg) for arg in args) |
0 | 73 | |
3
1dc58f44d556
Can now write dat files, added direct color handling
Santeri Piippo
parents:
1
diff
changeset
|
74 | class LineSegment(BasePolygon): |
1dc58f44d556
Can now write dat files, added direct color handling
Santeri Piippo
parents:
1
diff
changeset
|
75 | def textual_representation(self): |
1dc58f44d556
Can now write dat files, added direct color handling
Santeri Piippo
parents:
1
diff
changeset
|
76 | return '2 ' + self.base_textual_representation() |
0 | 77 | |
3
1dc58f44d556
Can now write dat files, added direct color handling
Santeri Piippo
parents:
1
diff
changeset
|
78 | class Triangle(BasePolygon): |
1dc58f44d556
Can now write dat files, added direct color handling
Santeri Piippo
parents:
1
diff
changeset
|
79 | def textual_representation(self): |
1dc58f44d556
Can now write dat files, added direct color handling
Santeri Piippo
parents:
1
diff
changeset
|
80 | return '3 ' + self.base_textual_representation() |
1dc58f44d556
Can now write dat files, added direct color handling
Santeri Piippo
parents:
1
diff
changeset
|
81 | |
1dc58f44d556
Can now write dat files, added direct color handling
Santeri Piippo
parents:
1
diff
changeset
|
82 | class Quadrilateral(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 '4 ' + self.base_textual_representation() |
1 | 85 | |
35 | 86 | class ConditionalLine(LineSegment): |
4 | 87 | def __init__(self, *, colour, geometry, control_points): |
88 | super().__init__(colour = colour, geometry = geometry) | |
1 | 89 | self.control_points = control_points |
90 | assert(len(self.control_points) == 2) | |
91 | def __repr__(self): | |
35 | 92 | return str.format('linetypes.ConditionalLine(' \ |
4 | 93 | 'colour = {colour!r}, ' \ |
1 | 94 | 'geometry = {geometry!r}, ' \ |
95 | 'control_points = {control_points!r})', **self.__dict__) | |
3
1dc58f44d556
Can now write dat files, added direct color handling
Santeri Piippo
parents:
1
diff
changeset
|
96 | def textual_representation(self): |
1dc58f44d556
Can now write dat files, added direct color handling
Santeri Piippo
parents:
1
diff
changeset
|
97 | result = '5 ' + self.base_textual_representation() |
1dc58f44d556
Can now write dat files, added direct color handling
Santeri Piippo
parents:
1
diff
changeset
|
98 | for control_point in self.control_points: |
1dc58f44d556
Can now write dat files, added direct color handling
Santeri Piippo
parents:
1
diff
changeset
|
99 | 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
|
100 | result += ' ' |
1dc58f44d556
Can now write dat files, added direct color handling
Santeri Piippo
parents:
1
diff
changeset
|
101 | result += ' '.join(strings) |
1dc58f44d556
Can now write dat files, added direct color handling
Santeri Piippo
parents:
1
diff
changeset
|
102 | return result |
38
66c9591b733d
added proper handling of syntax errors
Teemu Piippo <teemu@hecknology.net>
parents:
35
diff
changeset
|
103 | |
66c9591b733d
added proper handling of syntax errors
Teemu Piippo <teemu@hecknology.net>
parents:
35
diff
changeset
|
104 | class Error: |
66c9591b733d
added proper handling of syntax errors
Teemu Piippo <teemu@hecknology.net>
parents:
35
diff
changeset
|
105 | def __init__(self, line, reason): |
66c9591b733d
added proper handling of syntax errors
Teemu Piippo <teemu@hecknology.net>
parents:
35
diff
changeset
|
106 | self.line = line |
66c9591b733d
added proper handling of syntax errors
Teemu Piippo <teemu@hecknology.net>
parents:
35
diff
changeset
|
107 | self.reason = reason |
66c9591b733d
added proper handling of syntax errors
Teemu Piippo <teemu@hecknology.net>
parents:
35
diff
changeset
|
108 | def __repr__(self): |
66c9591b733d
added proper handling of syntax errors
Teemu Piippo <teemu@hecknology.net>
parents:
35
diff
changeset
|
109 | return str.format( |
66c9591b733d
added proper handling of syntax errors
Teemu Piippo <teemu@hecknology.net>
parents:
35
diff
changeset
|
110 | 'linetypes.Error(line = {line!r}, reason = {reason!r})', |
66c9591b733d
added proper handling of syntax errors
Teemu Piippo <teemu@hecknology.net>
parents:
35
diff
changeset
|
111 | line = self.line, |
66c9591b733d
added proper handling of syntax errors
Teemu Piippo <teemu@hecknology.net>
parents:
35
diff
changeset
|
112 | reason = self.reason, |
66c9591b733d
added proper handling of syntax errors
Teemu Piippo <teemu@hecknology.net>
parents:
35
diff
changeset
|
113 | ) |
66c9591b733d
added proper handling of syntax errors
Teemu Piippo <teemu@hecknology.net>
parents:
35
diff
changeset
|
114 | def textual_representation(self): |
66c9591b733d
added proper handling of syntax errors
Teemu Piippo <teemu@hecknology.net>
parents:
35
diff
changeset
|
115 | return self.line |