1 |
A similar size formatting function was used in two places in emerge |
2 |
code. Instead, create a single function in portage.localization module |
3 |
that formats sizes using the current locale and a common set of rules. |
4 |
|
5 |
I'm not really convinced about 'ceiling' all sizes but I understand |
6 |
the original point about not outputting '0 KiB'. If you have better |
7 |
ideas, I'd be happy to change it. |
8 |
|
9 |
The code was overall simplified, and a proper number formatting function |
10 |
is used (instead of string splitting). Locale grouping rules are |
11 |
respected now, and ISO/IEC binary prefixes are used for unambiguity. |
12 |
--- |
13 |
pym/_emerge/resolver/output.py | 5 +++-- |
14 |
pym/_emerge/resolver/output_helpers.py | 20 +++----------------- |
15 |
pym/_emerge/search.py | 10 +++------- |
16 |
pym/portage/localization.py | 12 ++++++++++++ |
17 |
4 files changed, 21 insertions(+), 26 deletions(-) |
18 |
|
19 |
diff --git a/pym/_emerge/resolver/output.py b/pym/_emerge/resolver/output.py |
20 |
index 5f550be..aefc3f4 100644 |
21 |
--- a/pym/_emerge/resolver/output.py |
22 |
+++ b/pym/_emerge/resolver/output.py |
23 |
@@ -18,6 +18,7 @@ from portage.dbapi.dep_expand import dep_expand |
24 |
from portage.dep import cpvequal, _repo_separator, _slot_separator |
25 |
from portage.eapi import _get_eapi_attrs |
26 |
from portage.exception import InvalidDependString, SignatureException |
27 |
+from portage.localization import localized_size |
28 |
from portage.package.ebuild.config import _get_feature_flags |
29 |
from portage.package.ebuild._spawn_nofetch import spawn_nofetch |
30 |
from portage.output import ( blue, colorize, create_color_func, |
31 |
@@ -30,7 +31,7 @@ from portage.versions import best, cpv_getversion |
32 |
from _emerge.Blocker import Blocker |
33 |
from _emerge.create_world_atom import create_world_atom |
34 |
from _emerge.resolver.output_helpers import ( _DisplayConfig, _tree_display, |
35 |
- _PackageCounters, _create_use_string, _format_size, _calc_changelog, PkgInfo) |
36 |
+ _PackageCounters, _create_use_string, _calc_changelog, PkgInfo) |
37 |
from _emerge.show_invalid_depstring_notice import show_invalid_depstring_notice |
38 |
|
39 |
if sys.hexversion >= 0x3000000: |
40 |
@@ -330,7 +331,7 @@ class Display(object): |
41 |
self.myfetchlist.add(myfetchfile) |
42 |
if pkg_info.ordered: |
43 |
self.counters.totalsize += mysize |
44 |
- self.verboseadd += _format_size(mysize) |
45 |
+ self.verboseadd += localized_size(mysize) |
46 |
|
47 |
if self.quiet_repo_display: |
48 |
# overlay verbose |
49 |
diff --git a/pym/_emerge/resolver/output_helpers.py b/pym/_emerge/resolver/output_helpers.py |
50 |
index 58b2694..e0ee3e0 100644 |
51 |
--- a/pym/_emerge/resolver/output_helpers.py |
52 |
+++ b/pym/_emerge/resolver/output_helpers.py |
53 |
@@ -1,4 +1,4 @@ |
54 |
-# Copyright 2010-2013 Gentoo Foundation |
55 |
+# Copyright 2010-2014 Gentoo Foundation |
56 |
# Distributed under the terms of the GNU General Public License v2 |
57 |
|
58 |
"""Contains private support functions for the Display class |
59 |
@@ -17,6 +17,7 @@ import sys |
60 |
from portage import os |
61 |
from portage import _encodings, _unicode_encode |
62 |
from portage._sets.base import InternalPackageSet |
63 |
+from portage.localization import localized_size |
64 |
from portage.output import (blue, bold, colorize, create_color_func, |
65 |
green, red, teal, turquoise, yellow) |
66 |
bad = create_color_func("BAD") |
67 |
@@ -158,7 +159,7 @@ class _PackageCounters(object): |
68 |
myoutput.append(", ".join(details)) |
69 |
if total_installs != 0: |
70 |
myoutput.append(")") |
71 |
- myoutput.append(", Size of downloads: %s" % _format_size(self.totalsize)) |
72 |
+ myoutput.append(", Size of downloads: %s" % localized_size(self.totalsize)) |
73 |
if self.restrict_fetch: |
74 |
myoutput.append("\nFetch Restriction: %s package" % \ |
75 |
self.restrict_fetch) |
76 |
@@ -234,21 +235,6 @@ class _DisplayConfig(object): |
77 |
self.pkg = depgraph._pkg |
78 |
|
79 |
|
80 |
-# formats a size given in bytes nicely |
81 |
-def _format_size(mysize): |
82 |
- if isinstance(mysize, basestring): |
83 |
- return mysize |
84 |
- if 0 != mysize % 1024: |
85 |
- # Always round up to the next kB so that it doesn't show 0 kB when |
86 |
- # some small file still needs to be fetched. |
87 |
- mysize += 1024 - mysize % 1024 |
88 |
- mystr=str(mysize//1024) |
89 |
- mycount=len(mystr) |
90 |
- while (mycount > 3): |
91 |
- mycount-=3 |
92 |
- mystr=mystr[:mycount]+","+mystr[mycount:] |
93 |
- return mystr+" kB" |
94 |
- |
95 |
def _create_use_string(conf, name, cur_iuse, iuse_forced, cur_use, |
96 |
old_iuse, old_use, |
97 |
is_new, feature_flags, reinst_flags): |
98 |
diff --git a/pym/_emerge/search.py b/pym/_emerge/search.py |
99 |
index bd74fb7..4b0fd9f 100644 |
100 |
--- a/pym/_emerge/search.py |
101 |
+++ b/pym/_emerge/search.py |
102 |
@@ -1,4 +1,4 @@ |
103 |
-# Copyright 1999-2013 Gentoo Foundation |
104 |
+# Copyright 1999-2014 Gentoo Foundation |
105 |
# Distributed under the terms of the GNU General Public License v2 |
106 |
|
107 |
from __future__ import print_function |
108 |
@@ -7,6 +7,7 @@ import re |
109 |
import portage |
110 |
from portage import os |
111 |
from portage.dbapi.porttree import _parse_uri_map |
112 |
+from portage.localization import localized_size |
113 |
from portage.output import bold, bold as white, darkgreen, green, red |
114 |
from portage.util import writemsg_stdout |
115 |
|
116 |
@@ -348,12 +349,7 @@ class search(object): |
117 |
break |
118 |
|
119 |
if myebuild and file_size_str is None: |
120 |
- mystr = str(mysum[0] // 1024) |
121 |
- mycount = len(mystr) |
122 |
- while (mycount > 3): |
123 |
- mycount -= 3 |
124 |
- mystr = mystr[:mycount] + "," + mystr[mycount:] |
125 |
- file_size_str = mystr + " kB" |
126 |
+ file_size_str = localized_size(mysum[0]) |
127 |
|
128 |
if self.verbose: |
129 |
if available: |
130 |
diff --git a/pym/portage/localization.py b/pym/portage/localization.py |
131 |
index b54835a..e4d87b6 100644 |
132 |
--- a/pym/portage/localization.py |
133 |
+++ b/pym/portage/localization.py |
134 |
@@ -2,6 +2,9 @@ |
135 |
# Copyright 2004-2014 Gentoo Foundation |
136 |
# Distributed under the terms of the GNU General Public License v2 |
137 |
|
138 |
+import locale |
139 |
+import math |
140 |
+ |
141 |
from portage import _unicode_decode |
142 |
|
143 |
# We define this to make the transition easier for us. |
144 |
@@ -25,3 +28,12 @@ def localization_example(): |
145 |
print(_("A: %(a)s -- B: %(b)s -- C: %(c)s") % |
146 |
{"a": a_value, "b": b_value, "c": c_value}) |
147 |
|
148 |
+def localized_size(num_bytes): |
149 |
+ """ |
150 |
+ Return pretty localized size string for num_bytes size |
151 |
+ (given in bytes). The output will be in kibibytes. |
152 |
+ """ |
153 |
+ |
154 |
+ # always round up, so that small files don't end up as '0 KiB' |
155 |
+ num_kib = math.ceil(num_bytes / 1024) |
156 |
+ return locale.format('%d', num_kib, grouping=True) + ' KiB' |
157 |
-- |
158 |
1.9.1 |