Allow to select objects using regular expressions
Change-Id: I39d644f9244cfea1fbc50a354a9cc5cfe1e8be9c
This commit is contained in:
parent
2fe5481f65
commit
801ed2fea3
|
@ -13,6 +13,7 @@
|
||||||
# under the License.
|
# under the License.
|
||||||
from __future__ import absolute_import
|
from __future__ import absolute_import
|
||||||
|
|
||||||
|
import re
|
||||||
import typing
|
import typing
|
||||||
|
|
||||||
from tobiko import _exception
|
from tobiko import _exception
|
||||||
|
@ -88,8 +89,8 @@ def equal_attributes(obj,
|
||||||
attributes: typing.Dict[str, typing.Any],
|
attributes: typing.Dict[str, typing.Any],
|
||||||
inverse=False) \
|
inverse=False) \
|
||||||
-> bool:
|
-> bool:
|
||||||
for key, value in attributes.items():
|
for key, matcher in attributes.items():
|
||||||
matching = value == getattr(obj, key)
|
matching = match(matcher, getattr(obj, key))
|
||||||
if matching is inverse:
|
if matching is inverse:
|
||||||
return False
|
return False
|
||||||
return True
|
return True
|
||||||
|
@ -98,13 +99,23 @@ def equal_attributes(obj,
|
||||||
def equal_items(obj: typing.Dict,
|
def equal_items(obj: typing.Dict,
|
||||||
items: typing.Dict,
|
items: typing.Dict,
|
||||||
inverse=False) -> bool:
|
inverse=False) -> bool:
|
||||||
for key, value in items.items():
|
for key, matcher in items.items():
|
||||||
matching = value == obj[key]
|
matching = match(matcher, obj[key])
|
||||||
if matching is inverse:
|
if matching is inverse:
|
||||||
return False
|
return False
|
||||||
return True
|
return True
|
||||||
|
|
||||||
|
|
||||||
|
PatternType = type(re.compile("", 0))
|
||||||
|
|
||||||
|
|
||||||
|
def match(matcher: typing.Any, value: typing.Any) -> bool:
|
||||||
|
if isinstance(matcher, PatternType):
|
||||||
|
return matcher.match(value) is not None
|
||||||
|
else:
|
||||||
|
return matcher == value
|
||||||
|
|
||||||
|
|
||||||
class ObjectNotFound(_exception.TobikoException):
|
class ObjectNotFound(_exception.TobikoException):
|
||||||
message = "Object not found"
|
message = "Object not found"
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue