Mon, 24 Jun 2019 09:45:41 +0300
moved the bad whitespace test to a new file
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 | |
69
a24c4490d9f2
added a check for keywords in non-parts
Teemu Piippo <teemu@hecknology.net>
parents:
67
diff
changeset
|
24 | @property |
a24c4490d9f2
added a check for keywords in non-parts
Teemu Piippo <teemu@hecknology.net>
parents:
67
diff
changeset
|
25 | def effective_filetype(self): |
a24c4490d9f2
added a check for keywords in non-parts
Teemu Piippo <teemu@hecknology.net>
parents:
67
diff
changeset
|
26 | if self.filetype.startswith('Unofficial_'): |
a24c4490d9f2
added a check for keywords in non-parts
Teemu Piippo <teemu@hecknology.net>
parents:
67
diff
changeset
|
27 | return self.filetype.rsplit('Unofficial_')[1] |
a24c4490d9f2
added a check for keywords in non-parts
Teemu Piippo <teemu@hecknology.net>
parents:
67
diff
changeset
|
28 | else: |
a24c4490d9f2
added a check for keywords in non-parts
Teemu Piippo <teemu@hecknology.net>
parents:
67
diff
changeset
|
29 | return self.filetype |
79
eb93feb6d3a3
added a test for valid categories
Teemu Piippo <teemu@hecknology.net>
parents:
77
diff
changeset
|
30 | @property |
eb93feb6d3a3
added a test for valid categories
Teemu Piippo <teemu@hecknology.net>
parents:
77
diff
changeset
|
31 | def effective_category(self): |
eb93feb6d3a3
added a test for valid categories
Teemu Piippo <teemu@hecknology.net>
parents:
77
diff
changeset
|
32 | if self.category: |
eb93feb6d3a3
added a test for valid categories
Teemu Piippo <teemu@hecknology.net>
parents:
77
diff
changeset
|
33 | return self.category |
eb93feb6d3a3
added a test for valid categories
Teemu Piippo <teemu@hecknology.net>
parents:
77
diff
changeset
|
34 | else: |
84
55d52e25267f
fixed prefixed punctuations winding up in the effective categories of subparts
Teemu Piippo <teemu@hecknology.net>
parents:
79
diff
changeset
|
35 | import string |
55d52e25267f
fixed prefixed punctuations winding up in the effective categories of subparts
Teemu Piippo <teemu@hecknology.net>
parents:
79
diff
changeset
|
36 | category = self.description.split(' ', 1)[0] |
55d52e25267f
fixed prefixed punctuations winding up in the effective categories of subparts
Teemu Piippo <teemu@hecknology.net>
parents:
79
diff
changeset
|
37 | while category and category[0] in string.punctuation: |
55d52e25267f
fixed prefixed punctuations winding up in the effective categories of subparts
Teemu Piippo <teemu@hecknology.net>
parents:
79
diff
changeset
|
38 | category = category[1:] |
55d52e25267f
fixed prefixed punctuations winding up in the effective categories of subparts
Teemu Piippo <teemu@hecknology.net>
parents:
79
diff
changeset
|
39 | return category |
47 | 40 | |
41 | class BadHeader: | |
42 | def __init__(self, index, reason): | |
43 | self.index = index | |
44 | self.reason = reason | |
45 | def __repr__(self): | |
46 | return str.format( | |
47 | 'header.BadHeader(index = {index!r}, reason = {reason!r})', | |
48 | index = self.index, | |
49 | reason = self.reason, | |
50 | ) | |
48 | 51 | @property |
52 | def valid(self): | |
53 | return False | |
47 | 54 | |
56
ed6d39c59e56
fixed BFC INVERTNEXT being interpreted as a header command
Teemu Piippo <teemu@hecknology.net>
parents:
54
diff
changeset
|
55 | def is_invertnext(entry): |
ed6d39c59e56
fixed BFC INVERTNEXT being interpreted as a header command
Teemu Piippo <teemu@hecknology.net>
parents:
54
diff
changeset
|
56 | return isinstance(entry, linetypes.MetaCommand) \ |
ed6d39c59e56
fixed BFC INVERTNEXT being interpreted as a header command
Teemu Piippo <teemu@hecknology.net>
parents:
54
diff
changeset
|
57 | and entry.text == "BFC INVERTNEXT" |
ed6d39c59e56
fixed BFC INVERTNEXT being interpreted as a header command
Teemu Piippo <teemu@hecknology.net>
parents:
54
diff
changeset
|
58 | |
47 | 59 | 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
|
60 | if is_invertnext(entry): |
ed6d39c59e56
fixed BFC INVERTNEXT being interpreted as a header command
Teemu Piippo <teemu@hecknology.net>
parents:
54
diff
changeset
|
61 | # 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
|
62 | return False |
47 | 63 | return not any( |
64 | isinstance(entry, linetype) | |
65 | for linetype in [ | |
77
d98502ae1f33
improved header extent scanning
Teemu Piippo <teemu@hecknology.net>
parents:
76
diff
changeset
|
66 | linetypes.SubfileReference, |
d98502ae1f33
improved header extent scanning
Teemu Piippo <teemu@hecknology.net>
parents:
76
diff
changeset
|
67 | linetypes.LineSegment, |
d98502ae1f33
improved header extent scanning
Teemu Piippo <teemu@hecknology.net>
parents:
76
diff
changeset
|
68 | linetypes.Triangle, |
d98502ae1f33
improved header extent scanning
Teemu Piippo <teemu@hecknology.net>
parents:
76
diff
changeset
|
69 | linetypes.Quadrilateral, |
d98502ae1f33
improved header extent scanning
Teemu Piippo <teemu@hecknology.net>
parents:
76
diff
changeset
|
70 | linetypes.ConditionalLine, |
47 | 71 | linetypes.Comment, |
72 | linetypes.Error, | |
73 | ] | |
74 | ) | |
75 | ||
76 | class HeaderError(Exception): | |
77 | def __init__(self, index, reason): | |
78 | self.index, self.reason = index, reason | |
79 | def __repr__(self): | |
80 | return str.format( | |
81 | 'HeaderError({index!r}, {reason!r})', | |
82 | index = self.index, | |
83 | reason = self.reason, | |
84 | ) | |
85 | def __str__(self): | |
86 | return reason | |
87 | ||
48 | 88 | class HistoryEntry: |
89 | def __init__(self, date, user, text): | |
90 | self.date, self.user, self.text = date, user, text | |
91 | def __repr__(self): | |
92 | return str.format( | |
93 | 'HistoryEntry({date!r}, {user!r}, {text!r})', | |
94 | date = self.date, | |
95 | user = self.user, | |
96 | text = self.text) | |
97 | ||
47 | 98 | class HeaderParser: |
99 | def __init__(self): | |
100 | self.model_body = None | |
101 | self.cursor = 0 | |
102 | self.problems = [] | |
103 | def parse(self, model_body): | |
104 | result = Header() | |
48 | 105 | self.result = result |
47 | 106 | self.order = [] |
107 | self.cursor = -1 | |
108 | self.model_body = model_body | |
109 | self.skip_to_next() | |
110 | result.description = self.current() | |
111 | self.skip_to_next() | |
52
cd2b4f3c1189
fix author parsing getting extra spaces in the name
Teemu Piippo <teemu@hecknology.net>
parents:
49
diff
changeset
|
112 | result.name = self.parse_pattern(r'^Name: (.+)$', 'name')[0] |
47 | 113 | self.skip_to_next() |
76
c73432653fd9
fixed choking on 'Author: [PTAdmin]'-lines
Teemu Piippo <teemu@hecknology.net>
parents:
69
diff
changeset
|
114 | result.author, result.username = self.parse_pattern(r'^Author: (?:([^ \[]*[^\[]+) )?(?:\[([^\]]+)\])?', 'author') |
c73432653fd9
fixed choking on 'Author: [PTAdmin]'-lines
Teemu Piippo <teemu@hecknology.net>
parents:
69
diff
changeset
|
115 | if not result.author and not result.username: |
c73432653fd9
fixed choking on 'Author: [PTAdmin]'-lines
Teemu Piippo <teemu@hecknology.net>
parents:
69
diff
changeset
|
116 | self.parse_error('author line does not contain a name nor username') |
47 | 117 | for header_entry in self.get_more_header_stuff(): |
118 | 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
|
119 | r'^!LDRAW_ORG ' \ |
49 | 120 | r'((?:Unofficial_)?(?:' \ |
52
cd2b4f3c1189
fix author parsing getting extra spaces in the name
Teemu Piippo <teemu@hecknology.net>
parents:
49
diff
changeset
|
121 | r'Part|' \ |
cd2b4f3c1189
fix author parsing getting extra spaces in the name
Teemu Piippo <teemu@hecknology.net>
parents:
49
diff
changeset
|
122 | r'Subpart|' \ |
cd2b4f3c1189
fix author parsing getting extra spaces in the name
Teemu Piippo <teemu@hecknology.net>
parents:
49
diff
changeset
|
123 | r'Primitive|' \ |
cd2b4f3c1189
fix author parsing getting extra spaces in the name
Teemu Piippo <teemu@hecknology.net>
parents:
49
diff
changeset
|
124 | r'8_Primitive|' \ |
cd2b4f3c1189
fix author parsing getting extra spaces in the name
Teemu Piippo <teemu@hecknology.net>
parents:
49
diff
changeset
|
125 | r'48_Primitive|' \ |
cd2b4f3c1189
fix author parsing getting extra spaces in the name
Teemu Piippo <teemu@hecknology.net>
parents:
49
diff
changeset
|
126 | r'Shortcut' \ |
cd2b4f3c1189
fix author parsing getting extra spaces in the name
Teemu Piippo <teemu@hecknology.net>
parents:
49
diff
changeset
|
127 | r'))\s?' \ |
cd2b4f3c1189
fix author parsing getting extra spaces in the name
Teemu Piippo <teemu@hecknology.net>
parents:
49
diff
changeset
|
128 | r'(.*)$', |
47 | 129 | 'part type'): |
48 | 130 | result.filetype = self.groups[0] |
131 | result.qualifiers = re.findall(r'(?:Physical_Colour|Alias|ORIGINAL|UPDATE \d\d\d\d-\d\d)', self.groups[1]) | |
47 | 132 | 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
|
133 | r'^!LICENSE (.+)$', |
47 | 134 | 'license'): |
48 | 135 | result.license = self.groups[0] |
136 | 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
|
137 | r'BFC (CERTIFY CW|CERTIFY CCW|NOCERTIFY)', |
48 | 138 | 'bfc'): |
139 | result.bfc = self.groups[0] | |
140 | elif self.try_to_match( | |
141 | r'!HISTORY (\d{4}-\d{2}-\d{2}) ([\[{][^\]}]+[\]}]) (.+)$', | |
142 | 'history'): | |
59
0f3e70a2bb4b
report invalid ISO dates instead of crashing
Teemu Piippo <teemu@hecknology.net>
parents:
56
diff
changeset
|
143 | try: |
0f3e70a2bb4b
report invalid ISO dates instead of crashing
Teemu Piippo <teemu@hecknology.net>
parents:
56
diff
changeset
|
144 | time_object = datetime.datetime.strptime( |
0f3e70a2bb4b
report invalid ISO dates instead of crashing
Teemu Piippo <teemu@hecknology.net>
parents:
56
diff
changeset
|
145 | self.groups[0], |
0f3e70a2bb4b
report invalid ISO dates instead of crashing
Teemu Piippo <teemu@hecknology.net>
parents:
56
diff
changeset
|
146 | '%Y-%m-%d', |
0f3e70a2bb4b
report invalid ISO dates instead of crashing
Teemu Piippo <teemu@hecknology.net>
parents:
56
diff
changeset
|
147 | ) |
0f3e70a2bb4b
report invalid ISO dates instead of crashing
Teemu Piippo <teemu@hecknology.net>
parents:
56
diff
changeset
|
148 | except ValueError: |
0f3e70a2bb4b
report invalid ISO dates instead of crashing
Teemu Piippo <teemu@hecknology.net>
parents:
56
diff
changeset
|
149 | self.parse_error("invalid ISO date in history") |
48 | 150 | result.history.append(HistoryEntry( |
59
0f3e70a2bb4b
report invalid ISO dates instead of crashing
Teemu Piippo <teemu@hecknology.net>
parents:
56
diff
changeset
|
151 | date = time_object.date(), |
48 | 152 | user = self.groups[1], |
153 | text = self.groups[2], | |
154 | )) | |
155 | elif self.try_to_match( | |
156 | r'!HELP (.+)', | |
157 | 'help'): | |
158 | if result.help: | |
159 | result.help += '\n' | |
160 | result.help += self.groups[0] | |
161 | elif self.try_to_match( | |
162 | r'!CATEGORY (.+)', | |
163 | 'category'): | |
164 | result.category = self.groups[0] | |
165 | elif self.try_to_match( | |
166 | r'!KEYWORDS (.+)', | |
167 | 'keywords'): | |
168 | if result.keywords: | |
169 | result.keywords += '\n' | |
170 | result.keywords += self.groups[0] | |
171 | elif self.try_to_match( | |
172 | r'!CMDLINE (.+)', | |
173 | 'cmdline'): | |
174 | result.cmdline = self.groups[0] | |
47 | 175 | else: |
77
d98502ae1f33
improved header extent scanning
Teemu Piippo <teemu@hecknology.net>
parents:
76
diff
changeset
|
176 | self.cursor -= 1 |
d98502ae1f33
improved header extent scanning
Teemu Piippo <teemu@hecknology.net>
parents:
76
diff
changeset
|
177 | break |
67
afaa4d3bc3e5
complain if LDRAW_ORG line is missing
Teemu Piippo <teemu@hecknology.net>
parents:
60
diff
changeset
|
178 | if not result.filetype: |
afaa4d3bc3e5
complain if LDRAW_ORG line is missing
Teemu Piippo <teemu@hecknology.net>
parents:
60
diff
changeset
|
179 | self.parse_error('LDRAW_ORG line is missing') |
47 | 180 | return { |
181 | 'header': result, | |
182 | 'end-index': self.cursor + 1, | |
183 | } | |
184 | def parse_error(self, message): | |
185 | raise HeaderError(index = self.cursor, reason = message) | |
186 | def get_more_header_stuff(self): | |
77
d98502ae1f33
improved header extent scanning
Teemu Piippo <teemu@hecknology.net>
parents:
76
diff
changeset
|
187 | self.cursor += 1 |
d98502ae1f33
improved header extent scanning
Teemu Piippo <teemu@hecknology.net>
parents:
76
diff
changeset
|
188 | new_cursor = self.cursor |
d98502ae1f33
improved header extent scanning
Teemu Piippo <teemu@hecknology.net>
parents:
76
diff
changeset
|
189 | while new_cursor < len(self.model_body): |
d98502ae1f33
improved header extent scanning
Teemu Piippo <teemu@hecknology.net>
parents:
76
diff
changeset
|
190 | entry = self.model_body[new_cursor] |
47 | 191 | if not is_suitable_header_object(entry): |
192 | break | |
193 | if isinstance(entry, linetypes.MetaCommand): | |
77
d98502ae1f33
improved header extent scanning
Teemu Piippo <teemu@hecknology.net>
parents:
76
diff
changeset
|
194 | self.cursor = new_cursor |
47 | 195 | yield entry |
77
d98502ae1f33
improved header extent scanning
Teemu Piippo <teemu@hecknology.net>
parents:
76
diff
changeset
|
196 | new_cursor += 1 |
47 | 197 | def skip_to_next(self, *, spaces_expected = 0): |
198 | while True: | |
199 | 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
|
200 | self.parse_error('file does not have a proper header') |
47 | 201 | self.cursor += 1 |
202 | entry = self.model_body[self.cursor] | |
203 | if not is_suitable_header_object(entry): | |
204 | self.parse_error('header is incomplete') | |
205 | if isinstance(entry, linetypes.MetaCommand): | |
206 | return | |
207 | def try_to_match(self, pattern, patterntype): | |
208 | try: | |
209 | self.groups = self.parse_pattern(pattern, patterntype) | |
48 | 210 | return True |
47 | 211 | except: |
212 | return False | |
213 | def current(self): | |
214 | entry = self.model_body[self.cursor] | |
215 | assert isinstance(entry, linetypes.MetaCommand) | |
216 | return entry.text | |
217 | def parse_pattern(self, pattern, description): | |
218 | match = re.search(pattern, self.current()) | |
219 | if match: | |
220 | self.order.append(description) | |
48 | 221 | if description not in self.result.first_occurrence: |
222 | self.result.first_occurrence[description] = self.cursor | |
47 | 223 | return match.groups() |
224 | else: | |
225 | self.parse_error(str.format("couldn't parse {}", description)) |