From d73c6b2245ec471ffe2cd65538d15632a5a3ba31 Mon Sep 17 00:00:00 2001 From: Bezleputh Date: Wed, 8 Oct 2014 11:55:41 +0200 Subject: [PATCH] Add a " LIMIT " keyword in conditions --- tools/make_man.py | 13 ++++++++++--- weboob/tools/application/base.py | 5 +++++ weboob/tools/application/results.py | 8 ++++++++ 3 files changed, 23 insertions(+), 3 deletions(-) diff --git a/tools/make_man.py b/tools/make_man.py index f8ca7388..603c8aa7 100755 --- a/tools/make_man.py +++ b/tools/make_man.py @@ -166,7 +166,7 @@ def analyze_application(app, script_name): script_name, app.VERSION.replace('.', '\\&.')) name = ".SH NAME\n%s \- %s" % (script_name, application.SHORT_DESCRIPTION) condition = """.SH CONDITION -The \-c and \-\-condition is a flexible way to sort and get only interesting results. It supports conditions on numerical values, dates, and strings. Dates are given in YYYY\-MM\-DD format. +The \-c and \-\-condition is a flexible way to sort and get only interesting results. It supports conditions on numerical values, dates, and strings. Dates are given in YYYY\-MM\-DD or YYYY\-MM\-DD HH:MM format. The syntax of one expression is "\\fBfield operator value\\fR". The field to test is always the left member of the expression. .LP The field is a member of the objects returned by the command. For example, a bank account has "balance", "coming" or "label" fields. @@ -187,8 +187,10 @@ Test if object.field is less than the value. If object.field is date, return tru | This operator is available only for string fields. It works like the Unix standard \\fBgrep\\fR command, and returns True if the pattern specified in the value is in object.field. .SS Expression combination -You can make a expression combinations with the keywords \\fB" AND "\\fR and \\fB" OR "\\fR. - +.LP +You can make a expression combinations with the keywords \\fB" AND "\\fR, \\fB" OR "\\fR an \\fB" LIMIT "\\fR. +.LP +The \\fBLIMIT\\fR keyword can be used to limit the number of items upon which running the expression. \\fBLIMIT\\fR can only be placed at the end of the expression followed by the number of elements you want. .SS Examples: .nf .B boobank ls \-\-condition 'label=Livret A' @@ -209,6 +211,11 @@ Get transactions containing "rewe". .B boobank history account@backend \-\-condition 'date>2013\-12\-01 AND date<2013\-12\-09' .fi Get transactions betweens the 2th December and 8th December 2013. +.PP +.nf +.B boobank history account@backend \-\-condition 'date>2013\-12\-01 LIMIT 10' +.fi +Get transactions after the 2th December in the last 10 transactions """ footer = """.SH COPYRIGHT %s diff --git a/weboob/tools/application/base.py b/weboob/tools/application/base.py index 21683c65..c22af8e4 100644 --- a/weboob/tools/application/base.py +++ b/weboob/tools/application/base.py @@ -278,8 +278,13 @@ class Application(object): def _do_complete_iter(self, backend, count, fields, res): modif = 0 + for i, sub in enumerate(res): sub = self._do_complete_obj(backend, fields, sub) + if self.condition and self.condition.limit and \ + self.condition.limit == i: + return + if self.condition and not self.condition.is_valid(sub): modif += 1 else: diff --git a/weboob/tools/application/results.py b/weboob/tools/application/results.py index 2472cb45..0f94e3a6 100644 --- a/weboob/tools/application/results.py +++ b/weboob/tools/application/results.py @@ -69,7 +69,15 @@ class ResultsCondition(IResultsCondition): # =, != for strings # We build a list of list. Return true if each conditions of one list is TRUE def __init__(self, condition_str): + self.limit = None or_list = [] + _condition_str = condition_str.split(' LIMIT ') + if len(_condition_str) == 2: + try: + self.limit = int(_condition_str[1]) + except ValueError: + raise ResultsConditionError(u'Syntax error in the condition expression, please check documentation') + condition_str= _condition_str[0] for _or in condition_str.split(' OR '): and_list = [] for _and in _or.split(' AND '):