Show More
@@ -6,7 +6,7 b'' | |||
|
6 | 6 | # GNU General Public License version 2 or any later version. |
|
7 | 7 | |
|
8 | 8 | from i18n import _ |
|
9 | import errno, getpass, os, socket, sys, tempfile, traceback | |
|
9 | import errno, getpass, os, re, socket, sys, tempfile, traceback | |
|
10 | 10 | import config, scmutil, util, error, formatter |
|
11 | 11 | |
|
12 | 12 | class ui(object): |
@@ -262,6 +262,45 b' class ui(object):' | |||
|
262 | 262 | raise error.ConfigError(_("%s.%s is not an integer ('%s')") |
|
263 | 263 | % (section, name, v)) |
|
264 | 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)) | |
|
303 | ||
|
265 | 304 | def configlist(self, section, name, default=None, untrusted=False): |
|
266 | 305 | """parse a configuration element as a list of comma/space separated |
|
267 | 306 | strings |
General Comments 0
You need to be logged in to leave comments.
Login now