From 3fb2bb657501430192990a2f7d991155c1ac5017 Mon Sep 17 00:00:00 2001 From: Romain Bignon Date: Thu, 11 Nov 2010 01:43:58 +0100 Subject: [PATCH] factorization of CallErrors exception handlers --- weboob/applications/havesex/havesex.py | 14 +++-- weboob/applications/monboob/monboob.py | 7 +-- weboob/tools/application/base.py | 27 +++++++-- weboob/tools/application/repl.py | 78 ++++++++++++++++---------- 4 files changed, 78 insertions(+), 48 deletions(-) diff --git a/weboob/applications/havesex/havesex.py b/weboob/applications/havesex/havesex.py index 24418527..aea6f464 100644 --- a/weboob/applications/havesex/havesex.py +++ b/weboob/applications/havesex/havesex.py @@ -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)) diff --git a/weboob/applications/monboob/monboob.py b/weboob/applications/monboob/monboob.py index e1daf20a..d57cd8ea 100644 --- a/weboob/applications/monboob/monboob.py +++ b/weboob/applications/monboob/monboob.py @@ -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') diff --git a/weboob/tools/application/base.py b/weboob/tools/application/base.py index b627fb6e..e4ca7b93 100644 --- a/weboob/tools/application/base.py +++ b/weboob/tools/application/base.py @@ -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() diff --git a/weboob/tools/application/repl.py b/weboob/tools/application/repl.py index 7da75bf8..81ceee06 100644 --- a/weboob/tools/application/repl.py +++ b/weboob/tools/application/repl.py @@ -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):