From: Henryk Plötz Date: Sun, 12 Aug 2018 22:52:45 +0000 (+0200) Subject: Move Mixins to utils to get them out of the autodoc X-Git-Tag: v2.0.0~1^2~109 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=aeacd83658add4e6fcb1944740e2d07d5cc9b50e;p=thirdparty%2Fpython-fints.git Move Mixins to utils to get them out of the autodoc --- diff --git a/docs/developer.rst b/docs/developer.rst index 1d50781..5bd9943 100644 --- a/docs/developer.rst +++ b/docs/developer.rst @@ -30,7 +30,7 @@ Example usage: .. note:: - In general parsing followed by serialization is not idempotent: A message may contain empty list elements at the end, but our parser will never generate them. + In general parsing followed by serialization is not idempotent: A message may contain empty list elements at the end, but our serializer will never generate them. FinTS Segment Sequence ---------------------- diff --git a/fints/fields.py b/fints/fields.py index 3ab38de..e366420 100644 --- a/fints/fields.py +++ b/fints/fields.py @@ -3,7 +3,10 @@ import warnings from contextlib import suppress import fints.types -from fints.utils import Password, SubclassesMixin +from fints.utils import ( + DocTypeMixin, FieldRenderFormatStringMixin, + FixedLengthMixin, Password, SubclassesMixin, +) class Field: @@ -104,39 +107,9 @@ class TypedField(Field, SubclassesMixin): self.type = type or getattr(self.__class__, 'type', None) -class DocTypeMixin: - _DOC_TYPE = None - - def __init__(self, *args, **kwargs): - super().__init__(*args, **kwargs) - - type_ = self._DOC_TYPE - if type_ is None: - if isinstance(getattr(self, 'type', None), type): - type_ = getattr(self, 'type') - - if type_ is not None: - if not self.__doc__: - self.__doc__ = "" - - name = type_.__name__ - if type_.__module__ != 'builtins': - name = "{}.{}".format(type_.__module__, name) - - self.__doc__ = self.__doc__ + "\n\n:type: :class:`{}`".format(name) - class DataElementField(DocTypeMixin, TypedField): pass -class FieldRenderFormatStringMixin: - _FORMAT_STRING = None - - def _render_value(self, value): - retval = self._FORMAT_STRING.format(value) - self._check_value_length(retval) - - return retval - class ContainerField(TypedField): def _check_value(self, value): if self.type: @@ -233,16 +206,6 @@ class BinaryField(DataElementField): def _parse_value(self, value): return bytes(value) -class FixedLengthMixin: - _FIXED_LENGTH = [None, None, None] - _DOC_TYPE = str - - 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' _DOC_TYPE = str diff --git a/fints/formals.py b/fints/formals.py index 0ae9f13..4a65cbb 100644 --- a/fints/formals.py +++ b/fints/formals.py @@ -1,26 +1,10 @@ import re -from fints.types import * # The order is important! +from fints.types import * # The order is important! from fints.fields import * +from fints.utils import ShortReprMixin -class ShortReprMixin: - def __repr__(self): - return "{}{}({})".format( - "{}.".format(self.__class__.__module__), - self.__class__.__name__, - ", ".join( - ("{!r}".format(value) if not name.startswith("_") else "{}={!r}".format(name, value)) - for (name, value) in self._repr_items - ) - ) - - def print_nested(self, stream=None, level=0, indent=" ", prefix="", first_level_indent=True, trailer=""): - stream.write( - ( (prefix + level*indent) if first_level_indent else "") - + "{!r}{}\n".format(self, trailer) - ) - class DataElementGroup(Container): pass diff --git a/fints/utils.py b/fints/utils.py index 7e64a16..987847d 100644 --- a/fints/utils.py +++ b/fints/utils.py @@ -63,6 +63,67 @@ class SubclassesMixin: yield cls +class DocTypeMixin: + _DOC_TYPE = None + + def __init__(self, *args, **kwargs): + super().__init__(*args, **kwargs) + + type_ = self._DOC_TYPE + if type_ is None: + if isinstance(getattr(self, 'type', None), type): + type_ = getattr(self, 'type') + + if type_ is not None: + if not self.__doc__: + self.__doc__ = "" + + name = type_.__name__ + if type_.__module__ != 'builtins': + name = "{}.{}".format(type_.__module__, name) + + self.__doc__ = self.__doc__ + "\n\n:type: :class:`{}`".format(name) + + +class FieldRenderFormatStringMixin: + _FORMAT_STRING = None + + def _render_value(self, value): + retval = self._FORMAT_STRING.format(value) + self._check_value_length(retval) + + return retval + + +class FixedLengthMixin: + _FIXED_LENGTH = [None, None, None] + _DOC_TYPE = str + + 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 ShortReprMixin: + def __repr__(self): + return "{}{}({})".format( + "{}.".format(self.__class__.__module__), + self.__class__.__name__, + ", ".join( + ("{!r}".format(value) if not name.startswith("_") else "{}={!r}".format(name, value)) + for (name, value) in self._repr_items + ) + ) + + def print_nested(self, stream=None, level=0, indent=" ", prefix="", first_level_indent=True, trailer=""): + stream.write( + ( (prefix + level*indent) if first_level_indent else "") + + "{!r}{}\n".format(self, trailer) + ) + + class MT535_Miniparser: re_identification = re.compile(r"^:35B:ISIN\s(.*)\|(.*)\|(.*)$") re_marketprice = re.compile(r"^:90B::MRKT\/\/ACTU\/([A-Z]{3})(\d*),{1}(\d*)$")