Use newer form of catching exceptions

autopep8 -i --select=W602
Also some other minor deprecated syntax changes, like "while 1".
I did not commit the less obvious changes.
This commit is contained in:
Laurent Bachelier 2013-07-27 15:13:48 +02:00
commit a6ad7e83ff
72 changed files with 151 additions and 154 deletions

View file

@ -193,7 +193,7 @@ class Boobill(ReplApplication):
try:
with open(dest, 'w') as f:
f.write(buf)
except IOError, e:
except IOError as e:
print >>sys.stderr, 'Unable to write bill in "%s": %s' % (dest, e)
return 1
return
@ -210,7 +210,7 @@ class Boobill(ReplApplication):
try:
with open(dest, 'w') as f:
f.write(buf)
except IOError, e:
except IOError as e:
print >>sys.stderr, 'Unable to write bill in "%s": %s' % (dest, e)
return 1

View file

@ -373,7 +373,7 @@ class Boobmsg(ReplApplication):
try:
self.do('post_message', message, backends=backend_name).wait()
except CallErrors, errors:
except CallErrors as errors:
self.bcall_errors_handler(errors)
else:
if self.interactive:

View file

@ -490,11 +490,11 @@ class Cineoob(ReplApplication):
try:
with open(dest, 'w') as f:
f.write(buf)
except IOError, e:
except IOError as e:
print >>sys.stderr, 'Unable to write .torrent in "%s": %s' % (dest, e)
return 1
return
except CallErrors, errors:
except CallErrors as errors:
for backend, error, backtrace in errors:
if isinstance(error, MagnetOnly):
print >>sys.stderr, u'Error(%s): No direct URL available, ' \
@ -598,11 +598,11 @@ class Cineoob(ReplApplication):
try:
with open(dest, 'w') as f:
f.write(buf)
except IOError, e:
except IOError as e:
print >>sys.stderr, 'Unable to write file in "%s": %s' % (dest, e)
return 1
return
except CallErrors, errors:
except CallErrors as errors:
for backend, error, backtrace in errors:
self.bcall_error_handler(backend, error, backtrace)

View file

@ -141,7 +141,7 @@ class Cookboob(ReplApplication):
try:
with codecs.open(dest, 'w', 'utf-8') as f:
f.write(xmlstring)
except IOError, e:
except IOError as e:
print >>sys.stderr, 'Unable to write .kreml in "%s": %s' % (dest, e)
return 1
return

View file

@ -70,7 +70,7 @@ class HaveDate(Boobmsg):
try:
self.do('init_optimizations').wait()
except CallErrors, e:
except CallErrors as e:
self.bcall_errors_handler(e)
optimizations = self.storage.get('optims')
@ -157,7 +157,7 @@ class HaveDate(Boobmsg):
except KeyError:
pass
sys.stdout.write('.\n')
except CallErrors, errors:
except CallErrors as errors:
for backend, error, backtrace in errors:
if isinstance(error, OptimizationNotFound):
self.logger.error(u'Error(%s): Optimization "%s" not found' % (backend.name, optim_name))

View file

@ -66,14 +66,14 @@ class MonboobScheduler(Scheduler):
port = self.app.options.smtpd
try:
FakeSMTPD(self.app, host, int(port))
except socket.error, e:
except socket.error as e:
self.logger.error('Unable to start the SMTP daemon: %s' % e)
return False
# XXX Fuck, we shouldn't copy this piece of code from
# weboob.scheduler.Scheduler.run().
try:
while 1:
while True:
self.stop_event.wait(0.1)
if self.app.options.smtpd:
asyncore.loop(timeout=0.1, count=1)
@ -178,10 +178,10 @@ class Monboob(ReplApplication):
content += unicode(s, charset)
else:
content += unicode(s)
except UnicodeError, e:
except UnicodeError as e:
self.logger.warning('Unicode error: %s' % e)
continue
except Exception, e:
except Exception as e:
self.logger.exception(e)
continue
else:
@ -235,7 +235,7 @@ class Monboob(ReplApplication):
content=content)
try:
backend.post_message(message)
except Exception, e:
except Exception as e:
content = u'Unable to send message to %s:\n' % thread_id
content += u'\n\t%s\n' % to_unicode(e)
if logging.root.level == logging.DEBUG:
@ -269,7 +269,7 @@ class Monboob(ReplApplication):
for backend, message in self.weboob.do('iter_unread_messages'):
if self.send_email(backend, message):
backend.set_message_read(message)
except CallErrors, e:
except CallErrors as e:
self.bcall_errors_handler(e)
def send_email(self, backend, mail):
@ -363,7 +363,7 @@ class Monboob(ReplApplication):
try:
smtp = SMTP(self.config.get('smtp'))
smtp.sendmail(sender, recipient, msg.as_string())
except Exception, e:
except Exception as e:
self.logger.error('Unable to deliver mail: %s' % e)
return False
else:

View file

@ -88,7 +88,7 @@ class Pastoob(ReplApplication):
try:
with codecs.open(filename, encoding=locale.getpreferredencoding()) as fp:
contents = fp.read()
except IOError, e:
except IOError as e:
print >>sys.stderr, 'Unable to open file "%s": %s' % (filename, e.strerror)
return 1

View file

@ -382,8 +382,7 @@ class MainWindow(QtMainWindow):
if self.ui.backendEdit.count() == 0:
self.backendsConfig()
langs = LANGUAGE_CONV.keys()
langs.sort()
langs = sorted(LANGUAGE_CONV.keys())
for lang in langs:
self.ui.langCombo.addItem(lang)
self.ui.langCombo.hide()

View file

@ -33,8 +33,7 @@ class Movie(QFrame):
self.parent = parent
self.ui = Ui_Movie()
self.ui.setupUi(self)
langs = LANGUAGE_CONV.keys()
langs.sort()
langs = sorted(LANGUAGE_CONV.keys())
for lang in langs:
self.ui.langCombo.addItem(lang)

View file

@ -82,7 +82,7 @@ class Subtitle(QFrame):
try:
with open(dest, 'w') as f:
f.write(data)
except IOError, e:
except IOError as e:
print >>sys.stderr, 'Unable to write subtitle file in "%s": %s' % (dest, e)
return 1
return

View file

@ -86,7 +86,7 @@ class Torrent(QFrame):
try:
with open(unicode(dest), 'w') as f:
f.write(data)
except IOError, e:
except IOError as e:
print >>sys.stderr, 'Unable to write .torrent in "%s": %s' % (dest, e)
return 1
return

View file

@ -105,7 +105,7 @@ class Recipe(QFrame):
try:
with codecs.open(dest, 'w', 'utf-8') as f:
f.write(data)
except IOError, e:
except IOError as e:
print >>sys.stderr, 'Unable to write Krecipe file in "%s": %s' % (dest, e)
return 1
return

View file

@ -94,7 +94,7 @@ class Radioob(ReplApplication):
self.logger.debug(u'You can set the media_player key to the player you prefer in the radioob '
'configuration file.')
self.player.play(radio.streams[0], player_name=player_name, player_args=media_player_args)
except (InvalidMediaPlayer, MediaPlayerNotFound), e:
except (InvalidMediaPlayer, MediaPlayerNotFound) as e:
print '%s\nRadio URL: %s' % (e, radio.streams[0].url)
def complete_info(self, text, line, *ignored):

View file

@ -157,7 +157,7 @@ class Suboob(ReplApplication):
try:
with open(dest, 'w') as f:
f.write(buf)
except IOError, e:
except IOError as e:
print >>sys.stderr, 'Unable to write file in "%s": %s' % (dest, e)
return 1
else:

View file

@ -101,7 +101,7 @@ class Traveloob(ReplApplication):
try:
filters.departure_time = self.parse_datetime(self.options.departure_time)
filters.arrival_time = self.parse_datetime(self.options.arrival_time)
except ValueError, e:
except ValueError as e:
print >>sys.stderr, 'Invalid datetime value: %s' % e
print >>sys.stderr, 'Please enter a datetime in form "yyyy-mm-dd HH:MM" or "HH:MM".'
return 1

View file

@ -154,7 +154,7 @@ class Videoob(ReplApplication):
self.logger.info(u'You can set the media_player key to the player you prefer in the videoob '
'configuration file.')
self.player.play(video, player_name=player_name, player_args=media_player_args)
except (InvalidMediaPlayer, MediaPlayerNotFound), e:
except (InvalidMediaPlayer, MediaPlayerNotFound) as e:
print '%s\nVideo URL: %s' % (e, video.url)
def complete_info(self, text, line, *ignored):

View file

@ -113,7 +113,7 @@ class WebContentEdit(ReplApplication):
sys.stdout.flush()
try:
self.do('push_content', content, message, minor=minor, backends=[content.backend]).wait()
except CallErrors, e:
except CallErrors as e:
errors.errors += e.errors
sys.stdout.write(' error (content saved in %s)\n' % path)
else:
@ -136,7 +136,7 @@ class WebContentEdit(ReplApplication):
sys.stdout.flush()
try:
self.do('push_content', content, message, minor=minor, backends=[content.backend]).wait()
except CallErrors, e:
except CallErrors as e:
errors.errors += e.errors
sys.stdout.write(' error\n')
else:

View file

@ -120,7 +120,7 @@ class WeboobCfg(ReplApplication):
for instance_name, name, params in sorted(self.weboob.backends_config.iter_backends()):
try:
module = self.weboob.modules_loader.get_or_load_module(name)
except ModuleLoadError, e:
except ModuleLoadError as e:
self.logger.warning('Unable to load module %r: %s' % (name, e))
continue

View file

@ -95,7 +95,7 @@ class WeboobRepos(ReplApplication):
try:
with open(index_file, 'r') as fp:
r.parse_index(fp)
except IOError, e:
except IOError as e:
print >>sys.stderr, 'Unable to open repository: %s' % e
print >>sys.stderr, 'Use the "create" command before.'
return 1
@ -131,7 +131,7 @@ class WeboobRepos(ReplApplication):
if not os.path.exists(krname):
raise Exception('No valid key file found.')
kr_mtime = mktime(strptime(str(r.key_update), '%Y%m%d%H%M'))
os.chmod(krname, 0644)
os.chmod(krname, 0o644)
os.utime(krname, (kr_mtime, kr_mtime))
else:
print 'Keyring is up to date'

View file

@ -137,11 +137,11 @@ class Weboorrents(ReplApplication):
try:
with open(dest, 'w') as f:
f.write(buf)
except IOError, e:
except IOError as e:
print >>sys.stderr, 'Unable to write .torrent in "%s": %s' % (dest, e)
return 1
return
except CallErrors, errors:
except CallErrors as errors:
for backend, error, backtrace in errors:
if isinstance(error, MagnetOnly):
print >>sys.stderr, u'Error(%s): No direct URL available, ' \