~sergio-incaser/banking-addons/banking-addons

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
# -*- encoding: utf-8 -*-
##############################################################################
#
#    Copyright (C) 2009 EduSense BV (<http://www.edusense.nl>).
#    All Rights Reserved
#
#    This program is free software: you can redistribute it and/or modify
#    it under the terms of the GNU Affero General Public License as published by
#    the Free Software Foundation, either version 3 of the License, or
#    (at your option) any later version.
#
#    This program is distributed in the hope that it will be useful,
#    but WITHOUT ANY WARRANTY; without even the implied warranty of
#    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#    GNU Affero General Public License for more details.
#
#    You should have received a copy of the GNU Affero General Public License
#    along with this program.  If not, see <http://www.gnu.org/licenses/>.
#
##############################################################################

__all__ = [
    'Field', 'Filler', 'DateField', 'NumberField', 'RightAlignedField',
    'RecordType', 'Record', 'asciify'
]

__doc__ = '''Ease working with fixed length records in files'''

from datetime import datetime, date

# Correct python2.4 issues
try:
    datetime.strptime
    def strpdate(str, format):
        return datetime.strptime(str, format).date()
except AttributeError:
    import time
    def strpdate(str, format):
        tm = time.strptime(str, format)
        return date(tm.tm_year, tm.tm_mon, tm.tm_mday)

import unicodedata

class Field(object):
    '''Base Field class - fixed length left aligned string field in a record'''
    def __init__(self, name, length=1, fillchar=' ', cast=str):
        self.name = name.replace(' ', '_')
        self.length = length
        self.fillchar = fillchar
        self.cast = cast

    def format(self, value):
        value = self.cast(value)
        if len(value) > self.length:
            return value[:self.length]
        return value.ljust(self.length, self.fillchar)

    def take(self, buffer):
        offset = hasattr(self, 'offset') and self.offset or 0
        return self.cast(buffer[offset:offset + self.length].rstrip(self.fillchar))

    def __repr__(self):
        return '%s "%s"' % (self.__class__.__name__, self.name)

class Filler(Field):
    '''Constant value field'''
    def __init__(self, name, length=1, value=' '):
        super(Filler, self).__init__(name, length, cast=str)
        self.value = str(value)

    def take(self, buffer):
        return self.format(buffer)

    def format(self, value):
        return super(Filler, self).format(
            self.value * (self.length / len(self.value) +1)
        )

class DateField(Field):
    '''Variable date field'''
    def __init__(self, name, format='%Y-%m-%d', auto=False, cast=str):
        length = len(date.today().strftime(format))
        super(DateField, self).__init__(name, length, cast=cast)
        self.dateformat = format
        self.auto = auto

    def format(self, value):
        if isinstance(value, (str, unicode)) and \
           len(value.strip()) == self.length:
            value = strpdate(value, self.dateformat)
        elif not isinstance(value, (datetime, date)):
            value = date.today()
        return value.strftime(self.dateformat)

    def take(self, buffer):
        value = super(DateField, self).take(buffer)
        if value:
            return strpdate(value, self.dateformat)
        return self.auto and date.today() or None

class RightAlignedField(Field):
    '''Deviation of Field: right aligned'''
    def format(self, value):
        if len(value) > self.length:
            return value[-self.length:]
        return value.rjust(self.length, self.fillchar)

    def take(self, buffer):
        offset = hasattr(self, 'offset') and self.offset or 0
        return self.cast(buffer[offset:offset + self.length].lstrip(self.fillchar))

class NumberField(RightAlignedField):
    '''Deviation of Field: left zero filled'''
    def __init__(self, *args, **kwargs):
        kwargs['fillchar'] = '0'
        super(NumberField, self).__init__(*args, **kwargs)

    def format(self, value):
        return super(NumberField, self).format(self.cast(value or ''))

class RecordType(object):
    fields = []

    def __init__(self, fields=[]):
        if fields:
            self.fields = fields
        offset = 0
        for field in self.fields:
            field.offset = offset
            offset += field.length

    def __len__(self):
        return reduce(lambda x,y: x+y.length, self.fields, 0)

    def __contains__(self, key):
        return any(lambda x, y=key: x.name == y, self.fields)

    def __getitem__(self, key):
        for field in self.fields:
            if field.name == key:
                return field
        raise KeyError, 'No such field: %s' % key

    def format(self, buffer):
        result = []
        for field in self.fields:
            result.append(field.format(field.take(buffer)))
        return ''.join(result)

    def take(self, buffer):
        return dict(zip([x.name for x in self.fields],
                        [x.take(buffer) for x in self.fields]
                       ))

class Record(object):
    _recordtype = None

    def __init__(self, recordtype=None, value=''):
        if hasattr(self, '_fields') and self._fields:
            self._recordtype = RecordType(self._fields)
        if not self._recordtype and not recordtype:
            raise ValueError, 'No recordtype specified'
        if not self._recordtype:
            self._recordtype = recordtype()
        self._length = len(self._recordtype)
        self._value = value.ljust(self._length)[:self._length]
    
    def __len__(self):
        return self._length

    def __setattr__(self, attr, value):
        if attr.startswith('_'):
            super(Record, self).__setattr__(attr, value)
        else:
            field = self._recordtype[attr]
            self._value = self._value[:field.offset] + \
                    field.format(value) + \
                    self._value[field.offset + field.length:]

    def __getattr__(self, attr):
        if attr.startswith('_'):
            return super(Record, self).__getattr__(attr)
        field = self._recordtype[attr]
        return field.take(self._value)
    
    def __str__(self):
        return self._recordtype.format(self._value)

    def __unicode__(self):
        return unicode(self.cast(self))

def asciify(str):
    return unicodedata.normalize('NFKD', str).encode('ascii', 'ignore')

# vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4: