|
| 1 | +"""RuleBasedSegment module.""" |
| 2 | + |
| 3 | +import logging |
| 4 | + |
| 5 | +from splitio.models import MatcherNotFoundException |
| 6 | +from splitio.models.splits import _DEFAULT_CONDITIONS_TEMPLATE |
| 7 | +from splitio.models.grammar import condition |
| 8 | + |
| 9 | +_LOGGER = logging.getLogger(__name__) |
| 10 | + |
| 11 | +class RuleBasedSegment(object): |
| 12 | + """RuleBasedSegment object class.""" |
| 13 | + |
| 14 | + def __init__(self, name, traffic_yype_Name, change_number, status, conditions, excluded): |
| 15 | + """ |
| 16 | + Class constructor. |
| 17 | +
|
| 18 | + :param name: Segment name. |
| 19 | + :type name: str |
| 20 | + :param traffic_yype_Name: traffic type name. |
| 21 | + :type traffic_yype_Name: str |
| 22 | + :param change_number: change number. |
| 23 | + :type change_number: str |
| 24 | + :param status: status. |
| 25 | + :type status: str |
| 26 | + :param conditions: List of conditions belonging to the segment. |
| 27 | + :type conditions: List |
| 28 | + :param excluded: excluded objects. |
| 29 | + :type excluded: Excluded |
| 30 | + """ |
| 31 | + self._name = name |
| 32 | + self._traffic_yype_Name = traffic_yype_Name |
| 33 | + self._change_number = change_number |
| 34 | + self._status = status |
| 35 | + self._conditions = conditions |
| 36 | + self._excluded = excluded |
| 37 | + |
| 38 | + @property |
| 39 | + def name(self): |
| 40 | + """Return segment name.""" |
| 41 | + return self._name |
| 42 | + |
| 43 | + @property |
| 44 | + def traffic_yype_Name(self): |
| 45 | + """Return traffic type name.""" |
| 46 | + return self._traffic_yype_Name |
| 47 | + |
| 48 | + @property |
| 49 | + def change_number(self): |
| 50 | + """Return change number.""" |
| 51 | + return self._change_number |
| 52 | + |
| 53 | + @property |
| 54 | + def status(self): |
| 55 | + """Return status.""" |
| 56 | + return self._status |
| 57 | + |
| 58 | + @property |
| 59 | + def conditions(self): |
| 60 | + """Return conditions.""" |
| 61 | + return self._conditions |
| 62 | + |
| 63 | + @property |
| 64 | + def excluded(self): |
| 65 | + """Return excluded.""" |
| 66 | + return self._excluded |
| 67 | + |
| 68 | +def from_raw(raw_rule_based_segment): |
| 69 | + """ |
| 70 | + Parse a Rule based segment from a JSON portion of splitChanges. |
| 71 | +
|
| 72 | + :param raw_rule_based_segment: JSON object extracted from a splitChange's response |
| 73 | + :type raw_rule_based_segment: dict |
| 74 | +
|
| 75 | + :return: A parsed RuleBasedSegment object capable of performing evaluations. |
| 76 | + :rtype: RuleBasedSegment |
| 77 | + """ |
| 78 | + try: |
| 79 | + conditions = [condition.from_raw(c) for c in raw_rule_based_segment['conditions']] |
| 80 | + except MatcherNotFoundException as e: |
| 81 | + _LOGGER.error(str(e)) |
| 82 | + _LOGGER.debug("Using default conditions template for feature flag: %s", raw_rule_based_segment['name']) |
| 83 | + conditions = [condition.from_raw(_DEFAULT_CONDITIONS_TEMPLATE)] |
| 84 | + return RuleBasedSegment( |
| 85 | + raw_rule_based_segment['name'], |
| 86 | + raw_rule_based_segment['trafficTypeName'], |
| 87 | + raw_rule_based_segment['changeNumber'], |
| 88 | + raw_rule_based_segment['status'], |
| 89 | + conditions, |
| 90 | + Excluded(raw_rule_based_segment['excluded']['keys'], raw_rule_based_segment['excluded']['segments']) |
| 91 | + ) |
| 92 | + |
| 93 | +class Excluded(object): |
| 94 | + |
| 95 | + def __init__(self, keys, segments): |
| 96 | + """ |
| 97 | + Class constructor. |
| 98 | +
|
| 99 | + :param keys: List of excluded keys in a rule based segment. |
| 100 | + :type keys: List |
| 101 | + :param segments: List of excluded segments in a rule based segment. |
| 102 | + :type segments: List |
| 103 | + """ |
| 104 | + self._keys = keys |
| 105 | + self._segments = segments |
| 106 | + |
| 107 | + def get_excluded_keys(self): |
| 108 | + """Return excluded keys.""" |
| 109 | + return self._keys |
| 110 | + |
| 111 | + def get_excluded_segments(self): |
| 112 | + """Return excluded segments""" |
| 113 | + return self._segments |
0 commit comments