-b is handled by BaseApplication

This commit is contained in:
Romain Bignon 2010-09-24 21:56:49 -04:00
commit aee0e49cc8
5 changed files with 40 additions and 35 deletions

View file

@ -122,6 +122,7 @@ class BaseApplication(object):
def __init__(self, option_parser=None):
self.weboob = self.create_weboob()
self.config = None
self.options = None
if option_parser is None:
self._parser = OptionParser(self.SYNOPSIS, version=self._get_optparse_version())
else:
@ -199,6 +200,8 @@ class BaseApplication(object):
raise NotImplementedError()
def load_backends(self, caps=None, names=None, *args, **kwargs):
if names is None and self.options.backends:
names = self.options.backends.split(',')
loaded = self.weboob.load_backends(caps, names, *args, **kwargs)
if not loaded:
logging.warning(u'No backend loaded')
@ -248,6 +251,14 @@ class BaseApplication(object):
else:
return self._do_complete_obj(backend, fields, res)
def parse_args(self, args):
self.options, args = self._parser.parse_args(args)
self._handle_options()
self.handle_application_options()
return args
@classmethod
def run(klass, args=None):
"""
@ -267,9 +278,7 @@ class BaseApplication(object):
if args is None:
args = [(sys.stdin.encoding and arg.decode(sys.stdin.encoding) or arg) for arg in sys.argv]
app = klass()
app.options, args = app._parser.parse_args(args)
app.set_requested_backends(app.options.backends.split(',') if app.options.backends else None)
args = app.parse_args(args)
if app.options.shell_completion:
items = set()
@ -292,9 +301,6 @@ class BaseApplication(object):
logging.basicConfig(stream=sys.stdout, level=level, format=log_format)
logging.root.level = level
app._handle_options()
app.handle_application_options()
try:
try:
sys.exit(app.main(args))
@ -306,6 +312,3 @@ class BaseApplication(object):
sys.exit(1)
finally:
app.deinit()
def set_requested_backends(self, requested_backends):
pass

View file

@ -48,7 +48,6 @@ class ConsoleApplication(BaseApplication):
SYNOPSIS += ' %prog [--help] [--version]'
def __init__(self):
self.enabled_backends = set()
option_parser = OptionParser(self.SYNOPSIS, version=self._get_optparse_version())
try:
@ -271,8 +270,6 @@ class ConsoleApplication(BaseApplication):
command = staticmethod(command)
def load_backends(self, caps=None, names=None, *args, **kwargs):
if names is None:
names = self.enabled_backends
loaded_backends = BaseApplication.load_backends(self, caps, names, *args, **kwargs)
if not loaded_backends:
print 'Cannot start application: no configured backend was found.'
@ -314,9 +311,6 @@ class ConsoleApplication(BaseApplication):
except BackendNotFound, e:
logging.error(e)
def set_requested_backends(self, requested_backends):
self.enabled_backends = requested_backends
def do(self, function, *args, **kwargs):
"""
Call Weboob.do(), after having filled the yielded object, if selected fields are given by user.

View file

@ -107,7 +107,7 @@ class ReplApplication(Cmd, BaseApplication):
import readline
except ImportError:
pass
finally:
else:
history_filepath = os.path.join(self.weboob.WORKDIR, '%s_history' % self.APPNAME)
try:
readline.read_history_file(history_filepath)
@ -118,18 +118,16 @@ class ReplApplication(Cmd, BaseApplication):
atexit.register(savehist)
self._interactive = False
self.enabled_backends = []
@property
def interactive(self):
return self._interactive
def set_requested_backends(self, requested_backends):
self.load_default_backends()
if requested_backends:
self.enabled_backends = set(backend for backend in self.weboob.iter_backends()
if backend.name in requested_backends)
else:
self.enabled_backends = list(self.weboob.iter_backends())
def load_backends(self, *args, **kwargs):
ret = super(ReplApplication, self).load_backends(*args, **kwargs)
self.enabled_backends = list(self.weboob.iter_backends())
return ret
def load_default_backends(self):
"""
@ -168,8 +166,12 @@ class ReplApplication(Cmd, BaseApplication):
raise
except NotEnoughArguments, e:
print >>sys.stderr, 'Error: no enough arguments.'
except (KeyboardInterrupt,EOFError):
# ^C during a command process doesn't exit application.
print '\nAborted.'
def main(self, argv):
self.load_default_backends()
cmd_args = argv[1:]
if cmd_args:
if cmd_args[0] == 'help':
@ -197,6 +199,8 @@ class ReplApplication(Cmd, BaseApplication):
# XXX IT ABSOLUTLY DOESN'T WORK, OBJ ISN'T EXISTANT.
# PLEASE REVIEW THIS CODE.
#fields = [k for k, v in iter_fields(obj)]
# TODO Perhaps this is the core goal to determine what fields to use,
# by creating a singleton AllFields.
fields = None
return self.weboob.do(self._do_complete, self.options.count, fields, function, *args, **kwargs)