factorization of CallErrors exception handlers

This commit is contained in:
Romain Bignon 2010-11-11 01:43:58 +01:00
commit 3fb2bb6575
4 changed files with 78 additions and 48 deletions

View file

@ -17,7 +17,6 @@
import sys
import logging
import weboob
from weboob.tools.application.repl import ReplApplication
@ -86,7 +85,10 @@ class HaveSex(ReplApplication):
def main(self, argv):
self.load_config()
self.do('init_optimizations').wait()
try:
self.do('init_optimizations').wait()
except weboob.core.CallErrors, e:
self.bcall_errors_handler(e)
optimizations = self.storage.get('optims')
for optim, backends in optimizations.iteritems():
@ -116,6 +118,10 @@ class HaveSex(ReplApplication):
return True
def edit_optims(self, backend_names, optims_names, stop=False):
if optims_names is None:
print >>sys.stderr, 'Error: missing parameters.'
return 1
for optim_name in optims_names.split():
backends_optims = {}
for backend, optim in self.do('get_optimization', optim_name, backends=backend_names):
@ -181,9 +187,7 @@ class HaveSex(ReplApplication):
if isinstance(error, OptimizationNotFound):
self.logger.error(u'Error(%s): Optimization "%s" not found' % (backend.name, optim_name))
else:
self.logger.error(u'Error(%s): %s' % (backend.name, error))
if logging.root.level == logging.DEBUG:
self.logger.error(backtrace)
self.bcall_error_handler(backend, error, backtrace)
if store:
if len(storage_optim) > 0:
self.storage.set('optims', optim_name, list(storage_optim))

View file

@ -221,12 +221,7 @@ class Monboob(ReplApplication):
if self.send_email(backend, message):
backend.set_message_read(message)
except CallErrors, e:
for backend, error, backtrace in e.errors:
print >>sys.stderr, u'Error(%s): %s' % (backend.name, error)
if logging.root.level == logging.DEBUG:
print >>sys.stderr, backtrace
if logging.root.level != logging.DEBUG:
print >>sys.stderr, 'Use --debug option to print backtraces.'
self.bcall_errors_handler(e)
def send_email(self, backend, mail):
domain = self.config.get('domain')

View file

@ -255,6 +255,25 @@ class BaseApplication(object):
else:
return self._do_complete_obj(backend, selected_fields, res)
def bcall_error_handler(self, backend, error, backtrace):
"""
Handler for an exception inside the CallErrors exception.
This method can be overrided to support more exceptions types.
"""
print >>sys.stderr, u'Error(%s): %s' % (backend.name, error)
if logging.root.level == logging.DEBUG:
print >>sys.stderr, backtrace
def bcall_errors_handler(self, errors):
"""
Handler for the CallErrors exception.
"""
for backend, error, backtrace in errors.errors:
self.bcall_error_handler(backend, error, backtrace)
if logging.root.level != logging.DEBUG:
print >>sys.stderr, 'Use --debug option to print backtraces.'
def parse_args(self, args):
self.options, args = self._parser.parse_args(args)
@ -351,11 +370,7 @@ class BaseApplication(object):
print 'Configuration error: %s' % e
sys.exit(1)
except CallErrors, e:
for backend, error, backtrace in e.errors:
print >>sys.stderr, u'Error(%s): %s' % (backend.name, error)
if logging.root.level == logging.DEBUG:
print >>sys.stderr, backtrace
if logging.root.level != logging.DEBUG:
print >>sys.stderr, 'Use --debug option to print backtraces.'
app.bcall_errors_handler(e)
sys.exit(1)
finally:
app.deinit()

View file

@ -351,6 +351,52 @@ class ReplApplication(Cmd, BaseApplication):
stop = None
return stop
def bcall_error_handler(self, backend, error, backtrace):
"""
Handler for an exception inside the CallErrors exception.
This method can be overrided to support more exceptions types.
"""
if isinstance(error, BrowserIncorrectPassword):
msg = unicode(error)
if not msg:
msg = 'invalid login/password.'
print >>sys.stderr, 'Error(%s): %s' % (backend.name, msg)
if self.ask('Do you want to reconfigure this backend?', default=True):
self.unload_backends(names=[backend.name])
self.edit_backend(backend.name)
self.load_backends(names=[backend.name])
elif isinstance(error, BrowserUnavailable):
msg = unicode(error)
if not msg:
msg = 'website is unavailable.'
print >>sys.stderr, u'Error(%s): %s' % (backend.name, msg)
elif isinstance(error, NotImplementedError):
print >>sys.stderr, u'Error(%s): this feature is not supported yet by this backend.' % backend.name
print >>sys.stderr, u' %s To help the maintainer of this backend implement this feature,' % (' ' * len(backend.name))
print >>sys.stderr, u' %s please contact: %s <%s>' % (' ' * len(backend.name), backend.MAINTAINER, backend.EMAIL)
else:
print >>sys.stderr, u'Error(%s): %s' % (backend.name, error)
if logging.root.level == logging.DEBUG:
print >>sys.stderr, backtrace
else:
return True
def bcall_errors_handler(self, errors):
"""
Handler for the CallErrors exception.
"""
ask_debug_mode = False
for backend, error, backtrace in errors.errors:
if self.bcall_error_handler(backend, error, backtrace):
ask_debug_mode = True
if ask_debug_mode:
if self.interactive:
print >>sys.stderr, 'Use "logging debug" option to print backtraces.'
else:
print >>sys.stderr, 'Use --debug option to print backtraces.'
def onecmd(self, line):
"""
This REPL method is overrided to catch some particular exceptions.
@ -367,37 +413,7 @@ class ReplApplication(Cmd, BaseApplication):
try:
return super(ReplApplication, self).onecmd(line)
except CallErrors, e:
ask_debug_mode = False
for backend, error, backtrace in e.errors:
if isinstance(error, BrowserIncorrectPassword):
msg = unicode(error)
if not msg:
msg = 'invalid login/password.'
print >>sys.stderr, 'Error(%s): %s' % (backend.name, msg)
if self.ask('Do you want to reconfigure this backend?', default=True):
self.unload_backends(names=[backend.name])
self.edit_backend(backend.name)
self.load_backends(names=[backend.name])
elif isinstance(error, BrowserUnavailable):
msg = unicode(error)
if not msg:
msg = 'website is unavailable.'
print >>sys.stderr, u'Error(%s): %s' % (backend.name, msg)
elif isinstance(error, NotImplementedError):
print >>sys.stderr, u'Error(%s): this feature is not supported yet by this backend.' % backend.name
print >>sys.stderr, u' %s To help the maintainer of this backend implement this feature,' % (' ' * len(backend.name))
print >>sys.stderr, u' %s please contact: %s <%s>' % (' ' * len(backend.name), backend.MAINTAINER, backend.EMAIL)
else:
print >>sys.stderr, u'Error(%s): %s' % (backend.name, error)
if logging.root.level == logging.DEBUG:
print >>sys.stderr, backtrace
else:
ask_debug_mode = True
if ask_debug_mode:
if self.interactive:
print >>sys.stderr, 'Use "logging debug" option to print backtraces.'
else:
print >>sys.stderr, 'Use --debug option to print backtraces.'
self.bcall_error_handler(e)
except NotEnoughArguments, e:
print >>sys.stderr, 'Error: no enough arguments.'
except (KeyboardInterrupt,EOFError):