Repository navigation
Expand file tree
/
Copy pathcondition.py
More file actions
136 lines (108 loc) · 4.15 KB
/
Copy pathcondition.py
File metadata and controls
136 lines (108 loc) · 4.15 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
"""Split conditions module."""
from enum import Enum
from splitio.models import MatcherNotFoundException
from splitio.models.grammar import matchers
from splitio.models.grammar import partitions
_MATCHER_COMBINERS = {
'AND': lambda ms, k, a, c: all(m.evaluate(k, a, c) for m in ms)
}
class ConditionType(Enum):
"""Feature Flag possible condition types."""
WHITELIST = 'WHITELIST'
ROLLOUT = 'ROLLOUT'
class Condition(object):
"""Condition object class."""
def __init__( # pylint: disable=too-many-arguments
self,
matcher_list,
combiner, parts, label,
condition_type=ConditionType.WHITELIST
):
"""
Class constructor.
:param matcher: A combining matcher
:type matcher: CombiningMatcher
:param parts: A list of partitions
:type parts: list
"""
self._matchers = matcher_list
self._combiner = combiner
self._partitions = tuple(parts)
self._label = label
self._condition_type = condition_type
@property
def matchers(self):
"""Return the list of matchers associated to the condition."""
return self._matchers
@property
def partitions(self):
"""Return the list of partitions associated with the condition."""
return self._partitions
@property
def label(self):
"""Return the label of this condition."""
return self._label
@property
def condition_type(self):
"""Return the condition type."""
return self._condition_type
def matches(self, key, attributes=None, context=None):
"""
Check whether the condition matches against user submitted input.
:param key: User key
:type key: splitio.client.key.Key
:param attributes: User custom attributes.
:type attributes: dict
:param context: Evaluation context
:type context: dict
"""
return self._combiner(self._matchers, key, attributes, context)
def get_segment_names(self):
"""
Fetch segment names for all IN_SEGMENT matchers.
:return: List of segment names
:rtype: list(str)
"""
return [
matcher._segment_name for matcher in self.matchers # pylint: disable=protected-access
if isinstance(matcher, matchers.UserDefinedSegmentMatcher)
]
def __str__(self):
"""Return the string representation of the condition."""
return '{matcher} then split {parts}'.format(
matcher=self._matchers, parts=','.join(
'{size}:{treatment}'.format(size=partition.size,
treatment=partition.treatment)
for partition in self._partitions))
def to_json(self):
"""Return the JSON representation of this condition."""
return {
'conditionType': self._condition_type.name,
'label': self._label,
'matcherGroup': {
'combiner': next(
(k, v) for k, v in _MATCHER_COMBINERS.items() if v == self._combiner
)[0],
'matchers': [m.to_json() for m in self.matchers]
},
'partitions': [p.to_json() for p in self.partitions]
}
def from_raw(raw_condition):
"""
Parse a condition from a JSON portion of splitChanges.
:param raw_condition: JSON object extracted from a feature flag's conditions array.
:type raw_condition: dict
:return: A condition object.
:rtype: Condition
"""
parsed_partitions = []
if raw_condition.get("partitions") is not None:
parsed_partitions = [
partitions.from_raw(raw_partition)
for raw_partition in raw_condition['partitions']
]
matcher_objects = [matchers.from_raw(x) for x in raw_condition['matcherGroup']['matchers']]
combiner = _MATCHER_COMBINERS[raw_condition['matcherGroup']['combiner']]
label = raw_condition.get('label')
condition_type = ConditionType(raw_condition.get('conditionType', ConditionType.WHITELIST))
return Condition(matcher_objects, combiner, parsed_partitions, label, condition_type)