header.py

Sat, 01 Jun 2019 10:47:27 +0300

author
Teemu Piippo <teemu@hecknology.net>
date
Sat, 01 Jun 2019 10:47:27 +0300
changeset 55
388df1fa18a2
parent 54
0c686d10eb49
child 56
ed6d39c59e56
permissions
-rw-r--r--

fixed handling of invalid subfiles

47
4da025d0b283 added work on header check
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
1 import re
4da025d0b283 added work on header check
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
2 import linetypes
48
38b0919c1934 added some header tests
Teemu Piippo <teemu@hecknology.net>
parents: 47
diff changeset
3 import datetime
47
4da025d0b283 added work on header check
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
4
4da025d0b283 added work on header check
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
5 class Header:
4da025d0b283 added work on header check
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
6 def __init__(self):
4da025d0b283 added work on header check
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
7 self.description = None
4da025d0b283 added work on header check
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
8 self.name = None
4da025d0b283 added work on header check
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
9 self.author = None
4da025d0b283 added work on header check
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
10 self.username = None
4da025d0b283 added work on header check
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
11 self.filetype = None
4da025d0b283 added work on header check
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
12 self.qualifiers = None
4da025d0b283 added work on header check
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
13 self.license = None
48
38b0919c1934 added some header tests
Teemu Piippo <teemu@hecknology.net>
parents: 47
diff changeset
14 self.help = ''
47
4da025d0b283 added work on header check
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
15 self.bfc = None
4da025d0b283 added work on header check
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
16 self.category = None
48
38b0919c1934 added some header tests
Teemu Piippo <teemu@hecknology.net>
parents: 47
diff changeset
17 self.keywords = ''
47
4da025d0b283 added work on header check
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
18 self.cmdline = None
4da025d0b283 added work on header check
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
19 self.history = []
48
38b0919c1934 added some header tests
Teemu Piippo <teemu@hecknology.net>
parents: 47
diff changeset
20 self.first_occurrence = dict()
38b0919c1934 added some header tests
Teemu Piippo <teemu@hecknology.net>
parents: 47
diff changeset
21 @property
38b0919c1934 added some header tests
Teemu Piippo <teemu@hecknology.net>
parents: 47
diff changeset
22 def valid(self):
38b0919c1934 added some header tests
Teemu Piippo <teemu@hecknology.net>
parents: 47
diff changeset
23 return True
47
4da025d0b283 added work on header check
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
24
4da025d0b283 added work on header check
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
25 class BadHeader:
4da025d0b283 added work on header check
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
26 def __init__(self, index, reason):
4da025d0b283 added work on header check
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
27 self.index = index
4da025d0b283 added work on header check
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
28 self.reason = reason
4da025d0b283 added work on header check
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
29 def __repr__(self):
4da025d0b283 added work on header check
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
30 return str.format(
4da025d0b283 added work on header check
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
31 'header.BadHeader(index = {index!r}, reason = {reason!r})',
4da025d0b283 added work on header check
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
32 index = self.index,
4da025d0b283 added work on header check
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
33 reason = self.reason,
4da025d0b283 added work on header check
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
34 )
48
38b0919c1934 added some header tests
Teemu Piippo <teemu@hecknology.net>
parents: 47
diff changeset
35 @property
38b0919c1934 added some header tests
Teemu Piippo <teemu@hecknology.net>
parents: 47
diff changeset
36 def valid(self):
38b0919c1934 added some header tests
Teemu Piippo <teemu@hecknology.net>
parents: 47
diff changeset
37 return False
47
4da025d0b283 added work on header check
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
38
4da025d0b283 added work on header check
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
39 geometrical_types = [
4da025d0b283 added work on header check
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
40 linetypes.LineSegment,
4da025d0b283 added work on header check
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
41 linetypes.Triangle,
4da025d0b283 added work on header check
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
42 linetypes.Quadrilateral,
4da025d0b283 added work on header check
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
43 linetypes.ConditionalLine,
4da025d0b283 added work on header check
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
44 ]
4da025d0b283 added work on header check
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
45
4da025d0b283 added work on header check
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
46 def is_suitable_header_object(entry):
4da025d0b283 added work on header check
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
47 return not any(
4da025d0b283 added work on header check
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
48 isinstance(entry, linetype)
4da025d0b283 added work on header check
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
49 for linetype in [
4da025d0b283 added work on header check
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
50 *geometrical_types,
4da025d0b283 added work on header check
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
51 linetypes.Comment,
4da025d0b283 added work on header check
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
52 linetypes.Error,
4da025d0b283 added work on header check
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
53 ]
4da025d0b283 added work on header check
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
54 )
4da025d0b283 added work on header check
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
55
4da025d0b283 added work on header check
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
56 class HeaderError(Exception):
4da025d0b283 added work on header check
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
57 def __init__(self, index, reason):
4da025d0b283 added work on header check
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
58 self.index, self.reason = index, reason
4da025d0b283 added work on header check
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
59 def __repr__(self):
4da025d0b283 added work on header check
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
60 return str.format(
4da025d0b283 added work on header check
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
61 'HeaderError({index!r}, {reason!r})',
4da025d0b283 added work on header check
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
62 index = self.index,
4da025d0b283 added work on header check
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
63 reason = self.reason,
4da025d0b283 added work on header check
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
64 )
4da025d0b283 added work on header check
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
65 def __str__(self):
4da025d0b283 added work on header check
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
66 return reason
4da025d0b283 added work on header check
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
67
48
38b0919c1934 added some header tests
Teemu Piippo <teemu@hecknology.net>
parents: 47
diff changeset
68 class HistoryEntry:
38b0919c1934 added some header tests
Teemu Piippo <teemu@hecknology.net>
parents: 47
diff changeset
69 def __init__(self, date, user, text):
38b0919c1934 added some header tests
Teemu Piippo <teemu@hecknology.net>
parents: 47
diff changeset
70 self.date, self.user, self.text = date, user, text
38b0919c1934 added some header tests
Teemu Piippo <teemu@hecknology.net>
parents: 47
diff changeset
71 def __repr__(self):
38b0919c1934 added some header tests
Teemu Piippo <teemu@hecknology.net>
parents: 47
diff changeset
72 return str.format(
38b0919c1934 added some header tests
Teemu Piippo <teemu@hecknology.net>
parents: 47
diff changeset
73 'HistoryEntry({date!r}, {user!r}, {text!r})',
38b0919c1934 added some header tests
Teemu Piippo <teemu@hecknology.net>
parents: 47
diff changeset
74 date = self.date,
38b0919c1934 added some header tests
Teemu Piippo <teemu@hecknology.net>
parents: 47
diff changeset
75 user = self.user,
38b0919c1934 added some header tests
Teemu Piippo <teemu@hecknology.net>
parents: 47
diff changeset
76 text = self.text)
38b0919c1934 added some header tests
Teemu Piippo <teemu@hecknology.net>
parents: 47
diff changeset
77
47
4da025d0b283 added work on header check
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
78 class HeaderParser:
4da025d0b283 added work on header check
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
79 def __init__(self):
4da025d0b283 added work on header check
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
80 self.model_body = None
4da025d0b283 added work on header check
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
81 self.cursor = 0
4da025d0b283 added work on header check
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
82 self.problems = []
4da025d0b283 added work on header check
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
83 def parse(self, model_body):
4da025d0b283 added work on header check
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
84 result = Header()
48
38b0919c1934 added some header tests
Teemu Piippo <teemu@hecknology.net>
parents: 47
diff changeset
85 self.result = result
47
4da025d0b283 added work on header check
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
86 self.order = []
4da025d0b283 added work on header check
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
87 self.cursor = -1
4da025d0b283 added work on header check
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
88 self.model_body = model_body
4da025d0b283 added work on header check
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
89 self.skip_to_next()
4da025d0b283 added work on header check
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
90 result.description = self.current()
4da025d0b283 added work on header check
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
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
4da025d0b283 added work on header check
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
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
4da025d0b283 added work on header check
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
95 for header_entry in self.get_more_header_stuff():
4da025d0b283 added work on header check
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
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
a1f5c12fa45c fix LDRAW_ORG parsing
Teemu Piippo <teemu@hecknology.net>
parents: 48
diff changeset
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
4da025d0b283 added work on header check
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
107 'part type'):
48
38b0919c1934 added some header tests
Teemu Piippo <teemu@hecknology.net>
parents: 47
diff changeset
108 result.filetype = self.groups[0]
38b0919c1934 added some header tests
Teemu Piippo <teemu@hecknology.net>
parents: 47
diff changeset
109 result.qualifiers = re.findall(r'(?:Physical_Colour|Alias|ORIGINAL|UPDATE \d\d\d\d-\d\d)', self.groups[1])
47
4da025d0b283 added work on header check
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
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
4da025d0b283 added work on header check
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
112 'license'):
48
38b0919c1934 added some header tests
Teemu Piippo <teemu@hecknology.net>
parents: 47
diff changeset
113 result.license = self.groups[0]
38b0919c1934 added some header tests
Teemu Piippo <teemu@hecknology.net>
parents: 47
diff changeset
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
38b0919c1934 added some header tests
Teemu Piippo <teemu@hecknology.net>
parents: 47
diff changeset
116 'bfc'):
38b0919c1934 added some header tests
Teemu Piippo <teemu@hecknology.net>
parents: 47
diff changeset
117 result.bfc = self.groups[0]
38b0919c1934 added some header tests
Teemu Piippo <teemu@hecknology.net>
parents: 47
diff changeset
118 elif self.try_to_match(
38b0919c1934 added some header tests
Teemu Piippo <teemu@hecknology.net>
parents: 47
diff changeset
119 r'!HISTORY (\d{4}-\d{2}-\d{2}) ([\[{][^\]}]+[\]}]) (.+)$',
38b0919c1934 added some header tests
Teemu Piippo <teemu@hecknology.net>
parents: 47
diff changeset
120 'history'):
38b0919c1934 added some header tests
Teemu Piippo <teemu@hecknology.net>
parents: 47
diff changeset
121 result.history.append(HistoryEntry(
38b0919c1934 added some header tests
Teemu Piippo <teemu@hecknology.net>
parents: 47
diff changeset
122 date = datetime.datetime.strptime(self.groups[0], '%Y-%m-%d').date(),
38b0919c1934 added some header tests
Teemu Piippo <teemu@hecknology.net>
parents: 47
diff changeset
123 user = self.groups[1],
38b0919c1934 added some header tests
Teemu Piippo <teemu@hecknology.net>
parents: 47
diff changeset
124 text = self.groups[2],
38b0919c1934 added some header tests
Teemu Piippo <teemu@hecknology.net>
parents: 47
diff changeset
125 ))
38b0919c1934 added some header tests
Teemu Piippo <teemu@hecknology.net>
parents: 47
diff changeset
126 elif self.try_to_match(
38b0919c1934 added some header tests
Teemu Piippo <teemu@hecknology.net>
parents: 47
diff changeset
127 r'!HELP (.+)',
38b0919c1934 added some header tests
Teemu Piippo <teemu@hecknology.net>
parents: 47
diff changeset
128 'help'):
38b0919c1934 added some header tests
Teemu Piippo <teemu@hecknology.net>
parents: 47
diff changeset
129 if result.help:
38b0919c1934 added some header tests
Teemu Piippo <teemu@hecknology.net>
parents: 47
diff changeset
130 result.help += '\n'
38b0919c1934 added some header tests
Teemu Piippo <teemu@hecknology.net>
parents: 47
diff changeset
131 result.help += self.groups[0]
38b0919c1934 added some header tests
Teemu Piippo <teemu@hecknology.net>
parents: 47
diff changeset
132 elif self.try_to_match(
38b0919c1934 added some header tests
Teemu Piippo <teemu@hecknology.net>
parents: 47
diff changeset
133 r'!CATEGORY (.+)',
38b0919c1934 added some header tests
Teemu Piippo <teemu@hecknology.net>
parents: 47
diff changeset
134 'category'):
38b0919c1934 added some header tests
Teemu Piippo <teemu@hecknology.net>
parents: 47
diff changeset
135 result.category = self.groups[0]
38b0919c1934 added some header tests
Teemu Piippo <teemu@hecknology.net>
parents: 47
diff changeset
136 elif self.try_to_match(
38b0919c1934 added some header tests
Teemu Piippo <teemu@hecknology.net>
parents: 47
diff changeset
137 r'!KEYWORDS (.+)',
38b0919c1934 added some header tests
Teemu Piippo <teemu@hecknology.net>
parents: 47
diff changeset
138 'keywords'):
38b0919c1934 added some header tests
Teemu Piippo <teemu@hecknology.net>
parents: 47
diff changeset
139 if result.keywords:
38b0919c1934 added some header tests
Teemu Piippo <teemu@hecknology.net>
parents: 47
diff changeset
140 result.keywords += '\n'
38b0919c1934 added some header tests
Teemu Piippo <teemu@hecknology.net>
parents: 47
diff changeset
141 result.keywords += self.groups[0]
38b0919c1934 added some header tests
Teemu Piippo <teemu@hecknology.net>
parents: 47
diff changeset
142 elif self.try_to_match(
38b0919c1934 added some header tests
Teemu Piippo <teemu@hecknology.net>
parents: 47
diff changeset
143 r'!CMDLINE (.+)',
38b0919c1934 added some header tests
Teemu Piippo <teemu@hecknology.net>
parents: 47
diff changeset
144 'cmdline'):
38b0919c1934 added some header tests
Teemu Piippo <teemu@hecknology.net>
parents: 47
diff changeset
145 result.cmdline = self.groups[0]
47
4da025d0b283 added work on header check
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
146 else:
48
38b0919c1934 added some header tests
Teemu Piippo <teemu@hecknology.net>
parents: 47
diff changeset
147 self.parse_error("couldn't understand header syntax: " + repr(header_entry.text))
47
4da025d0b283 added work on header check
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
148 return {
4da025d0b283 added work on header check
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
149 'header': result,
4da025d0b283 added work on header check
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
150 'end-index': self.cursor + 1,
4da025d0b283 added work on header check
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
151 }
4da025d0b283 added work on header check
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
152 def parse_error(self, message):
4da025d0b283 added work on header check
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
153 raise HeaderError(index = self.cursor, reason = message)
4da025d0b283 added work on header check
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
154 def get_more_header_stuff(self):
4da025d0b283 added work on header check
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
155 while True:
4da025d0b283 added work on header check
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
156 self.cursor += 1
4da025d0b283 added work on header check
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
157 if self.cursor >= len(self.model_body):
4da025d0b283 added work on header check
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
158 break
4da025d0b283 added work on header check
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
159 entry = self.model_body[self.cursor]
4da025d0b283 added work on header check
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
160 if not is_suitable_header_object(entry):
4da025d0b283 added work on header check
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
161 break
4da025d0b283 added work on header check
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
162 if isinstance(entry, linetypes.MetaCommand):
4da025d0b283 added work on header check
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
163 yield entry
4da025d0b283 added work on header check
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
164 def skip_to_next(self, *, spaces_expected = 0):
4da025d0b283 added work on header check
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
165 while True:
4da025d0b283 added work on header check
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
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
4da025d0b283 added work on header check
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
168 self.cursor += 1
4da025d0b283 added work on header check
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
169 entry = self.model_body[self.cursor]
4da025d0b283 added work on header check
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
170 if not is_suitable_header_object(entry):
4da025d0b283 added work on header check
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
171 self.parse_error('header is incomplete')
4da025d0b283 added work on header check
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
172 if isinstance(entry, linetypes.MetaCommand):
4da025d0b283 added work on header check
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
173 return
4da025d0b283 added work on header check
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
174 def try_to_match(self, pattern, patterntype):
4da025d0b283 added work on header check
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
175 try:
4da025d0b283 added work on header check
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
176 self.groups = self.parse_pattern(pattern, patterntype)
48
38b0919c1934 added some header tests
Teemu Piippo <teemu@hecknology.net>
parents: 47
diff changeset
177 return True
47
4da025d0b283 added work on header check
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
178 except:
4da025d0b283 added work on header check
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
179 return False
4da025d0b283 added work on header check
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
180 def current(self):
4da025d0b283 added work on header check
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
181 entry = self.model_body[self.cursor]
4da025d0b283 added work on header check
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
182 assert isinstance(entry, linetypes.MetaCommand)
4da025d0b283 added work on header check
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
183 return entry.text
4da025d0b283 added work on header check
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
184 def parse_pattern(self, pattern, description):
4da025d0b283 added work on header check
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
185 match = re.search(pattern, self.current())
4da025d0b283 added work on header check
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
186 if match:
4da025d0b283 added work on header check
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
187 self.order.append(description)
48
38b0919c1934 added some header tests
Teemu Piippo <teemu@hecknology.net>
parents: 47
diff changeset
188 if description not in self.result.first_occurrence:
38b0919c1934 added some header tests
Teemu Piippo <teemu@hecknology.net>
parents: 47
diff changeset
189 self.result.first_occurrence[description] = self.cursor
47
4da025d0b283 added work on header check
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
190 return match.groups()
4da025d0b283 added work on header check
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
191 else:
4da025d0b283 added work on header check
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
192 self.parse_error(str.format("couldn't parse {}", description))

mercurial