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