From b1d0512f0e20b709858151818ddc508fd00dfe4a Mon Sep 17 00:00:00 2001 From: Anton Khirnov Date: Sat, 16 Jan 2021 10:32:48 +0100 Subject: helper: move humanize_size to the only place where it is used --- alot/db/attachment.py | 24 ++++++++++++++++++++++-- 1 file changed, 22 insertions(+), 2 deletions(-) (limited to 'alot/db') 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): """ -- cgit v1.2.3