~therp-nl/aeroo/7.0-lp1226576-restore_babel_compatibility

« back to all changes in this revision

Viewing changes to report_aeroo/ctt_objects.py

  • Committer: root
  • Date: 2013-05-16 15:53:25 UTC
  • Revision ID: root@erp.kndati.lv-20130516155325-x7v319i9a6avw58g
1.2.0

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
#!/usr/bin/python
 
2
# -*- coding: utf8 -*-
 
3
################################################################################
 
4
# Developed by Kaspars Vilkens - Alistek Ltd (c) 2011
 
5
#
 
6
# Supported sum: 0 ... 999999999999.99
 
7
# Supported languages: for more reference see languages forlder
 
8
# Supported currencies: see particular language for reference
 
9
################################################################################
 
10
 
 
11
import os
 
12
supported_language = {}
 
13
 
 
14
if __name__ == '__main__':
 
15
    from sys import exit
 
16
    error = '''This code is part of Report Aeroo package!
 
17
    Not to be used separately...'''
 
18
    exit(error)
 
19
    
 
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)
 
23
    else:
 
24
        suppl = supported_language[language_code]
 
25
        return suppl.currency_to_text(sum, currency_code)
 
26
 
 
27
class ctt_language(object):
 
28
    def _init_lang(self):
 
29
        pass
 
30
 
 
31
    def __repr__(self):
 
32
        return self.name
 
33
 
 
34
    def __init__(self):
 
35
        self.supported_currency = {}
 
36
        self.minbound = 0
 
37
        self.maxbound = 999999999999.99
 
38
        currencies = self._init_lang()
 
39
        supported_language.update({self.name : self})
 
40
        import_submodules('currency', currencies, 0)
 
41
 
 
42
    def check_sum(self):
 
43
        if sum < self.minbound or sum > self.maxbound : 
 
44
            raise Exception(\
 
45
                """Sum out of bounds: must be from %s to %s""" % \
 
46
                (str(self.minbound), str(self.maxbound)))
 
47
 
 
48
    def check_currency(self):
 
49
        if currency not in supported_currency: 
 
50
            raise Exception(\
 
51
                """Unsupported or no currency: must be one of (%s)""" % \
 
52
                ', '.join(self.supported_currency))
 
53
 
 
54
    def dtowords(self, sum_integers, gender):
 
55
        diginwords = u''
 
56
        if sum_integers == 0:
 
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) :
 
62
            nrchunks+=1
 
63
        inc = 1
 
64
        while inc <= nrchunks:
 
65
            startpos = (lengthx - inc * 3)
 
66
            chunklength = 3
 
67
            if startpos < 0:
 
68
                chunklength += startpos
 
69
                startpos = 0
 
70
            chunk = str(sum_integers)[startpos : startpos + chunklength]
 
71
            #print str(startpos)+' '+str(chunklength)+' '+ chunk
 
72
            if inc == 1:
 
73
                wordified = self.wordify(chunk, inc-1, gender)
 
74
            else:
 
75
                wordified = self.wordify(chunk, inc-1, 'm')
 
76
            inc += 1
 
77
            spacer = ''
 
78
            if len(diginwords) > 0 and wordified:
 
79
                spacer = ' '
 
80
            diginwords = wordified + spacer + diginwords
 
81
        return diginwords
 
82
 
 
83
 
 
84
    def currency_to_text(self, sum, currency):
 
85
        #--------------for currencies with 100 fractions
 
86
        sum = float(sum)
 
87
        sum = round(sum, 2)
 
88
        # find out digits before floating point - currency
 
89
        sum_cur = int(sum)
 
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
        #------------------------------------
 
96
 
 
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')
 
99
 
 
100
 
 
101
class ctt_currency(object):
 
102
    def _init_currency(self):
 
103
        pass
 
104
 
 
105
    def __repr__(self):
 
106
        return self.code
 
107
 
 
108
    def __init__(self):
 
109
        self._init_currency()
 
110
        suppl = supported_language.get(self.language)
 
111
        suppl.supported_currency.update({self.code : self})
 
112
 
 
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
 
126
        else:
 
127
            return self.cur_plural
 
128
 
 
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
 
142
        else:
 
143
            return self.frc_plural
 
144
 
 
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("__"):
 
150
                yield name[:-3]
 
151
        if to_import == 'language':
 
152
            if len(name) == 5 and not name.startswith("__"):
 
153
                yield name
 
154
 
 
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)
 
159
 
 
160
import ctt_languages
 
161
import_submodules('language', ctt_languages, 0)