Passed
Pull Request — master (#496)
by
unknown
03:06
created

annif.transformer   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 43
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 33
dl 0
loc 43
rs 10
c 0
b 0
f 0
wmc 6

2 Functions

Rating   Name   Duplication   Size   Complexity  
A parse_specs() 0 14 3
A get_transform() 0 10 3
1
# TODO Add docstring
2
import re
3
from . import transformer
4
from . import inputlimiter
5
from .transformer import IdentityTransform
6
from annif.util import parse_args
7
from annif.exception import ConfigurationException
8
9
__all__ = ["IdentityTransform"]
10
11
12
def parse_specs(transform_specs):
13
    """parse a configuration definition such as 'A(x),B(y=1),C' into a tuples
14
    of ((A, [x], {}), (B, [None], {y: 1}))..."""  # TODO
15
    parsed = []
16
    # Split by commas not inside parentheses
17
    parts = re.split(r',\s*(?![^()]*\))', transform_specs)
18
    for part in parts:
19
        match = re.match(r'(\w+)(\((.*)\))?', part)
20
        if match is None:
21
            continue
22
        transform = match.group(1)
23
        posargs, kwargs = parse_args(match.group(3))
24
        parsed.append((transform, posargs, kwargs))
25
    return parsed
26
27
28
def get_transform(transform_specs, project):
29
    transform_defs = parse_specs(transform_specs)
30
    transform_classes = []
31
    args = []
32
    for trans, posargs, kwargs in transform_defs:
33
        if trans not in _transforms:
34
            raise ConfigurationException(f"No such transform {trans}")
35
        transform_classes.append(_transforms[trans])
36
        args.append((posargs, kwargs))
37
    return transformer.TransformChain(transform_classes, args, project)
38
39
40
_transforms = {
41
    transformer.IdentityTransform.name: transformer.IdentityTransform,
42
    inputlimiter.InputLimiter.name: inputlimiter.InputLimiter}
43