-
Notifications
You must be signed in to change notification settings - Fork 81
/
Copy pathfields.py
315 lines (227 loc) · 8.33 KB
/
fields.py
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
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
import datetime
import decimal
import re
import warnings
from fints.types import Container, SegmentSequence, TypedField
from fints.utils import (
DocTypeMixin, FieldRenderFormatStringMixin, FixedLengthMixin, Password,
)
class DataElementField(DocTypeMixin, TypedField):
pass
class ContainerField(TypedField):
def _check_value(self, value):
if self.type:
if not isinstance(value, self.type):
raise TypeError("Value {!r} is not of type {!r}".format(value, self.type))
super()._check_value(value)
def _default_value(self):
return self.type()
class DataElementGroupField(DocTypeMixin, ContainerField):
pass
class GenericField(FieldRenderFormatStringMixin, DataElementField):
type = None
_FORMAT_STRING = "{}"
def _parse_value(self, value):
warnings.warn("Generic field used for type {!r} value {!r}".format(self.type, value))
return value
class GenericGroupField(DataElementGroupField):
type = None
def _default_value(self):
if self.type is None:
return Container()
else:
return self.type()
def _parse_value(self, value):
if self.type is None:
warnings.warn("Generic field used for type {!r} value {!r}".format(self.type, value))
return value
class TextField(FieldRenderFormatStringMixin, DataElementField):
type = 'txt'
_DOC_TYPE = str
_FORMAT_STRING = "{}" # FIXME Restrict CRLF
def _parse_value(self, value): return str(value)
class AlphanumericField(TextField):
type = 'an'
class DTAUSField(DataElementField):
type = 'dta'
class NumericField(FieldRenderFormatStringMixin, DataElementField):
type = 'num'
_DOC_TYPE = int
_FORMAT_STRING = "{:d}"
def _parse_value(self, value):
_value = str(value)
if len(_value) > 1 and _value[0] == '0':
raise ValueError("Leading zeroes not allowed for value of type 'num': {!r}".format(value))
return int(_value, 10)
class ZeroPaddedNumericField(NumericField):
type = ''
_DOC_TYPE = int
def __init__(self, *args, **kwargs):
if not kwargs.get('length', None):
raise ValueError("ZeroPaddedNumericField needs length argument")
super().__init__(*args, **kwargs)
@property
def _FORMAT_STRING(self):
return "{:0" + str(self.length) + "d}"
def _parse_value(self, value):
_value = str(value)
return int(_value, 10)
class DigitsField(FieldRenderFormatStringMixin, DataElementField):
type = 'dig'
_DOC_TYPE = str
_FORMAT_STRING = "{}"
def _parse_value(self, value):
_value = str(value)
if not re.match(r'^\d*$', _value):
raise TypeError("Only digits allowed for value of type 'dig': {!r}".format(value))
return _value
class FloatField(DataElementField):
type = 'float'
_DOC_TYPE = float
_FORMAT_STRING = "{:.12f}" # Warning: Python's float is not exact!
# FIXME: Needs test
def _parse_value(self, value):
if isinstance(value, float):
return value
if isinstance(value, decimal.Decimal):
value = str(value.normalize()).replace(".", ",")
_value = str(value)
if not re.match(r'^(?:0|[1-9]\d*),(?:\d*[1-9]|)$', _value):
raise TypeError("Only digits and ',' allowed for value of type 'float', no superfluous leading or trailing zeroes allowed: {!r}".format(value))
return float(_value.replace(",", "."))
def _render_value(self, value):
retval = self._FORMAT_STRING.format(value)
retval = retval.replace('.', ',').rstrip('0')
self._check_value_length(retval)
return retval
class AmountField(FixedLengthMixin, DataElementField):
type = 'wrt'
_DOC_TYPE = decimal.Decimal
_FIXED_LENGTH = [None, None, 15]
# FIXME Needs test
def _parse_value(self, value):
if isinstance(value, float):
return value
if isinstance(value, decimal.Decimal):
return value
_value = str(value)
if not re.match(r'^(?:0|[1-9]\d*)(?:,)?(?:\d*[1-9]|)$', _value):
raise TypeError("Only digits and ',' allowed for value of type 'decimal', no superfluous leading or trailing zeroes allowed: {!r}".format(value))
return decimal.Decimal(_value.replace(",", "."))
def _render_value(self, value):
retval = str(value)
retval = retval.replace('.', ',').rstrip('0')
self._check_value_length(retval)
return retval
class BinaryField(DataElementField):
type = 'bin'
_DOC_TYPE = bytes
def _render_value(self, value):
retval = bytes(value)
self._check_value_length(retval)
return retval
def _parse_value(self, value): return bytes(value)
class IDField(FixedLengthMixin, AlphanumericField):
type = 'id'
_DOC_TYPE = str
_FIXED_LENGTH = [None, None, 30]
class BooleanField(FixedLengthMixin, AlphanumericField):
type = 'jn'
_DOC_TYPE = bool
_FIXED_LENGTH = [1]
def _render_value(self, value):
return "J" if value else "N"
def _parse_value(self, value):
if value is None:
return None
if value == "J" or value is True:
return True
elif value == "N" or value is False:
return False
else:
raise ValueError("Invalid value {!r} for BooleanField".format(value))
class CodeFieldMixin:
# FIXME Need tests
def __init__(self, enum=None, *args, **kwargs):
if enum:
self._DOC_TYPE = enum
self._enum = enum
else:
self._enum = None
super().__init__(*args, **kwargs)
def _parse_value(self, value):
retval = super()._parse_value(value)
if self._enum:
retval = self._enum(retval)
return retval
def _render_value(self, value):
retval = value
if self._enum:
retval = str(value.value)
return super()._render_value(retval)
def _inline_doc_comment(self, value):
retval = super()._inline_doc_comment(value)
if self._enum:
addendum = value.__doc__
if addendum and addendum is not value.__class__.__doc__:
if not retval:
retval = " # "
else:
retval = retval + ": "
retval = retval + addendum
return retval
class CodeField(CodeFieldMixin, AlphanumericField):
type = 'code'
_DOC_TYPE = str
class IntCodeField(CodeFieldMixin, NumericField):
type = ''
_DOC_TYPE = int
_FORMAT_STRING = "{}"
class CountryField(FixedLengthMixin, DigitsField):
type = 'ctr'
_FIXED_LENGTH = [3]
class CurrencyField(FixedLengthMixin, AlphanumericField):
type = 'cur'
_FIXED_LENGTH = [3]
class DateField(FixedLengthMixin, NumericField):
type = 'dat' # FIXME Need test
_DOC_TYPE = datetime.date
_FIXED_LENGTH = [8]
def _parse_value(self, value):
if isinstance(value, datetime.date):
return value
val = super()._parse_value(value)
val = str(val)
return datetime.date(int(val[0:4]), int(val[4:6]), int(val[6:8]))
def _render_value(self, value):
val = "{:04d}{:02d}{:02d}".format(value.year, value.month, value.day)
val = int(val)
return super()._render_value(val)
class TimeField(FixedLengthMixin, DigitsField):
type = 'tim' # FIXME Need test
_DOC_TYPE = datetime.time
_FIXED_LENGTH = [6]
def _parse_value(self, value):
if isinstance(value, datetime.time):
return value
val = super()._parse_value(value)
return datetime.time(int(val[0:2]), int(val[2:4]), int(val[4:6]))
def _render_value(self, value):
val = "{:02d}{:02d}{:02d}".format(value.hour, value.minute, value.second)
return super()._render_value(val)
class PasswordField(AlphanumericField):
type = ''
_DOC_TYPE = Password
def _parse_value(self, value):
return Password(value)
def _render_value(self, value):
return str(value)
class SegmentSequenceField(DataElementField):
type = 'sf'
def _parse_value(self, value):
if isinstance(value, SegmentSequence):
return value
else:
return SegmentSequence(value)
def _render_value(self, value):
return value.render_bytes()