wingify-fme-python-sdk 1.70.0__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (186) hide show
  1. tests/data/__init__.py +13 -0
  2. tests/data/dummy_test_data_reader.py +63 -0
  3. tests/data/user_storage.py +48 -0
  4. tests/e2e/__init__.py +13 -0
  5. tests/e2e/custom_bucketing_test.py +514 -0
  6. tests/e2e/force_users_test.py +368 -0
  7. tests/e2e/get_flag_test.py +195 -0
  8. tests/unit/__init__.py +13 -0
  9. tests/unit/packages/__init__.py +13 -0
  10. tests/unit/packages/decision_maker/__init__.py +13 -0
  11. tests/unit/packages/decision_maker/decision_maker_test.py +83 -0
  12. tests/unit/packages/segmentation_evaluator/__init__.py +13 -0
  13. tests/unit/packages/segmentation_evaluator/segmentation_evaluator_test.py +284 -0
  14. tests/unit/packages/segmentation_evaluator/segmentation_manager_test.py +215 -0
  15. tests/unit/packages/segmentation_evaluator/web_testing_segment_test.py +216 -0
  16. tests/unit/services/__init__.py +0 -0
  17. tests/unit/services/hooks_manager_test.py +80 -0
  18. tests/unit/services/internal_events_sampling_service_test.py +160 -0
  19. tests/unit/settings/__init__.py +13 -0
  20. tests/unit/settings/settings_schema_validation_test.py +67 -0
  21. tests/unit/utils/__init__.py +0 -0
  22. tests/unit/utils/campaign_util_test.py +248 -0
  23. tests/unit/utils/decision_util_force_users_test.py +790 -0
  24. tests/unit/utils/log_message_test.py +65 -0
  25. tests/unit/vwo_wingify_test.py +175 -0
  26. tests/unit/wingify_client_validation_test.py +72 -0
  27. vwo/__init__.py +35 -0
  28. vwo/models/__init__.py +13 -0
  29. vwo/models/batch_event_data.py +19 -0
  30. vwo/models/user/__init__.py +13 -0
  31. vwo/models/user/context_vwo_model.py +21 -0
  32. vwo/models/user/get_flag.py +19 -0
  33. vwo/models/vwo_options_model.py +21 -0
  34. vwo/packages/__init__.py +13 -0
  35. vwo/packages/logger/__init__.py +13 -0
  36. vwo/packages/logger/enums/__init__.py +13 -0
  37. vwo/packages/logger/enums/log_level_enum.py +19 -0
  38. vwo/packages/storage/__init__.py +13 -0
  39. vwo/packages/storage/connector.py +19 -0
  40. vwo/vwo.py +58 -0
  41. vwo/vwo_builder.py +21 -0
  42. vwo/vwo_client.py +22 -0
  43. wingify/__init__.py +36 -0
  44. wingify/api/__init__.py +13 -0
  45. wingify/api/get_flag_api.py +727 -0
  46. wingify/api/set_attribute_api.py +60 -0
  47. wingify/api/track_api.py +99 -0
  48. wingify/constants/Constants.py +94 -0
  49. wingify/constants/__init__.py +13 -0
  50. wingify/constants/sampled_debug_error_template_keys.py +34 -0
  51. wingify/decorators/__init__.py +13 -0
  52. wingify/decorators/storage_decorator.py +92 -0
  53. wingify/enums/__init__.py +13 -0
  54. wingify/enums/api_enum.py +26 -0
  55. wingify/enums/campaign_type_enum.py +22 -0
  56. wingify/enums/debug_category_enum.py +24 -0
  57. wingify/enums/event_enum.py +26 -0
  58. wingify/enums/headers_enum.py +21 -0
  59. wingify/enums/hooks_enum.py +24 -0
  60. wingify/enums/status_enum.py +21 -0
  61. wingify/enums/storage_enum.py +25 -0
  62. wingify/enums/url_enum.py +29 -0
  63. wingify/models/Alias.py +60 -0
  64. wingify/models/AliasSetResponse.py +43 -0
  65. wingify/models/__init__.py +13 -0
  66. wingify/models/batch_event_data.py +68 -0
  67. wingify/models/campaign/__init__.py +13 -0
  68. wingify/models/campaign/campaign_model.py +140 -0
  69. wingify/models/campaign/feature_model.py +95 -0
  70. wingify/models/campaign/holdout_model.py +94 -0
  71. wingify/models/campaign/impact_campaign_model.py +60 -0
  72. wingify/models/campaign/metric_model.py +42 -0
  73. wingify/models/campaign/rule_model.py +42 -0
  74. wingify/models/campaign/variable_model.py +44 -0
  75. wingify/models/campaign/variation_model.py +109 -0
  76. wingify/models/gateway_service_model.py +48 -0
  77. wingify/models/schemas/__init__.py +13 -0
  78. wingify/models/schemas/campaign_group_schema.py +19 -0
  79. wingify/models/schemas/campaign_metric_schema.py +26 -0
  80. wingify/models/schemas/campaign_schema.py +37 -0
  81. wingify/models/schemas/campaign_variation_schema.py +34 -0
  82. wingify/models/schemas/empty_object_schema.py +15 -0
  83. wingify/models/schemas/feature_schema.py +38 -0
  84. wingify/models/schemas/group_schema.py +37 -0
  85. wingify/models/schemas/holdout_schema.py +29 -0
  86. wingify/models/schemas/rule_schema.py +24 -0
  87. wingify/models/schemas/settings_schema.py +47 -0
  88. wingify/models/schemas/variable_schema.py +24 -0
  89. wingify/models/settings/__init__.py +13 -0
  90. wingify/models/settings/internal_event_sampling_settings_model.py +53 -0
  91. wingify/models/settings/settings_model.py +127 -0
  92. wingify/models/storage/__init__.py +13 -0
  93. wingify/models/storage/storage_model.py +84 -0
  94. wingify/models/user/__init__.py +13 -0
  95. wingify/models/user/context_model.py +113 -0
  96. wingify/models/user/context_vwo_model.py +34 -0
  97. wingify/models/user/get_flag.py +47 -0
  98. wingify/models/vwo_options_model.py +21 -0
  99. wingify/models/wingify_options_model.py +129 -0
  100. wingify/packages/__init__.py +13 -0
  101. wingify/packages/decision_maker/__init__.py +13 -0
  102. wingify/packages/decision_maker/decision_maker.py +89 -0
  103. wingify/packages/logger/__init__.py +13 -0
  104. wingify/packages/logger/core/__init__.py +13 -0
  105. wingify/packages/logger/core/log_manager.py +123 -0
  106. wingify/packages/logger/core/log_transport_manager.py +57 -0
  107. wingify/packages/logger/enums/__init__.py +13 -0
  108. wingify/packages/logger/enums/log_level_enum.py +24 -0
  109. wingify/packages/logger/enums/log_level_number_enum.py +21 -0
  110. wingify/packages/logger/log_message_builder.py +70 -0
  111. wingify/packages/logger/logger.py +38 -0
  112. wingify/packages/logger/transports/__init__.py +13 -0
  113. wingify/packages/logger/transports/console_transport.py +43 -0
  114. wingify/packages/network_layer/__init__.py +13 -0
  115. wingify/packages/network_layer/client/__init__.py +13 -0
  116. wingify/packages/network_layer/client/network_client.py +167 -0
  117. wingify/packages/network_layer/handlers/__init__.py +13 -0
  118. wingify/packages/network_layer/handlers/request_handler.py +40 -0
  119. wingify/packages/network_layer/manager/__init__.py +13 -0
  120. wingify/packages/network_layer/manager/network_manager.py +79 -0
  121. wingify/packages/network_layer/models/__init__.py +13 -0
  122. wingify/packages/network_layer/models/global_request_model.py +70 -0
  123. wingify/packages/network_layer/models/request_model.py +133 -0
  124. wingify/packages/network_layer/models/response_model.py +55 -0
  125. wingify/packages/segmentation_evaluator/__init__.py +13 -0
  126. wingify/packages/segmentation_evaluator/core/__init__.py +13 -0
  127. wingify/packages/segmentation_evaluator/core/segmentation_manager.py +140 -0
  128. wingify/packages/segmentation_evaluator/enums/__init__.py +13 -0
  129. wingify/packages/segmentation_evaluator/enums/segment_operand_regex_enum.py +31 -0
  130. wingify/packages/segmentation_evaluator/enums/segment_operand_value_enum.py +29 -0
  131. wingify/packages/segmentation_evaluator/enums/segment_operator_value_enum.py +37 -0
  132. wingify/packages/segmentation_evaluator/evaluators/__init__.py +13 -0
  133. wingify/packages/segmentation_evaluator/evaluators/segment_evaluator.py +237 -0
  134. wingify/packages/segmentation_evaluator/evaluators/segment_operand_evaluator.py +518 -0
  135. wingify/packages/segmentation_evaluator/utils/__init__.py +13 -0
  136. wingify/packages/segmentation_evaluator/utils/segment_util.py +33 -0
  137. wingify/packages/segmentation_evaluator/utils/web_testing_segment_util.py +295 -0
  138. wingify/packages/storage/__init__.py +13 -0
  139. wingify/packages/storage/connector.py +25 -0
  140. wingify/packages/storage/storage.py +39 -0
  141. wingify/resources/__init__.py +13 -0
  142. wingify/resources/debug-messages.json +21 -0
  143. wingify/resources/error-messages.json +62 -0
  144. wingify/resources/info-message.json +47 -0
  145. wingify/resources/trace-messages.json +1 -0
  146. wingify/resources/warn-messages.json +1 -0
  147. wingify/services/__init__.py +13 -0
  148. wingify/services/batch_event_queue.py +140 -0
  149. wingify/services/campaign_decision_service.py +268 -0
  150. wingify/services/hooks_manager.py +65 -0
  151. wingify/services/internal_events_sampling_service.py +62 -0
  152. wingify/services/settings_manager.py +293 -0
  153. wingify/services/storage_service.py +58 -0
  154. wingify/services/url_service.py +51 -0
  155. wingify/utils/__init__.py +13 -0
  156. wingify/utils/aliasing_util.py +108 -0
  157. wingify/utils/brand_context.py +26 -0
  158. wingify/utils/brand_util.py +37 -0
  159. wingify/utils/campaign_util.py +592 -0
  160. wingify/utils/data_type_util.py +125 -0
  161. wingify/utils/debugger_service_util.py +58 -0
  162. wingify/utils/decision_util.py +589 -0
  163. wingify/utils/event_util.py +85 -0
  164. wingify/utils/function_util.py +188 -0
  165. wingify/utils/gateway_service_util.py +179 -0
  166. wingify/utils/holdout_util.py +264 -0
  167. wingify/utils/impression_util.py +151 -0
  168. wingify/utils/internal_events_sampling_util.py +106 -0
  169. wingify/utils/log_message_util.py +95 -0
  170. wingify/utils/meg_util.py +561 -0
  171. wingify/utils/model_utils.py +136 -0
  172. wingify/utils/network_util.py +706 -0
  173. wingify/utils/rule_evaluation_util.py +102 -0
  174. wingify/utils/settings_util.py +39 -0
  175. wingify/utils/usage_stats_util.py +116 -0
  176. wingify/utils/uuid_util.py +81 -0
  177. wingify/vwo_client.py +21 -0
  178. wingify/wingify.py +159 -0
  179. wingify/wingify_builder.py +282 -0
  180. wingify/wingify_client.py +520 -0
  181. wingify_fme_python_sdk-1.70.0.dist-info/METADATA +1415 -0
  182. wingify_fme_python_sdk-1.70.0.dist-info/RECORD +186 -0
  183. wingify_fme_python_sdk-1.70.0.dist-info/WHEEL +5 -0
  184. wingify_fme_python_sdk-1.70.0.dist-info/licenses/LICENSE +202 -0
  185. wingify_fme_python_sdk-1.70.0.dist-info/licenses/NOTICE +18 -0
  186. wingify_fme_python_sdk-1.70.0.dist-info/top_level.txt +3 -0
@@ -0,0 +1,518 @@
1
+ # Copyright 2024-2026 Wingify Software Pvt. Ltd.
2
+ #
3
+ # Licensed under the Apache License, Version 2.0 (the "License");
4
+ # you may not use this file except in compliance with the License.
5
+ # You may obtain a copy of the License at
6
+ #
7
+ # http://www.apache.org/licenses/LICENSE-2.0
8
+ #
9
+ # Unless required by applicable law or agreed to in writing, software
10
+ # distributed under the License is distributed on an "AS IS" BASIS,
11
+ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
+ # See the License for the specific language governing permissions and
13
+ # limitations under the License.
14
+
15
+
16
+ import re
17
+ from typing import Dict, Any
18
+ from ..utils.segment_util import get_key_value, match_with_regex
19
+ from ..utils.web_testing_segment_util import (
20
+ coerce_campaign_variation_operand_to_str,
21
+ parse_web_testing_campaigns_from_context,
22
+ evaluate_web_testing_campaign_variation,
23
+ )
24
+ from ..enums.segment_operand_regex_enum import SegmentOperandRegexEnum
25
+ from ..enums.segment_operand_value_enum import SegmentOperandValueEnum
26
+ from ..enums.segment_operator_value_enum import SegmentOperatorValueEnum
27
+ from ...logger.core.log_manager import LogManager
28
+ from ....utils.gateway_service_util import get_from_gateway_service
29
+ from ....enums.url_enum import UrlEnum
30
+ from ....utils.data_type_util import is_boolean
31
+ from ....models.user.context_model import ContextModel
32
+ from ....services.settings_manager import SettingsManager
33
+ from ....enums.api_enum import ApiEnum
34
+
35
+ class SegmentOperandEvaluator:
36
+ def evaluate_custom_variable_dsl(
37
+ self, dsl_operand_value, properties: Dict[str, Any], context: ContextModel
38
+ ):
39
+ """
40
+ Evaluates a custom variable DSL expression.
41
+
42
+ :param dsl_operand_value: The DSL expression for the custom variable.
43
+ :param properties: The properties object containing the actual values to be matched against.
44
+ :return: A boolean indicating if the DSL condition is met.
45
+ """
46
+ key, value = get_key_value(dsl_operand_value)
47
+ operand_key = key
48
+ operand = value
49
+
50
+ if operand_key not in properties:
51
+ return False
52
+
53
+ if "inlist" in operand:
54
+ list_id_regex = r"inlist\([^)]*\)"
55
+ match = re.search(list_id_regex, operand)
56
+ if not match or len(match.groups()) < 1:
57
+ LogManager.get_instance().error_log("INVALID_ATTRIBUTE_LIST_FORMAT", debug_data={"an": ApiEnum.GET_FLAG.value, "uuid": context.get_vwo_uuid(), "sId": context.get_session_id()})
58
+ return False
59
+
60
+ tag_value = properties[operand_key]
61
+ attribute_value = self.pre_process_tag_value(tag_value)
62
+ list_id = match.group(1)
63
+ query_params_obj = {
64
+ "attribute": attribute_value,
65
+ "listId": list_id,
66
+ "accountId": SettingsManager.get_instance().account_id,
67
+ "sdkKey": SettingsManager.get_instance().sdk_key,
68
+ }
69
+
70
+ try:
71
+ res = get_from_gateway_service(
72
+ query_params_obj, UrlEnum.ATTRIBUTE_CHECK.value, context
73
+ )
74
+ if not res or res == "false":
75
+ return False
76
+ return res
77
+ except Exception as error:
78
+ LogManager.get_instance().error_log("ERROR_FETCHING_DATA_FROM_GATEWAY", data={"err": str(error)}, debug_data={"an": ApiEnum.GET_FLAG.value, "uuid": context.get_vwo_uuid(), "sId": context.get_session_id()})
79
+ return False
80
+ else:
81
+ tag_value = properties[operand_key]
82
+ tag_value = self.pre_process_tag_value(tag_value)
83
+ operand_type, operand_value = self.pre_process_operand_value(
84
+ operand
85
+ ).values()
86
+ processed_values = self.process_values(operand_value, tag_value)
87
+ tag_value = processed_values["tag_value"]
88
+ return self.extract_result(
89
+ operand_type, processed_values["operand_value"], tag_value
90
+ )
91
+
92
+ def evaluate_campaign_variation_dsl(
93
+ self, dsl_operand_value: Any, context: ContextModel
94
+ ) -> bool:
95
+ """
96
+ Evaluates a campaignVariation DSL operand against the user's web testing campaign assignments.
97
+
98
+ :param dsl_operand_value: The raw operand value from the DSL (e.g. "123_1", "!123", 100).
99
+ :param context: The context object containing platform_variables.web_testing_campaigns.
100
+ :return: True if the segment condition is satisfied, otherwise False.
101
+ """
102
+ operand_str = coerce_campaign_variation_operand_to_str(dsl_operand_value)
103
+ if operand_str is None:
104
+ return False
105
+
106
+ operand_str = operand_str.strip()
107
+ campaigns_map = parse_web_testing_campaigns_from_context(context)
108
+ result, _invalid = evaluate_web_testing_campaign_variation(operand_str, campaigns_map)
109
+ return result
110
+
111
+ def evaluate_user_dsl(self, dsl_operand_value, properties):
112
+ """
113
+ Evaluates a user DSL expression to check if a user ID is in a specified List.
114
+
115
+ :param dsl_operand_value: The DSL expression containing user IDs.
116
+ :param properties: The properties object containing the actual user ID to check.
117
+ :return: True if the user ID is in the List, otherwise False.
118
+ """
119
+ users = dsl_operand_value.split(",")
120
+ return any(user.strip() == properties.get("_vwoUserId") for user in users)
121
+
122
+ def evaluate_user_agent_dsl(self, dsl_operand_value, context: ContextModel):
123
+ """
124
+ Evaluates a user agent DSL expression.
125
+
126
+ :param dsl_operand_value: The DSL expression for the user agent.
127
+ :param context: The context object containing the user agent string.
128
+ :return: True if the user agent matches the DSL condition, otherwise False.
129
+ """
130
+ operand = dsl_operand_value
131
+ if not context.get_user_agent():
132
+ LogManager.get_instance().error_log("INVALID_USER_AGENT_IN_CONTEXT_FOR_PRE_SEGMENTATION", debug_data={"an": ApiEnum.GET_FLAG.value, "uuid": context.get_vwo_uuid(), "sId": context.get_session_id()})
133
+ return False
134
+
135
+ tag_value = context.get_user_agent()
136
+ tag_value = self.pre_process_tag_value(tag_value)
137
+ operand_type, operand_value = self.pre_process_operand_value(operand).values()
138
+ processed_values = self.process_values(operand_value, tag_value)
139
+ tag_value = processed_values["tag_value"]
140
+ return self.extract_result(
141
+ operand_type, processed_values["operand_value"], tag_value
142
+ )
143
+
144
+ def pre_process_tag_value(self, tag_value):
145
+ """
146
+ Pre-processes the tag value to ensure it is in the correct format for evaluation.
147
+
148
+ :param tag_value: The value to be processed.
149
+ :return: The processed tag value, either as a string or a boolean.
150
+ """
151
+ if tag_value is None:
152
+ return ""
153
+
154
+ if is_boolean(tag_value):
155
+ return bool(tag_value)
156
+
157
+ if tag_value is not None:
158
+ return str(tag_value)
159
+
160
+ return tag_value
161
+
162
+ def pre_process_operand_value(self, operand):
163
+ """
164
+ Pre-processes the operand value to determine its type and extract the value based on regex matches.
165
+
166
+ :param operand: The operand to be processed.
167
+ :return: A dictionary containing the operand type and value.
168
+ """
169
+ operand_type: str = None
170
+ operand_value: str = None
171
+
172
+ if match_with_regex(operand, SegmentOperandRegexEnum.LOWER_MATCH.value):
173
+ operand_type = SegmentOperandValueEnum.LOWER_VALUE.value
174
+ operand_value = self.extract_operand_value(
175
+ operand, SegmentOperandRegexEnum.LOWER_MATCH.value
176
+ )
177
+ elif match_with_regex(operand, SegmentOperandRegexEnum.WILDCARD_MATCH.value):
178
+ operand_value = self.extract_operand_value(
179
+ operand, SegmentOperandRegexEnum.WILDCARD_MATCH.value
180
+ )
181
+ starting_star = match_with_regex(
182
+ operand_value, SegmentOperandRegexEnum.STARTING_STAR.value
183
+ )
184
+ ending_star = match_with_regex(
185
+ operand_value, SegmentOperandRegexEnum.ENDING_STAR.value
186
+ )
187
+
188
+ if starting_star and ending_star:
189
+ operand_type = SegmentOperandValueEnum.STARTING_ENDING_STAR_VALUE.value
190
+ elif starting_star:
191
+ operand_type = SegmentOperandValueEnum.STARTING_STAR_VALUE.value
192
+ elif ending_star:
193
+ operand_type = SegmentOperandValueEnum.ENDING_STAR_VALUE.value
194
+
195
+ operand_value = operand_value.lstrip("*").rstrip("*")
196
+ elif match_with_regex(operand, SegmentOperandRegexEnum.REGEX_MATCH.value):
197
+ operand_type = SegmentOperandValueEnum.REGEX_VALUE.value
198
+ operand_value = self.extract_operand_value(
199
+ operand, SegmentOperandRegexEnum.REGEX_MATCH.value
200
+ )
201
+ elif match_with_regex(
202
+ operand, SegmentOperandRegexEnum.GREATER_THAN_MATCH.value
203
+ ):
204
+ operand_type = SegmentOperandValueEnum.GREATER_THAN_VALUE.value
205
+ operand_value = self.extract_operand_value(
206
+ operand, SegmentOperandRegexEnum.GREATER_THAN_MATCH.value
207
+ )
208
+ elif match_with_regex(
209
+ operand, SegmentOperandRegexEnum.GREATER_THAN_EQUAL_TO_MATCH.value
210
+ ):
211
+ operand_type = SegmentOperandValueEnum.GREATER_THAN_EQUAL_TO_VALUE.value
212
+ operand_value = self.extract_operand_value(
213
+ operand, SegmentOperandRegexEnum.GREATER_THAN_EQUAL_TO_MATCH.value
214
+ )
215
+ elif match_with_regex(operand, SegmentOperandRegexEnum.LESS_THAN_MATCH.value):
216
+ operand_type = SegmentOperandValueEnum.LESS_THAN_VALUE.value
217
+ operand_value = self.extract_operand_value(
218
+ operand, SegmentOperandRegexEnum.LESS_THAN_MATCH.value
219
+ )
220
+ elif match_with_regex(
221
+ operand, SegmentOperandRegexEnum.LESS_THAN_EQUAL_TO_MATCH.value
222
+ ):
223
+ operand_type = SegmentOperandValueEnum.LESS_THAN_EQUAL_TO_VALUE.value
224
+ operand_value = self.extract_operand_value(
225
+ operand, SegmentOperandRegexEnum.LESS_THAN_EQUAL_TO_MATCH.value
226
+ )
227
+ else:
228
+ operand_type = SegmentOperandValueEnum.EQUAL_VALUE.value
229
+ operand_value = operand
230
+
231
+ return {"operand_type": operand_type, "operand_value": operand_value}
232
+
233
+ def extract_operand_value(self, operand, regex):
234
+ """
235
+ Extracts the operand value from a string based on a specified regex pattern.
236
+
237
+ :param operand: The operand string to extract from.
238
+ :param regex: The regex pattern to use for extraction.
239
+ :return: The extracted value.
240
+ """
241
+ match = re.search(regex, operand)
242
+ return match.group(1) if match else None
243
+
244
+ def process_values(self, operand_value, tag_value):
245
+ """
246
+ Processes numeric values from operand and tag values, converting them to strings.
247
+
248
+ :param operand_value: The operand value to process.
249
+ :param tag_value: The tag value to process.
250
+ :return: A dictionary containing the processed operand and tag values as strings.
251
+ """
252
+ # Process both operand and tag values
253
+ processed_operand_value = self.convert_value(operand_value)
254
+ processed_tag_value = self.convert_value(tag_value)
255
+
256
+ return {
257
+ "operand_value": processed_operand_value,
258
+ "tag_value": processed_tag_value,
259
+ }
260
+
261
+ def extract_result(self, operand_type: str, operand_value: str, tag_value: str):
262
+ """
263
+ Extracts the result of the evaluation based on the operand type and values.
264
+
265
+ :param operand_type: The type of the operand.
266
+ :param operand_value: The value of the operand.
267
+ :param tag_value: The value of the tag to compare against.
268
+ :return: The result of the evaluation.
269
+ """
270
+ result = False
271
+
272
+ if tag_value is None:
273
+ return False
274
+
275
+ # Ensure operand_value and tag_value are strings
276
+ operand_value_str = str(operand_value)
277
+ tag_value_str = str(tag_value)
278
+
279
+ if operand_type == SegmentOperandValueEnum.LOWER_VALUE.value:
280
+ result = operand_value_str.lower() == tag_value_str.lower()
281
+ elif operand_type == SegmentOperandValueEnum.STARTING_ENDING_STAR_VALUE.value:
282
+ result = tag_value_str.find(operand_value_str) != -1
283
+ elif operand_type == SegmentOperandValueEnum.STARTING_STAR_VALUE.value:
284
+ result = tag_value_str.endswith(operand_value_str)
285
+ elif operand_type == SegmentOperandValueEnum.ENDING_STAR_VALUE.value:
286
+ result = tag_value_str.startswith(operand_value_str)
287
+ elif operand_type == SegmentOperandValueEnum.REGEX_VALUE.value:
288
+ try:
289
+ pattern = re.compile(operand_value_str)
290
+ matcher = pattern.search(tag_value_str)
291
+ result = matcher is not None
292
+ except re.error:
293
+ result = False
294
+ elif operand_type == SegmentOperandValueEnum.GREATER_THAN_VALUE.value:
295
+ # Only compare if both values are of the same type (both numeric or both non-numeric)
296
+ if self.is_numeric_version(tag_value_str) == self.is_numeric_version(
297
+ operand_value_str
298
+ ):
299
+ result = self.compare_versions(tag_value_str, operand_value_str) > 0
300
+ else:
301
+ result = False # Different types cannot be compared
302
+ elif operand_type == SegmentOperandValueEnum.GREATER_THAN_EQUAL_TO_VALUE.value:
303
+ # Only compare if both values are of the same type (both numeric or both non-numeric)
304
+ if self.is_numeric_version(tag_value_str) == self.is_numeric_version(
305
+ operand_value_str
306
+ ):
307
+ result = self.compare_versions(tag_value_str, operand_value_str) >= 0
308
+ else:
309
+ result = False # Different types cannot be compared
310
+ elif operand_type == SegmentOperandValueEnum.LESS_THAN_VALUE.value:
311
+ # Only compare if both values are of the same type (both numeric or both non-numeric)
312
+ if self.is_numeric_version(tag_value_str) == self.is_numeric_version(
313
+ operand_value_str
314
+ ):
315
+ result = self.compare_versions(tag_value_str, operand_value_str) < 0
316
+ else:
317
+ result = False # Different types cannot be compared
318
+ elif operand_type == SegmentOperandValueEnum.LESS_THAN_EQUAL_TO_VALUE.value:
319
+ # Only compare if both values are of the same type (both numeric or both non-numeric)
320
+ if self.is_numeric_version(tag_value_str) == self.is_numeric_version(
321
+ operand_value_str
322
+ ):
323
+ result = self.compare_versions(tag_value_str, operand_value_str) <= 0
324
+ else:
325
+ result = False # Different types cannot be compared
326
+ else:
327
+ result = tag_value_str == operand_value_str
328
+
329
+ return result
330
+
331
+ def convert_value(self, value):
332
+ # Check if the value is a boolean
333
+ if isinstance(value, bool):
334
+ return str(value) # Return "True" or "False" as a string
335
+ try:
336
+ numeric_value = float(value)
337
+ # Check if the numeric value is actually an integer
338
+ if numeric_value.is_integer():
339
+ return str(
340
+ int(numeric_value)
341
+ ) # Convert to int and then to string to remove '.0'
342
+ else:
343
+ return str(numeric_value) # Keep as float and convert to string
344
+ except ValueError:
345
+ # Return the value as is if it's not a number
346
+ return value
347
+
348
+ def evaluate_string_operand_dsl(
349
+ self,
350
+ dsl_operand_value,
351
+ context: ContextModel,
352
+ operand_type: SegmentOperatorValueEnum,
353
+ ):
354
+ """
355
+ Evaluates a given string tag value against a DSL operand value.
356
+
357
+ :param dsl_operand_value: The DSL operand string (e.g., "contains(\"value\")").
358
+ :param context: The context object containing the value to evaluate.
359
+ :param operand_type: The type of operand being evaluated (ip_address, browser_version, os_version).
360
+ :return: True if tag value matches DSL operand criteria, false otherwise.
361
+ """
362
+ operand = str(dsl_operand_value)
363
+
364
+ # Determine the tag value based on operand type
365
+ tag_value = self.get_tag_value_for_operand_type(context, operand_type)
366
+
367
+ if tag_value is None:
368
+ self.log_missing_context_error(operand_type, context)
369
+ return False
370
+
371
+ operand_type_and_value = self.pre_process_operand_value(operand)
372
+ processed_values = self.process_values(
373
+ operand_type_and_value["operand_value"], tag_value
374
+ )
375
+ tag_value = processed_values["tag_value"]
376
+
377
+ return self.extract_result(
378
+ operand_type_and_value["operand_type"],
379
+ processed_values["operand_value"].strip().replace('"', ""),
380
+ tag_value,
381
+ )
382
+
383
+ def get_tag_value_for_operand_type(
384
+ self, context: ContextModel, operand_type: SegmentOperatorValueEnum
385
+ ):
386
+ """
387
+ Gets the appropriate tag value based on the operand type.
388
+
389
+ :param context: The context object.
390
+ :param operand_type: The type of operand.
391
+ :return: The tag value or None if not available.
392
+ """
393
+ if operand_type == SegmentOperatorValueEnum.IP.value:
394
+ return context.get_ip_address()
395
+ elif operand_type == SegmentOperatorValueEnum.BROWSER_VERSION.value:
396
+ return self.get_browser_version_from_context(context)
397
+ else:
398
+ # Default works for OS version
399
+ return self.get_os_version_from_context(context)
400
+
401
+ def get_browser_version_from_context(self, context: ContextModel):
402
+ """
403
+ Gets browser version from context.
404
+
405
+ :param context: The context object.
406
+ :return: The browser version or None if not available.
407
+ """
408
+ if (
409
+ context.get_vwo() is None
410
+ or context.get_vwo().get_ua_info() is None
411
+ or len(context.get_vwo().get_ua_info()) == 0
412
+ ):
413
+ return None
414
+
415
+ user_agent = context.get_vwo().get_ua_info()
416
+
417
+ # Assuming UserAgent dictionary contains browser_version
418
+ if "browser_version" in user_agent:
419
+ return (
420
+ str(user_agent["browser_version"])
421
+ if user_agent["browser_version"] is not None
422
+ else None
423
+ )
424
+ return None
425
+
426
+ def get_os_version_from_context(self, context: ContextModel):
427
+ """
428
+ Gets OS version from context.
429
+
430
+ :param context: The context object.
431
+ :return: The OS version or None if not available.
432
+ """
433
+ if (
434
+ context.get_vwo() is None
435
+ or context.get_vwo().get_ua_info() is None
436
+ or len(context.get_vwo().get_ua_info()) == 0
437
+ ):
438
+ return None
439
+
440
+ user_agent = context.get_vwo().get_ua_info()
441
+ # Assuming UserAgent dictionary contains os_version
442
+ if "os_version" in user_agent:
443
+ return (
444
+ str(user_agent["os_version"])
445
+ if user_agent["os_version"] is not None
446
+ else None
447
+ )
448
+ return None
449
+
450
+ def log_missing_context_error(self, operand_type: SegmentOperatorValueEnum, context: ContextModel):
451
+ """
452
+ Logs appropriate error message for missing context.
453
+
454
+ :param operand_type: The type of operand.
455
+ """
456
+ if operand_type == SegmentOperatorValueEnum.IP.value:
457
+ LogManager.get_instance().error_log("INVALID_IP_ADDRESS_IN_CONTEXT_FOR_PRE_SEGMENTATION", debug_data={"an": ApiEnum.GET_FLAG.value, "uuid": context.get_vwo_uuid(), "sId": context.get_session_id()})
458
+ elif operand_type == SegmentOperatorValueEnum.BROWSER_VERSION.value:
459
+ LogManager.get_instance().info(
460
+ "To evaluate browser version segmentation, please provide userAgent in context"
461
+ )
462
+ else:
463
+ LogManager.get_instance().info(
464
+ "To evaluate OS version segmentation, please provide userAgent in context"
465
+ )
466
+
467
+ def is_numeric_version(self, version_str: str) -> bool:
468
+ """
469
+ Checks if a version string represents a numeric version.
470
+ A numeric version is one where all parts separated by dots are numeric.
471
+
472
+ :param version_str: The version string to check
473
+ :return: True if all parts are numeric, False otherwise
474
+ """
475
+ if not version_str:
476
+ return False
477
+
478
+ parts = version_str.split(".")
479
+ return all(part.isdigit() for part in parts)
480
+
481
+ def compare_versions(self, version1: str, version2: str):
482
+ """
483
+ Compares two version strings using semantic versioning rules.
484
+ Supports formats like "1.2.3", "1.0", "2.1.4.5", etc.
485
+ Assumes both versions are of the same type (both numeric or both non-numeric).
486
+
487
+ :param version1: First version string
488
+ :param version2: Second version string
489
+ :return: -1 if version1 < version2, 0 if equal, 1 if version1 > version2
490
+ """
491
+ # Split versions by dots
492
+ parts1 = version1.split(".")
493
+ parts2 = version2.split(".")
494
+
495
+ # Find the maximum length to handle different version formats
496
+ max_length = max(len(parts1), len(parts2))
497
+
498
+ for i in range(max_length):
499
+ part1 = parts1[i] if i < len(parts1) else "0"
500
+ part2 = parts2[i] if i < len(parts2) else "0"
501
+
502
+ # Since we ensure both versions are of the same type before calling this method,
503
+ # we can check if the first part is numeric to determine the comparison type
504
+ if part1.isdigit():
505
+ # Both are numeric versions, compare as integers
506
+ num1, num2 = int(part1), int(part2)
507
+ if num1 < num2:
508
+ return -1
509
+ elif num1 > num2:
510
+ return 1
511
+ else:
512
+ # Both are non-numeric versions, compare as strings
513
+ if part1 < part2:
514
+ return -1
515
+ elif part1 > part2:
516
+ return 1
517
+
518
+ return 0 # Versions are equal
@@ -0,0 +1,13 @@
1
+ # Copyright 2024-2026 Wingify Software Pvt. Ltd.
2
+ #
3
+ # Licensed under the Apache License, Version 2.0 (the "License");
4
+ # you may not use this file except in compliance with the License.
5
+ # You may obtain a copy of the License at
6
+ #
7
+ # http://www.apache.org/licenses/LICENSE-2.0
8
+ #
9
+ # Unless required by applicable law or agreed to in writing, software
10
+ # distributed under the License is distributed on an "AS IS" BASIS,
11
+ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
+ # See the License for the specific language governing permissions and
13
+ # limitations under the License.
@@ -0,0 +1,33 @@
1
+ # Copyright 2024-2026 Wingify Software Pvt. Ltd.
2
+ #
3
+ # Licensed under the Apache License, Version 2.0 (the "License");
4
+ # you may not use this file except in compliance with the License.
5
+ # You may obtain a copy of the License at
6
+ #
7
+ # http://www.apache.org/licenses/LICENSE-2.0
8
+ #
9
+ # Unless required by applicable law or agreed to in writing, software
10
+ # distributed under the License is distributed on an "AS IS" BASIS,
11
+ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
+ # See the License for the specific language governing permissions and
13
+ # limitations under the License.
14
+
15
+
16
+ import re
17
+ from ....utils.data_type_util import is_object
18
+ from typing import List
19
+
20
+
21
+ def get_key_value(obj):
22
+ if not is_object(obj):
23
+ return None
24
+ key = list(obj.keys())[0]
25
+ value = obj[key]
26
+ return key, value
27
+
28
+
29
+ def match_with_regex(string, regex):
30
+ try:
31
+ return re.findall(regex, string)
32
+ except re.error:
33
+ return None