1
|
1 |
|
from typing import Any, Dict, List |
2
|
1 |
|
|
3
|
|
|
from etlt.condition.Condition import Condition |
4
|
|
|
from etlt.condition.SimpleCondition import SimpleCondition |
5
|
1 |
|
from etlt.condition.SimpleConditionFactory import SimpleConditionFactory |
6
|
|
|
|
7
|
|
|
|
8
|
|
|
class InListCondition(Condition): |
9
|
|
|
""" |
10
|
|
|
A list condition matches a single field against a list of conditions. |
11
|
1 |
|
""" |
12
|
|
|
|
13
|
|
|
# ------------------------------------------------------------------------------------------------------------------ |
14
|
|
|
def __init__(self, field: str): |
15
|
|
|
""" |
16
|
|
|
Object contructor. |
17
|
1 |
|
|
18
|
|
|
:param str field: The name of the field in the row that must be match against the expression. |
19
|
|
|
""" |
20
|
|
|
self._field: str = field |
21
|
|
|
""" |
22
|
|
|
The name of the field in the row that must be match against the list of values. |
23
|
|
|
""" |
24
|
1 |
|
|
25
|
|
|
self._values: List[str] = list() |
26
|
|
|
""" |
27
|
|
|
The list of values of plain conditions. |
28
|
|
|
""" |
29
|
|
|
|
30
|
|
|
self._conditions: List[SimpleCondition] = list() |
31
|
1 |
|
""" |
32
|
1 |
|
The list of other conditions. |
33
|
|
|
""" |
34
|
|
|
|
35
|
|
|
# ------------------------------------------------------------------------------------------------------------------ |
36
|
|
|
@property |
37
|
|
|
def field(self) -> str: |
38
|
|
|
""" |
39
|
1 |
|
Getter for field. |
40
|
1 |
|
""" |
41
|
|
|
return self._field |
42
|
|
|
|
43
|
|
|
# ------------------------------------------------------------------------------------------------------------------ |
44
|
|
|
def populate_values(self, rows: List[Dict[str, Any]], field: str): |
45
|
|
|
""" |
46
|
|
|
Populates the filter values of this filter using list of rows. |
47
|
|
|
|
48
|
|
|
:param rows: The row set. |
49
|
1 |
|
:param field: The field name. |
50
|
|
|
""" |
51
|
|
|
self._values.clear() |
52
|
|
|
for row in rows: |
53
|
|
|
condition = SimpleConditionFactory.create_condition(self._field, row[field]) |
54
|
|
|
if condition.scheme == 'plain': |
55
|
|
|
self._values.append(condition.expression) |
56
|
1 |
|
else: |
57
|
1 |
|
self._conditions.append(condition) |
58
|
1 |
|
|
59
|
1 |
|
# ------------------------------------------------------------------------------------------------------------------ |
60
|
1 |
|
def match(self, row: Dict[str, Any]) -> bool: |
61
|
|
|
""" |
62
|
1 |
|
Returns whether the field is in the list of conditions. |
63
|
|
|
|
64
|
|
|
:param dict row: The row. |
65
|
1 |
|
""" |
66
|
|
|
if row[self._field] in self._values: |
67
|
|
|
return True |
68
|
|
|
|
69
|
|
|
for condition in self._conditions: |
70
|
|
|
if condition.match(row): |
71
|
|
|
return True |
72
|
|
|
|
73
|
1 |
|
return False |
74
|
1 |
|
|
75
|
|
|
# ---------------------------------------------------------------------------------------------------------------------- |
76
|
|
|
|