VirtualMailManager/common.py
branchv0.6.x
changeset 383 ac5ac03b58da
parent 366 d6573da35b5f
child 393 fb2ba1456bc5
equal deleted inserted replaced
382:5e6bcb2e010e 383:ac5ac03b58da
    65         raise VMMError(_(u"File is not executable: '%s'") %
    65         raise VMMError(_(u"File is not executable: '%s'") %
    66                        get_unicode(binary), NOT_EXECUTABLE)
    66                        get_unicode(binary), NOT_EXECUTABLE)
    67     return binary
    67     return binary
    68 
    68 
    69 
    69 
       
    70 def size_in_bytes(size):
       
    71     """Converts the string `size` to a long (size in bytes).
       
    72 
       
    73     The string `size` can be suffixed with *b* (bytes), *k* (kilobytes),
       
    74     *M* (megabytes) or *G* (gigabytes).
       
    75     """
       
    76     if not isinstance(size, basestring) or not size:
       
    77         raise TypeError('size must be a non empty string.')
       
    78     if size[-1].upper() in ('B', 'K', 'M', 'G'):
       
    79         try:
       
    80             num = int(size[:-1])
       
    81         except ValueError:
       
    82             raise ValueError('Not a valid integer value: %r' % size[:-1])
       
    83         unit = size[-1].upper()
       
    84         if unit == 'B':
       
    85             return num
       
    86         elif unit == 'K':
       
    87             return num << 10L
       
    88         elif unit == 'M':
       
    89             return num << 20L
       
    90         else:
       
    91             return num << 30L
       
    92     else:
       
    93         try:
       
    94             num = int(size)
       
    95         except ValueError:
       
    96             raise ValueError('Not a valid size value: %r' % size)
       
    97         return num
       
    98 
       
    99 
    70 def version_hex(version_string):
   100 def version_hex(version_string):
    71     """Converts a Dovecot version, e.g.: '1.2.3' or '2.0.beta4', to an int.
   101     """Converts a Dovecot version, e.g.: '1.2.3' or '2.0.beta4', to an int.
    72     Raises a `ValueError` if the *version_string* has the wrong™ format.
   102     Raises a `ValueError` if the *version_string* has the wrong™ format.
    73 
   103 
    74     version_hex('1.2.3') -> 270548736
   104     version_hex('1.2.3') -> 270548736