added support for sizes specified in human readable format
authormartinko <gamato@users.sf.net>
Fri, 17 Feb 2012 09:43:22 +0000 (10:43 +0100)
committermartinko <gamato@users.sf.net>
Fri, 17 Feb 2012 09:43:22 +0000 (10:43 +0100)
Config options that hold size in bytes can now be specified in human readable format.
Examples: 1, 2 B, 3K, 4 MB

python/skytools/__init__.py
python/skytools/config.py
python/skytools/parsing.py

index 51d4e26cf9fd57343054f685553f062dad40f04c..6bdeb51e1c87679f705634bc14cad157c5253aff 100644 (file)
@@ -33,6 +33,7 @@ _symbols = {
     'gzip_append': 'skytools.gzlog:gzip_append',
     # skytools.parsing
     'dedent': 'skytools.parsing:dedent',
+    'hsize_to_bytes': 'skytools.parsing:hsize_to_bytes',
     'parse_acl': 'skytools.parsing:parse_acl',
     'parse_logtriga_sql': 'skytools.parsing:parse_logtriga_sql',
     'parse_pgarray': 'skytools.parsing:parse_pgarray',
index 9f19306f06185d22abe95dc9579aa10a23d1686f..3e58139bfc2ab288c14225b3d49c81b501a8883a 100644 (file)
@@ -3,6 +3,8 @@
 
 import os, os.path, ConfigParser, socket
 
+import skytools
+
 __all__ = ['Config']
 
 class Config(object):
@@ -161,6 +163,19 @@ class Config(object):
 
         return fn
 
+    def getbytes(self, key, default=None):
+        """Reads a size value in human format, if not set then default.
+
+        Examples: 1, 2 B, 3K, 4 MB
+        """
+        try:
+            s = self.cf.get(self.main_section, key)
+        except ConfigParser.NoOptionError:
+            if default is None:
+                raise Exception("Config value not set: " + key)
+            s = default
+        return skytools.hsize_to_bytes(s)
+
     def get_wildcard(self, key, values=[], default=None):
         """Reads a wildcard property from conf and returns its string value, if not set then default."""
 
index ec40550ed57a28cea354e7949d5fa606adb575b9..c7061ed42f54921d24f23b17626fd502bdc980ef 100644 (file)
@@ -7,7 +7,7 @@ import skytools
 __all__ = [
     "parse_pgarray", "parse_logtriga_sql", "parse_tabbed_table",
     "parse_statements", 'sql_tokenizer', 'parse_sqltriga_sql',
-    "parse_acl", "dedent"]
+    "parse_acl", "dedent", "hsize_to_bytes"]
 
 _rc_listelem = re.compile(r'( [^,"}]+ | ["] ( [^"\\]+ | [\\]. )* ["] )', re.X)
 
@@ -219,7 +219,7 @@ def parse_sqltriga_sql(op, sql, pklist=None, splitkeys=False):
 
 def parse_tabbed_table(txt):
     r"""Parse a tab-separated table into list of dicts.
-    
+
     Expect first row to be column names.
 
     Very primitive.
@@ -434,7 +434,18 @@ def dedent(doc):
     res.append('')
     return '\n'.join(res)
 
+
+def hsize_to_bytes (input):
+    """ Convert sizes from human format to bytes (string to integer) """
+
+    assert isinstance (input, str)
+    m = re.match (r"^([0-9]+) *([KMGTPEZY]?)B?$", input.strip(), re.IGNORECASE)
+    if not m: raise ValueError ("cannot parse: %s" % input)
+    units = ['', 'K', 'M', 'G', 'T', 'P', 'E', 'Z', 'Y']
+    bytes = int(m.group(1)) * 1024 ** units.index(m.group(2).upper())
+    return bytes
+
+
 if __name__ == '__main__':
     import doctest
     doctest.testmod()
-