client/test: allow matching and replacing regex-es in nmcli output

This allows us to sanitize unpredictable UUIDs in client output in
--offline mode (where we can't just ask the mock service about the
actual UUID).
This commit is contained in:
Lubomir Rintel 2022-04-06 11:59:21 +02:00
parent 6fa1323ce5
commit beebde9e56

View file

@ -202,6 +202,14 @@ class Util:
t = basestring t = basestring
return isinstance(s, t) return isinstance(s, t)
@staticmethod
def is_regex_pattern(s):
if Util.python_has_version(3):
t = re.Pattern
else:
t = re._pattern_type
return isinstance(s, t)
@staticmethod @staticmethod
def memoize_nullary(nullary_func): def memoize_nullary(nullary_func):
result = [] result = []
@ -347,12 +355,21 @@ class Util:
v_search = replace[0]() v_search = replace[0]()
except TypeError: except TypeError:
v_search = replace[0] v_search = replace[0]
v_replace = replace[1]
v_replace = v_replace.encode("utf-8")
if Util.is_regex_pattern(v_search):
text2 = []
for t in text:
text2.append(v_search.sub(v_replace, t))
text = text2
continue
assert v_search is None or Util.is_string(v_search) assert v_search is None or Util.is_string(v_search)
if not v_search: if not v_search:
continue continue
v_replace = replace[1]
v_search = v_search.encode("utf-8") v_search = v_search.encode("utf-8")
v_replace = v_replace.encode("utf-8")
text2 = [] text2 = []
for t in text: for t in text:
if isinstance(t, tuple): if isinstance(t, tuple):