factorization of CallErrors exception handlers
This commit is contained in:
parent
111fc19683
commit
3fb2bb6575
4 changed files with 78 additions and 48 deletions
|
|
@ -17,7 +17,6 @@
|
||||||
|
|
||||||
|
|
||||||
import sys
|
import sys
|
||||||
import logging
|
|
||||||
|
|
||||||
import weboob
|
import weboob
|
||||||
from weboob.tools.application.repl import ReplApplication
|
from weboob.tools.application.repl import ReplApplication
|
||||||
|
|
@ -86,7 +85,10 @@ class HaveSex(ReplApplication):
|
||||||
def main(self, argv):
|
def main(self, argv):
|
||||||
self.load_config()
|
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')
|
optimizations = self.storage.get('optims')
|
||||||
for optim, backends in optimizations.iteritems():
|
for optim, backends in optimizations.iteritems():
|
||||||
|
|
@ -116,6 +118,10 @@ class HaveSex(ReplApplication):
|
||||||
return True
|
return True
|
||||||
|
|
||||||
def edit_optims(self, backend_names, optims_names, stop=False):
|
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():
|
for optim_name in optims_names.split():
|
||||||
backends_optims = {}
|
backends_optims = {}
|
||||||
for backend, optim in self.do('get_optimization', optim_name, backends=backend_names):
|
for backend, optim in self.do('get_optimization', optim_name, backends=backend_names):
|
||||||
|
|
@ -181,9 +187,7 @@ class HaveSex(ReplApplication):
|
||||||
if isinstance(error, OptimizationNotFound):
|
if isinstance(error, OptimizationNotFound):
|
||||||
self.logger.error(u'Error(%s): Optimization "%s" not found' % (backend.name, optim_name))
|
self.logger.error(u'Error(%s): Optimization "%s" not found' % (backend.name, optim_name))
|
||||||
else:
|
else:
|
||||||
self.logger.error(u'Error(%s): %s' % (backend.name, error))
|
self.bcall_error_handler(backend, error, backtrace)
|
||||||
if logging.root.level == logging.DEBUG:
|
|
||||||
self.logger.error(backtrace)
|
|
||||||
if store:
|
if store:
|
||||||
if len(storage_optim) > 0:
|
if len(storage_optim) > 0:
|
||||||
self.storage.set('optims', optim_name, list(storage_optim))
|
self.storage.set('optims', optim_name, list(storage_optim))
|
||||||
|
|
|
||||||
|
|
@ -221,12 +221,7 @@ class Monboob(ReplApplication):
|
||||||
if self.send_email(backend, message):
|
if self.send_email(backend, message):
|
||||||
backend.set_message_read(message)
|
backend.set_message_read(message)
|
||||||
except CallErrors, e:
|
except CallErrors, e:
|
||||||
for backend, error, backtrace in e.errors:
|
self.bcall_errors_handler(e)
|
||||||
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.'
|
|
||||||
|
|
||||||
def send_email(self, backend, mail):
|
def send_email(self, backend, mail):
|
||||||
domain = self.config.get('domain')
|
domain = self.config.get('domain')
|
||||||
|
|
|
||||||
|
|
@ -255,6 +255,25 @@ class BaseApplication(object):
|
||||||
else:
|
else:
|
||||||
return self._do_complete_obj(backend, selected_fields, res)
|
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):
|
def parse_args(self, args):
|
||||||
self.options, args = self._parser.parse_args(args)
|
self.options, args = self._parser.parse_args(args)
|
||||||
|
|
||||||
|
|
@ -351,11 +370,7 @@ class BaseApplication(object):
|
||||||
print 'Configuration error: %s' % e
|
print 'Configuration error: %s' % e
|
||||||
sys.exit(1)
|
sys.exit(1)
|
||||||
except CallErrors, e:
|
except CallErrors, e:
|
||||||
for backend, error, backtrace in e.errors:
|
app.bcall_errors_handler(e)
|
||||||
print >>sys.stderr, u'Error(%s): %s' % (backend.name, error)
|
sys.exit(1)
|
||||||
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.'
|
|
||||||
finally:
|
finally:
|
||||||
app.deinit()
|
app.deinit()
|
||||||
|
|
|
||||||
|
|
@ -351,6 +351,52 @@ class ReplApplication(Cmd, BaseApplication):
|
||||||
stop = None
|
stop = None
|
||||||
return stop
|
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):
|
def onecmd(self, line):
|
||||||
"""
|
"""
|
||||||
This REPL method is overrided to catch some particular exceptions.
|
This REPL method is overrided to catch some particular exceptions.
|
||||||
|
|
@ -367,37 +413,7 @@ class ReplApplication(Cmd, BaseApplication):
|
||||||
try:
|
try:
|
||||||
return super(ReplApplication, self).onecmd(line)
|
return super(ReplApplication, self).onecmd(line)
|
||||||
except CallErrors, e:
|
except CallErrors, e:
|
||||||
ask_debug_mode = False
|
self.bcall_error_handler(e)
|
||||||
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.'
|
|
||||||
except NotEnoughArguments, e:
|
except NotEnoughArguments, e:
|
||||||
print >>sys.stderr, 'Error: no enough arguments.'
|
print >>sys.stderr, 'Error: no enough arguments.'
|
||||||
except (KeyboardInterrupt,EOFError):
|
except (KeyboardInterrupt,EOFError):
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue