summaryrefslogtreecommitdiff
path: root/alot/db
diff options
context:
space:
mode:
authorAnton Khirnov <anton@khirnov.net>2021-01-16 10:32:48 +0100
committerAnton Khirnov <anton@khirnov.net>2021-01-16 10:32:48 +0100
commitb1d0512f0e20b709858151818ddc508fd00dfe4a (patch)
tree92b8d774376184c0d6f1e91484a63c85be563153 /alot/db
parentf0b24807e42ad95395a3885775cb1ee3322571a6 (diff)
helper: move humanize_size to the only place where it is used
Diffstat (limited to 'alot/db')
-rw-r--r--alot/db/attachment.py24
1 files changed, 22 insertions, 2 deletions
diff --git a/alot/db/attachment.py b/alot/db/attachment.py
index fb6523e1..0ebe6fc9 100644
--- a/alot/db/attachment.py
+++ b/alot/db/attachment.py
@@ -7,10 +7,30 @@ import tempfile
import email.charset as charset
from copy import deepcopy
-from ..helper import humanize_size, guess_mimetype
+from ..helper import guess_mimetype
charset.add_charset('utf-8', charset.QP, charset.QP, 'utf-8')
+def _humanize_size(size):
+ """Create a nice human readable representation of the given number
+ (understood as bytes) using the "KiB" and "MiB" suffixes to indicate
+ kibibytes and mebibytes. A kibibyte is defined as 1024 bytes (as opposed to
+ a kilobyte which is 1000 bytes) and a mibibyte is 1024**2 bytes (as opposed
+ to a megabyte which is 1000**2 bytes).
+
+ :param size: the number to convert
+ :type size: int
+ :returns: the human readable representation of size
+ :rtype: str
+ """
+ for factor, format_string in ((1, '%i'),
+ (1024, '%iKiB'),
+ (1024 * 1024, '%.1fMiB')):
+ if size / factor < 1024:
+ return format_string % (size / factor)
+ return format_string % (size / factor)
+
+
class Attachment:
"""represents a mail attachment"""
@@ -25,7 +45,7 @@ class Attachment:
def __str__(self):
return '%s:%s (%s)' % (self.get_content_type(),
self.get_filename(),
- humanize_size(self.get_size()))
+ _humanize_size(self.get_size()))
def get_filename(self):
"""