Merge branch 'regexreplace' of git://github.com/jacobweber/ansible into jacobweber-regexreplace

This commit is contained in:
James Tanner 2014-03-31 13:48:13 -04:00
commit efba8b4771
2 changed files with 25 additions and 0 deletions

View file

@ -127,6 +127,15 @@ def search(value, pattern='', ignorecase=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, 'search')
def regex_replace(value='', pattern='', replacement='', ignorecase=False):
''' Perform a `re.sub` returning a string '''
if ignorecase:
flags = re.I
else:
flags = 0
_re = re.compile(pattern, flags=flags)
return _re.sub(replacement, value)
def unique(a): def unique(a):
return set(a) return set(a)
@ -196,6 +205,7 @@ class FilterModule(object):
'match': match, 'match': match,
'search': search, 'search': search,
'regex': regex, 'regex': regex,
'regex_replace': regex_replace,
# list # list
'unique' : unique, 'unique' : unique,

View file

@ -116,6 +116,21 @@ class TestFilters(unittest.TestCase):
True) True)
assert a == True assert a == True
def test_regex_replace_case_sensitive(self):
a = ansible.runner.filter_plugins.core.regex_replace('ansible', '^a.*i(.*)$',
'a\\1')
assert a == 'able'
def test_regex_replace_case_insensitive(self):
a = ansible.runner.filter_plugins.core.regex_replace('ansible', '^A.*I(.*)$',
'a\\1', True)
assert a == 'able'
def test_regex_replace_no_match(self):
a = ansible.runner.filter_plugins.core.regex_replace('ansible', '^b.*i(.*)$',
'a\\1')
assert a == 'ansible'
#def test_filters(self): #def test_filters(self):
# this test is pretty low level using a playbook, hence I am disabling it for now -- MPD. # this test is pretty low level using a playbook, hence I am disabling it for now -- MPD.