use formatters

This commit is contained in:
Christophe Benz 2010-04-20 21:34:40 +02:00
commit 81c416cd6e
2 changed files with 27 additions and 29 deletions

View file

@ -28,31 +28,31 @@ import weboob
from weboob.capabilities.bank import ICapBank, AccountNotFound from weboob.capabilities.bank import ICapBank, AccountNotFound
from weboob.tools.application import ConsoleApplication from weboob.tools.application import ConsoleApplication
__all__ = ['Boobank']
class Boobank(ConsoleApplication): class Boobank(ConsoleApplication):
APPNAME = 'boobank' APPNAME = 'boobank'
def main(self, argv): def main(self, argv):
self.weboob.load_backends(ICapBank) self.weboob.load_backends(ICapBank, names=self.enabled_backends)
return self.process_command(*argv[1:]) return self.process_command(*argv[1:])
@ConsoleApplication.command('List every available accounts') @ConsoleApplication.command('List every available accounts')
def command_list(self): def command_list(self):
accounts = [] results = {'HEADER': ('ID', 'label', 'balance', 'coming')}
for backend in self.weboob.iter_backends(): for backend in self.weboob.iter_backends():
rows = []
try: try:
for account in backend.iter_accounts(): for account in backend.iter_accounts():
accounts.append('%17s %-20s %11.2f %11.2f' % ( row = [account.id, account.label, account.balance, account.coming]
account.id, account.label, account.balance, account.coming)) rows.append(row)
except weboob.tools.browser.BrowserIncorrectPassword: except weboob.tools.browser.BrowserIncorrectPassword:
print >>sys.stderr, 'Error: Incorrect password for backend %s' % backend.name print >>sys.stderr, 'Error: Incorrect password for backend %s' % backend.name
return 1 return 1
if len(accounts): results[backend.name] = rows
print ' ID Account Balance Coming ' return results
print '+-----------------+---------------------+--------------+-------------+'
print '\n'.join(accounts)
else:
print 'No accounts found'
@ConsoleApplication.command('Display all future operations') @ConsoleApplication.command('Display all future operations')
def command_coming(self, id): def command_coming(self, id):
@ -78,6 +78,3 @@ class Boobank(ConsoleApplication):
print '\n'.join(operations) print '\n'.join(operations)
else: else:
print 'No coming operations for ID=%s' % id print 'No coming operations for ID=%s' % id
if __name__ == '__main__':
Boobank.run()

View file

@ -36,43 +36,44 @@ class Videoob(ConsoleApplication):
video = backend.get_video(_id) video = backend.get_video(_id)
if video is None: if video is None:
continue continue
result = [] rows = []
rows.append(('ID', video.id))
if video.title: if video.title:
result.append(('Video title', video.title)) rows.append(('Video title', video.title))
if video.duration: if video.duration:
result.append(('Duration', '%d:%02d:%02d' % ( rows.append(('Duration', '%d:%02d:%02d' % (
video.duration / 3600, (video.duration % 3600) / 60, video.duration % 60))) video.duration / 3600, (video.duration % 3600) / 60, video.duration % 60)))
if video.url: if video.url:
result.append(('URL', video.url)) rows.append(('URL', video.url))
if video.author: if video.author:
result.append(('Author', video.author)) rows.append(('Author', video.author))
if video.date: if video.date:
result.append(('Date', video.date)) rows.append(('Date', video.date))
if video.rating_max: if video.rating_max:
result.append(('Rating', '%s / %s' % (video.rating, video.rating_max))) rows.append(('Rating', '%s / %s' % (video.rating, video.rating_max)))
elif video.rating: elif video.rating:
result.append(('Rating', video.rating)) rows.append(('Rating', video.rating))
results[backend.name] = result results[backend.name] = rows
return results return results
@ConsoleApplication.command('Search videos') @ConsoleApplication.command('Search videos')
def command_search(self, pattern=None): def command_search(self, pattern=None):
results = {} results = {}
if pattern: if pattern:
results['HEADER'] = u'Search pattern: %s' % pattern results['BEFORE'] = u'Search pattern: %s' % pattern
else: else:
results['HEADER'] = u'Last videos' results['BEFORE'] = u'Last videos'
for backend in self.weboob.iter_backends(): for backend in self.weboob.iter_backends():
result = []
try: try:
iterator = backend.iter_search_results(pattern) iterator = backend.iter_search_results(pattern)
except NotImplementedError: except NotImplementedError:
continue continue
else: else:
rows = []
for video in iterator: for video in iterator:
result.append(('ID', video.id)) rows.append(('ID', video.id))
result.append(('Title', video.title)) rows.append(('Title', video.title))
results[backend.name] = result results[backend.name] = rows
return results return results
@ConsoleApplication.command('Get video file URL from page URL') @ConsoleApplication.command('Get video file URL from page URL')