Merge lp:~openerp-dev/openobject-server/trunk-bug-1015867-api into lp:openobject-server

Proposed by Arnaud Pineux (OpenERP)
Status: Needs review
Proposed branch: lp:~openerp-dev/openobject-server/trunk-bug-1015867-api
Merge into: lp:openobject-server
Diff against target: 279 lines (+86/-142)
3 files modified
openerp/tools/__init__.py (+0/-1)
openerp/tools/amount_to_text.py (+86/-7)
openerp/tools/amount_to_text_en.py (+0/-134)
To merge this branch: bzr merge lp:~openerp-dev/openobject-server/trunk-bug-1015867-api
Reviewer Review Type Date Requested Status
Vo Minh Thu (community) Approve
Review via email: mp+133957@code.launchpad.net
To post a comment you must log in.
4545. By Arnaud Pineux (OpenERP)

[REVERT] changelog, lt_LT, nl_NL, UK_UA

4546. By Vo Minh Thu

[MERGE] merged trunk.

4547. By Vo Minh Thu

[IMP] amount_to_text: use logging (as did the previous amount_to_text_en).

Revision history for this message
Vo Minh Thu (thu) wrote :

Seems good.

Just waiting to be sure for the corresponding addons branch https://code.launchpad.net/~openerp-dev/openobject-addons/trunk-bug-1015867-api/+merge/133958

review: Approve

Unmerged revisions

4547. By Vo Minh Thu

[IMP] amount_to_text: use logging (as did the previous amount_to_text_en).

4546. By Vo Minh Thu

[MERGE] merged trunk.

4545. By Arnaud Pineux (OpenERP)

[REVERT] changelog, lt_LT, nl_NL, UK_UA

4544. By Arnaud Pineux (OpenERP)

[FIX] account_check_writing and amout_to_text

4543. By Arnaud Pineux (OpenERP)

[FIX] account_check_writing and amout_to_text

Preview Diff

[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
1=== modified file 'openerp/tools/__init__.py'
2--- openerp/tools/__init__.py 2012-11-06 11:54:20 +0000
3+++ openerp/tools/__init__.py 2012-12-04 16:21:23 +0000
4@@ -28,7 +28,6 @@
5 from graph import graph
6 from image import *
7 from amount_to_text import *
8-from amount_to_text_en import *
9 from pdf_utils import *
10 from yaml_import import *
11 from sql import *
12
13=== modified file 'openerp/tools/amount_to_text.py'
14--- openerp/tools/amount_to_text.py 2011-06-23 09:03:11 +0000
15+++ openerp/tools/amount_to_text.py 2012-12-04 16:21:23 +0000
16@@ -19,6 +19,10 @@
17 #
18 ##############################################################################
19
20+import logging
21+
22+_logger = logging.getLogger(__name__)
23+
24 #-------------------------------------------------------------
25 # French
26 #-------------------------------------------------------------
27@@ -156,18 +160,96 @@
28 final_result = start_word +' '+units_name+' '+ end_word +' '+cents_name
29 return final_result
30
31+
32+#-------------------------------------------------------------
33+# ENGLISH
34+#-------------------------------------------------------------
35+
36+to_19 = ( 'Zero', 'One', 'Two', 'Three', 'Four', 'Five', 'Six',
37+ 'Seven', 'Eight', 'Nine', 'Ten', 'Eleven', 'Twelve', 'Thirteen',
38+ 'Fourteen', 'Fifteen', 'Sixteen', 'Seventeen', 'Eighteen', 'Nineteen' )
39+tens = ( 'Twenty', 'Thirty', 'Forty', 'Fifty', 'Sixty', 'Seventy', 'Eighty', 'Ninety')
40+denom = ( '',
41+ 'Thousand', 'Million', 'Billion', 'Trillion', 'Quadrillion',
42+ 'Quintillion', 'Sextillion', 'Septillion', 'Octillion', 'Nonillion',
43+ 'Decillion', 'Undecillion', 'Duodecillion', 'Tredecillion', 'Quattuordecillion',
44+ 'Sexdecillion', 'Septendecillion', 'Octodecillion', 'Novemdecillion', 'Vigintillion' )
45+
46+def _convert_nn(val):
47+ """convert a value < 100 to English.
48+ """
49+ if val < 20:
50+ return to_19[val]
51+ for (dcap, dval) in ((k, 20 + (10 * v)) for (v, k) in enumerate(tens)):
52+ if dval + 10 > val:
53+ if val % 10:
54+ return dcap + '-' + to_19[val % 10]
55+ return dcap
56+
57+def _convert_nnn(val):
58+ """
59+ convert a value < 1000 to english, special cased because it is the level that kicks
60+ off the < 100 special case. The rest are more general. This also allows you to
61+ get strings in the form of 'forty-five hundred' if called directly.
62+ """
63+ word = ''
64+ (mod, rem) = (val % 100, val // 100)
65+ if rem > 0:
66+ word = to_19[rem] + ' Hundred'
67+ if mod > 0:
68+ word = word + ' '
69+ if mod > 0:
70+ word = word + _convert_nn(mod)
71+ return word
72+
73+def english_number(val):
74+ if val < 100:
75+ return _convert_nn(val)
76+ if val < 1000:
77+ return _convert_nnn(val)
78+ for (didx, dval) in ((v - 1, 1000 ** v) for v in range(len(denom))):
79+ if dval > val:
80+ mod = 1000 ** didx
81+ l = val // mod
82+ r = val - (l * mod)
83+ ret = _convert_nnn(l) + ' ' + denom[didx]
84+ if r > 0:
85+ ret = ret + ', ' + english_number(r)
86+ return ret
87+
88+def amount_to_text_en(number, currency):
89+ number = '%.2f' % number
90+ if currency == "EUR":
91+ currency = "euro"
92+ if currency == "USD":
93+ currency = "dollars"
94+ units_name = currency
95+ list = str(number).split('.')
96+ start_word = english_number(int(list[0]))
97+ end_word = english_number(int(list[1]))
98+ cents_number = int(list[1])
99+ cents_name = (cents_number > 1) and 'Cents' or 'Cent'
100+
101+ return ' '.join(filter(None, [start_word, units_name, (start_word or units_name) and (end_word or cents_name) and 'and', end_word, cents_name]))
102+
103+
104+
105 #-------------------------------------------------------------
106 # Generic functions
107 #-------------------------------------------------------------
108
109-_translate_funcs = {'fr' : amount_to_text_fr, 'nl' : amount_to_text_nl}
110+_translate_funcs = {
111+ 'en': amount_to_text_en,
112+ 'fr': amount_to_text_fr,
113+ 'nl': amount_to_text_nl,
114+}
115
116 def add_amount_to_text_function(lang, func):
117 _translate_funcs[lang] = func
118
119 #TODO: we should use the country AND language (ex: septante VS soixante dix)
120 #TODO: we should use en by default, but the translation func is yet to be implemented
121-def amount_to_text(nbr, lang='fr', currency='euro'):
122+def amount_to_text(nbr, lang='en', currency='euro'):
123 """ Converts an integer to its textual representation, using the language set in the context if any.
124
125 Example::
126@@ -178,12 +260,9 @@
127 ##TODO: use logger
128 # print "WARNING: number too large '%d', can't translate it!" % (nbr,)
129 # return str(nbr)
130-
131 if not _translate_funcs.has_key(lang):
132-#TODO: use logger
133- print "WARNING: no translation function found for lang: '%s'" % (lang,)
134-#TODO: (default should be en) same as above
135- lang = 'fr'
136+ _logger.warning(_("No translation function found for language '%s'."), lang)
137+ lang = 'en'
138 return _translate_funcs[lang](abs(nbr), currency)
139
140 if __name__=='__main__':
141
142=== removed file 'openerp/tools/amount_to_text_en.py'
143--- openerp/tools/amount_to_text_en.py 2012-09-04 10:02:50 +0000
144+++ openerp/tools/amount_to_text_en.py 1970-01-01 00:00:00 +0000
145@@ -1,134 +0,0 @@
146-# -*- coding: utf-8 -*-
147-##############################################################################
148-#
149-# OpenERP, Open Source Management Solution
150-# Copyright (C) 2004-2009 Tiny SPRL (<http://tiny.be>).
151-#
152-# This program is free software: you can redistribute it and/or modify
153-# it under the terms of the GNU Affero General Public License as
154-# published by the Free Software Foundation, either version 3 of the
155-# License, or (at your option) any later version.
156-#
157-# This program is distributed in the hope that it will be useful,
158-# but WITHOUT ANY WARRANTY; without even the implied warranty of
159-# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
160-# GNU Affero General Public License for more details.
161-#
162-# You should have received a copy of the GNU Affero General Public License
163-# along with this program. If not, see <http://www.gnu.org/licenses/>.
164-#
165-##############################################################################
166-
167-import logging
168-from translate import _
169-
170-_logger = logging.getLogger(__name__)
171-
172-#-------------------------------------------------------------
173-#ENGLISH
174-#-------------------------------------------------------------
175-
176-to_19 = ( 'Zero', 'One', 'Two', 'Three', 'Four', 'Five', 'Six',
177- 'Seven', 'Eight', 'Nine', 'Ten', 'Eleven', 'Twelve', 'Thirteen',
178- 'Fourteen', 'Fifteen', 'Sixteen', 'Seventeen', 'Eighteen', 'Nineteen' )
179-tens = ( 'Twenty', 'Thirty', 'Forty', 'Fifty', 'Sixty', 'Seventy', 'Eighty', 'Ninety')
180-denom = ( '',
181- 'Thousand', 'Million', 'Billion', 'Trillion', 'Quadrillion',
182- 'Quintillion', 'Sextillion', 'Septillion', 'Octillion', 'Nonillion',
183- 'Decillion', 'Undecillion', 'Duodecillion', 'Tredecillion', 'Quattuordecillion',
184- 'Sexdecillion', 'Septendecillion', 'Octodecillion', 'Novemdecillion', 'Vigintillion' )
185-
186-def _convert_nn(val):
187- """convert a value < 100 to English.
188- """
189- if val < 20:
190- return to_19[val]
191- for (dcap, dval) in ((k, 20 + (10 * v)) for (v, k) in enumerate(tens)):
192- if dval + 10 > val:
193- if val % 10:
194- return dcap + '-' + to_19[val % 10]
195- return dcap
196-
197-def _convert_nnn(val):
198- """
199- convert a value < 1000 to english, special cased because it is the level that kicks
200- off the < 100 special case. The rest are more general. This also allows you to
201- get strings in the form of 'forty-five hundred' if called directly.
202- """
203- word = ''
204- (mod, rem) = (val % 100, val // 100)
205- if rem > 0:
206- word = to_19[rem] + ' Hundred'
207- if mod > 0:
208- word = word + ' '
209- if mod > 0:
210- word = word + _convert_nn(mod)
211- return word
212-
213-def english_number(val):
214- if val < 100:
215- return _convert_nn(val)
216- if val < 1000:
217- return _convert_nnn(val)
218- for (didx, dval) in ((v - 1, 1000 ** v) for v in range(len(denom))):
219- if dval > val:
220- mod = 1000 ** didx
221- l = val // mod
222- r = val - (l * mod)
223- ret = _convert_nnn(l) + ' ' + denom[didx]
224- if r > 0:
225- ret = ret + ', ' + english_number(r)
226- return ret
227-
228-def amount_to_text(number, currency):
229- number = '%.2f' % number
230- units_name = currency
231- list = str(number).split('.')
232- start_word = english_number(int(list[0]))
233- end_word = english_number(int(list[1]))
234- cents_number = int(list[1])
235- cents_name = (cents_number > 1) and 'Cents' or 'Cent'
236-
237- return ' '.join(filter(None, [start_word, units_name, (start_word or units_name) and (end_word or cents_name) and 'and', end_word, cents_name]))
238-
239-
240-#-------------------------------------------------------------
241-# Generic functions
242-#-------------------------------------------------------------
243-
244-_translate_funcs = {'en' : amount_to_text}
245-
246-#TODO: we should use the country AND language (ex: septante VS soixante dix)
247-#TODO: we should use en by default, but the translation func is yet to be implemented
248-def amount_to_text(nbr, lang='en', currency='euro'):
249- """ Converts an integer to its textual representation, using the language set in the context if any.
250-
251- Example::
252-
253- 1654: thousands six cent cinquante-quatre.
254- """
255- import openerp.loglevels as loglevels
256-# if nbr > 10000000:
257-# _logger.warning(_("Number too large '%d', can not translate it"))
258-# return str(nbr)
259-
260- if not _translate_funcs.has_key(lang):
261- _logger.warning(_("no translation function found for lang: '%s'"), lang)
262- #TODO: (default should be en) same as above
263- lang = 'en'
264- return _translate_funcs[lang](abs(nbr), currency)
265-
266-if __name__=='__main__':
267- from sys import argv
268-
269- lang = 'nl'
270- if len(argv) < 2:
271- for i in range(1,200):
272- print i, ">>", int_to_text(i, lang)
273- for i in range(200,999999,139):
274- print i, ">>", int_to_text(i, lang)
275- else:
276- print int_to_text(int(argv[1]), lang)
277-
278-
279-# vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4: