From: Henryk Plötz Date: Thu, 9 Aug 2018 22:39:58 +0000 (+0200) Subject: Implement more generic Field types X-Git-Tag: v2.0.0~1^2~132 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=c8ac15bfbe066cb2772f3aa7016bd131956ab8ba;p=thirdparty%2Fpython-fints.git Implement more generic Field types Cleanup warnings in test --- diff --git a/fints/formals.py b/fints/formals.py index f25ff79..71adfc5 100644 --- a/fints/formals.py +++ b/fints/formals.py @@ -275,6 +275,7 @@ class DigitsField(FieldRenderFormatStringMixin, DataElementField): class FloatField(FieldRenderFormatStringMixin, DataElementField): type = 'float' + ## FIXME: Not implemented, no one uses this? class BinaryField(DataElementField): type = 'bin' @@ -287,6 +288,56 @@ class BinaryField(DataElementField): def _parse_value(self, value): return bytes(value) +class FixedLengthMixin: + FIXED_LENGTH = [None, None, None] + + def __init__(self, *args, **kwargs): + for i, a in enumerate(('length', 'min_length', 'max_length')): + kwargs[a] = self.FIXED_LENGTH[i] if len(self.FIXED_LENGTH) > i else None + + super().__init__(*args, **kwargs) + +class IDField(FixedLengthMixin, AlphanumericField): + type = 'id' + FIXED_LENGTH = [None, None, 30] + +class BooleanField(FixedLengthMixin, AlphanumericField): + type = 'jn' + FIXED_LENGTH = [1] + + def _render_field(self, value): + return "J" if value else "N" + + def _parse_field(self, value): + if value is None: + return None + if value == "J": + return True + elif value == "N": + return False + else: + raise ValueError("Invalid value {!r} for BooleanField".format(value)) + +class CodeField(AlphanumericField): + type = 'code' + + ## FIXME: Not further implemented, might want to use Enums + +class CountryField(FixedLengthMixin, DigitsField): + type = 'ctr' + FIXED_LENGTH = [3] + +class CurrencyField(FixedLengthMixin, AlphanumericField): + type = 'cur' + FIXED_LENGTH = [3] + +class DateField(FixedLengthMixin, NumericField): + type = 'dat' + FIXED_LENGTH = [8] + +class TimeField(FixedLengthMixin, DigitsField): + type = 'tim' + FIXED_LENGTH = [6] class SegmentSequence: def __init__(self, segments = None): diff --git a/tests/test_formals.py b/tests/test_formals.py index 68590fb..90b60de 100644 --- a/tests/test_formals.py +++ b/tests/test_formals.py @@ -331,4 +331,5 @@ def test_container_generic(): assert i1.a - i2 = A(a=[]) + with pytest.warns(UserWarning, match=r'Generic field used for type None value \[\]'): + i2 = A(a=[])