# HG changeset patch
# User Pascal Volk <neverseen@users.sourceforge.net>
# Date 1296863134 0
# Node ID ac5ac03b58da1898f4d8ce20f36cf2d3558ce0bc
# Parent  5e6bcb2e010e682070c6e06d68d022987ce138b0
VMM/common: Added function size_in_bytes().

diff -r 5e6bcb2e010e -r ac5ac03b58da VirtualMailManager/common.py
--- a/VirtualMailManager/common.py	Fri Feb 04 17:29:35 2011 +0000
+++ b/VirtualMailManager/common.py	Fri Feb 04 23:45:34 2011 +0000
@@ -67,6 +67,36 @@
     return binary
 
 
+def size_in_bytes(size):
+    """Converts the string `size` to a long (size in bytes).
+
+    The string `size` can be suffixed with *b* (bytes), *k* (kilobytes),
+    *M* (megabytes) or *G* (gigabytes).
+    """
+    if not isinstance(size, basestring) or not size:
+        raise TypeError('size must be a non empty string.')
+    if size[-1].upper() in ('B', 'K', 'M', 'G'):
+        try:
+            num = int(size[:-1])
+        except ValueError:
+            raise ValueError('Not a valid integer value: %r' % size[:-1])
+        unit = size[-1].upper()
+        if unit == 'B':
+            return num
+        elif unit == 'K':
+            return num << 10L
+        elif unit == 'M':
+            return num << 20L
+        else:
+            return num << 30L
+    else:
+        try:
+            num = int(size)
+        except ValueError:
+            raise ValueError('Not a valid size value: %r' % size)
+        return num
+
+
 def version_hex(version_string):
     """Converts a Dovecot version, e.g.: '1.2.3' or '2.0.beta4', to an int.
     Raises a `ValueError` if the *version_string* has the wrong™ format.