Sat, 01 Jun 2019 10:47:27 +0300
fixed handling of invalid subfiles
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 | ||
46 | def is_suitable_header_object(entry): | |
47 | return not any( | |
48 | isinstance(entry, linetype) | |
49 | for linetype in [ | |
50 | *geometrical_types, | |
51 | linetypes.Comment, | |
52 | linetypes.Error, | |
53 | ] | |
54 | ) | |
55 | ||
56 | class HeaderError(Exception): | |
57 | def __init__(self, index, reason): | |
58 | self.index, self.reason = index, reason | |
59 | def __repr__(self): | |
60 | return str.format( | |
61 | 'HeaderError({index!r}, {reason!r})', | |
62 | index = self.index, | |
63 | reason = self.reason, | |
64 | ) | |
65 | def __str__(self): | |
66 | return reason | |
67 | ||
48 | 68 | class HistoryEntry: |
69 | def __init__(self, date, user, text): | |
70 | self.date, self.user, self.text = date, user, text | |
71 | def __repr__(self): | |
72 | return str.format( | |
73 | 'HistoryEntry({date!r}, {user!r}, {text!r})', | |
74 | date = self.date, | |
75 | user = self.user, | |
76 | text = self.text) | |
77 | ||
47 | 78 | class HeaderParser: |
79 | def __init__(self): | |
80 | self.model_body = None | |
81 | self.cursor = 0 | |
82 | self.problems = [] | |
83 | def parse(self, model_body): | |
84 | result = Header() | |
48 | 85 | self.result = result |
47 | 86 | self.order = [] |
87 | self.cursor = -1 | |
88 | self.model_body = model_body | |
89 | self.skip_to_next() | |
90 | result.description = self.current() | |
91 | self.skip_to_next() | |
52
cd2b4f3c1189
fix author parsing getting extra spaces in the name
Teemu Piippo <teemu@hecknology.net>
parents:
49
diff
changeset
|
92 | result.name = self.parse_pattern(r'^Name: (.+)$', 'name')[0] |
47 | 93 | self.skip_to_next() |
52
cd2b4f3c1189
fix author parsing getting extra spaces in the name
Teemu Piippo <teemu@hecknology.net>
parents:
49
diff
changeset
|
94 | result.author, result.username = self.parse_pattern(r'^Author: ((?:\s*\w+)+) (?:\[([^\]]+)\])?', 'author') |
47 | 95 | for header_entry in self.get_more_header_stuff(): |
96 | 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
|
97 | r'^!LDRAW_ORG ' \ |
49 | 98 | r'((?:Unofficial_)?(?:' \ |
52
cd2b4f3c1189
fix author parsing getting extra spaces in the name
Teemu Piippo <teemu@hecknology.net>
parents:
49
diff
changeset
|
99 | r'Part|' \ |
cd2b4f3c1189
fix author parsing getting extra spaces in the name
Teemu Piippo <teemu@hecknology.net>
parents:
49
diff
changeset
|
100 | r'Subpart|' \ |
cd2b4f3c1189
fix author parsing getting extra spaces in the name
Teemu Piippo <teemu@hecknology.net>
parents:
49
diff
changeset
|
101 | r'Primitive|' \ |
cd2b4f3c1189
fix author parsing getting extra spaces in the name
Teemu Piippo <teemu@hecknology.net>
parents:
49
diff
changeset
|
102 | r'8_Primitive|' \ |
cd2b4f3c1189
fix author parsing getting extra spaces in the name
Teemu Piippo <teemu@hecknology.net>
parents:
49
diff
changeset
|
103 | r'48_Primitive|' \ |
cd2b4f3c1189
fix author parsing getting extra spaces in the name
Teemu Piippo <teemu@hecknology.net>
parents:
49
diff
changeset
|
104 | r'Shortcut' \ |
cd2b4f3c1189
fix author parsing getting extra spaces in the name
Teemu Piippo <teemu@hecknology.net>
parents:
49
diff
changeset
|
105 | r'))\s?' \ |
cd2b4f3c1189
fix author parsing getting extra spaces in the name
Teemu Piippo <teemu@hecknology.net>
parents:
49
diff
changeset
|
106 | r'(.*)$', |
47 | 107 | 'part type'): |
48 | 108 | result.filetype = self.groups[0] |
109 | result.qualifiers = re.findall(r'(?:Physical_Colour|Alias|ORIGINAL|UPDATE \d\d\d\d-\d\d)', self.groups[1]) | |
47 | 110 | 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
|
111 | r'^!LICENSE (.+)$', |
47 | 112 | 'license'): |
48 | 113 | result.license = self.groups[0] |
114 | 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
|
115 | r'BFC (CERTIFY CW|CERTIFY CCW|NOCERTIFY)', |
48 | 116 | 'bfc'): |
117 | result.bfc = self.groups[0] | |
118 | elif self.try_to_match( | |
119 | r'!HISTORY (\d{4}-\d{2}-\d{2}) ([\[{][^\]}]+[\]}]) (.+)$', | |
120 | 'history'): | |
121 | result.history.append(HistoryEntry( | |
122 | date = datetime.datetime.strptime(self.groups[0], '%Y-%m-%d').date(), | |
123 | user = self.groups[1], | |
124 | text = self.groups[2], | |
125 | )) | |
126 | elif self.try_to_match( | |
127 | r'!HELP (.+)', | |
128 | 'help'): | |
129 | if result.help: | |
130 | result.help += '\n' | |
131 | result.help += self.groups[0] | |
132 | elif self.try_to_match( | |
133 | r'!CATEGORY (.+)', | |
134 | 'category'): | |
135 | result.category = self.groups[0] | |
136 | elif self.try_to_match( | |
137 | r'!KEYWORDS (.+)', | |
138 | 'keywords'): | |
139 | if result.keywords: | |
140 | result.keywords += '\n' | |
141 | result.keywords += self.groups[0] | |
142 | elif self.try_to_match( | |
143 | r'!CMDLINE (.+)', | |
144 | 'cmdline'): | |
145 | result.cmdline = self.groups[0] | |
47 | 146 | else: |
48 | 147 | self.parse_error("couldn't understand header syntax: " + repr(header_entry.text)) |
47 | 148 | return { |
149 | 'header': result, | |
150 | 'end-index': self.cursor + 1, | |
151 | } | |
152 | def parse_error(self, message): | |
153 | raise HeaderError(index = self.cursor, reason = message) | |
154 | def get_more_header_stuff(self): | |
155 | while True: | |
156 | self.cursor += 1 | |
157 | if self.cursor >= len(self.model_body): | |
158 | break | |
159 | entry = self.model_body[self.cursor] | |
160 | if not is_suitable_header_object(entry): | |
161 | break | |
162 | if isinstance(entry, linetypes.MetaCommand): | |
163 | yield entry | |
164 | def skip_to_next(self, *, spaces_expected = 0): | |
165 | while True: | |
166 | 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
|
167 | self.parse_error('file does not have a proper header') |
47 | 168 | self.cursor += 1 |
169 | entry = self.model_body[self.cursor] | |
170 | if not is_suitable_header_object(entry): | |
171 | self.parse_error('header is incomplete') | |
172 | if isinstance(entry, linetypes.MetaCommand): | |
173 | return | |
174 | def try_to_match(self, pattern, patterntype): | |
175 | try: | |
176 | self.groups = self.parse_pattern(pattern, patterntype) | |
48 | 177 | return True |
47 | 178 | except: |
179 | return False | |
180 | def current(self): | |
181 | entry = self.model_body[self.cursor] | |
182 | assert isinstance(entry, linetypes.MetaCommand) | |
183 | return entry.text | |
184 | def parse_pattern(self, pattern, description): | |
185 | match = re.search(pattern, self.current()) | |
186 | if match: | |
187 | self.order.append(description) | |
48 | 188 | if description not in self.result.first_occurrence: |
189 | self.result.first_occurrence[description] = self.cursor | |
47 | 190 | return match.groups() |
191 | else: | |
192 | self.parse_error(str.format("couldn't parse {}", description)) |