]> git.ipfire.org Git - thirdparty/python-fints.git/commitdiff
Allow querying by class instead of segment type name
authorHenryk Plötz <henryk@ploetzli.ch>
Sat, 11 Aug 2018 21:38:10 +0000 (23:38 +0200)
committerRaphael Michel <mail@raphaelmichel.de>
Mon, 3 Dec 2018 18:34:29 +0000 (19:34 +0100)
docs/developer.rst
fints/formals.py
tests/test_formals.py

index 40051c06cddc01f9b9022ff803650bd4136db580..a67d81528a183a4e4c187eefa6820f7fa1fb11cc 100644 (file)
@@ -53,6 +53,7 @@ All segment classes derive from  :class:`~fints.segments.FinTS3Segment`, which s
 .. autoclass:: fints.segments.FinTS3Segment
    :members:
    :inherited-members: print_nested
+   :member-order: bysource
 
    .. attribute:: TYPE
 
@@ -68,6 +69,7 @@ All segment classes derive from  :class:`~fints.segments.FinTS3Segment`, which s
 
 .. autoclass:: fints.formals.SegmentHeader
    :members:
+   :member-order: bysource
 
 The :class:`~fints.segments.FinTS3Segment` class and its base classes employ a number of dynamic programming techniques so that derived classes need only specify the name, order and type of fields and all type conversion, construction etc. will take place automatically. All derived classes basically should behave "as expected", returning only native Python datatypes.
 
@@ -177,4 +179,14 @@ ____________
     :members:
     :inherited-members:
     :exclude-members: print_nested
+    :member-order: bysource
 
+
+Field types
+___________
+
+.. automodule:: fints.formals
+    :members:
+    :undoc-members:
+    :exclude-members: print_nested
+    :member-order: bysource
index 69da3678a3b3567210128df99297cce42f7162bb..fd982ba513ae269718835048f35cce7dd6d33cdf 100644 (file)
@@ -386,10 +386,10 @@ class SegmentSequence:
             segment.print_nested(stream=stream, level=level+1, indent=indent, prefix=prefix, first_level_indent=True, trailer=",")
         stream.write( (prefix + level*indent) + "]){}\n".format(trailer) )
 
-    def find_segments(self, type=None, version=None, callback=None, recurse=True):
+    def find_segments(self, query=None, version=None, callback=None, recurse=True):
         """Yields an iterable of all matching segments.
 
-        :param type: Either a str specifying a segment type (such as 'HNHBK'), or a list or tuple of strings.
+        :param query: Either a str or class specifying a segment type (such as 'HNHBK', or :class:`~fints.segments.HNHBK3`), or a list or tuple of strings or classes.
                      If a list/tuple is specified, segments returning any matching type will be returned.
         :param version: Either an int specifying a segment version, or a list or tuple of ints.
                         If a list/tuple is specified, segments returning any matching version will be returned.
@@ -399,10 +399,10 @@ class SegmentSequence:
         The match results of all given parameters will be AND-combined.
         """
 
-        if type is None:
-            type = []
-        elif isinstance(type, str) or not isinstance(type, (list, tuple, Iterable)):
-            type = [type]
+        if query is None:
+            query = []
+        elif isinstance(query, str) or not isinstance(query, (list, tuple, Iterable)):
+            query = [query]
 
         if version is None:
             version = []
@@ -413,7 +413,7 @@ class SegmentSequence:
             callback = lambda s: True
 
         for s in self.segments:
-            if ((not type) or any(s.header.type == t for t in type)) and \
+            if ((not query) or any( (isinstance(s, t) if isinstance(t, type) else s.header.type == t) for t in query)) and \
                 ((not version) or any(s.header.version == v for v in version)) and \
                 callback(s):
                 yield s
@@ -423,7 +423,7 @@ class SegmentSequence:
                     if isinstance(field, SegmentSequenceField):
                         val = getattr(s, name)
                         if val:
-                            yield from val.find_segments(type=type, version=version, callback=callback, recurse=recurse)
+                            yield from val.find_segments(query=query, version=version, callback=callback, recurse=recurse)
 
     def find_segment_first(self, *args, **kwargs):
         """Finds the first matching segment.
index 3575d0b29bc86062984400efcf90109ff895c028..bae78ac1cfcb47775b9bc783f195d8c4b3dbc43d 100644 (file)
@@ -356,3 +356,14 @@ def test_find_1():
     assert len( m.find_segment_first('HITANS', 3).parameter.twostep_parameters ) == 6
 
     assert m.find_segment_first('HITANS', recurse=False) is None
+
+def test_find_by_class():
+    from conftest import TEST_MESSAGES
+    from fints.parser import FinTS3Parser
+    from fints.segments import HNHBS1
+
+    m = FinTS3Parser().parse_message(TEST_MESSAGES['basic_complicated'])
+
+    assert list(m.find_segments(HNHBS1))[0].__class__ == HNHBS1
+
+    assert m.find_segment_first(HNHBS1).header.type == 'HNHBS'