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):
return obj.label
class Boobill(ReplApplication):
APPNAME = 'boobill'
VERSION = '0.c'
@ -49,7 +50,6 @@ class Boobill(ReplApplication):
'ls': 'subscriptions',
}
def main(self, argv):
self.load_config()
return ReplApplication.main(self, argv)
@ -61,37 +61,42 @@ class Boobill(ReplApplication):
List subscriptions
"""
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.flush()
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)
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
for subscrib in self.get_object_list('iter_subscription'):
l.append((subscrib.id, subscrib.backend))
else:
l.append((id, backend_name))
# XXX: should be generated by backend? -Flo
# XXX: no, but you should do it in a specific formatter -romain
mysum = Detail()
mysum.label = u"Sum"
mysum.infos = u"Generated by boobill"
mysum.price = Decimal("0.")
for id, backend in l:
names = (backend,) if backend is not None else None
# XXX: should be generated by backend? -Flo
# XXX: no, but you should do it in a specific formatter -romain
mysum = Detail()
mysum.label = u"Sum"
mysum.infos = u"Generated by boobill"
mysum.price = Decimal("0.")
self.start_format()
for backend, detail in self.do('get_details', id, backends=names):
self.format(detail)
mysum.price = detail.price + mysum.price
self.start_format()
for backend, detail in self.do('get_details', id, backends=names):
self.format(detail)
mysum.price = detail.price + mysum.price
self.format(mysum)
self.flush()
self.format(mysum)
self.flush()
def do_history(self, id):
"""

View file

@ -45,6 +45,7 @@ __all__ = ['NotEnoughArguments', 'ReplApplication']
class NotEnoughArguments(Exception):
pass
class ReplOptionParser(OptionParser):
def format_option_help(self, formatter=None):
if not formatter:
@ -53,6 +54,7 @@ class ReplOptionParser(OptionParser):
return '%s\n%s' % (formatter.format_commands(self.commands),
OptionParser.format_option_help(self, formatter))
class ReplOptionFormatter(IndentedHelpFormatter):
def format_commands(self, commands):
s = u''
@ -67,6 +69,7 @@ class ReplOptionFormatter(IndentedHelpFormatter):
s += ' %s\n' % c
return s
class ReplApplication(Cmd, ConsoleApplication):
"""
Base application class for Repl applications.
@ -211,6 +214,18 @@ class ReplApplication(Cmd, ConsoleApplication):
backend.fillobj(obj, fields)
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):
self.objects = []
self.collections = []