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