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

Side by Side Diff: sitescripts/extensions/utils.py

Issue 29372215: Noissue - use with and contextlib.closing for closing urlopen stream (Closed)
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:
View unified diff | Download patch
« no previous file with comments | « no previous file | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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
11 # GNU General Public License for more details. 11 # GNU General Public License for more details.
12 # 12 #
13 # You should have received a copy of the GNU General Public License 13 # You should have received a copy of the GNU General Public License
14 # along with Adblock Plus. If not, see <http://www.gnu.org/licenses/>. 14 # along with Adblock Plus. If not, see <http://www.gnu.org/licenses/>.
15 15
16 import codecs 16 import codecs
17 import contextlib
17 import os 18 import os
18 import json 19 import json
19 import re 20 import re
20 import subprocess 21 import subprocess
21 import traceback 22 import traceback
22 import time 23 import time
23 import urlparse 24 import urlparse
24 import urllib 25 import urllib
25 import xml.dom.minidom as dom 26 import xml.dom.minidom as dom
26 from ConfigParser import SafeConfigParser, NoOptionError 27 from ConfigParser import SafeConfigParser, NoOptionError
(...skipping 215 matching lines...) Expand 10 before | Expand all | Expand 10 after
242 def _urlencode(value): 243 def _urlencode(value):
243 return urllib.quote(value.encode('utf-8'), '') 244 return urllib.quote(value.encode('utf-8'), '')
244 245
245 246
246 def _urlopen(url, attempts=3): 247 def _urlopen(url, attempts=3):
247 """ 248 """
248 Tries to open a particular URL, retries on failure. 249 Tries to open a particular URL, retries on failure.
249 """ 250 """
250 for i in range(attempts): 251 for i in range(attempts):
251 try: 252 try:
252 return urllib.urlopen(url) 253 return contextlib.closing(urllib.urlopen(url))
253 except IOError as e: 254 except IOError as e:
254 error = Exception('Error {0} while opening {1} url' 255 error = Exception('Error {0} while opening {1} url'
255 .format(e, url)) 256 .format(e, url))
256 time.sleep(5) 257 time.sleep(5)
257 raise error 258 raise error
258 259
259 260
260 def _parseXMLDocument(url, attempts=2): 261 def _parseXMLDocument(url, attempts=2):
261 for i in range(attempts): 262 for i in range(attempts):
262 page = _urlopen(url) 263 with _urlopen(url) as page:
263 content = page.read() 264 content = page.read()
264 page.close()
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):
274 """ 274 """
(...skipping 119 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)
OLDNEW
« no previous file with comments | « no previous file | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld