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