Show More
@@ -344,6 +344,34 b' def _sizetomax(s):' | |||||
344 | except ValueError: |
|
344 | except ValueError: | |
345 | raise error.ParseError(_("couldn't parse size: %s") % s) |
|
345 | raise error.ParseError(_("couldn't parse size: %s") % s) | |
346 |
|
346 | |||
|
347 | def sizematcher(x): | |||
|
348 | """Return a function(size) -> bool from the ``size()`` expression""" | |||
|
349 | ||||
|
350 | # i18n: "size" is a keyword | |||
|
351 | expr = getstring(x, _("size requires an expression")).strip() | |||
|
352 | if '-' in expr: # do we have a range? | |||
|
353 | a, b = expr.split('-', 1) | |||
|
354 | a = util.sizetoint(a) | |||
|
355 | b = util.sizetoint(b) | |||
|
356 | return lambda x: x >= a and x <= b | |||
|
357 | elif expr.startswith("<="): | |||
|
358 | a = util.sizetoint(expr[2:]) | |||
|
359 | return lambda x: x <= a | |||
|
360 | elif expr.startswith("<"): | |||
|
361 | a = util.sizetoint(expr[1:]) | |||
|
362 | return lambda x: x < a | |||
|
363 | elif expr.startswith(">="): | |||
|
364 | a = util.sizetoint(expr[2:]) | |||
|
365 | return lambda x: x >= a | |||
|
366 | elif expr.startswith(">"): | |||
|
367 | a = util.sizetoint(expr[1:]) | |||
|
368 | return lambda x: x > a | |||
|
369 | elif expr[0].isdigit or expr[0] == '.': | |||
|
370 | a = util.sizetoint(expr) | |||
|
371 | b = _sizetomax(expr) | |||
|
372 | return lambda x: x >= a and x <= b | |||
|
373 | raise error.ParseError(_("couldn't parse size: %s") % expr) | |||
|
374 | ||||
347 | @predicate('size(expression)', callexisting=True) |
|
375 | @predicate('size(expression)', callexisting=True) | |
348 | def size(mctx, x): |
|
376 | def size(mctx, x): | |
349 | """File size matches the given expression. Examples: |
|
377 | """File size matches the given expression. Examples: | |
@@ -353,33 +381,7 b' def size(mctx, x):' | |||||
353 | - size('>= .5MB') - files at least 524288 bytes |
|
381 | - size('>= .5MB') - files at least 524288 bytes | |
354 | - size('4k - 1MB') - files from 4096 bytes to 1048576 bytes |
|
382 | - size('4k - 1MB') - files from 4096 bytes to 1048576 bytes | |
355 | """ |
|
383 | """ | |
356 |
|
384 | m = sizematcher(x) | ||
357 | # i18n: "size" is a keyword |
|
|||
358 | expr = getstring(x, _("size requires an expression")).strip() |
|
|||
359 | if '-' in expr: # do we have a range? |
|
|||
360 | a, b = expr.split('-', 1) |
|
|||
361 | a = util.sizetoint(a) |
|
|||
362 | b = util.sizetoint(b) |
|
|||
363 | m = lambda x: x >= a and x <= b |
|
|||
364 | elif expr.startswith("<="): |
|
|||
365 | a = util.sizetoint(expr[2:]) |
|
|||
366 | m = lambda x: x <= a |
|
|||
367 | elif expr.startswith("<"): |
|
|||
368 | a = util.sizetoint(expr[1:]) |
|
|||
369 | m = lambda x: x < a |
|
|||
370 | elif expr.startswith(">="): |
|
|||
371 | a = util.sizetoint(expr[2:]) |
|
|||
372 | m = lambda x: x >= a |
|
|||
373 | elif expr.startswith(">"): |
|
|||
374 | a = util.sizetoint(expr[1:]) |
|
|||
375 | m = lambda x: x > a |
|
|||
376 | elif expr[0].isdigit or expr[0] == '.': |
|
|||
377 | a = util.sizetoint(expr) |
|
|||
378 | b = _sizetomax(expr) |
|
|||
379 | m = lambda x: x >= a and x <= b |
|
|||
380 | else: |
|
|||
381 | raise error.ParseError(_("couldn't parse size: %s") % expr) |
|
|||
382 |
|
||||
383 | return [f for f in mctx.existing() if m(mctx.ctx[f].size())] |
|
385 | return [f for f in mctx.existing() if m(mctx.ctx[f].size())] | |
384 |
|
386 | |||
385 | @predicate('encoding(name)', callexisting=True) |
|
387 | @predicate('encoding(name)', callexisting=True) |
General Comments 0
You need to be logged in to leave comments.
Login now