mirror of
https://github.com/ansible-collections/community.general.git
synced 2024-09-14 20:13:21 +02:00
Merge pull request #14828 from privateip/plugin_test_regex
adds multiline flag to regex test for search and match
This commit is contained in:
commit
e9ddbb135c
1 changed files with 9 additions and 8 deletions
|
@ -62,26 +62,27 @@ def skipped(*a, **kw):
|
||||||
skipped = item.get('skipped', False)
|
skipped = item.get('skipped', False)
|
||||||
return skipped
|
return skipped
|
||||||
|
|
||||||
def regex(value='', pattern='', ignorecase=False, match_type='search'):
|
def regex(value='', pattern='', ignorecase=False, multiline=False, match_type='search'):
|
||||||
''' Expose `re` as a boolean filter using the `search` method by default.
|
''' Expose `re` as a boolean filter using the `search` method by default.
|
||||||
This is likely only useful for `search` and `match` which already
|
This is likely only useful for `search` and `match` which already
|
||||||
have their own filters.
|
have their own filters.
|
||||||
'''
|
'''
|
||||||
|
flags = 0
|
||||||
if ignorecase:
|
if ignorecase:
|
||||||
flags = re.I
|
flags |= re.I
|
||||||
else:
|
if multiline:
|
||||||
flags = 0
|
flags |= re.M
|
||||||
_re = re.compile(pattern, flags=flags)
|
_re = re.compile(pattern, flags=flags)
|
||||||
_bool = __builtins__.get('bool')
|
_bool = __builtins__.get('bool')
|
||||||
return _bool(getattr(_re, match_type, 'search')(value))
|
return _bool(getattr(_re, match_type, 'search')(value))
|
||||||
|
|
||||||
def match(value, pattern='', ignorecase=False):
|
def match(value, pattern='', ignorecase=False, multiline=False):
|
||||||
''' Perform a `re.match` returning a boolean '''
|
''' Perform a `re.match` returning a boolean '''
|
||||||
return regex(value, pattern, ignorecase, 'match')
|
return regex(value, pattern, ignorecase, multiline, 'match')
|
||||||
|
|
||||||
def search(value, pattern='', ignorecase=False):
|
def search(value, pattern='', ignorecase=False, multiline=False):
|
||||||
''' Perform a `re.search` returning a boolean '''
|
''' Perform a `re.search` returning a boolean '''
|
||||||
return regex(value, pattern, ignorecase, 'search')
|
return regex(value, pattern, ignorecase, multiline, 'search')
|
||||||
|
|
||||||
class TestModule(object):
|
class TestModule(object):
|
||||||
''' Ansible core jinja2 tests '''
|
''' Ansible core jinja2 tests '''
|
||||||
|
|
Loading…
Reference in a new issue