Sat, 08 Jun 2019 01:51:50 +0300
changed severity names to be better understood
47 | 1 | import re |
2 | import linetypes | |
48 | 3 | import datetime |
47 | 4 | |
5 | class Header: | |
6 | def __init__(self): | |
7 | self.description = None | |
8 | self.name = None | |
9 | self.author = None | |
10 | self.username = None | |
11 | self.filetype = None | |
12 | self.qualifiers = None | |
13 | self.license = None | |
48 | 14 | self.help = '' |
47 | 15 | self.bfc = None |
16 | self.category = None | |
48 | 17 | self.keywords = '' |
47 | 18 | self.cmdline = None |
19 | self.history = [] | |
48 | 20 | self.first_occurrence = dict() |
21 | @property | |
22 | def valid(self): | |
23 | return True | |
47 | 24 | |
25 | class BadHeader: | |
26 | def __init__(self, index, reason): | |
27 | self.index = index | |
28 | self.reason = reason | |
29 | def __repr__(self): | |
30 | return str.format( | |
31 | 'header.BadHeader(index = {index!r}, reason = {reason!r})', | |
32 | index = self.index, | |
33 | reason = self.reason, | |
34 | ) | |
48 | 35 | @property |
36 | def valid(self): | |
37 | return False | |
47 | 38 | |
39 | geometrical_types = [ | |
40 | linetypes.LineSegment, | |
41 | linetypes.Triangle, | |
42 | linetypes.Quadrilateral, | |
43 | linetypes.ConditionalLine, | |
44 | ] | |
45 | ||
56
ed6d39c59e56
fixed BFC INVERTNEXT being interpreted as a header command
Teemu Piippo <teemu@hecknology.net>
parents:
54
diff
changeset
|
46 | def is_invertnext(entry): |
ed6d39c59e56
fixed BFC INVERTNEXT being interpreted as a header command
Teemu Piippo <teemu@hecknology.net>
parents:
54
diff
changeset
|
47 | return isinstance(entry, linetypes.MetaCommand) \ |
ed6d39c59e56
fixed BFC INVERTNEXT being interpreted as a header command
Teemu Piippo <teemu@hecknology.net>
parents:
54
diff
changeset
|
48 | and entry.text == "BFC INVERTNEXT" |
ed6d39c59e56
fixed BFC INVERTNEXT being interpreted as a header command
Teemu Piippo <teemu@hecknology.net>
parents:
54
diff
changeset
|
49 | |
47 | 50 | def is_suitable_header_object(entry): |
56
ed6d39c59e56
fixed BFC INVERTNEXT being interpreted as a header command
Teemu Piippo <teemu@hecknology.net>
parents:
54
diff
changeset
|
51 | if is_invertnext(entry): |
ed6d39c59e56
fixed BFC INVERTNEXT being interpreted as a header command
Teemu Piippo <teemu@hecknology.net>
parents:
54
diff
changeset
|
52 | # BFC INVERTNEXT is not a header command anymore. |
ed6d39c59e56
fixed BFC INVERTNEXT being interpreted as a header command
Teemu Piippo <teemu@hecknology.net>
parents:
54
diff
changeset
|
53 | return False |
47 | 54 | return not any( |
55 | isinstance(entry, linetype) | |
56 | for linetype in [ | |
57 | *geometrical_types, | |
58 | linetypes.Comment, | |
59 | linetypes.Error, | |
60 | ] | |
61 | ) | |
62 | ||
63 | class HeaderError(Exception): | |
64 | def __init__(self, index, reason): | |
65 | self.index, self.reason = index, reason | |
66 | def __repr__(self): | |
67 | return str.format( | |
68 | 'HeaderError({index!r}, {reason!r})', | |
69 | index = self.index, | |
70 | reason = self.reason, | |
71 | ) | |
72 | def __str__(self): | |
73 | return reason | |
74 | ||
48 | 75 | class HistoryEntry: |
76 | def __init__(self, date, user, text): | |
77 | self.date, self.user, self.text = date, user, text | |
78 | def __repr__(self): | |
79 | return str.format( | |
80 | 'HistoryEntry({date!r}, {user!r}, {text!r})', | |
81 | date = self.date, | |
82 | user = self.user, | |
83 | text = self.text) | |
84 | ||
47 | 85 | class HeaderParser: |
86 | def __init__(self): | |
87 | self.model_body = None | |
88 | self.cursor = 0 | |
89 | self.problems = [] | |
90 | def parse(self, model_body): | |
91 | result = Header() | |
48 | 92 | self.result = result |
47 | 93 | self.order = [] |
94 | self.cursor = -1 | |
95 | self.model_body = model_body | |
96 | self.skip_to_next() | |
97 | result.description = self.current() | |
98 | self.skip_to_next() | |
52
cd2b4f3c1189
fix author parsing getting extra spaces in the name
Teemu Piippo <teemu@hecknology.net>
parents:
49
diff
changeset
|
99 | result.name = self.parse_pattern(r'^Name: (.+)$', 'name')[0] |
47 | 100 | self.skip_to_next() |
60 | 101 | result.author, result.username = self.parse_pattern(r'^Author: ([^ \[]*[^\[]+) (?:\[([^\]]+)\])?', 'author') |
47 | 102 | for header_entry in self.get_more_header_stuff(): |
103 | if self.try_to_match( | |
52
cd2b4f3c1189
fix author parsing getting extra spaces in the name
Teemu Piippo <teemu@hecknology.net>
parents:
49
diff
changeset
|
104 | r'^!LDRAW_ORG ' \ |
49 | 105 | r'((?:Unofficial_)?(?:' \ |
52
cd2b4f3c1189
fix author parsing getting extra spaces in the name
Teemu Piippo <teemu@hecknology.net>
parents:
49
diff
changeset
|
106 | r'Part|' \ |
cd2b4f3c1189
fix author parsing getting extra spaces in the name
Teemu Piippo <teemu@hecknology.net>
parents:
49
diff
changeset
|
107 | r'Subpart|' \ |
cd2b4f3c1189
fix author parsing getting extra spaces in the name
Teemu Piippo <teemu@hecknology.net>
parents:
49
diff
changeset
|
108 | r'Primitive|' \ |
cd2b4f3c1189
fix author parsing getting extra spaces in the name
Teemu Piippo <teemu@hecknology.net>
parents:
49
diff
changeset
|
109 | r'8_Primitive|' \ |
cd2b4f3c1189
fix author parsing getting extra spaces in the name
Teemu Piippo <teemu@hecknology.net>
parents:
49
diff
changeset
|
110 | r'48_Primitive|' \ |
cd2b4f3c1189
fix author parsing getting extra spaces in the name
Teemu Piippo <teemu@hecknology.net>
parents:
49
diff
changeset
|
111 | r'Shortcut' \ |
cd2b4f3c1189
fix author parsing getting extra spaces in the name
Teemu Piippo <teemu@hecknology.net>
parents:
49
diff
changeset
|
112 | r'))\s?' \ |
cd2b4f3c1189
fix author parsing getting extra spaces in the name
Teemu Piippo <teemu@hecknology.net>
parents:
49
diff
changeset
|
113 | r'(.*)$', |
47 | 114 | 'part type'): |
48 | 115 | result.filetype = self.groups[0] |
116 | result.qualifiers = re.findall(r'(?:Physical_Colour|Alias|ORIGINAL|UPDATE \d\d\d\d-\d\d)', self.groups[1]) | |
47 | 117 | elif self.try_to_match( |
52
cd2b4f3c1189
fix author parsing getting extra spaces in the name
Teemu Piippo <teemu@hecknology.net>
parents:
49
diff
changeset
|
118 | r'^!LICENSE (.+)$', |
47 | 119 | 'license'): |
48 | 120 | result.license = self.groups[0] |
121 | elif self.try_to_match( | |
52
cd2b4f3c1189
fix author parsing getting extra spaces in the name
Teemu Piippo <teemu@hecknology.net>
parents:
49
diff
changeset
|
122 | r'BFC (CERTIFY CW|CERTIFY CCW|NOCERTIFY)', |
48 | 123 | 'bfc'): |
124 | result.bfc = self.groups[0] | |
125 | elif self.try_to_match( | |
126 | r'!HISTORY (\d{4}-\d{2}-\d{2}) ([\[{][^\]}]+[\]}]) (.+)$', | |
127 | 'history'): | |
59
0f3e70a2bb4b
report invalid ISO dates instead of crashing
Teemu Piippo <teemu@hecknology.net>
parents:
56
diff
changeset
|
128 | try: |
0f3e70a2bb4b
report invalid ISO dates instead of crashing
Teemu Piippo <teemu@hecknology.net>
parents:
56
diff
changeset
|
129 | time_object = datetime.datetime.strptime( |
0f3e70a2bb4b
report invalid ISO dates instead of crashing
Teemu Piippo <teemu@hecknology.net>
parents:
56
diff
changeset
|
130 | self.groups[0], |
0f3e70a2bb4b
report invalid ISO dates instead of crashing
Teemu Piippo <teemu@hecknology.net>
parents:
56
diff
changeset
|
131 | '%Y-%m-%d', |
0f3e70a2bb4b
report invalid ISO dates instead of crashing
Teemu Piippo <teemu@hecknology.net>
parents:
56
diff
changeset
|
132 | ) |
0f3e70a2bb4b
report invalid ISO dates instead of crashing
Teemu Piippo <teemu@hecknology.net>
parents:
56
diff
changeset
|
133 | except ValueError: |
0f3e70a2bb4b
report invalid ISO dates instead of crashing
Teemu Piippo <teemu@hecknology.net>
parents:
56
diff
changeset
|
134 | self.parse_error("invalid ISO date in history") |
48 | 135 | result.history.append(HistoryEntry( |
59
0f3e70a2bb4b
report invalid ISO dates instead of crashing
Teemu Piippo <teemu@hecknology.net>
parents:
56
diff
changeset
|
136 | date = time_object.date(), |
48 | 137 | user = self.groups[1], |
138 | text = self.groups[2], | |
139 | )) | |
140 | elif self.try_to_match( | |
141 | r'!HELP (.+)', | |
142 | 'help'): | |
143 | if result.help: | |
144 | result.help += '\n' | |
145 | result.help += self.groups[0] | |
146 | elif self.try_to_match( | |
147 | r'!CATEGORY (.+)', | |
148 | 'category'): | |
149 | result.category = self.groups[0] | |
150 | elif self.try_to_match( | |
151 | r'!KEYWORDS (.+)', | |
152 | 'keywords'): | |
153 | if result.keywords: | |
154 | result.keywords += '\n' | |
155 | result.keywords += self.groups[0] | |
156 | elif self.try_to_match( | |
157 | r'!CMDLINE (.+)', | |
158 | 'cmdline'): | |
159 | result.cmdline = self.groups[0] | |
47 | 160 | else: |
48 | 161 | self.parse_error("couldn't understand header syntax: " + repr(header_entry.text)) |
47 | 162 | return { |
163 | 'header': result, | |
164 | 'end-index': self.cursor + 1, | |
165 | } | |
166 | def parse_error(self, message): | |
167 | raise HeaderError(index = self.cursor, reason = message) | |
168 | def get_more_header_stuff(self): | |
169 | while True: | |
170 | self.cursor += 1 | |
171 | if self.cursor >= len(self.model_body): | |
172 | break | |
173 | entry = self.model_body[self.cursor] | |
174 | if not is_suitable_header_object(entry): | |
175 | break | |
176 | if isinstance(entry, linetypes.MetaCommand): | |
177 | yield entry | |
178 | def skip_to_next(self, *, spaces_expected = 0): | |
179 | while True: | |
180 | if self.cursor + 1 >= len(self.model_body): | |
54
0c686d10eb49
added tests for moved-to files and scaling in flat dimensions
Teemu Piippo <teemu@hecknology.net>
parents:
52
diff
changeset
|
181 | self.parse_error('file does not have a proper header') |
47 | 182 | self.cursor += 1 |
183 | entry = self.model_body[self.cursor] | |
184 | if not is_suitable_header_object(entry): | |
185 | self.parse_error('header is incomplete') | |
186 | if isinstance(entry, linetypes.MetaCommand): | |
187 | return | |
188 | def try_to_match(self, pattern, patterntype): | |
189 | try: | |
190 | self.groups = self.parse_pattern(pattern, patterntype) | |
48 | 191 | return True |
47 | 192 | except: |
193 | return False | |
194 | def current(self): | |
195 | entry = self.model_body[self.cursor] | |
196 | assert isinstance(entry, linetypes.MetaCommand) | |
197 | return entry.text | |
198 | def parse_pattern(self, pattern, description): | |
199 | match = re.search(pattern, self.current()) | |
200 | if match: | |
201 | self.order.append(description) | |
48 | 202 | if description not in self.result.first_occurrence: |
203 | self.result.first_occurrence[description] = self.cursor | |
47 | 204 | return match.groups() |
205 | else: | |
206 | self.parse_error(str.format("couldn't parse {}", description)) |