Get all available subscriptions for details command

Add a new method get_object_list to ReplApplication
Should close #846.
This commit is contained in:
Florent 2012-04-24 19:38:15 +02:00 committed by Romain Bignon
commit 92d5e9ea0f
2 changed files with 43 additions and 23 deletions

View file

@ -35,6 +35,7 @@ class SubscriptionsFormatter(PrettyFormatter):
def get_title(self, obj): def get_title(self, obj):
return obj.label return obj.label
class Boobill(ReplApplication): class Boobill(ReplApplication):
APPNAME = 'boobill' APPNAME = 'boobill'
VERSION = '0.c' VERSION = '0.c'
@ -49,7 +50,6 @@ class Boobill(ReplApplication):
'ls': 'subscriptions', 'ls': 'subscriptions',
} }
def main(self, argv): def main(self, argv):
self.load_config() self.load_config()
return ReplApplication.main(self, argv) return ReplApplication.main(self, argv)
@ -61,23 +61,28 @@ class Boobill(ReplApplication):
List subscriptions List subscriptions
""" """
self.start_format() self.start_format()
for backend, subscription in self.do('iter_subscription'): for subscription in self.get_object_list('iter_subscription'):
self.cached_format(subscription) self.cached_format(subscription)
self.flush() self.flush()
def do_details(self, id): def do_details(self, id):
""" """
details Id details [ID]
Get details of a subscription. Get details of subscriptions.
If no ID given, display all details of all backends
""" """
l = []
id, backend_name = self.parse_id(id) id, backend_name = self.parse_id(id)
if not id:
print >>sys.stderr, 'Error: please give a subscription ID (hint: use subscriptions command)'
return 2
names = (backend_name,) if backend_name is not None else None
if not id:
for subscrib in self.get_object_list('iter_subscription'):
l.append((subscrib.id, subscrib.backend))
else:
l.append((id, backend_name))
for id, backend in l:
names = (backend,) if backend is not None else None
# XXX: should be generated by backend? -Flo # XXX: should be generated by backend? -Flo
# XXX: no, but you should do it in a specific formatter -romain # XXX: no, but you should do it in a specific formatter -romain
mysum = Detail() mysum = Detail()

View file

@ -45,6 +45,7 @@ __all__ = ['NotEnoughArguments', 'ReplApplication']
class NotEnoughArguments(Exception): class NotEnoughArguments(Exception):
pass pass
class ReplOptionParser(OptionParser): class ReplOptionParser(OptionParser):
def format_option_help(self, formatter=None): def format_option_help(self, formatter=None):
if not formatter: if not formatter:
@ -53,6 +54,7 @@ class ReplOptionParser(OptionParser):
return '%s\n%s' % (formatter.format_commands(self.commands), return '%s\n%s' % (formatter.format_commands(self.commands),
OptionParser.format_option_help(self, formatter)) OptionParser.format_option_help(self, formatter))
class ReplOptionFormatter(IndentedHelpFormatter): class ReplOptionFormatter(IndentedHelpFormatter):
def format_commands(self, commands): def format_commands(self, commands):
s = u'' s = u''
@ -67,6 +69,7 @@ class ReplOptionFormatter(IndentedHelpFormatter):
s += ' %s\n' % c s += ' %s\n' % c
return s return s
class ReplApplication(Cmd, ConsoleApplication): class ReplApplication(Cmd, ConsoleApplication):
""" """
Base application class for Repl applications. Base application class for Repl applications.
@ -211,6 +214,18 @@ class ReplApplication(Cmd, ConsoleApplication):
backend.fillobj(obj, fields) backend.fillobj(obj, fields)
return obj return obj
def get_object_list(self, method=None):
# return cache if not empty
if len(self.objects) > 0:
return self.objects
elif method is not None:
for backend, object in self.do(method):
self.add_object(object)
return self.objects
# XXX: what can we do without method?
else:
return tuple()
def unload_backends(self, *args, **kwargs): def unload_backends(self, *args, **kwargs):
self.objects = [] self.objects = []
self.collections = [] self.collections = []