fix #372 conditions should not be treated by formatters

This commit is contained in:
Christophe Benz 2010-11-14 15:04:02 +01:00
commit 5fc8c14b2d
3 changed files with 16 additions and 14 deletions

View file

@ -179,6 +179,7 @@ class Weboob(object):
@param function backend's method name, or callable object
@param backends list of backends to iterate on
@param caps iterate on backends with this caps
@param condition a condition to validate to keep the result
@return an iterator of results
"""
backends = self.backend_instances.values()
@ -204,8 +205,14 @@ class Weboob(object):
if 'caps' in kwargs:
caps = kwargs.pop('caps')
backends = [backend for backend in backends if backend.has_caps(caps)]
return BackendsCall(backends, function, *args, **kwargs)
condition = kwargs.pop('condition', None)
for backend, result in BackendsCall(backends, function, *args, **kwargs):
d = dict(result.iter_fields())
if condition:
if condition.is_valid(d):
yield backend, result
else:
yield backend, result
def schedule(self, interval, function, *args):
return self.scheduler.schedule(interval, function, *args)

View file

@ -77,17 +77,15 @@ class IFormatter(object):
def flush(self):
raise NotImplementedError()
def format(self, obj, selected_fields=None, condition=None):
def format(self, obj, selected_fields=None):
"""
Format an object to be human-readable.
An object has fields which can be selected, and the objects
can be filtered using a condition (like SELECT and WHERE in SQL).
An object has fields which can be selected.
If the object provides an iter_fields() method, the formatter will
call it. It can be used to specify the fields order.
@param obj [object] object to format
@param selected_fields [tuple] fields to display. If None, all fields are selected
@param condition [Condition] condition to objects to display
@return a string of the formatted object
"""
assert isinstance(obj, (dict, CapBaseObject, tuple))
@ -97,7 +95,7 @@ class IFormatter(object):
elif isinstance(obj, tuple):
item = OrderedDict([(k, v) for k, v in obj])
else:
item = self.to_dict(obj, condition, selected_fields)
item = self.to_dict(obj, selected_fields)
if item is None:
return None
@ -122,7 +120,7 @@ class IFormatter(object):
if self.display_header:
print string.encode('utf-8')
def to_dict(self, obj, condition=None, selected_fields=None):
def to_dict(self, obj, selected_fields=None):
def iter_select(d):
if selected_fields is None or '*' in selected_fields:
fields = d.iterkeys()
@ -145,6 +143,4 @@ class IFormatter(object):
fields_iterator = obj.iter_fields()
d = OrderedDict(iter_decorate(fields_iterator))
if condition is not None and not condition.is_valid(d):
return None
return OrderedDict([(k, v) for k, v in iter_select(d)])
return OrderedDict((k, v) for k, v in iter_select(d))

View file

@ -450,6 +450,7 @@ class ReplApplication(Cmd, BaseApplication):
"""
backends = kwargs.pop('backends', None)
kwargs['backends'] = self.enabled_backends if backends is None else backends
kwargs['condition'] = self.condition
fields = self.selected_fields
if fields == '$direct':
fields = []
@ -1019,11 +1020,9 @@ class ReplApplication(Cmd, BaseApplication):
if fields in ('$direct', '$full'):
fields = None
try:
self.formatter.format(obj=result, selected_fields=fields, condition=self.condition)
self.formatter.format(obj=result, selected_fields=fields)
except FieldNotFound, e:
print e
except ResultsConditionException, e:
print e
def flush(self):
self.formatter.flush()