@@ -34,15 +34,18 @@ class InvalidContext(Exception):
3434class ContextValue :
3535 """ Value for dimension """
3636
37- def __init__ (self , origin ):
37+ def __init__ (self , raw ):
3838 """
3939 ContextValue("foo-1.2.3")
4040 ContextValue(["foo", "1", "2", "3"])
4141 """
42- if isinstance (origin , (tuple , list )):
43- self ._to_compare = tuple (origin )
42+ if isinstance (raw , (tuple , list )):
43+ self ._to_compare = tuple (raw )
4444 else :
45- self ._to_compare = self ._split_to_version (origin )
45+ self ._to_compare = self ._split_to_version (raw )
46+
47+ # Store the original string for regexp processing
48+ self .raw = raw
4649
4750 def __eq__ (self , other ):
4851 if isinstance (other , self .__class__ ):
@@ -238,6 +241,22 @@ def comparator(dimension_value, it_val):
238241
239242 return self ._op_core (dimension_name , values , comparator )
240243
244+ def _op_match (self , dimension_name , values ):
245+ """ '~' operator, regular expression matches """
246+
247+ def comparator (dimension_value , it_val ):
248+ return re .search (it_val .raw , dimension_value .raw ) is not None
249+
250+ return self ._op_core (dimension_name , values , comparator )
251+
252+ def _op_not_match (self , dimension_name , values ):
253+ """ '~' operator, regular expression does not match """
254+
255+ def comparator (dimension_value , it_val ):
256+ return re .search (it_val .raw , dimension_value .raw ) is None
257+
258+ return self ._op_core (dimension_name , values , comparator )
259+
241260 def _op_minor_eq (self , dimension_name , values ):
242261 """ '~=' operator """
243262
@@ -371,6 +390,8 @@ def _op_core(self, dimension_name, values, comparator):
371390 "~>=" : _op_minor_greater_or_equal ,
372391 ">" : _op_greater ,
373392 "~>" : _op_minor_greater ,
393+ "~" : _op_match ,
394+ "!~" : _op_not_match ,
374395 }
375396
376397 # Triple expression: dimension operator values
@@ -379,7 +400,7 @@ def _op_core(self, dimension_name, values, comparator):
379400 r"(\w+)"
380401 + r"\s*("
381402 + r"|" .join (
382- set ( operator_map . keys ()) - { "is defined" , "is not defined" } )
403+ [ key for key in operator_map if key not in [ "is defined" , "is not defined" ]] )
383404 + r")\s*"
384405 + r"([^=].*)" )
385406 # Double expression: dimension operator
0 commit comments