Code Duplication    Length = 25-25 lines in 2 locations

src/ocrd_page_user_methods/get_AllRegions.py 1 location

@@ 5-29 (lines=25) @@
2
def _region_class(self, x): # pylint: disable=unused-argument
3
    return x.__class__.__name__.replace('RegionType', '')
4
5
def _get_recursive_regions(self, regions, level, classes=None):
6
    from .constants import PAGE_REGION_TYPES  # pylint: disable=relative-beyond-top-level,import-outside-toplevel
7
    if level == 1:
8
        # stop recursion, filter classes
9
        if classes:
10
            return [r for r in regions if self._region_class(r) in classes]
11
        if regions and regions[0].__class__.__name__ == 'PageType':
12
            regions = regions[1:]
13
        return regions
14
    # find more regions recursively
15
    more_regions = []
16
    for region in regions:
17
        more_regions.append([])
18
        for class_ in PAGE_REGION_TYPES:
19
            if class_ == 'Map' and not isinstance(region, PageType): # pylint: disable=undefined-variable
20
                # 'Map' is not recursive in 2019 schema
21
                continue
22
            more_regions[-1] += getattr(region, 'get_{}Region'.format(class_))()
23
    if not any(more_regions):
24
        return self._get_recursive_regions(regions, 1, classes)
25
    ret = []
26
    for r, more in zip(regions, more_regions):
27
        ret.append(r)
28
        ret += self._get_recursive_regions(more, level - 1 if level else 0, classes)
29
    return self._get_recursive_regions(ret, 1, classes)
30
31
def _get_recursive_reading_order(self, rogroup):
32
    if isinstance(rogroup, (OrderedGroupType, OrderedGroupIndexedType)): # pylint: disable=undefined-variable

src/ocrd_models/ocrd_page_generateds.py 1 location

@@ 3728-3752 (lines=25) @@
3725
    def _region_class(self, x): # pylint: disable=unused-argument
3726
        return x.__class__.__name__.replace('RegionType', '')
3727
    
3728
    def _get_recursive_regions(self, regions, level, classes=None):
3729
        from .constants import PAGE_REGION_TYPES  # pylint: disable=relative-beyond-top-level,import-outside-toplevel
3730
        if level == 1:
3731
            # stop recursion, filter classes
3732
            if classes:
3733
                return [r for r in regions if self._region_class(r) in classes]
3734
            if regions and regions[0].__class__.__name__ == 'PageType':
3735
                regions = regions[1:]
3736
            return regions
3737
        # find more regions recursively
3738
        more_regions = []
3739
        for region in regions:
3740
            more_regions.append([])
3741
            for class_ in PAGE_REGION_TYPES:
3742
                if class_ == 'Map' and not isinstance(region, PageType): # pylint: disable=undefined-variable
3743
                    # 'Map' is not recursive in 2019 schema
3744
                    continue
3745
                more_regions[-1] += getattr(region, 'get_{}Region'.format(class_))()
3746
        if not any(more_regions):
3747
            return self._get_recursive_regions(regions, 1, classes)
3748
        ret = []
3749
        for r, more in zip(regions, more_regions):
3750
            ret.append(r)
3751
            ret += self._get_recursive_regions(more, level - 1 if level else 0, classes)
3752
        return self._get_recursive_regions(ret, 1, classes)
3753
    
3754
    def _get_recursive_reading_order(self, rogroup):
3755
        if isinstance(rogroup, (OrderedGroupType, OrderedGroupIndexedType)): # pylint: disable=undefined-variable