-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

@ -57,7 +57,7 @@ class DefaultOptions():
class MyPlayer():
"""Black magic invoking a video player to this world.
Presently, due to strong disturbances in the holidays of the ether
world, the video player used is chosen from a static list of
programs. See PLAYERS for more information.
@ -82,11 +82,11 @@ class MyPlayer():
if self.options.verbose:
print "Video player is (%s,%s)" % (self.player,
self.player_stdin)
def play(self, video):
"""Play a video object, using programs from the PLAYERS list.
This function dispatch calls to either _play_default or
_play_rtmp for special rtmp streams using SWF verification.
"""
@ -289,7 +289,7 @@ class MyCmd(Cmd):
def do_search(self, pattern):
"""search [PATTERN]
Search for videos.
If no patterns are given, display the last entries.
"""
@ -307,7 +307,7 @@ class MyCmd(Cmd):
for i, (backend, video) in enumerate(videos_g):
self.videos.append((backend,video))
# code factorisatorminator: display the list of videos
self.do_ls("")
@ -334,8 +334,8 @@ class MyCmd(Cmd):
print_keys_values([
("url", video.url),
("duration", "%s seconds" % video.duration),
("rating", "%.2f/%.2f" % (video.rating,
video.rating_max))],
("rating", "%.2f/%.2f" % (video.rating or 0,
video.rating_max or 0))],
indent=4)

View file

@ -34,6 +34,9 @@ class WeboobCfg(ReplApplication):
VERSION = '0.1'
COPYRIGHT = 'Copyright(C) 2010 Christophe Benz, Romain Bignon'
def load_default_backends(self):
pass
def caps_included(self, modcaps, caps):
modcaps = [x.__name__ for x in modcaps]
for cap in caps:
@ -56,7 +59,7 @@ class WeboobCfg(ReplApplication):
self.weboob.modules_loader.load_all()
if name not in [_name for _name, backend in self.weboob.modules_loader.loaded.iteritems()]:
logging.error(u'Backend "%s" does not exist.' % name)
return 1
return
params = {}
# set backend params from command-line arguments
@ -65,7 +68,7 @@ class WeboobCfg(ReplApplication):
key, value = option.split('=', 1)
except ValueError:
logging.error(u'Parameters have to be formatted "key=value"')
return 1
return
params[key] = value
# ask for params non-specified on command-line arguments
backend = self.weboob.modules_loader.get_or_load_module(name)
@ -108,7 +111,7 @@ class WeboobCfg(ReplApplication):
except ConfigParser.DuplicateSectionError:
print 'Instance "%s" already exists for backend "%s".' % (new_name, name)
def do_listconfigured(self):
def do_list(self, line):
"""
list
@ -118,10 +121,11 @@ class WeboobCfg(ReplApplication):
for instance_name, name, params in sorted(self.weboob.backends_config.iter_backends()):
backend = self.weboob.modules_loader.get_or_load_module(name)
row = OrderedDict([('Instance name', instance_name),
('Backend name', name),
('Backend', name),
('Configuration', ', '.join('%s=%s' % (key, ('*****' if key in backend.config and backend.config[key].is_masked else value)) for key, value in params.iteritems())),
])
self.format(row)
self.flush()
def do_remove(self, instance_name):
"""

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)