From 572d5c1061c46d39f4c3ca5ab26ba8fe181c4d97 Mon Sep 17 00:00:00 2001 From: Romain Bignon Date: Mon, 1 Nov 2010 10:37:07 +0100 Subject: [PATCH] fix things related to conditions --- weboob/tools/application/formatters/iformatter.py | 13 +++++++++---- weboob/tools/application/results.py | 12 ++++++------ 2 files changed, 15 insertions(+), 10 deletions(-) diff --git a/weboob/tools/application/formatters/iformatter.py b/weboob/tools/application/formatters/iformatter.py index cb2da42b..59fbd6ee 100644 --- a/weboob/tools/application/formatters/iformatter.py +++ b/weboob/tools/application/formatters/iformatter.py @@ -107,7 +107,8 @@ class IFormatter(object): def format_dict(self, item): """ - Format an dict to be human-readable. The dict is already simplified if user provides selected fields. + Format a dict to be human-readable. The dict is already simplified + if user provides selected fields. Called by format(). This method has to be overridden in child classes. @@ -121,7 +122,7 @@ class IFormatter(object): print string.encode('utf-8') def to_dict(self, obj, condition=None, selected_fields=None): - def iter_select_and_decorate(d): + def iter_select(d): if selected_fields is None or '*' in selected_fields: fields = d.iterkeys() else: @@ -133,12 +134,16 @@ class IFormatter(object): except KeyError: raise FieldNotFound(obj, key) + yield key, value + + def iter_decorate(d): + for key, value in d: if key == 'id' and obj.backend is not None: value = self.build_id(value, obj.backend) yield key, value fields_iterator = obj.iter_fields() - d = OrderedDict(fields_iterator) + 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_and_decorate(d)]) + return OrderedDict([(k, v) for k, v in iter_select(d)]) diff --git a/weboob/tools/application/results.py b/weboob/tools/application/results.py index d8d66825..0fcb4c59 100644 --- a/weboob/tools/application/results.py +++ b/weboob/tools/application/results.py @@ -71,16 +71,16 @@ class ResultsCondition(object): condition_str = None def __init__(self, condition_str): - condition_str = condition_str.replace('OR', 'or') \ - .replace('AND', 'and') \ - .replace('NOT', 'not') + condition_str = condition_str.replace(' OR ', ' or ') \ + .replace(' AND ', ' and ') \ + .replace(' NOT ', ' not ') or_list = [] - for _or in condition_str.split('or'): + for _or in condition_str.split(' or '): and_dict = {} - for _and in _or.split('and'): + for _and in _or.split(' and '): if '!=' in _and: k, v = _and.split('!=') - k += '!' + k = k.strip() + '!' elif '=' in _and: k, v = _and.split('=') else: