1
|
|
|
""" |
2
|
|
|
ETLT |
3
|
|
|
|
4
|
|
|
Copyright 2016 Set Based IT Consultancy |
5
|
|
|
|
6
|
|
|
Licence MIT |
7
|
|
|
""" |
8
|
1 |
|
from etlt.condition.SimpleConditionFactory import SimpleConditionFactory |
9
|
|
|
|
10
|
1 |
|
from etlt.condition.Condition import Condition |
11
|
|
|
|
12
|
|
|
|
13
|
1 |
|
class InListCondition(Condition): |
14
|
|
|
""" |
15
|
|
|
A list condition matches a single field against a list of conditions. |
16
|
|
|
""" |
17
|
|
|
|
18
|
|
|
# ------------------------------------------------------------------------------------------------------------------ |
19
|
1 |
|
def __init__(self, field): |
20
|
|
|
""" |
21
|
|
|
Object contructor. |
22
|
|
|
|
23
|
|
|
:param str field: The name of the field in the row that must be match against the expression. |
24
|
|
|
""" |
25
|
1 |
|
self._field = field |
26
|
|
|
""" |
27
|
|
|
The name of the field in the row that must be match against the list of values. |
28
|
|
|
|
29
|
|
|
:type: str |
30
|
|
|
""" |
31
|
|
|
|
32
|
1 |
|
self._values = list() |
33
|
|
|
""" |
34
|
|
|
The list of values of plain conditions. |
35
|
|
|
|
36
|
|
|
:type: list[str] |
37
|
|
|
""" |
38
|
|
|
|
39
|
1 |
|
self._conditions = list() |
40
|
1 |
|
""" |
41
|
|
|
The list of other conditions. |
42
|
|
|
|
43
|
|
|
:type: list[etlt.condition.SimpleCondition.SimpleCondition] |
44
|
|
|
""" |
45
|
|
|
|
46
|
|
|
# ------------------------------------------------------------------------------------------------------------------ |
47
|
1 |
|
@property |
48
|
|
|
def field(self): |
49
|
|
|
""" |
50
|
|
|
Getter for field. |
51
|
|
|
|
52
|
|
|
:rtype: str |
53
|
|
|
""" |
54
|
|
|
return self._field |
55
|
|
|
|
56
|
|
|
# ------------------------------------------------------------------------------------------------------------------ |
57
|
1 |
|
@property |
58
|
|
|
def values(self): |
59
|
|
|
""" |
60
|
|
|
Getter for values. |
61
|
|
|
|
62
|
|
|
:rtype: list[str] |
63
|
|
|
""" |
64
|
|
|
return self._values |
65
|
|
|
|
66
|
|
|
# ------------------------------------------------------------------------------------------------------------------ |
67
|
1 |
|
def populate_values(self, rows, field): |
68
|
|
|
""" |
69
|
|
|
Populates the filter values of this filter using list of rows. |
70
|
|
|
|
71
|
|
|
:param list[dict[str,T]] rows: The row set. |
72
|
|
|
:param str field: The field name. |
73
|
|
|
""" |
74
|
1 |
|
self._values.clear() |
75
|
1 |
|
for row in rows: |
76
|
1 |
|
condition = SimpleConditionFactory.create_condition(self._field, row[field]) |
77
|
1 |
|
if condition.scheme == 'plain': |
78
|
1 |
|
self._values.append(condition.expression) |
79
|
|
|
else: |
80
|
1 |
|
self._conditions.append(condition) |
81
|
|
|
|
82
|
|
|
# ------------------------------------------------------------------------------------------------------------------ |
83
|
1 |
|
def match(self, row): |
84
|
|
|
""" |
85
|
|
|
Returns True if the field is in the list of conditions. Returns False otherwise. |
86
|
|
|
|
87
|
|
|
:param dict row: The row. |
88
|
|
|
|
89
|
|
|
:rtype: bool |
90
|
|
|
""" |
91
|
1 |
|
if row[self._field] in self._values: |
92
|
1 |
|
return True |
93
|
|
|
|
94
|
1 |
|
for condition in self._conditions: |
95
|
1 |
|
if condition.match(row): |
96
|
1 |
|
return True |
97
|
|
|
|
98
|
1 |
|
return False |
99
|
|
|
|
100
|
|
|
# ---------------------------------------------------------------------------------------------------------------------- |
101
|
|
|
|