Use a localized size formatting function
A similar size formatting function was used in two places in emerge
code. Instead, create a single function in portage.localization module
that formats sizes using the current locale and a common set of rules.
I'm not really convinced about 'ceiling' all sizes but I understand
the original point about not outputting '0 KiB'. If you have better
ideas, I'd be happy to change it.
The code was simplified, and a proper number formatting function
is used (instead of string splitting). Locale grouping rules are
respected now, and ISO/IEC binary prefixes are used for unambiguity.
diff --git a/pym/_emerge/resolver/output.py b/pym/_emerge/resolver/output.py
index 5f550be..aefc3f4 100644
--- a/pym/_emerge/resolver/output.py
+++ b/pym/_emerge/resolver/output.py
@@ -18,6 +18,7 @@
from portage.dep import cpvequal, _repo_separator, _slot_separator
from portage.eapi import _get_eapi_attrs
from portage.exception import InvalidDependString, SignatureException
+from portage.localization import localized_size
from portage.package.ebuild.config import _get_feature_flags
from portage.package.ebuild._spawn_nofetch import spawn_nofetch
from portage.output import ( blue, colorize, create_color_func,
@@ -30,7 +31,7 @@
from _emerge.Blocker import Blocker
from _emerge.create_world_atom import create_world_atom
from _emerge.resolver.output_helpers import ( _DisplayConfig, _tree_display,
- _PackageCounters, _create_use_string, _format_size, _calc_changelog, PkgInfo)
+ _PackageCounters, _create_use_string, _calc_changelog, PkgInfo)
from _emerge.show_invalid_depstring_notice import show_invalid_depstring_notice
if sys.hexversion >= 0x3000000:
@@ -330,7 +331,7 @@
self.myfetchlist.add(myfetchfile)
if pkg_info.ordered:
self.counters.totalsize += mysize
- self.verboseadd += _format_size(mysize)
+ self.verboseadd += localized_size(mysize)
if self.quiet_repo_display:
# overlay verbose
diff --git a/pym/_emerge/resolver/output_helpers.py b/pym/_emerge/resolver/output_helpers.py
index 58b2694..e0ee3e0 100644
--- a/pym/_emerge/resolver/output_helpers.py
+++ b/pym/_emerge/resolver/output_helpers.py
@@ -1,4 +1,4 @@
-# Copyright 2010-2013 Gentoo Foundation
+# Copyright 2010-2014 Gentoo Foundation
# Distributed under the terms of the GNU General Public License v2
"""Contains private support functions for the Display class
@@ -17,6 +17,7 @@
from portage import os
from portage import _encodings, _unicode_encode
from portage._sets.base import InternalPackageSet
+from portage.localization import localized_size
from portage.output import (blue, bold, colorize, create_color_func,
green, red, teal, turquoise, yellow)
bad = create_color_func("BAD")
@@ -158,7 +159,7 @@
myoutput.append(", ".join(details))
if total_installs != 0:
myoutput.append(")")
- myoutput.append(", Size of downloads: %s" % _format_size(self.totalsize))
+ myoutput.append(", Size of downloads: %s" % localized_size(self.totalsize))
if self.restrict_fetch:
myoutput.append("\nFetch Restriction: %s package" % \
self.restrict_fetch)
@@ -234,21 +235,6 @@
self.pkg = depgraph._pkg
-# formats a size given in bytes nicely
-def _format_size(mysize):
- if isinstance(mysize, basestring):
- return mysize
- if 0 != mysize % 1024:
- # Always round up to the next kB so that it doesn't show 0 kB when
- # some small file still needs to be fetched.
- mysize += 1024 - mysize % 1024
- mystr=str(mysize//1024)
- mycount=len(mystr)
- while (mycount > 3):
- mycount-=3
- mystr=mystr[:mycount]+","+mystr[mycount:]
- return mystr+" kB"
-
def _create_use_string(conf, name, cur_iuse, iuse_forced, cur_use,
old_iuse, old_use,
is_new, feature_flags, reinst_flags):
diff --git a/pym/_emerge/search.py b/pym/_emerge/search.py
index bd74fb7..4b0fd9f 100644
--- a/pym/_emerge/search.py
+++ b/pym/_emerge/search.py
@@ -1,4 +1,4 @@
-# Copyright 1999-2013 Gentoo Foundation
+# Copyright 1999-2014 Gentoo Foundation
# Distributed under the terms of the GNU General Public License v2
from __future__ import print_function
@@ -7,6 +7,7 @@
import portage
from portage import os
from portage.dbapi.porttree import _parse_uri_map
+from portage.localization import localized_size
from portage.output import bold, bold as white, darkgreen, green, red
from portage.util import writemsg_stdout
@@ -348,12 +349,7 @@
break
if myebuild and file_size_str is None:
- mystr = str(mysum[0] // 1024)
- mycount = len(mystr)
- while (mycount > 3):
- mycount -= 3
- mystr = mystr[:mycount] + "," + mystr[mycount:]
- file_size_str = mystr + " kB"
+ file_size_str = localized_size(mysum[0])
if self.verbose:
if available:
diff --git a/pym/portage/localization.py b/pym/portage/localization.py
index b54835a..e4d87b6 100644
--- a/pym/portage/localization.py
+++ b/pym/portage/localization.py
@@ -2,6 +2,9 @@
# Copyright 2004-2014 Gentoo Foundation
# Distributed under the terms of the GNU General Public License v2
+import locale
+import math
+
from portage import _unicode_decode
# We define this to make the transition easier for us.
@@ -25,3 +28,12 @@
print(_("A: %(a)s -- B: %(b)s -- C: %(c)s") %
{"a": a_value, "b": b_value, "c": c_value})
+def localized_size(num_bytes):
+ """
+ Return pretty localized size string for num_bytes size
+ (given in bytes). The output will be in kibibytes.
+ """
+
+ # always round up, so that small files don't end up as '0 KiB'
+ num_kib = math.ceil(num_bytes / 1024)
+ return locale.format('%d', num_kib, grouping=True) + ' KiB'