3
################################################################################
4
# Developed by Kaspars Vilkens - Alistek Ltd (c) 2011
6
# Supported sum: 0 ... 999999999999.99
7
# Supported languages: for more reference see languages forlder
8
# Supported currencies: see particular language for reference
9
################################################################################
12
supported_language = {}
14
if __name__ == '__main__':
16
error = '''This code is part of Report Aeroo package!
17
Not to be used separately...'''
20
def currency_to_text(sum, currency_code, language_code):
21
if language_code not in supported_language:
22
raise Exception('Not supported or no language: %s' % language_code)
24
suppl = supported_language[language_code]
25
return suppl.currency_to_text(sum, currency_code)
27
class ctt_language(object):
35
self.supported_currency = {}
37
self.maxbound = 999999999999.99
38
currencies = self._init_lang()
39
supported_language.update({self.name : self})
40
import_submodules('currency', currencies, 0)
43
if sum < self.minbound or sum > self.maxbound :
45
"""Sum out of bounds: must be from %s to %s""" % \
46
(str(self.minbound), str(self.maxbound)))
48
def check_currency(self):
49
if currency not in supported_currency:
51
"""Unsupported or no currency: must be one of (%s)""" % \
52
', '.join(self.supported_currency))
54
def dtowords(self, sum_integers, gender):
57
return self.wordify('0', 0, gender)
58
elif sum_integers > 0:
59
lengthx = len(str(sum_integers))
60
nrchunks = (lengthx / 3)
61
if nrchunks < (float(lengthx) / 3) :
64
while inc <= nrchunks:
65
startpos = (lengthx - inc * 3)
68
chunklength += startpos
70
chunk = str(sum_integers)[startpos : startpos + chunklength]
71
#print str(startpos)+' '+str(chunklength)+' '+ chunk
73
wordified = self.wordify(chunk, inc-1, gender)
75
wordified = self.wordify(chunk, inc-1, 'm')
78
if len(diginwords) > 0 and wordified:
80
diginwords = wordified + spacer + diginwords
84
def currency_to_text(self, sum, currency):
85
#--------------for currencies with 100 fractions
88
# find out digits before floating point - currency
90
# find out digits after floating point - fractions
91
sum_frc = int(round((sum - sum_cur) * 100,0))
92
curr = self.supported_currency.get(currency)
93
cur_in_words = self.dtowords(sum_cur, curr.cur_gram_gender)
94
frc_in_words = self.dtowords(sum_frc, curr.frc_gram_gender)
95
#------------------------------------
97
return (cur_in_words + curr.cur_to_text(sum_cur) + ' ' + frc_in_words +\
98
curr.frc_to_text(sum_frc)).strip().encode('utf-8')
101
class ctt_currency(object):
102
def _init_currency(self):
109
self._init_currency()
110
suppl = supported_language.get(self.language)
111
suppl.supported_currency.update({self.code : self})
113
def cur_to_text(self, sum_cur):
114
# is the currency sum one
115
if sum_cur == 1 or (str(sum_cur)[-1] == '1' and str(sum_cur)[-2] !='1'):
116
return self.cur_singular
117
# 2,3 and 4 yields different plural form, if defined
118
elif ((sum_cur in [2, 3, 4]) or (str(sum_cur)[-1] in ['2', '3', '4'] \
119
and str(sum_cur)[-2] != '1')) and hasattr(self, 'cur_plural_2to4'):
120
return self.cur_plural_2to4
121
# betwean 1 and 10 yields different plural form, if defined
122
elif (sum_cur > 1 and sum_cur < 10 or (int(str(sum_cur)[-1]) > 1 \
123
and str(sum_cur)[-2] != '1')) and hasattr(self, 'cur_plural_2to10'):
124
return self.cur_plural_2to10
125
# starting from 10 yields uses default plural form
127
return self.cur_plural
129
def frc_to_text(self, sum_frc):
130
# is the fraction sum one
131
if sum_frc == 1 or (str(sum_frc)[-1] == '1' and str(sum_frc)[-2] !='1'):
132
return self.frc_singular
133
# 2,3 and 4 yields different plural form, if defined
134
elif ((sum_frc in [2, 3, 4]) or (str(sum_frc)[-1] in ['2', '3', '4'] \
135
and str(sum_frc)[-2] != '1')) and hasattr(self, 'frc_plural_2to4'):
136
return self.frc_plural_2to4
137
# betwean 1 and 10 yields different plural form, if defined
138
elif (sum_frc > 1 and sum_frc < 10 or (int(str(sum_frc)[-1]) > 1 \
139
and str(sum_frc)[-2] != '1')) and hasattr(self, 'frc_plural_2to10'):
140
return self.frc_plural_2to10
141
# starting from 10 yields uses default plural form
143
return self.frc_plural
145
def __filter_names(to_import, package):
146
folder = os.path.split(package.__file__)[0]
147
for name in os.listdir(folder):
148
if to_import == 'currency':
149
if name.endswith(".py") and not name.startswith("__"):
151
if to_import == 'language':
152
if len(name) == 5 and not name.startswith("__"):
155
def import_submodules(to_import, package, level=-1):
156
names = list(__filter_names(to_import, package))
157
m = __import__(package.__name__, globals(), locals(), names, level)
158
return dict((name, getattr(m, name)) for name in names)
161
import_submodules('language', ctt_languages, 0)