Add a " LIMIT " keyword in conditions

This commit is contained in:
Bezleputh 2014-10-08 11:55:41 +02:00
commit d73c6b2245
3 changed files with 23 additions and 3 deletions

View file

@ -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

View file

@ -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:

View file

@ -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 '):