implement mandatory fields for custom formatters

This commit is contained in:
Christophe Benz 2010-11-23 14:46:32 +01:00
commit 560798bf7d
6 changed files with 37 additions and 1 deletions

View file

@ -27,6 +27,8 @@ __all__ = ['Boobank']
class TransferFormatter(IFormatter):
MANDATORY_FIELDS = ('id', 'date', 'origin', 'recipient', 'amount')
def flush(self):
pass
@ -38,7 +40,10 @@ class TransferFormatter(IFormatter):
result += u'Amount: %.2f\n' % item['amount']
return result
class AccountListFormatter(IFormatter):
MANDATORY_FIELDS = ('id', 'label', 'balance', 'coming')
count = 0
tot_balance = 0.0
tot_coming = 0.0

View file

@ -29,6 +29,8 @@ __all__ = ['Radioob']
class RadioListFormatter(IFormatter):
MANDATORY_FIELDS = ('id', 'title', 'description')
count = 0
def flush(self):
@ -47,6 +49,7 @@ class RadioListFormatter(IFormatter):
result += ' (Current: %s - %s)' % (item['current'].artist, item['current'].title)
return result
class Radioob(ReplApplication):
APPNAME = 'radioob'
VERSION = '0.4'

View file

@ -29,6 +29,8 @@ __all__ = ['Videoob']
class VideoListFormatter(IFormatter):
MANDATORY_FIELDS = ('id', 'title', 'duration', 'date')
count = 0
def flush(self):

View file

@ -26,13 +26,17 @@ from weboob.tools.application.formatters.iformatter import IFormatter
__all__ = ['Weboorrents']
def sizeof_fmt(num):
for x in ['bytes','KB','MB','GB','TB']:
if num < 1024.0:
return "%-4.1f%s" % (num, x)
num /= 1024.0
class TorrentInfoFormatter(IFormatter):
MANDATORY_FIELDS = ('id', 'name', 'size', 'seeders', 'leechers', 'url', 'files', 'description')
def flush(self):
pass
@ -50,7 +54,10 @@ class TorrentInfoFormatter(IFormatter):
result += item['description']
return result
class TorrentListFormatter(IFormatter):
MANDATORY_FIELDS = ('id', 'name', 'size', 'seeders', 'leechers')
count = 0
def flush(self):
@ -68,6 +75,7 @@ class TorrentListFormatter(IFormatter):
result += ' %10s (Seed: %2d / Leech: %2d)' % (size, item['seeders'], item['leechers'])
return result
class Weboorrents(ReplApplication):
APPNAME = 'weboorrents'
VERSION = '0.4'

View file

@ -43,9 +43,18 @@ from weboob.capabilities.base import CapBaseObject, FieldNotFound
from weboob.tools.ordereddict import OrderedDict
__all__ = ['IFormatter']
__all__ = ['IFormatter', 'MandatoryFieldsNotFound']
class MandatoryFieldsNotFound(Exception):
def __init__(self, missing_fields):
Exception.__init__(self, u'Mandatory fields not found: %s.' % ','.join(missing_fields))
class IFormatter(object):
MANDATORY_FIELDS = None
def __init__(self, display_keys=True, display_header=True, return_only=False):
self.display_keys = display_keys
self.display_header = display_header
@ -99,6 +108,12 @@ class IFormatter(object):
if item is None:
return None
if self.MANDATORY_FIELDS:
missing_fields = set(self.MANDATORY_FIELDS) - set(item.keys())
if missing_fields:
raise MandatoryFieldsNotFound(missing_fields)
formatted = self.format_dict(item=item)
if formatted:
self.after_format(formatted)

View file

@ -30,6 +30,7 @@ from weboob.capabilities.base import FieldNotFound
from weboob.core import CallErrors
from weboob.core.modules import ModuleLoadError
from weboob.core.backendscfg import BackendsConfig, BackendAlreadyExists
from weboob.tools.application.formatters.iformatter import MandatoryFieldsNotFound
from weboob.tools.browser import BrowserUnavailable, BrowserIncorrectPassword
from weboob.tools.value import Value, ValueBool, ValueFloat, ValueInt
@ -1063,6 +1064,8 @@ class ReplApplication(Cmd, BaseApplication):
self.formatter.format(obj=result, selected_fields=fields)
except FieldNotFound, e:
print e
except MandatoryFieldsNotFound, e:
print '%s Hint: select missing fields or use another formatter (ex: multiline).' % e
def flush(self):
self.formatter.flush()