fix: use ResultsCondition in BackendsCall instead of hack in Weboob (refs #372)

This commit is contained in:
Romain Bignon 2010-11-14 21:46:48 +01:00
commit c644aacd29
4 changed files with 38 additions and 25 deletions

View file

@ -34,7 +34,7 @@ from weboob.tools.value import Value, ValueBool, ValueFloat, ValueInt
from .base import BackendNotFound, BaseApplication
from .formatters.load import FormattersLoader, FormatterLoadError
from .results import ResultsCondition, ResultsConditionException
from .results import ResultsCondition, ResultsConditionError
__all__ = ['ReplApplication', 'NotEnoughArguments']
@ -375,6 +375,8 @@ class ReplApplication(Cmd, BaseApplication):
if not msg:
msg = 'website is unavailable.'
print >>sys.stderr, u'Error(%s): %s' % (backend.name, msg)
elif isinstance(error, ResultsConditionError):
print >>sys.stderr, u'Error(%s): condition error: %s' % (backend.name, error)
elif isinstance(error, NotImplementedError):
print >>sys.stderr, u'Error(%s): this feature is not supported yet by this backend.' % backend.name
print >>sys.stderr, u' %s To help the maintainer of this backend implement this feature,' % (' ' * len(backend.name))
@ -787,8 +789,8 @@ class ReplApplication(Cmd, BaseApplication):
else:
try:
self.condition = ResultsCondition(line)
except ResultsConditionException, e:
print e
except ResultsConditionError, e:
print >>sys.stderr, '%s' % e
else:
if self.condition is None:
print 'No condition is set.'

View file

@ -16,10 +16,13 @@
# Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
__all__ = ['ResultsCondition', 'ResultsConditionException']
from weboob.core.bcall import IResultsCondition, ResultsConditionError
class ResultsCondition(object):
__all__ = ['ResultsCondition', 'ResultsConditionError']
class ResultsCondition(IResultsCondition):
condition_str = None
def __init__(self, condition_str):
@ -36,13 +39,14 @@ class ResultsCondition(object):
elif '=' in _and:
k, v = _and.split('=')
else:
raise ResultsConditionException(u'Could not find = or != operator in sub-expression "%s"' % _and)
raise ResultsConditionError(u'Could not find = or != operator in sub-expression "%s"' % _and)
and_dict[k.strip()] = v.strip()
or_list.append(and_dict)
self.condition = or_list
self.condition_str = condition_str
def is_valid(self, d):
def is_valid(self, obj):
d = dict(obj.iter_fields())
for _or in self.condition:
for k, v in _or.iteritems():
if k.endswith('!'):
@ -58,7 +62,7 @@ class ResultsCondition(object):
if d[k] != v:
return False
else:
raise ResultsConditionException(u'Field "%s" is not valid.' % k)
raise ResultsConditionError(u'Field "%s" is not valid.' % k)
return True
def __str__(self):
@ -66,7 +70,3 @@ class ResultsCondition(object):
def __unicode__(self):
return self.condition_str
class ResultsConditionException(Exception):
pass