~eduardo-bayardo-bias/bias-trunk/bias_trunk

« back to all changes in this revision

Viewing changes to bias_fiscal_statements_v6/report_bak/rml_parse.py

  • Committer: Eduardo Bayardo
  • Date: 2011-03-25 22:50:09 UTC
  • mto: This revision was merged to the branch mainline in revision 169.
  • Revision ID: eduardo@eduardo-20110325225009-tm9qfude5sq51rpe
migración de bias_fiscal_statements a v6

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
# -*- encoding: utf-8 -*-
2
 
##############################################################################
3
 
#
4
 
#    OpenERP, Open Source Management Solution
5
 
#    Copyright (C) 2004-2009 Tiny SPRL (<http://tiny.be>). All Rights Reserved
6
 
#    $Id$
7
 
#
8
 
#    This program is free software: you can redistribute it and/or modify
9
 
#    it under the terms of the GNU General Public License as published by
10
 
#    the Free Software Foundation, either version 3 of the License, or
11
 
#    (at your option) any later version.
12
 
#
13
 
#    This program is distributed in the hope that it will be useful,
14
 
#    but WITHOUT ANY WARRANTY; without even the implied warranty of
15
 
#    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16
 
#    GNU General Public License for more details.
17
 
#
18
 
#    You should have received a copy of the GNU General Public License
19
 
#    along with this program.  If not, see <http://www.gnu.org/licenses/>.
20
 
#
21
 
##############################################################################
22
 
from report import report_sxw
23
 
import xml.dom.minidom
24
 
import os, time
25
 
import osv
26
 
import re
27
 
import tools
28
 
import pooler
29
 
import re
30
 
import sys
31
 
 
32
 
 
33
 
class rml_parse(report_sxw.rml_parse):
34
 
    def __init__(self, cr, uid, name, context):
35
 
        super(rml_parse, self).__init__(cr, uid, name, context=None)
36
 
        self.localcontext.update({
37
 
            'comma_me': self.comma_me,
38
 
            'format_date': self._get_and_change_date_format_for_swiss,
39
 
            'strip_name' : self._strip_name,
40
 
            'explode_name' : self._explode_name,
41
 
        })
42
 
 
43
 
    def comma_me(self,amount):
44
 
        #print "#" + str(amount) + "#"
45
 
        if not amount:
46
 
            amount = 0.0
47
 
        if  type(amount) is float :
48
 
            amount = str('%.2f'%amount)
49
 
        else :
50
 
            amount = str(amount)
51
 
        if (amount == '0'):
52
 
             return ' '
53
 
        orig = amount
54
 
        new = re.sub("^(-?\d+)(\d{3})", "\g<1>'\g<2>", amount)
55
 
        if orig == new:
56
 
            return new
57
 
        else:
58
 
            return self.comma_me(new)
59
 
    def _ellipsis(self, string, maxlen=100, ellipsis = '...'):
60
 
        ellipsis = ellipsis or ''
61
 
        try:
62
 
            return string[:maxlen - len(ellipsis) ] + (ellipsis, '')[len(string) < maxlen]
63
 
        except Exception, e:
64
 
            return False
65
 
    def _strip_name(self, name, maxlen=50):
66
 
        return self._ellipsis(name, maxlen, '...')
67
 
 
68
 
    def _get_and_change_date_format_for_swiss (self,date_to_format):
69
 
        date_formatted=''
70
 
        if date_to_format:
71
 
            date_formatted = strptime (date_to_format,'%Y-%m-%d').strftime('%d.%m.%Y')
72
 
        return date_formatted
73
 
 
74
 
    def _explode_name(self,chaine,length):
75
 
        # We will test if the size is less then account
76
 
        full_string = ''
77
 
        if (len(str(chaine)) <= length):
78
 
            return chaine
79
 
        #
80
 
        else:
81
 
            chaine = unicode(chaine,'utf8').encode('iso-8859-1')
82
 
            rup = 0
83
 
            for carac in chaine:
84
 
                rup = rup + 1
85
 
                if rup == length:
86
 
                    full_string = full_string + '\n'
87
 
                    full_string = full_string + carac
88
 
                    rup = 0
89
 
                else:
90
 
                    full_string = full_string + carac
91
 
 
92
 
        return full_string
93
 
 
94
 
    def makeAscii(self,str):
95
 
        try:
96
 
            Stringer = str.encode("utf-8")
97
 
        except UnicodeDecodeError:
98
 
            try:
99
 
                Stringer = str.encode("utf-16")
100
 
            except UnicodeDecodeError:
101
 
                print "UTF_16 Error"
102
 
                Stringer = str
103
 
            else:
104
 
                return Stringer
105
 
        else:
106
 
            return Stringer
107
 
        return Stringer
108
 
    def explode_this(self,chaine,length):
109
 
        #chaine = self.repair_string(chaine)
110
 
        chaine = rstrip(chaine)
111
 
        ast = list(chaine)
112
 
        i = length
113
 
        while i <= len(ast):
114
 
            ast.insert(i,'\n')
115
 
            i = i + length
116
 
        chaine = str("".join(ast))
117
 
        return chaine
118
 
    def repair_string(self,chaine):
119
 
        ast = list(chaine)
120
 
        UnicodeAst = []
121
 
        _previouslyfound = False
122
 
        i = 0
123
 
        #print str(ast)
124
 
        while i < len(ast):
125
 
            elem = ast[i]
126
 
            try:
127
 
                Stringer = elem.encode("utf-8")
128
 
            except UnicodeDecodeError:
129
 
                to_reencode = elem + ast[i+1]
130
 
                print str(to_reencode)
131
 
                Good_char = to_reencode.decode('utf-8')
132
 
                UnicodeAst.append(Good_char)
133
 
                i += i +2
134
 
            else:
135
 
                UnicodeAst.append(elem)
136
 
                i += i + 1
137
 
 
138
 
 
139
 
        return "".join(UnicodeAst)
140
 
 
141
 
    def ReencodeAscii(self,str):
142
 
        print sys.stdin.encoding
143
 
        try:
144
 
            Stringer = str.decode("ascii")
145
 
        except UnicodeEncodeError:
146
 
            print "REENCODING ERROR"
147
 
            return str.encode("ascii")
148
 
        except UnicodeDecodeError:
149
 
            print "DECODING ERROR"
150
 
            return str.encode("ascii")
151
 
 
152
 
        else:
153
 
            print Stringer
154
 
            return Stringer
155
 
 
156
 
 
157
 
    # def _add_header(self, node):
158
 
    #   rml_head = tools.file_open('specific_param/report/header/corporate_rml_header_ch.rml').read()
159
 
    #   head_dom = xml.dom.minidom.parseString(rml_head)
160
 
    #   #for frame in head_dom.getElementsByTagName('frame'):
161
 
    #   #   frame.parentNode.removeChild(frame)
162
 
    #   node2 = head_dom.documentElement
163
 
    #   for tag in node2.childNodes:
164
 
    #       if tag.nodeType==tag.ELEMENT_NODE:
165
 
    #           found = self._find_node(node, tag.localName)
166
 
    #   #       rml_frames = found.getElementsByTagName('frame')
167
 
    #           if found:
168
 
    #               if tag.hasAttribute('position') and (tag.getAttribute('position')=='inside'):
169
 
    #                   found.appendChild(tag)
170
 
    #               else:
171
 
    #                   found.parentNode.replaceChild(tag, found)
172
 
    #   #       for frame in rml_frames:
173
 
    #   #           tag.appendChild(frame)
174
 
    #   return True
175
 
 
176
 
 
177
 
 
178
 
# vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4: