##// END OF EJS Templates
util: migrate fileset._sizetoint to util.sizetoint...
Bryan O'Sullivan -
r19194:1d08df65 default
parent child Browse files
Show More
@@ -263,23 +263,10 b' def grep(mctx, x):'
263 raise error.ParseError(_('invalid match pattern: %s') % e)
263 raise error.ParseError(_('invalid match pattern: %s') % e)
264 return [f for f in mctx.existing() if r.search(mctx.ctx[f].data())]
264 return [f for f in mctx.existing() if r.search(mctx.ctx[f].data())]
265
265
266 _units = dict(k=2**10, K=2**10, kB=2**10, KB=2**10,
267 M=2**20, MB=2**20, G=2**30, GB=2**30)
268
269 def _sizetoint(s):
270 try:
271 s = s.strip()
272 for k, v in _units.items():
273 if s.endswith(k):
274 return int(float(s[:-len(k)]) * v)
275 return int(s)
276 except ValueError:
277 raise error.ParseError(_("couldn't parse size: %s") % s)
278
279 def _sizetomax(s):
266 def _sizetomax(s):
280 try:
267 try:
281 s = s.strip()
268 s = s.strip()
282 for k, v in _units.items():
269 for k, v in util._sizeunits:
283 if s.endswith(k):
270 if s.endswith(k):
284 # max(4k) = 5k - 1, max(4.5k) = 4.6k - 1
271 # max(4k) = 5k - 1, max(4.5k) = 4.6k - 1
285 n = s[:-len(k)]
272 n = s[:-len(k)]
@@ -306,23 +293,23 b' def size(mctx, x):'
306 expr = getstring(x, _("size requires an expression")).strip()
293 expr = getstring(x, _("size requires an expression")).strip()
307 if '-' in expr: # do we have a range?
294 if '-' in expr: # do we have a range?
308 a, b = expr.split('-', 1)
295 a, b = expr.split('-', 1)
309 a = _sizetoint(a)
296 a = util.sizetoint(a)
310 b = _sizetoint(b)
297 b = util.sizetoint(b)
311 m = lambda x: x >= a and x <= b
298 m = lambda x: x >= a and x <= b
312 elif expr.startswith("<="):
299 elif expr.startswith("<="):
313 a = _sizetoint(expr[2:])
300 a = util.sizetoint(expr[2:])
314 m = lambda x: x <= a
301 m = lambda x: x <= a
315 elif expr.startswith("<"):
302 elif expr.startswith("<"):
316 a = _sizetoint(expr[1:])
303 a = util.sizetoint(expr[1:])
317 m = lambda x: x < a
304 m = lambda x: x < a
318 elif expr.startswith(">="):
305 elif expr.startswith(">="):
319 a = _sizetoint(expr[2:])
306 a = util.sizetoint(expr[2:])
320 m = lambda x: x >= a
307 m = lambda x: x >= a
321 elif expr.startswith(">"):
308 elif expr.startswith(">"):
322 a = _sizetoint(expr[1:])
309 a = util.sizetoint(expr[1:])
323 m = lambda x: x > a
310 m = lambda x: x > a
324 elif expr[0].isdigit or expr[0] == '.':
311 elif expr[0].isdigit or expr[0] == '.':
325 a = _sizetoint(expr)
312 a = util.sizetoint(expr)
326 b = _sizetomax(expr)
313 b = _sizetomax(expr)
327 m = lambda x: x >= a and x <= b
314 m = lambda x: x >= a and x <= b
328 else:
315 else:
@@ -1924,3 +1924,25 b' def timed(func):'
1924 (' ' * _timenesting[0], func.__name__,
1924 (' ' * _timenesting[0], func.__name__,
1925 timecount(elapsed)))
1925 timecount(elapsed)))
1926 return wrapper
1926 return wrapper
1927
1928 _sizeunits = (('m', 2**20), ('k', 2**10), ('g', 2**30),
1929 ('kb', 2**10), ('mb', 2**20), ('gb', 2**30), ('b', 1))
1930
1931 def sizetoint(s):
1932 '''Convert a space specifier to a byte count.
1933
1934 >>> sizetoint('30')
1935 30
1936 >>> sizetoint('2.2kb')
1937 2252
1938 >>> sizetoint('6M')
1939 6291456
1940 '''
1941 t = s.strip().lower()
1942 try:
1943 for k, u in _sizeunits:
1944 if t.endswith(k):
1945 return int(float(t[:-len(k)]) * u)
1946 return int(t)
1947 except ValueError:
1948 raise error.ParseError(_("couldn't parse size: %s") % s)
General Comments 0
You need to be logged in to leave comments. Login now