summaryrefslogtreecommitdiff
path: root/python/skytools/parsing.py
diff options
context:
space:
mode:
authormartinko2012-02-17 09:43:22 +0000
committermartinko2012-02-17 09:43:22 +0000
commitcc6358bca8445898441af6883fa0357a3a695cc5 (patch)
treee9847537ec3c36ebfd299ac34b4438d917a6236c /python/skytools/parsing.py
parentb013f553319e0e2db7db2a5de0080ad23d2101cf (diff)
added support for sizes specified in human readable format
Config options that hold size in bytes can now be specified in human readable format. Examples: 1, 2 B, 3K, 4 MB
Diffstat (limited to 'python/skytools/parsing.py')
-rw-r--r--python/skytools/parsing.py17
1 files changed, 14 insertions, 3 deletions
diff --git a/python/skytools/parsing.py b/python/skytools/parsing.py
index ec40550e..c7061ed4 100644
--- a/python/skytools/parsing.py
+++ b/python/skytools/parsing.py
@@ -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()
-