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