@@ -423,9 +423,9 @@ def semver_equal_evaluator(self, index):
423
423
"""
424
424
result = self .compare_user_version_with_target_version (index )
425
425
if result is None :
426
- return result
426
+ return None
427
427
428
- return self . compare_user_version_with_target_version ( index ) == 0
428
+ return result == 0
429
429
430
430
def semver_greater_than_evaluator (self , index ):
431
431
""" Evaluate the given semantic version greater than match target version for the user version.
@@ -442,8 +442,9 @@ def semver_greater_than_evaluator(self, index):
442
442
"""
443
443
result = self .compare_user_version_with_target_version (index )
444
444
if result is None :
445
- return result
446
- return self .compare_user_version_with_target_version (index ) > 0
445
+ return None
446
+
447
+ return result > 0
447
448
448
449
def semver_less_than_evaluator (self , index ):
449
450
""" Evaluate the given semantic version less than match target version for the user version.
@@ -460,8 +461,9 @@ def semver_less_than_evaluator(self, index):
460
461
"""
461
462
result = self .compare_user_version_with_target_version (index )
462
463
if result is None :
463
- return result
464
- return self .compare_user_version_with_target_version (index ) < 0
464
+ return None
465
+
466
+ return result < 0
465
467
466
468
def semver_less_than_or_equal_evaluator (self , index ):
467
469
""" Evaluate the given semantic version less than or equal to match target version for the user version.
@@ -478,8 +480,9 @@ def semver_less_than_or_equal_evaluator(self, index):
478
480
"""
479
481
result = self .compare_user_version_with_target_version (index )
480
482
if result is None :
481
- return result
482
- return self .compare_user_version_with_target_version (index ) <= 0
483
+ return None
484
+
485
+ return result <= 0
483
486
484
487
def semver_greater_than_or_equal_evaluator (self , index ):
485
488
""" Evaluate the given semantic version greater than or equal to match target version for the user version.
@@ -496,22 +499,23 @@ def semver_greater_than_or_equal_evaluator(self, index):
496
499
"""
497
500
result = self .compare_user_version_with_target_version (index )
498
501
if result is None :
499
- return result
500
- return self .compare_user_version_with_target_version (index ) >= 0
502
+ return None
503
+
504
+ return result >= 0
501
505
502
506
EVALUATORS_BY_MATCH_TYPE = {
503
507
ConditionMatchTypes .EXACT : exact_evaluator ,
504
508
ConditionMatchTypes .EXISTS : exists_evaluator ,
505
509
ConditionMatchTypes .GREATER_THAN : greater_than_evaluator ,
510
+ ConditionMatchTypes .GREATER_THAN_OR_EQUAL : greater_than_or_equal_evaluator ,
506
511
ConditionMatchTypes .LESS_THAN : less_than_evaluator ,
512
+ ConditionMatchTypes .LESS_THAN_OR_EQUAL : less_than_or_equal_evaluator ,
507
513
ConditionMatchTypes .SEMVER_EQ : semver_equal_evaluator ,
508
514
ConditionMatchTypes .SEMVER_GE : semver_greater_than_or_equal_evaluator ,
509
515
ConditionMatchTypes .SEMVER_GT : semver_greater_than_evaluator ,
510
516
ConditionMatchTypes .SEMVER_LE : semver_less_than_or_equal_evaluator ,
511
517
ConditionMatchTypes .SEMVER_LT : semver_less_than_evaluator ,
512
- ConditionMatchTypes .SUBSTRING : substring_evaluator ,
513
- ConditionMatchTypes .LESS_THAN_OR_EQUAL : less_than_or_equal_evaluator ,
514
- ConditionMatchTypes .GREATER_THAN_OR_EQUAL : greater_than_or_equal_evaluator ,
518
+ ConditionMatchTypes .SUBSTRING : substring_evaluator
515
519
}
516
520
517
521
def split_semantic_version (self , target ):
@@ -523,36 +527,34 @@ def split_semantic_version(self, target):
523
527
Returns:
524
528
List:
525
529
- The array of version split into smaller parts i.e major, minor, patch etc
526
- Exception :
530
+ None :
527
531
- if the given version is invalid in format
528
532
"""
529
533
target_prefix = target
530
534
target_suffix = ""
531
535
target_parts = []
532
536
533
- # remove spaces from target version string
534
-
537
+ # check that target shouldn't have white space
535
538
if self .has_white_space (target ):
536
539
self .logger .warning (Errors .INVALID_ATTRIBUTE_FORMAT )
537
540
return None
538
541
539
542
# check for pre release e.g. 1.0.0-alpha where 'alpha' is a pre release
540
543
# otherwise check for build e.g. 1.0.0+001 where 001 is a build metadata
541
-
542
544
if self .is_pre_release (target ):
543
545
target_parts = target .split (SemverType .IS_PRE_RELEASE )
544
546
elif self .is_build (target ):
545
547
target_parts = target .split (SemverType .IS_BUILD )
546
548
547
- # validate target version into prefix and suffix
549
+ # split target version into prefix and suffix
548
550
if target_parts :
549
551
if len (target_parts ) < 1 :
550
552
self .logger .warning (Errors .INVALID_ATTRIBUTE_FORMAT )
551
553
return None
552
554
target_prefix = str (target_parts [0 ])
553
555
target_suffix = target_parts [1 :]
554
556
555
- # validate dot counts in a target version
557
+ # check dot counts in target_prefix
556
558
dot_count = target_prefix .count ("." )
557
559
if dot_count > 2 :
558
560
self .logger .warning (Errors .INVALID_ATTRIBUTE_FORMAT )
@@ -575,15 +577,15 @@ def evaluate(self, index):
575
577
""" Given a custom attribute audience condition and user attributes, evaluate the
576
578
condition against the attributes.
577
579
578
- Args:
579
- index: Index of the condition to be evaluated.
580
+ Args:
581
+ index: Index of the condition to be evaluated.
580
582
581
- Returns:
582
- Boolean:
583
- - True if the user attributes match the given condition.
584
- - False if the user attributes don't match the given condition.
585
- None: if the user attributes and condition can't be evaluated.
586
- """
583
+ Returns:
584
+ Boolean:
585
+ - True if the user attributes match the given condition.
586
+ - False if the user attributes don't match the given condition.
587
+ None: if the user attributes and condition can't be evaluated.
588
+ """
587
589
588
590
if self .condition_data [index ][2 ] != self .CUSTOM_ATTRIBUTE_CONDITION_TYPE :
589
591
self .logger .warning (audience_logs .UNKNOWN_CONDITION_TYPE .format (self ._get_condition_json (index )))
@@ -616,7 +618,7 @@ def evaluate(self, index):
616
618
617
619
class ConditionDecoder (object ):
618
620
""" Class which provides an object_hook method for decoding dict
619
- objects into a list when given a condition_decoder. """
621
+ objects into a list when given a condition_decoder. """
620
622
621
623
def __init__ (self , condition_decoder ):
622
624
self .condition_list = []
@@ -625,16 +627,16 @@ def __init__(self, condition_decoder):
625
627
626
628
def object_hook (self , object_dict ):
627
629
""" Hook which when passed into a json.JSONDecoder will replace each dict
628
- in a json string with its index and convert the dict to an object as defined
629
- by the passed in condition_decoder. The newly created condition object is
630
- appended to the conditions_list.
630
+ in a json string with its index and convert the dict to an object as defined
631
+ by the passed in condition_decoder. The newly created condition object is
632
+ appended to the conditions_list.
631
633
632
- Args:
633
- object_dict: Dict representing an object.
634
+ Args:
635
+ object_dict: Dict representing an object.
634
636
635
- Returns:
636
- An index which will be used as the placeholder in the condition_structure
637
- """
637
+ Returns:
638
+ An index which will be used as the placeholder in the condition_structure
639
+ """
638
640
instance = self .decoder (object_dict )
639
641
self .condition_list .append (instance )
640
642
self .index += 1
@@ -644,12 +646,12 @@ def object_hook(self, object_dict):
644
646
def _audience_condition_deserializer (obj_dict ):
645
647
""" Deserializer defining how dict objects need to be decoded for audience conditions.
646
648
647
- Args:
648
- obj_dict: Dict representing one audience condition.
649
+ Args:
650
+ obj_dict: Dict representing one audience condition.
649
651
650
- Returns:
651
- List consisting of condition key with corresponding value, type and match.
652
- """
652
+ Returns:
653
+ List consisting of condition key with corresponding value, type and match.
654
+ """
653
655
return [
654
656
obj_dict .get ('name' ),
655
657
obj_dict .get ('value' ),
@@ -660,16 +662,16 @@ def _audience_condition_deserializer(obj_dict):
660
662
661
663
def loads (conditions_string ):
662
664
""" Deserializes the conditions property into its corresponding
663
- components: the condition_structure and the condition_list.
665
+ components: the condition_structure and the condition_list.
664
666
665
- Args:
666
- conditions_string: String defining valid and/or conditions.
667
+ Args:
668
+ conditions_string: String defining valid and/or conditions.
667
669
668
- Returns:
669
- A tuple of (condition_structure, condition_list).
670
- condition_structure: nested list of operators and placeholders for operands.
671
- condition_list: list of conditions whose index correspond to the values of the placeholders.
672
- """
670
+ Returns:
671
+ A tuple of (condition_structure, condition_list).
672
+ condition_structure: nested list of operators and placeholders for operands.
673
+ condition_list: list of conditions whose index correspond to the values of the placeholders.
674
+ """
673
675
decoder = ConditionDecoder (_audience_condition_deserializer )
674
676
675
677
# Create a custom JSONDecoder using the ConditionDecoder's object_hook method
0 commit comments