fileset: split the logic for matching a size expression to a separate method
authorMatt Harbison <matt_harbison@yahoo.com>
Wed, 10 Jan 2018 22:35:08 -0500
changeset 35615 0e369eca888f
parent 35614 6d6d20658cce
child 35616 706aa203b396
fileset: split the logic for matching a size expression to a separate method This will be used in the next patch to build a simple filtering language, but where we won't have an mctx.
mercurial/fileset.py
--- a/mercurial/fileset.py	Fri Jan 12 23:13:38 2018 -0500
+++ b/mercurial/fileset.py	Wed Jan 10 22:35:08 2018 -0500
@@ -344,6 +344,34 @@
     except ValueError:
         raise error.ParseError(_("couldn't parse size: %s") % s)
 
+def sizematcher(x):
+    """Return a function(size) -> bool from the ``size()`` expression"""
+
+    # i18n: "size" is a keyword
+    expr = getstring(x, _("size requires an expression")).strip()
+    if '-' in expr: # do we have a range?
+        a, b = expr.split('-', 1)
+        a = util.sizetoint(a)
+        b = util.sizetoint(b)
+        return lambda x: x >= a and x <= b
+    elif expr.startswith("<="):
+        a = util.sizetoint(expr[2:])
+        return lambda x: x <= a
+    elif expr.startswith("<"):
+        a = util.sizetoint(expr[1:])
+        return lambda x: x < a
+    elif expr.startswith(">="):
+        a = util.sizetoint(expr[2:])
+        return lambda x: x >= a
+    elif expr.startswith(">"):
+        a = util.sizetoint(expr[1:])
+        return lambda x: x > a
+    elif expr[0].isdigit or expr[0] == '.':
+        a = util.sizetoint(expr)
+        b = _sizetomax(expr)
+        return lambda x: x >= a and x <= b
+    raise error.ParseError(_("couldn't parse size: %s") % expr)
+
 @predicate('size(expression)', callexisting=True)
 def size(mctx, x):
     """File size matches the given expression. Examples:
@@ -353,33 +381,7 @@
     - size('>= .5MB') - files at least 524288 bytes
     - size('4k - 1MB') - files from 4096 bytes to 1048576 bytes
     """
-
-    # i18n: "size" is a keyword
-    expr = getstring(x, _("size requires an expression")).strip()
-    if '-' in expr: # do we have a range?
-        a, b = expr.split('-', 1)
-        a = util.sizetoint(a)
-        b = util.sizetoint(b)
-        m = lambda x: x >= a and x <= b
-    elif expr.startswith("<="):
-        a = util.sizetoint(expr[2:])
-        m = lambda x: x <= a
-    elif expr.startswith("<"):
-        a = util.sizetoint(expr[1:])
-        m = lambda x: x < a
-    elif expr.startswith(">="):
-        a = util.sizetoint(expr[2:])
-        m = lambda x: x >= a
-    elif expr.startswith(">"):
-        a = util.sizetoint(expr[1:])
-        m = lambda x: x > a
-    elif expr[0].isdigit or expr[0] == '.':
-        a = util.sizetoint(expr)
-        b = _sizetomax(expr)
-        m = lambda x: x >= a and x <= b
-    else:
-        raise error.ParseError(_("couldn't parse size: %s") % expr)
-
+    m = sizematcher(x)
     return [f for f in mctx.existing() if m(mctx.ctx[f].size())]
 
 @predicate('encoding(name)', callexisting=True)