rewrite of the formatters system

This commit is contained in:
Romain Bignon 2012-04-03 22:40:18 +02:00
commit fc849995f4
22 changed files with 441 additions and 580 deletions

View file

@ -22,30 +22,17 @@ import sys
from weboob.capabilities.bill import ICapBill, Detail, Subscription
from weboob.tools.application.repl import ReplApplication
from weboob.tools.application.formatters.iformatter import IFormatter
from weboob.tools.application.formatters.iformatter import PrettyFormatter
__all__ = ['Boobill']
class SubscriptionsFormatter(IFormatter):
class SubscriptionsFormatter(PrettyFormatter):
MANDATORY_FIELDS = ('id', 'label')
count = 0
def flush(self):
self.count = 0
def format_dict(self, item):
self.count += 1
if self.interactive:
backend = item['id'].split('@', 1)[1]
id = '#%d (%s)' % (self.count, backend)
else:
id = item['id']
return u'%s%s%s %s' % (self.BOLD, id, self.NC, item['label'])
def get_title(self, obj):
return obj.label
class Boobill(ReplApplication):
APPNAME = 'boobill'
@ -72,9 +59,9 @@ class Boobill(ReplApplication):
List subscriptions
"""
self.start_format()
for backend, subscription in self.do('iter_subscription'):
self.add_object(subscription)
self.format(subscription)
self.cached_format(subscription)
self.flush()
def do_details(self, id):
@ -90,15 +77,15 @@ class Boobill(ReplApplication):
return 2
names = (backend_name,) if backend_name is not None else None
def do(backend):
return backend.get_details(id)
# XXX: should be generated by backend?
# XXX: should be generated by backend? -Flo
# XXX: no, but you should do it in a specific formatter -romain
mysum = Detail()
mysum.label = "Sum"
mysum.infos = "Generated by boobill"
mysum.price = 0.
for backend, detail in self.do(do, backends=names):
self.start_format()
for backend, detail in self.do('get_details', id, backends=names):
self.format(detail)
mysum.price = detail.price + mysum.price
@ -118,10 +105,8 @@ class Boobill(ReplApplication):
return 2
names = (backend_name,) if backend_name is not None else None
def do(backend):
return backend.iter_history(id)
for backend, history in self.do(do, backends=names):
self.start_format()
for backend, history in self.do('iter_history', id, backends=names):
self.format(history)
self.flush()
@ -129,7 +114,7 @@ class Boobill(ReplApplication):
"""
bills Id
Get the list of bills documents for subscription
Get the list of bills documents for subscription
id is the identifier of the backend
"""
@ -139,10 +124,8 @@ class Boobill(ReplApplication):
return 2
names = (backend_name,) if backend_name is not None else None
def do(backend):
return backend.iter_bills(id)
for backend, date in self.do(do, backends=names):
self.start_format()
for backend, date in self.do('iter_bills', id, backends=names):
self.format(date)
self.flush()
@ -150,7 +133,7 @@ class Boobill(ReplApplication):
"""
download Id [FILENAME]
download the bill
download the bill
id is the identifier of the bill (hint: try bills command)
FILENAME is where to write the file. If FILENAME is '-',
the file is written to stdout.
@ -178,5 +161,3 @@ class Boobill(ReplApplication):
print >>sys.stderr, 'Unable to write bill in "%s": %s' % (dest, e)
return 1
return