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

Delta Between Two Patch Sets: sitescripts/extensions/utils.py

Issue 29372215: Noissue - use with and contextlib.closing for closing urlopen stream (Closed)
Left Patch Set: Created Jan. 17, 2017, 11:05 a.m.
Right Patch Set: Move contextlib.closing into _urlopen Created Jan. 17, 2017, 1:10 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 | « no previous file | no next file » | 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-2016 Eyeo GmbH 2 # Copyright (C) 2006-2016 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 232 matching lines...) Expand 10 before | Expand all | Expand 10 after
243 def _urlencode(value): 243 def _urlencode(value):
244 return urllib.quote(value.encode('utf-8'), '') 244 return urllib.quote(value.encode('utf-8'), '')
245 245
246 246
247 def _urlopen(url, attempts=3): 247 def _urlopen(url, attempts=3):
248 """ 248 """
249 Tries to open a particular URL, retries on failure. 249 Tries to open a particular URL, retries on failure.
250 """ 250 """
251 for i in range(attempts): 251 for i in range(attempts):
252 try: 252 try:
253 return urllib.urlopen(url) 253 return contextlib.closing(urllib.urlopen(url))
254 except IOError as e: 254 except IOError as e:
255 error = Exception('Error {0} while opening {1} url' 255 error = Exception('Error {0} while opening {1} url'
256 .format(e, url)) 256 .format(e, url))
257 time.sleep(5) 257 time.sleep(5)
258 raise error 258 raise error
259 259
260 260
261 def _parseXMLDocument(url, attempts=2): 261 def _parseXMLDocument(url, attempts=2):
262 for i in range(attempts): 262 for i in range(attempts):
263 with contextlib.closing( _urlopen(url)) as page: 263 with _urlopen(url) as page:
Sebastian Noack 2017/01/17 11:16:43 Since we already have a wrapper for urlopen(), i.e
Vasily Kuznetsov 2017/01/17 11:39:13 Yeah, that would be nice. However, the context man
Sebastian Noack 2017/01/17 11:56:43 I don't see any issues with that restriction. It s
Vasily Kuznetsov 2017/01/17 13:13:51 Fair enough, it is bad practice and in general I g
264 content = page.read() 264 content = page.read()
265 try: 265 try:
266 return dom.parseString(content) 266 return dom.parseString(content)
267 except ExpatError as err: 267 except ExpatError as err:
268 exception = Exception('Error {0} while parsing xml:\n{1}\nfrom {2}' 268 exception = Exception('Error {0} while parsing xml:\n{1}\nfrom {2}'
269 .format(err, content, url)) 269 .format(err, content, url))
270 raise exception 270 raise exception
271 271
272 272
273 def _getMozillaDownloadLink(galleryID): 273 def _getMozillaDownloadLink(galleryID):
(...skipping 120 matching lines...) Expand 10 before | Expand all | Expand 10 after
394 if not extensions: 394 if not extensions:
395 return 395 return
396 396
397 updates = {} 397 updates = {}
398 for extension in extensions: 398 for extension in extensions:
399 updates[extension['basename']] = { 399 updates[extension['basename']] = {
400 'url': extension['updateURL'], 400 'url': extension['updateURL'],
401 'version': extension['version'] 401 'version': extension['version']
402 } 402 }
403 writeLibabpUpdateManifest(path, updates) 403 writeLibabpUpdateManifest(path, updates)
LEFTRIGHT
« no previous file | no next file » | Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Toggle Comments ('s')

Powered by Google App Engine
This is Rietveld