Patchwork [1,of,3,v4] util: migrate fileset._sizetoint, make public, add docstring

login
register
mail settings
Submitter Bryan O'Sullivan
Date April 19, 2013, 6:59 p.m.
Message ID <baa450479d62cd362f16.1366397965@australite.local>
Download mbox | patch
Permalink /patch/1457/
State Deferred
Headers show

Comments

Bryan O'Sullivan - April 19, 2013, 6:59 p.m.
# HG changeset patch
# User Bryan O'Sullivan <bryano@fb.com>
# Date 1366397579 25200
#      Fri Apr 19 11:52:59 2013 -0700
# Node ID baa450479d62cd362f16c168373da260d91e76c9
# Parent  63a783d1ac852d60e60669b2f8551a9b26de2947
util: migrate fileset._sizetoint, make public, add docstring

This function will be used in another module in an upcoming patch.

Patch

diff --git a/mercurial/fileset.py b/mercurial/fileset.py
--- a/mercurial/fileset.py
+++ b/mercurial/fileset.py
@@ -263,19 +263,6 @@  def grep(mctx, x):
         raise error.ParseError(_('invalid match pattern: %s') % e)
     return [f for f in mctx.existing() if r.search(mctx.ctx[f].data())]
 
-_units = dict(k=2**10, K=2**10, kB=2**10, KB=2**10,
-              M=2**20, MB=2**20, G=2**30, GB=2**30)
-
-def _sizetoint(s):
-    try:
-        s = s.strip()
-        for k, v in _units.items():
-            if s.endswith(k):
-                return int(float(s[:-len(k)]) * v)
-        return int(s)
-    except ValueError:
-        raise error.ParseError(_("couldn't parse size: %s") % s)
-
 def _sizetomax(s):
     try:
         s = s.strip()
@@ -306,23 +293,23 @@  def size(mctx, x):
     expr = getstring(x, _("size requires an expression")).strip()
     if '-' in expr: # do we have a range?
         a, b = expr.split('-', 1)
-        a = _sizetoint(a)
-        b = _sizetoint(b)
+        a = util.sizetoint(a)
+        b = util.sizetoint(b)
         m = lambda x: x >= a and x <= b
     elif expr.startswith("<="):
-        a = _sizetoint(expr[2:])
+        a = util.sizetoint(expr[2:])
         m = lambda x: x <= a
     elif expr.startswith("<"):
-        a = _sizetoint(expr[1:])
+        a = util.sizetoint(expr[1:])
         m = lambda x: x < a
     elif expr.startswith(">="):
-        a = _sizetoint(expr[2:])
+        a = util.sizetoint(expr[2:])
         m = lambda x: x >= a
     elif expr.startswith(">"):
-        a = _sizetoint(expr[1:])
+        a = util.sizetoint(expr[1:])
         m = lambda x: x > a
     elif expr[0].isdigit or expr[0] == '.':
-        a = _sizetoint(expr)
+        a = util.sizetoint(expr)
         b = _sizetomax(expr)
         m = lambda x: x >= a and x <= b
     else:
diff --git a/mercurial/util.py b/mercurial/util.py
--- a/mercurial/util.py
+++ b/mercurial/util.py
@@ -1924,3 +1924,24 @@  def timed(func):
                              (' ' * _timenesting[0], func.__name__,
                               timecount(elapsed)))
     return wrapper
+
+_sizeunits = dict(k=2**10, kb=2**10, m=2**20, mb=2**20, g=2**30, gb=2**30)
+
+def sizetoint(s):
+    '''Convert a space specifier to a byte count.
+
+    >>> sizetoint('30')
+    30
+    >>> sizetoint('2.2kb')
+    2252
+    >>> sizetoint('6M')
+    6291456
+    '''
+    s = s.strip().lower()
+    try:
+        for k in _sizeunits:
+            if s.endswith(k):
+                return int(float(s[:-len(k)]) * _sizeunits[k])
+        return int(s)
+    except ValueError:
+        raise error.ParseError(_("couldn't parse size: %s") % s)