Passed
Push — issue844-exclude-rules ( 85c329...ecaff4 )
by Osma
03:24
created

annif.vocab.rules.kwargs_to_exclude_uris()   F

Complexity

Conditions 21

Size

Total Lines 36
Code Lines 34

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 21
eloc 34
nop 2
dl 0
loc 36
rs 0
c 0
b 0
f 0

How to fix   Complexity   

Complexity

Complex classes like annif.vocab.rules.kwargs_to_exclude_uris() often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.

Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.

1
"""Support for exclude/include rules for subject vocabularies"""
2
3
from rdflib import RDF, Graph, URIRef
4
from rdflib.namespace import SKOS
5
6
from annif.exception import ConfigurationException
7
8
9
def uris_by_type(graph: Graph, type: str) -> list[str]:
10
    return [str(uri) for uri in graph.subjects(RDF.type, URIRef(type))]
11
12
13
def uris_by_scheme(graph: Graph, type: str) -> list[str]:
14
    return [str(uri) for uri in graph.subjects(SKOS.inScheme, URIRef(type))]
15
16
17
def uris_by_collection(graph: Graph, type: str) -> list[str]:
18
    return [str(uri) for uri in graph.objects(URIRef(type), SKOS.member)]
19
20
21
def kwargs_to_exclude_uris(graph: Graph, kwargs: dict[str, str]) -> set[str]:
22
    exclude_uris = set()
23
    for key, value in kwargs.items():
24
        vals = value.split("|")
25
        if key == "exclude":
26
            if "*" in vals:
27
                exclude_uris.update(uris_by_type(graph, SKOS.Concept))
28
            else:
29
                exclude_uris.update(vals)
30
        elif key == "exclude_type":
31
            for val in vals:
32
                exclude_uris.update(uris_by_type(graph, val))
33
        elif key == "exclude_scheme":
34
            for val in vals:
35
                exclude_uris.update(uris_by_scheme(graph, val))
36
        elif key == "exclude_collection":
37
            for val in vals:
38
                exclude_uris.update(uris_by_collection(graph, val))
39
        elif key == "include":
40
            for val in vals:
41
                exclude_uris.remove(val)
42
        elif key == "include_type":
43
            for val in vals:
44
                for uri in uris_by_type(graph, val):
45
                    exclude_uris.remove(uri)
46
        elif key == "include_scheme":
47
            for val in vals:
48
                for uri in uris_by_scheme(graph, val):
49
                    exclude_uris.remove(uri)
50
        elif key == "include_collection":
51
            for val in vals:
52
                for uri in uris_by_collection(graph, val):
53
                    exclude_uris.remove(uri)
54
        else:
55
            raise ConfigurationException(f"unknown vocab keyword argument {key}")
56
    return exclude_uris
57