changeset 230d7bc47b71 in tryton:5.0
details: https://hg.tryton.org/tryton?cmd=changeset;node=230d7bc47b71
description:
Use locale.localize() and new format to format Decimal
This allows to use the new format syntax on Decimal which prevents a
conversion to float.
As locale.localize() is not yet added to released Python, we monkey
patch
older version.
We also replace locale.format by locale.format_string as it has been
deprecated in Python 3.7.
issue8574
review282011002
(grafted from bfb8b9a6d64d26af6dbad9687869306bfb5e4ab1)
diffstat:
tryton/__init__.py | 22 +++++++++++++++++
tryton/common/domain_parser.py | 3 +-
tryton/common/timedelta.py | 3 +-
tryton/gui/window/view_form/model/field.py | 8 +++---
tryton/gui/window/view_form/view/form_gtk/dictionary.py | 5 ++-
tryton/gui/window/view_form/view/graph_gtk/bar.py | 3 +-
tryton/gui/window/view_form/view/graph_gtk/graph.py | 2 +-
tryton/gui/window/view_form/view/graph_gtk/line.py | 3 +-
tryton/gui/window/view_form/view/graph_gtk/pie.py | 15 ++++++-----
tryton/gui/window/view_form/view/list.py | 15 ++++++-----
10 files changed, 54 insertions(+), 25 deletions(-)
diffs (202 lines):
diff -r 5e2f5dfc2520 -r 230d7bc47b71 tryton/__init__.py
--- a/tryton/__init__.py Mon Jan 20 11:10:51 2020 +0100
+++ b/tryton/__init__.py Mon Jan 20 11:26:30 2020 +0100
@@ -5,6 +5,7 @@
import gi
import pygtkcompat
+import locale
pygtkcompat.enable()
pygtkcompat.enable_gtk(version='3.0')
@@ -198,3 +199,24 @@
with gtk.gdk.lock:
return super(Dialog, self).run()
gtk.Dialog = Dialog
+
+if not hasattr(locale, 'localize'):
+ def localize(formatted, grouping=False, monetary=False):
+ if '.' in formatted:
+ seps = 0
+ parts = formatted.split('.')
+ if grouping:
+ parts[0], seps = locale._group(parts[0], monetary=monetary)
+ decimal_point = locale.localeconv()[
+ monetary and 'mon_decimal_point' or 'decimal_point']
+ formatted = decimal_point.join(parts)
+ if seps:
+ formatted = locale._strip_padding(formatted, seps)
+ else:
+ seps = 0
+ if grouping:
+ formatted, seps = locale._group(formatted, monetary=monetary)
+ if seps:
+ formatted = locale._strip_padding(formatted, seps)
+ return formatted
+ setattr(locale, 'localize', localize)
diff -r 5e2f5dfc2520 -r 230d7bc47b71 tryton/common/domain_parser.py
--- a/tryton/common/domain_parser.py Mon Jan 20 11:10:51 2020 +0100
+++ b/tryton/common/domain_parser.py Mon Jan 20 11:26:30 2020 +0100
@@ -509,7 +509,8 @@
digit = len(str(value * factor).rstrip('0').split('.')[1])
except IndexError:
digit = 0
- return locale.format('%.*f', (digit, value * factor or 0), True)
+ return locale.localize(
+ '{0:.{1}f}'.format(value * factor or 0, digit), True)
def format_selection():
selections = dict(field['selection'])
diff -r 5e2f5dfc2520 -r 230d7bc47b71 tryton/common/timedelta.py
--- a/tryton/common/timedelta.py Mon Jan 20 11:10:51 2020 +0100
+++ b/tryton/common/timedelta.py Mon Jan 20 11:26:30 2020 +0100
@@ -56,7 +56,8 @@
for (k, _), v in zip(converter[:-3], values):
if v:
- text.append(locale.format('%d', v, True) + _get_separators()[k])
+ text.append(
+ locale.format_string('%d', v, True) + _get_separators()[k])
if any(values[-3:]) or not text:
time = '%02d:%02d' % tuple(values[-3:-1])
if values[-1] or value:
diff -r 5e2f5dfc2520 -r 230d7bc47b71 tryton/gui/window/view_form/model/field.py
--- a/tryton/gui/window/view_form/model/field.py Mon Jan 20 11:10:51
2020 +0100
+++ b/tryton/gui/window/view_form/model/field.py Mon Jan 20 11:26:30
2020 +0100
@@ -356,14 +356,14 @@
value = record.value.get(self.name)
if value is not None:
digits = self.digits(record, factor=factor)
+ d = value * factor
+ if not isinstance(d, Decimal):
+ d = Decimal(repr(d))
if digits:
p = int(digits[1])
else:
- d = value * factor
- if not isinstance(d, Decimal):
- d = Decimal(repr(d))
p = -int(d.as_tuple().exponent)
- return locale.format('%.*f', (p, value * factor), True)
+ return locale.localize('{0:.{1}f}'.format(d, p), True)
else:
return ''
diff -r 5e2f5dfc2520 -r 230d7bc47b71
tryton/gui/window/view_form/view/form_gtk/dictionary.py
--- a/tryton/gui/window/view_form/view/form_gtk/dictionary.py Mon Jan 20
11:10:51 2020 +0100
+++ b/tryton/gui/window/view_form/view/form_gtk/dictionary.py Mon Jan 20
11:26:30 2020 +0100
@@ -187,7 +187,7 @@
def set_value(self, value):
if value is not None:
- txt_val = locale.format('%d', value, True)
+ txt_val = locale.format_string('%d', value, True)
else:
txt_val = ''
self.widget.set_text(txt_val)
@@ -243,7 +243,8 @@
def set_value(self, value):
digits = self.digits()
if value is not None:
- txt_val = locale.format('%.' + str(digits[1]) + 'f', value, True)
+ txt_val = locale.localize(
+ '{0:.{1}f}'.format(value, digits[1]), True)
else:
txt_val = ''
self.widget.set_width_chars(sum(digits))
diff -r 5e2f5dfc2520 -r 230d7bc47b71
tryton/gui/window/view_form/view/graph_gtk/bar.py
--- a/tryton/gui/window/view_form/view/graph_gtk/bar.py Mon Jan 20 11:10:51
2020 +0100
+++ b/tryton/gui/window/view_form/view/graph_gtk/bar.py Mon Jan 20 11:26:30
2020 +0100
@@ -93,7 +93,8 @@
label = common.timedelta.format(
datetime.timedelta(seconds=bar.yval), converter)
else:
- label = locale.format('%.2f', bar.yval, True)
+ label = locale.localize(
+ '{:.2f}'.format(bar.yval), True)
label += '\n'
label += str(self.labels[bar.xname])
self.popup.set_text(label)
diff -r 5e2f5dfc2520 -r 230d7bc47b71
tryton/gui/window/view_form/view/graph_gtk/graph.py
--- a/tryton/gui/window/view_form/view/graph_gtk/graph.py Mon Jan 20
11:10:51 2020 +0100
+++ b/tryton/gui/window/view_form/view/graph_gtk/graph.py Mon Jan 20
11:26:30 2020 +0100
@@ -179,7 +179,7 @@
for i in range(int(self.yrange / base) + 1):
val = int(self.minyval / base) * base + i * base
h = (val - self.minyval) * self.yscale
- label = locale.format('%.2f', val, True)
+ label = locale.localize('{:.2f}'.format(val), True)
ylabels.append((h, label))
return ylabels
diff -r 5e2f5dfc2520 -r 230d7bc47b71
tryton/gui/window/view_form/view/graph_gtk/line.py
--- a/tryton/gui/window/view_form/view/graph_gtk/line.py Mon Jan 20
11:10:51 2020 +0100
+++ b/tryton/gui/window/view_form/view/graph_gtk/line.py Mon Jan 20
11:26:30 2020 +0100
@@ -201,7 +201,8 @@
yfields_timedelta[point.yname])
label += common.timedelta.format(point.yval, converter)
else:
- label += locale.format('%.2f', point.yval, True)
+ label += locale.localize(
+ '{:2f}'.format(point.yval), True)
label += '\n'
label += str(self.labels[point.xname])
self.popup.set_text(label)
diff -r 5e2f5dfc2520 -r 230d7bc47b71
tryton/gui/window/view_form/view/graph_gtk/pie.py
--- a/tryton/gui/window/view_form/view/graph_gtk/pie.py Mon Jan 20 11:10:51
2020 +0100
+++ b/tryton/gui/window/view_form/view/graph_gtk/pie.py Mon Jan 20 11:26:30
2020 +0100
@@ -33,7 +33,7 @@
math.cos(normalisedAngle) * (self.radius + 10)
label = '%s (%s%%)' % (self.labels[slice.xname],
- locale.format('%.2f', slice.fraction * 100))
+ locale.localize('{:.2f}'.format(slice.fraction * 100)))
extents = cr.text_extents(label)
labelWidth = extents[2]
labelHeight = extents[3]
@@ -156,12 +156,13 @@
sum = common.timedelta.format(
datetime.timedelta(seconds=self.sum), converter)
else:
- value = locale.format('%.2f',
- slice.fraction * self.sum)
- sum = locale.format('%.2f', self.sum)
- label = '%s (%s%%)\n%s/%s' % (self.labels[slice.xname],
- locale.format('%.2f', slice.fraction * 100),
- value, sum)
+ value = locale.localize(
+ '{:.2f}'.format(slice.fraction * self.sum))
+ sum = locale.localize('{:.2f}'.format(self.sum))
+ label = '%s (%s%%)\n%s/%s' % (
+ self.labels[slice.xname],
+ locale.localize('{:.2f}'.format(slice.fraction * 100)),
+ value, sum)
self.popup.set_text(label)
self.queue_draw()
else:
diff -r 5e2f5dfc2520 -r 230d7bc47b71 tryton/gui/window/view_form/view/list.py
--- a/tryton/gui/window/view_form/view/list.py Mon Jan 20 11:10:51 2020 +0100
+++ b/tryton/gui/window/view_form/view/list.py Mon Jan 20 11:26:30 2020 +0100
@@ -1105,14 +1105,15 @@
selected_sum = common.timedelta.format(
selected_sum, converter)
sum_ = common.timedelta.format(sum_, converter)
- elif digit:
- selected_sum = locale.format(
- '%.*f', (digit, selected_sum or 0), True)
- sum_ = locale.format('%.*f', (digit, sum_ or 0), True)
+ elif digit is not None:
+ selected_sum = locale.localize(
+ '{0:.{1}f}'.format(selected_sum or 0, digit), True)
+ sum_ = locale.localize(
+ '{0:.{1}f}'.format(sum_ or 0, digit), True)
else:
- selected_sum = locale.format(
- '%s', selected_sum or 0, True)
- sum_ = locale.format('%s', sum_ or 0, True)
+ selected_sum = locale.localize(
+ '{}'.format(selected_sum or 0), True)
+ sum_ = locale.localize('{}'.format(sum_ or 0), True)
text = '%s / %s' % (selected_sum, sum_)
else: