Patchwork [2,of,3,v4] ui: add a configbytes method, for space configuration

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

Comments

Bryan O'Sullivan - April 19, 2013, 6:59 p.m.
# HG changeset patch
# User Bryan O'Sullivan <bryano@fb.com>
# Date 1366397592 25200
#      Fri Apr 19 11:53:12 2013 -0700
# Node ID 1a7d9ff79aad0ada89da06d1c9f33d8fb1046c91
# Parent  baa450479d62cd362f16c168373da260d91e76c9
ui: add a configbytes method, for space configuration

Patch

diff --git a/mercurial/ui.py b/mercurial/ui.py
--- a/mercurial/ui.py
+++ b/mercurial/ui.py
@@ -6,7 +6,7 @@ 
 # GNU General Public License version 2 or any later version.
 
 from i18n import _
-import errno, getpass, os, socket, sys, tempfile, traceback
+import errno, getpass, os, re, socket, sys, tempfile, traceback
 import config, scmutil, util, error, formatter
 
 class ui(object):
@@ -262,6 +262,39 @@  class ui(object):
             raise error.ConfigError(_("%s.%s is not an integer ('%s')")
                                     % (section, name, v))
 
+    def configbytes(self, section, name, default=0, untrusted=False):
+        """parse a configuration element as a quantity in bytes
+
+        Units can be specified as nothing (bytes), 'k' or 'kb' (kilobytes),
+        'm' or 'mb' (megabytes), 'g' or 'gb' (gigabytes).
+
+        >>> u = ui(); s = 'foo'
+        >>> u.setconfig(s, 'val1', '42')
+        >>> u.configbytes(s, 'val1')
+        42
+        >>> u.setconfig(s, 'val2', '42.5kb')
+        >>> u.configbytes(s, 'val2')
+        43520
+        >>> u.configbytes(s, 'unknown', '7MB')
+        7340032
+        >>> u.setconfig(s, 'invalid', 'somevalue')
+        >>> u.configbytes(s, 'invalid')
+        Traceback (most recent call last):
+            ...
+        ConfigError: foo.invalid is not a byte quantity ('somevalue')
+        """
+
+        value = self.config(section, name)
+        if value is None:
+            if not isinstance(default, str):
+                return default
+            value = default
+        try:
+            return util.sizetoint(value)
+        except error.ParseError:
+            raise error.ConfigError(_("%s.%s is not a byte quantity ('%s')")
+                                    % (section, name, value))
+
     def configlist(self, section, name, default=None, untrusted=False):
         """parse a configuration element as a list of comma/space separated
         strings