VirtualMailManager/common.py
branchv0.7.x
changeset 670 f374ef062c94
parent 653 cf07e4468934
child 676 2bc11dada296
equal deleted inserted replaced
669:d765a878f0fc 670:f374ef062c94
    72     """Converts the `size` in bytes in human readable format."""
    72     """Converts the `size` in bytes in human readable format."""
    73     if not isinstance(size, int):
    73     if not isinstance(size, int):
    74         try:
    74         try:
    75             size = int(size)
    75             size = int(size)
    76         except ValueError:
    76         except ValueError:
    77             raise TypeError("'size' must be a positive long or int.")
    77             raise TypeError("'size' must be a positive integer.")
    78     if size < 0:
    78     if size < 0:
    79         raise ValueError("'size' must be a positive long or int.")
    79         raise ValueError("'size' must be a positive integer.")
    80     if size < 1024:
    80     if size < 1024:
    81         return str(size)
    81         return str(size)
    82     # TP: abbreviations of gibibyte, tebibyte kibibyte and mebibyte
    82     # TP: abbreviations of gibibyte, tebibyte kibibyte and mebibyte
    83     prefix_multiply = ((_('TiB'), 1 << 40), (_('GiB'), 1 << 30),
    83     prefix_multiply = ((_('TiB'), 1 << 40), (_('GiB'), 1 << 30),
    84                        (_('MiB'), 1 << 20), (_('KiB'), 1 << 10))
    84                        (_('MiB'), 1 << 20), (_('KiB'), 1 << 10))
    90                                           True),
    90                                           True),
    91                     'prefix': prefix}
    91                     'prefix': prefix}
    92 
    92 
    93 
    93 
    94 def size_in_bytes(size):
    94 def size_in_bytes(size):
    95     """Converts the string `size` to a long (size in bytes).
    95     """Converts the string `size` to an integer (size in bytes).
    96 
    96 
    97     The string `size` can be suffixed with *b* (bytes), *k* (kilobytes),
    97     The string `size` can be suffixed with *b* (bytes), *k* (kilobytes),
    98     *M* (megabytes) or *G* (gigabytes).
    98     *M* (megabytes) or *G* (gigabytes).
    99     """
    99     """
   100     if not isinstance(size, str) or not size:
   100     if not isinstance(size, str) or not size:
   181 
   181 
   182 
   182 
   183 def version_str(version):
   183 def version_str(version):
   184     """Converts a Dovecot version previously converted with version_hex back to
   184     """Converts a Dovecot version previously converted with version_hex back to
   185     a string.
   185     a string.
   186     Raises a `TypeError` if *version* is not an int/long.
   186     Raises a `TypeError` if *version* is not an integer.
   187     Raises a `ValueError` if *version* is an incorrect int version.
   187     Raises a `ValueError` if *version* is an incorrect int version.
   188     """
   188     """
   189     global _version_cache
   189     global _version_cache
   190     if version in _version_cache:
   190     if version in _version_cache:
   191         return _version_cache[version]
   191         return _version_cache[version]
   192     if not isinstance(version, int):
   192     if not isinstance(version, int):
   193         raise TypeError('Argument is not a int/long: %r', version)
   193         raise TypeError('Argument is not a integer: %r', version)
   194     major = (version >> 28) & 0xFF
   194     major = (version >> 28) & 0xFF
   195     minor = (version >> 20) & 0xFF
   195     minor = (version >> 20) & 0xFF
   196     patch = (version >> 12) & 0xFF
   196     patch = (version >> 12) & 0xFF
   197     level = (version >> 8) & 0x0F
   197     level = (version >> 8) & 0x0F
   198     serial = version & 0xFF
   198     serial = version & 0xFF