move repl code from ConsoleApplication to ReplApplication

This commit is contained in:
Romain Bignon 2011-05-08 18:08:29 +02:00
commit 1dafee2b47
3 changed files with 40 additions and 28 deletions

View file

@ -256,7 +256,6 @@ class Boobmsg(ReplApplication):
thread_id = receiver
parent_id = None
thread = Thread(thread_id)
message = Message(thread,
0,

View file

@ -22,6 +22,7 @@ from copy import deepcopy
import getpass
import logging
import sys
import os
from weboob.capabilities.account import ICapAccount, Account, AccountRegisterError
from weboob.core.backendscfg import BackendAlreadyExists
@ -31,8 +32,16 @@ from weboob.tools.value import Value, ValueBool, ValueFloat, ValueInt
from .base import BackendNotFound, BaseApplication
__all__ = ['ConsoleApplication', 'BackendNotGiven']
class BackendNotGiven(Exception):
pass
def __init__(self, id, backends):
self.id = id
self.backends = backends
Exception.__init__(self, 'Please specify a backend to use for this argument (%s@backend_name). '
'Availables: %s.' % (id, ', '.join(name for name, backend in backends)))
class ConsoleApplication(BaseApplication):
"""
@ -92,7 +101,7 @@ class ConsoleApplication(BaseApplication):
def check_loaded_backends(self, default_config=None):
while len(self.enabled_backends) == 0:
print 'Warning: there is currently no configured backend for %s' % self.APPNAME
if not self.interactive or not self.ask('Do you want to configure backends?', default=True):
if not os.isatty(sys.stdout.fileno()) or not self.ask('Do you want to configure backends?', default=True):
return False
self.prompt_create_backends(default_config)
@ -167,25 +176,11 @@ class ConsoleApplication(BaseApplication):
except ValueError:
backend_name = None
if unique_backend and not backend_name:
backends = []
for name, backend in sorted(self.weboob.modules_loader.loaded.iteritems()):
if self.CAPS and not self.caps_included(backend.iter_caps(), self.CAPS.__name__):
continue
backends.append((name, backend))
if self.interactive:
while not backend_name:
print 'This command works with a unique backend. Availables:'
for index, (name, backend) in enumerate(backends):
print '%s%d)%s %s%-15s%s %s' % (self.BOLD, index + 1, self.NC, self.BOLD, name, self.NC,
backend.description)
i = int(self.ask('Select a backend to proceed', regexp='^\d+$'))
if i < 0 or i >= len(backends):
print >>sys.stderr, 'Error: %s is not a valid choice' % i
continue
backend_name = backends[i][0]
backends = [(b.name, b) for b in self.enabled_backends]
if len(backends) == 1:
backend_name = backends[0]
else:
raise BackendNotGiven('Please specify a backend to use for this argument (%s@backend_name). '
'Availables: %s.' % (_id, ', '.join(name for name, backend in backends)))
raise BackendNotGiven(_id, backends)
return _id, backend_name
def caps_included(self, modcaps, caps):
@ -432,7 +427,7 @@ class ConsoleApplication(BaseApplication):
else:
return True
def bcall_errors_handler(self, errors):
def bcall_errors_handler(self, errors, debugmsg='Use --debug option to print backtraces'):
"""
Handler for the CallErrors exception.
"""
@ -442,7 +437,4 @@ class ConsoleApplication(BaseApplication):
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.'
print >>sys.stderr, debugmsg

View file

@ -157,7 +157,7 @@ class ReplApplication(Cmd, ConsoleApplication):
def _complete_object(self):
return ['%s@%s' % (obj.id, obj.backend) for obj in self.objects]
def parse_id(self, id):
def parse_id(self, id, unique_backend=False):
if self.interactive:
try:
obj = self.objects[int(id) - 1]
@ -166,7 +166,22 @@ class ReplApplication(Cmd, ConsoleApplication):
else:
if isinstance(obj, CapBaseObject):
id = '%s@%s' % (obj.id, obj.backend)
return ConsoleApplication.parse_id(self, id)
try:
return ConsoleApplication.parse_id(self, id, unique_backend)
except BackendNotGiven, e:
backend_name = None
while not backend_name:
print 'This command works with an unique backend. Availables:'
for index, (name, backend) in enumerate(e.backends):
print '%s%d)%s %s%-15s%s %s' % (self.BOLD, index + 1, self.NC, self.BOLD, name, self.NC,
backend.DESCRIPTION)
i = int(self.ask('Select a backend to proceed', regexp='^\d+$'))
if i < 0 or i > len(e.backends):
print >>sys.stderr, 'Error: %s is not a valid choice' % i
continue
backend_name = e.backends[i-1][0]
return id, backend_name
def get_object(self, _id, method, fields=None):
if self.interactive:
@ -357,6 +372,12 @@ class ReplApplication(Cmd, ConsoleApplication):
else:
return super(ReplApplication, self).bcall_error_handler(backend, error, backtrace)
def bcall_errors_handler(self, errors):
if self.interactive:
ConsoleApplication.bcall_errors_handler(self, errors, 'Use "logging debug" option to print backtraces.')
else:
ConsoleApplication.bcall_errors_handler(self, errors)
# -- options related methods -------------
def _handle_options(self):
if self.options.formatter: