Rietveld Code Review Tool
Help | Bug tracker | Discussion group | Source code

Delta Between Two Patch Sets: sitescripts/extensions/pad/validation.py

Issue 5723465818570752: Issue 520 - Generate PAD files for download portals when updating download links (Closed)
Left Patch Set: Added PAD file for URL fixer Created May 27, 2014, 4:33 p.m.
Right Patch Set: Addressed comments Created June 4, 2014, 4:35 p.m.
Left:
Right:
Use n/p to move between diff chunks; N/P to move between comments.
Jump to:
Left: Side by side diff | Download
Right: Side by side diff | Download
« no previous file with change/comment | « sitescripts/extensions/pad/language.py ('k') | sitescripts/extensions/template/pad.xml » ('j') | no next file with change/comment »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
LEFTRIGHT
1 # This file is part of the Adblock Plus web scripts, 1 # This file is part of the Adblock Plus web scripts,
2 # Copyright (C) 2006-2014 Eyeo GmbH 2 # Copyright (C) 2006-2014 Eyeo GmbH
3 # 3 #
4 # Adblock Plus is free software: you can redistribute it and/or modify 4 # Adblock Plus is free software: you can redistribute it and/or modify
5 # it under the terms of the GNU General Public License version 3 as 5 # it under the terms of the GNU General Public License version 3 as
6 # published by the Free Software Foundation. 6 # published by the Free Software Foundation.
7 # 7 #
8 # Adblock Plus is distributed in the hope that it will be useful, 8 # Adblock Plus is distributed in the hope that it will be useful,
9 # but WITHOUT ANY WARRANTY; without even the implied warranty of 9 # but WITHOUT ANY WARRANTY; without even the implied warranty of
10 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 10 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
(...skipping 119 matching lines...) Expand 10 before | Expand all | Expand 10 after
130 (['XML_DIZ_INFO', 'ASP', 'ASP_Member_Number'], None), 130 (['XML_DIZ_INFO', 'ASP', 'ASP_Member_Number'], None),
131 ] 131 ]
132 132
133 def validate_fields(fields, nodes, filename): 133 def validate_fields(fields, nodes, filename):
134 expected_nodes = set() 134 expected_nodes = set()
135 135
136 for node_name, fields in itertools.groupby(fields, lambda (path, regex): path[ 0]): 136 for node_name, fields in itertools.groupby(fields, lambda (path, regex): path[ 0]):
137 expected_nodes.add(node_name) 137 expected_nodes.add(node_name)
138 138
139 regex = None 139 regex = None
140 leaf = False
141 nested_fields = [] 140 nested_fields = []
142 for path, regex_ in fields: 141 for path, regex_ in fields:
143 if path == [node_name]: 142 if path == [node_name]:
144 regex = regex_ 143 regex = regex_
145 leaf = True
146 else: 144 else:
147 nested_fields.append((path[1:], regex_)) 145 nested_fields.append((path[1:], regex_))
148 146
149 found = False 147 found = False
150 for node in nodes: 148 for node in nodes:
151 if node.nodeName == node_name: 149 if node.nodeName == node_name:
152 if found: 150 if found:
153 warnings.warn('invalid PAD file (duplicate node)\n' 151 warnings.warn('invalid PAD file (duplicate node)\n'
154 'filename: %s\n' 152 'filename: %s\n'
155 'node: %s' % (filename, node_name)) 153 'node: %s' % (filename, node_name))
156 154
157 if regex: 155 if regex:
158 value = ''.join(child.toxml() for child in node.childNodes) 156 value = ''.join(child.toxml() for child in node.childNodes)
159 if not re.match(regex, value): 157 if not re.match(regex, value):
160 warnings.warn('invalid PAD file (invalid value)\n' 158 warnings.warn('invalid PAD file (invalid value)\n'
161 'filename: %s\n' 159 'filename: %s\n'
162 'node: %s\n' 160 'node: %s\n'
163 'value: %s\n' 161 'value: %s\n'
164 'regex: %s' % (filename, node_name, value, regex)) 162 'regex: %s' % (filename, node_name, value, regex))
165 163
166 if nested_fields: 164 if nested_fields:
167 » validate_fields(nested_fields, node.childNodes, filename) 165 validate_fields(nested_fields, node.childNodes, filename)
168 166
169 found = True 167 found = True
170 168
171 if not found: 169 if not found:
172 if regex and not re.match(regex, ''): 170 if regex and not re.match(regex, ''):
173 warnings.warn('invalid PAD file (missing node)\n' 171 warnings.warn('invalid PAD file (missing node)\n'
174 'filename: %s\n' 172 'filename: %s\n'
175 'node: %s' % (filename, node_name)) 173 'node: %s' % (filename, node_name))
176 174
177 validate_fields(nested_fields, [], filename) 175 validate_fields(nested_fields, [], filename)
(...skipping 19 matching lines...) Expand all
197 for field in doc.getElementsByTagName('Field'): 195 for field in doc.getElementsByTagName('Field'):
198 path, regex = [ 196 path, regex = [
199 ''.join(node.nodeValue for node in field.getElementsByTagName(name)[0].chi ldNodes) 197 ''.join(node.nodeValue for node in field.getElementsByTagName(name)[0].chi ldNodes)
200 for name in ('Path', 'RegEx') 198 for name in ('Path', 'RegEx')
201 ] 199 ]
202 print ' (%r, %s),' % (str(path).split('/'), "r'%s'" % regex if regex else ' None') 200 print ' (%r, %s),' % (str(path).split('/'), "r'%s'" % regex if regex else ' None')
203 print ']' 201 print ']'
204 202
205 if __name__ == '__main__': 203 if __name__ == '__main__':
206 print_fields() 204 print_fields()
LEFTRIGHT

Powered by Google App Engine
This is Rietveld