4 # |
4 # |
5 # This software may be used and distributed according to the terms of the |
5 # This software may be used and distributed according to the terms of the |
6 # GNU General Public License version 2 or any later version. |
6 # GNU General Public License version 2 or any later version. |
7 |
7 |
8 from i18n import _ |
8 from i18n import _ |
9 import errno, getpass, os, socket, sys, tempfile, traceback |
9 import errno, getpass, os, re, socket, sys, tempfile, traceback |
10 import config, scmutil, util, error, formatter |
10 import config, scmutil, util, error, formatter |
11 |
11 |
12 class ui(object): |
12 class ui(object): |
13 def __init__(self, src=None): |
13 def __init__(self, src=None): |
14 self._buffers = [] |
14 self._buffers = [] |
259 try: |
259 try: |
260 return int(v) |
260 return int(v) |
261 except ValueError: |
261 except ValueError: |
262 raise error.ConfigError(_("%s.%s is not an integer ('%s')") |
262 raise error.ConfigError(_("%s.%s is not an integer ('%s')") |
263 % (section, name, v)) |
263 % (section, name, v)) |
|
264 |
|
265 def configbytes(self, section, name, default=0, untrusted=False): |
|
266 """parse a configuration element as a quantity in bytes |
|
267 |
|
268 Units can be specified as b (bytes), k or kb (kilobytes), m or |
|
269 mb (megabytes), g or gb (gigabytes). |
|
270 |
|
271 >>> u = ui(); s = 'foo' |
|
272 >>> u.setconfig(s, 'val1', '42') |
|
273 >>> u.configbytes(s, 'val1') |
|
274 42 |
|
275 >>> u.setconfig(s, 'val2', '42.5 kb') |
|
276 >>> u.configbytes(s, 'val2') |
|
277 43520 |
|
278 >>> u.configbytes(s, 'unknown', '7 MB') |
|
279 7340032 |
|
280 >>> u.setconfig(s, 'invalid', 'somevalue') |
|
281 >>> u.configbytes(s, 'invalid') |
|
282 Traceback (most recent call last): |
|
283 ... |
|
284 ConfigError: foo.invalid is not a byte quantity ('somevalue') |
|
285 """ |
|
286 |
|
287 orig = string = self.config(section, name) |
|
288 if orig is None: |
|
289 if not isinstance(default, str): |
|
290 return default |
|
291 orig = string = default |
|
292 multiple = 1 |
|
293 m = re.match(r'([^kmbg]+?)\s*([kmg]?)b?$', string, re.I) |
|
294 if m: |
|
295 string, key = m.groups() |
|
296 key = key.lower() |
|
297 multiple = dict(k=1024, m=1048576, g=1073741824).get(key, 1) |
|
298 try: |
|
299 return int(float(string) * multiple) |
|
300 except ValueError: |
|
301 raise error.ConfigError(_("%s.%s is not a byte quantity ('%s')") |
|
302 % (section, name, orig)) |
264 |
303 |
265 def configlist(self, section, name, default=None, untrusted=False): |
304 def configlist(self, section, name, default=None, untrusted=False): |
266 """parse a configuration element as a list of comma/space separated |
305 """parse a configuration element as a list of comma/space separated |
267 strings |
306 strings |
268 |
307 |