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
|
|
|
|