Skip to content

Commit

Permalink
sq: adding '-~' op - No tests yet
Browse files Browse the repository at this point in the history
  • Loading branch information
lukaszachy committed May 14, 2024
1 parent aa39def commit 8c3b29e
Showing 1 changed file with 29 additions and 0 deletions.
29 changes: 29 additions & 0 deletions fmf/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -204,6 +204,33 @@ def _merge_regexp(self, data, key, value):
"MergeError: Key '{0}' in {1} (wrong type).".format(
key, self.name))

import pysnooper

@pysnooper.snoop()
def _merge_minus_regexp(self, data, key, value):
""" Handle removing current values if they match regexp """
# A bit faster but essentially `any`
def lazy_any_search(item, patterns):
for p in patterns:
if re.search(p, item):
return True
return False
if isinstance(value, str):
value = [value]
if isinstance(data[key], list):
data[key] = [item for item in data[key] if not lazy_any_search(item, value)]
elif isinstance(data[key], str):
if lazy_any_search(data[key], value):
data[key] = ''
elif isinstance(data[key], dict):
for k in list(data.keys()):
if lazy_any_search(k, value):
data[key].pop(k, None)
else:
raise utils.MergeError(
"MergeError: Key '{0}' in {1} (wrong type).".format(
key, self.name))

def _merge_minus(self, data, key, value):
""" Handle reducing attributes using the '-' suffix """
# Cannot reduce attribute if key is not present in parent
Expand Down Expand Up @@ -238,6 +265,8 @@ def _merge_special(self, data, source):
self._merge_plus(data, key.rstrip('+'), value)
elif key.endswith('+<'):
self._merge_plus(data, key.rstrip('+<'), value, prepend=True)
elif key.endswith('-~'):
self._merge_minus_regexp(data, key.rstrip('-~'), value)
elif key.endswith('-'):
self._merge_minus(data, key.rstrip('-'), value)
elif key.endswith('~'):
Expand Down

0 comments on commit 8c3b29e

Please sign in to comment.