Completed
Push — master ( 9d90ba...7d87fd )
by P.R.
01:17
created

SimpleConditionFactory   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 63
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 7
c 1
b 0
f 0
dl 0
loc 63
rs 10

3 Methods

Rating   Name   Duplication   Size   Complexity  
A create_condition() 0 15 2
A register_scheme() 0 15 3
A _split_scheme() 0 18 2
1
"""
2
ETLT
3
4
Copyright 2016 Set Based IT Consultancy
5
6
Licence MIT
7
"""
8
import re
9
10
from etlt.condition.GlobCondition import GlobCondition
11
from etlt.condition.PlainCondition import PlainCondition
12
from etlt.condition.RegularExpressionCondition import RegularExpressionCondition
13
from etlt.condition.TrueCondition import TrueCondition
14
15
16
class SimpleConditionFactory:
17
    """
18
    A factory for simple conditions.
19
    """
20
    _constructors = {}
21
    """
22
    A map from scheme to object constructors.
23
24
    dict[str, callable]
25
    """
26
    # ------------------------------------------------------------------------------------------------------------------
27
    @staticmethod
28
    def _split_scheme(expression):
29
        """
30
        Splits the scheme and actual expression
31
32
        :param str expression: The expression.
33
34
        :rtype: str
35
        """
36
        match = re.match(r'/^([a-z]+):(.*)/', expression)
37
        if not match:
38
            scheme = 'plain'
39
            actual = expression
40
        else:
41
            scheme = match.group(1)
42
            actual = match.group(2)
43
44
        return scheme, actual
45
46
    # ------------------------------------------------------------------------------------------------------------------
47
    @staticmethod
48
    def register_scheme(scheme, constructor):
49
        """
50
        Registers a scheme.
51
52
        :param str scheme: The scheme.
53
        :param callable constructor: The SimpleCondition constructor.
54
        """
55
        if not re.match(r'/^([a-z]+)$/', scheme):
56
            raise ValueError('%s is not a valid scheme' % scheme)
57
58
        if scheme in SimpleConditionFactory._constructors:
59
            raise ValueError('Scheme %s is registered already' % scheme)
60
61
        SimpleConditionFactory._constructors[scheme] = constructor
62
63
    # ------------------------------------------------------------------------------------------------------------------
64
    @staticmethod
65
    def create_condition(field, expression):
66
        """
67
68
        :param str field: The name of the field.
69
        :param expression: The expression (including scheme).
70
71
        :rtype: gdwh.map.SimpleCondition.SimpleCondition
72
        """
73
        scheme, actual = SimpleConditionFactory._split_scheme(expression)
74
75
        if scheme not in SimpleConditionFactory._constructors:
76
            raise ValueError('Scheme %s is not registered' % scheme)
77
78
        return SimpleConditionFactory._constructors[scheme](field, actual)
79
80
# ----------------------------------------------------------------------------------------------------------------------
81
SimpleConditionFactory.register_scheme('glob', GlobCondition.__init__)
82
SimpleConditionFactory.register_scheme('plain', PlainCondition.__init__)
83
SimpleConditionFactory.register_scheme('re', RegularExpressionCondition.__init__)
84
SimpleConditionFactory.register_scheme('true', TrueCondition.__init__)
85