implement mandatory fields for custom formatters
This commit is contained in:
parent
bebd4318ec
commit
560798bf7d
6 changed files with 37 additions and 1 deletions
|
|
@ -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)
|
||||
|
|
|
|||
|
|
@ -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()
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue